Working with Sets
- Subsets Set is a subset of set if every elements that is in is also in
- Union
- Intersection
- Difference is the set of all elements that are in but not in
- The Complement of can be denoted by
Representing Sets in Julia
- Create a set
Set() - Judge whether two sets are equal
isequal() - Symmetric difference
symdiff() - Union
union() - Intersection
intersect() - Difference :
setdiff(A, B) - Judge whether an element is in a set
in(element, set) - Judge whether a set is the subset of other set
issubset(subset, set) union!()intersect!symdiff!()setdiff!()will change the first parameter.
A = Set([2,7,2,3])
B = Set(1:6)
omega = Set(1:10)
AunionB = union(A, B)
AintersectionB = intersect(A, B)
BdifferenceA = setdiff(B,A)
Bcomplement = setdiff(omega, B)
AsymDifferenceB = union(setdiff(A,B), setdiff(B,A))
println("A = $A, B = $B")
println("A union B = $AunionB")
println("A intersection B = $AintersectionB")
println("B diff A = $BdifferenceA")
println("B complement = $Bcomplement")
println("A symDifference B = $AsymDifferenceB")
println("The element '6' is an element of A: $(in(6,A))")
println("Symmetric difference and intersection are subsets of the union: ",
issubset(AsymDifferenceB, AunionB),", ", issubset(AintersectionB, AunionB))
Probability of a Union
- Example 1 Consider all lowercase letter is sample space, Let be the event that letter is a vowel ; let be the event that the letter is one of the first three letters , thus
- Example 2 Consider the case where is the set of vowels as before, but , thus
- Solutions by Julia
- In mcEst1, each of the random experiments involves two separate calls to
sample(omega). Hence the code in line 12 simulates a situation where the simple space is composed of pairs of letters, not single letters! Thus the event is First element of the tuple is a vowel, the second element of tuple is one of the last three letters. and are not disjoint events:
- In mcEst1, each of the random experiments involves two separate calls to
using Random, StatsBase, DataFrames
Random.seed!(1)
A = Set(['a', 'e', 'i', 'o', 'u'])
B = Set(['x', 'y', 'z'])
omega = 'a':'z'
N = 10^6
df = DataFrame(No=Int64[], mcEst1=Float64[], mcEst2=Float64[], mcEst3=Float64[])
for i in 1:5
mcEst1 = sum([in(sample(omega),A) || in(sample(omega),B) for _ in 1:N])/N
mcEst2 = sum([in(sample(omega), union(A,B)) for _ in 1:N])/N
mcEst3 = sum([i ∈ A || i ∈ B for i in sample(omega,N)])/N
push!(df, [i, mcEst1, mcEst2, mcEst3])
end
print(df)
Secretary with Envelopes
- The -th term has summands.
- Example Suppose that a secretary has an equal number of pre-labeled envelopes and business cards, . Suppose that at the end of the day, he is in such a rush to go home that he puts each business card in an envelop at random without any though of business card to its intended recipient on the envelop. What is the probability that each of the business cards will go to a wrong envelope?
- Let be the event that the -th business card is put in the correct envelope. Thus we want to know the probability of event , according to De Morgan's laws , thus:
using Random, StatsBase, Combinatorics, DataFrames
Random.seed!
function bruteSetsProbabilityAllMiss(n)
omega = collect(permutations(1:n))
matchEvents = []
for i in 1:n
event = []
for p in omega
if p[i] == i
push!(event,p)
end
end
push!(matchEvents, event)
end
nomatch = setdiff(omega, union(matchEvents...))
return length(nomatch)/length(omega)
end
formulaCallAllMiss(n) = sum([(-1)^k/factorial(k) for k in 0:n])
function mcAllMiss(n,N)
function envelopeStuffer()
envelopes = Random.shuffle!(collect(1:n))
return sum([envelopes[i] == i for i in 1:n]) == 0
end
data = [envelopeStuffer() for _ in 1:N]
return sum(data)/N
end
N = 10^6
df = DataFrame(Brute_Force=Float64[], Formula=Float64[], Monte_Carlo=Float64[], Asymptotic=Float64[])
for n in 1:6
bruteForce = bruteSetsProbabilityAllMiss(n)
fromFormula = formulaCallAllMiss(n)
fromMC = mcAllMiss(n,N)
fromAsymptotic=1/MathConstants.e
push!(df, [bruteForce, fromFormula, fromMC, fromAsymptotic])
end
print(df)
An Occupancy Problem
- Example Considering a problem related to the previous exmaple. Imagine now the secretary placing identical business cards randomly into envelopes, with and no limit on the number of business cards that can fit in a envelope. What is the probability that all envelopes are non-enmpty?
- Let be the event that -th envelope is empty, is the probability of at least envelopes being empty, thus
- Solution by Julia ```julia
```