Day 2 - Probability, Compound Event Probability
Basic probability with dices
Problem
In this challenge, we practice calculating probability. In a single toss of 2 fair (evenly-weighted) six-sided dice, find the probability that their sum will be at most 9.
Mathematical explanation
A nice way to think about sums-of-two-dice problems is to lay out the sums in a 6-by-6 grid in the obvious manner.
1 | 2 | 3 | 4 | 5 | 6 | |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
2 | 3 | 4 | 5 | 6 | 7 | 8 |
3 | 4 | 5 | 6 | 7 | 8 | 9 |
4 | 5 | 6 | 7 | 8 | 9 | 10 |
5 | 6 | 7 | 8 | 9 | 10 | 11 |
6 | 7 | 8 | 9 | 10 | 11 | 12 |
We see that the identic values are on the same diagonal. The number of elements on the diagonal varies from 1 to 6 and then back to 1.
let's call A < x the event : the sum all the 2 tosses is at most x.
The value of \(P(A = i) = \frac{i-1}{36}\) if \(i \leq 7\) and \(P(A = i) = \frac{13-i}{36}\)
hence
Let's program it
sum([1 for d1 in range(1,7) for d2 in range(1,7) if d1+d2<=9]) / 36
0.8333333333333334
More dices
Problem
In a single toss of 2 fair (evenly-weighted) six-sided dice, find the probability that the values rolled by each die will be different and the two dice have a sum of 6.
Mathematical explanation
Let's consider 2 events : A and B. A compound event is a combination of 2 or more simple events. If A and B are simple events, then A∪B denotes the occurence of either A or B. A∩B denotes the occurence of A and B together.
We denote A the event "the values of each dice is different". The opposit event is A' "the values of each dice is the same".
We denote B the event "the two dice have a sum of 6", this probability has been computed on the first part of the article :
The probability of having 2 dice different of sum 6 is :
The probability that both A and B occure is equal to P(A∩B).
Since \(P(A|B)=\frac{P(A∩B)}{P(B)}\)
Let's program it
sum([1 for d1 in range(1,7) for d2 in range(1,7) if (d1+d2==6) and (d1!=d2)]) / 36
0.1111111111111111
Compound Event Probability
Problem
There are 3 urns labeled X, Y, and Z.
- Urn X contains 4 red balls and 3 black balls.
- Urn Y contains 5 red balls and 4 black balls.
- Urn Z contains 4 red balls and 4 black balls.
One ball is drawn from each of the urns. What is the probability that, of the 3 balls drawn, are 2 red and is 1 black?
Mathematical explanation
Let's write the different probabilities:
Red ball | Black ball | |
---|---|---|
Urne X | $$\frac{4}{7}$$ | $$\frac{3}{7}$$ |
Urne Y | $$\frac{5}{9}$$ | $$\frac{4}{9}$$ |
Urne Z | $$\frac{1}{2}$$ | $$\frac{1}{2}$$ |
Addition rule
A and B are said to be mutually exclusive or disjoint if they have no events in common (i.e., and A∩B=∅ and P(A∩B)=0. The probability of any of 2 or more events occurring is the union (∪) of events. Because disjoint probabilities have no common events, the probability of the union of disjoint events is the sum of the events' individual probabilities. A and B are said to be collectively exhaustive if their union covers all events in the sample space (i.e., A∪B=S and P(A∪B)=1). This brings us to our next fundamental rule of probability: if 2 events, A and B, are disjoint, then the probability of either event is the sum of the probabilities of the 2 events (i.e., P(A or B) = P(A)+P(B))
Mutliplication rule
If the outcome of the first event (A) has no impact on the second event (B), then they are considered to be independent (e.g., tossing a fair coin). This brings us to the next fundamental rule of probability: the multiplication rule. It states that if two events, A and B, are independent, then the probability of both events is the product of the probabilities for each event (i.e., P(A and B)= P(A)xP(B)). The chance of all events occurring in a sequence of events is called the intersection (∩) of those events.
The balls drawn from the urns are independant hence :
p = P(2 red (R) and 1 back (B))
Each of those 3 probability if equal to the product of the probability of drawing each ball \(P(RRB) = P(R|X) * P(R|Y) * P(B|Z) = 4/7*5/9*1/2\)
-
\(P(RRB) = 20/126\)
-
\(P(RBR) = 16/126\)
-
\(P(BRR) = 15/126\)
this leads to
- \(p = 51/126\)
and finally
Let's program it
X = 3*["B"]+4*["R"]
Y = 4*["B"]+5*["R"]
Z = 4*["B"]+4*["R"]
target = ["BRR", "RRB", "RBR"]
sum([1 for x in X for y in Y for z in Z if x+y+z in target])/sum([1 for x in X for y in Y for z in Z])
0.40476190476190477