Skip to content

Instantly share code, notes, and snippets.

@albertyumol
Created October 24, 2019 05:47
Show Gist options
  • Save albertyumol/7b27fc2d18ba00bb59d2b84f54dfd431 to your computer and use it in GitHub Desktop.
Save albertyumol/7b27fc2d18ba00bb59d2b84f54dfd431 to your computer and use it in GitHub Desktop.
Calculating conditional probabilities in Python.
import random
def random_kid():
return random.choice(['boy','girl'])
both_girls = 0
older_girl = 0
either_girl = 0
random.seed(7)
for i in range(1000000):
younger = random_kid()
older = random_kid()
if older == 'girl':
older_girl += 1
if older == 'girl' and younger == 'girl':
both_girls += 1
if older == 'girl' or younger == 'girl':
either_girl += 1
problem_one = both_girls/older_girl
problem_two = both_girls/either_girl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment