In [1]:
import numpy as np
from enum import Enum
from matplotlib import pyplot as plt


---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-ad45ab85d81b> in <module>()
----> 1 import numpy as np
      2 from enum import Enum
      3 from matplotlib import pyplot as plt

ImportError: No module named numpy

Boy or Girl Paradox


In [2]:
class gender(Enum):
    male = 1
    female = 2

class dayofweek(Enum):
    Sunday = 1
    Monday = 2
    Tuesday = 3
    Wednesday = 4 
    Thursday = 5 
    Friday = 6 
    Saturday = 7 
    
class child():
    def __init__(self, gender, birth_day):
        self.gender = gender
        self.birth_day = birth_day 
            
class fam_2p():
    def __init__(self, younger_sibling, older_sibling):
        self.young = younger_sibling
        self.old = older_sibling

In [3]:
N = 1000000
fams = [] 
for i in range(0, N):
    youngen = child(gender(np.random.randint(1,3)), dayofweek(np.random.randint(1,8)))
    oldmen = child(gender(np.random.randint(1,3)), dayofweek(np.random.randint(1,8)))
    fams.append(fam_2p(youngen, oldmen))

In [4]:
fams_atleast1boy = []
fams_youngerisboy = []
fams_atleast1boytuesday = []
for fam in fams:
    if fam.young.gender == gender.male:
        fams_youngerisboy.append(fam)
        fams_atleast1boy.append(fam)
        if fam.young.birth_day == dayofweek.Tuesday:
            fams_atleast1boytuesday.append(fam)
    elif fam.old.gender == gender.male:
        fams_atleast1boy.append(fam)
        if fam.old.birth_day == dayofweek.Tuesday:
            fams_atleast1boytuesday.append(fam)
    
print(len(fams_youngerisboy) / len(fams))
print(len(fams_atleast1boy) / len(fams))
        
num_fams_bothboys = 0 
for fam in fams_youngerisboy:
    if fam.old.gender == gender.male:
        num_fams_bothboys += 1
print(num_fams_bothboys / len(fams_youngerisboy))

num_fams_bothboys = 0 
for fam in fams_atleast1boy:
    if fam.young.gender == gender.male and fam.old.gender == gender.male:
        num_fams_bothboys += 1
print(num_fams_bothboys / len(fams_atleast1boy))

num_fams_bothboys = 0
for fam in fams_atleast1boytuesday:
    if fam.young.gender == gender.male and fam.old.gender == gender.male:
        num_fams_bothboys += 1
print(num_fams_bothboys / len(fams_atleast1boytuesday))


0
0
0
0
0

Draw til 0


In [5]:
def draw_til_zero(N):
    """picks a number between 0 and N-1 with equal probabibility \
    then repeats with the number drawn until reaching zero \
    returns the number of iterations this takes"""
    num_draws = 0 
    n = N
    while n > 0:
        n = np.random.randint(0, n)
        num_draws += 1
    return num_draws

def expectation_val(func, args, num_trials):
    total = 0
    n = 0
    while n < num_trials:
        total += func(*args)
        n += 1
    return total / num_trials

def harmonic_sum_simple(n):
    sum = 0 
    for k in range(1, n+1):
        sum += 1/k
    return sum

In [6]:
N = 100
print(harmonic_sum_simple(N))
print(expectation_val(draw_til_zero, (N,), 10000))


1
5

testing variable scope


In [7]:
if True:
    inscope = 5
    
print(inscope)
print(n)


5
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-7-a9bac9f46631> in <module>()
      3 
      4 print(inscope)
----> 5 print(n)

NameError: name 'n' is not defined

Boy or Girl Paradox II


In [2]:
import itertools
from fractions import Fraction
from __future__ import print_function

sex = 'BG'

def product(*variables):
    return map(''.join, itertools.product(*variables))

two_kids = product(sex, sex)

two_kids


Out[2]:
['BB', 'BG', 'GB', 'GG']

In [17]:
g = itertools.product(sex, sex)
print(next(g))
print(next(g))
str.join?
map?


('B', 'B')
('B', 'G')

In [12]:
# at least one boy 
one_boy = [s for s in two_kids if 'B' in s] 

def two_boys(s): return s.count('B') == 2

def condP(predicate, event):
    '''Conditional probability: P(predicate(s) | s in event)
    The proportion of states in event for which predicate is true.'''
    pred = [s for s in event if predicate(s)]
    return Fraction(len(pred), len(event))

print(condP(two_boys, one_boy))


1/3

In [13]:
day = 'SMTWRFA'
#Thurs = R and Sat = A

two_kids_bday = product(sex, day, sex, day)

boy_tuesday = [s for s in two_kids_bday if 'BT' in s]

print(condP(two_boys, boy_tuesday))


13/27

In [ ]: