Created
August 14, 2021 20:16
-
-
Save lvaughn/df3399c44de3b137fafbb213057ee480 to your computer and use it in GitHub Desktop.
My answer to 538's Riddler Express for 8/13/2021
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# | |
# A script that answers the Riddler Express for 8/13/21 three different ways | |
# https://fivethirtyeight.com/features/are-you-clever-enough/ | |
# | |
# 1) Using a Monte Carlo simulation with the possibility of resample | |
# 2) Slow Monte Carlo that doesn't have resample | |
# 3) Calculating the odds using a Binomial Distribution and averaging it across the | |
# all the percentiles form 90th to 99.999th | |
# | |
# Author: lowell@lowellvaughn.com | |
import random | |
POPULATION = 1000 | |
ROUNDS = 10000000 | |
# Method 1 | |
n_cleverest = 0 | |
for _ in range(ROUNDS): | |
my_cleverness = 0.9 + random.random()/10 | |
is_cleverest = True | |
for i in range(9): | |
if random.random() > my_cleverness: | |
is_cleverest = False | |
break | |
if is_cleverest: | |
n_cleverest += 1 | |
print(f"Monte Carlo 1: p={n_cleverest/ROUNDS:.4f}") | |
# Method 2 | |
n_cleverest = 0 | |
for i in range(ROUNDS): | |
population = list(range(POPULATION)) | |
me = random.choice(population[-(POPULATION//10):]) | |
population.remove(me) | |
group = random.choices(population, k=9) | |
if not any(a > me for a in group): | |
n_cleverest += 1 | |
print(f"Monte Carlo 2, the slowening: p={n_cleverest/ROUNDS:.4f}") | |
# Method 3 | |
total_prob = 0 | |
for i in range(ROUNDS): | |
my_cleverness = 0.9 + i/(ROUNDS*10) | |
total_prob += my_cleverness ** 9 | |
print(f"Binomial averaging: p={total_prob/ROUNDS:.4f}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment