Mach 9.3 Probability

213 days ago by mpaul

A sample space is a set S of all possible outcomes O of an experiment.

An event E is a subset of a sample space.  So E is also a set of outcomes.

The probability P of an event E is:  P(E) = \frac{n(E)}{n(S)}.

 
       

What is the sample space S for throwing 2 dice?

S = [(die1, die2) for die1 in [1..6] for die2 in [1..6]] S 
       
len(S) 
       

What is the event space E for the sum of the dice is 7?

E = [throw for throw in S if sum(throw) == 7] E 
       
len(E) 
       

When throwing two dice, what is P( sum of 7)?

e = Integer(len(E)) s = Integer(len(S)) e/s 
       
def P(E, S): return Integer(len(E))/Integer(len(S)) 
       
bothprime = [(a,b) for (a,b) in S if is_prime(a) and is_prime(b)] bothprime; P(bothprime, S) 
       
 
       

Probability Function

A probability function P assigns a real number to each outcome O in a sample space S such that:

  1. 0 \le P(O) \le 1 for every O in S.
  2. \sum _{i = 1}^{n} P(O_i) = 1
  3. P(\emptyset) = 0
[P([O], S) for O in S] 
       
sum(_) 
       
sums = [sum(O) for O in S] [(s, sums.count(s)) for s in [2..12]] 
       
A = [O for O in S if is_even(sum(O))] B = [O for O in S if is_prime(sum(O))] 
       
A; len(A) 
       
P(A,S) 
       
B; len(B) 
       
P(B,S) 
       

Conditional Probability

P(A | B) = \frac{P(A \cap B)}{P(B)}

def intersect(A, B): return [O for O in S if O in A and O in B] 
       
intersect(A,B) 
       
P(intersect(A,B),S) 
       
P(intersect(A,B),B) 
       

Multiplication Principle 

P(A \cap B) = P(A) \cdot P(B | A)

P(A,S)*P(intersect(B,A),A) 
       
P(intersect(A,B),S) 
       

Addition Principle

P(A \cup B) = P(A) + P(B) - P(A \cap B)

def unite(A, B): return [O for O in S if O in A or O in B] 
       
unite(A, B) 
       
P(unite(A,B),S) 
       
P(A,S) + P(B,S) 
       
P(A,S) + P(B,S) - P(intersect(A,B),S) 
       

Binomial Distribution

\sum _{r = 0} ^{n} {n \choose r}p^{n-r}q^{r} where p + q = n.

#var('p q') p = 1/4 q = 1-p n = 10 [binomial(n,r)*p^(n-r)*q^r for r in [0..n]] 
       
[x.n(digits = 5) for x in _] 
       
sum(_)