Skip to content

Instantly share code, notes, and snippets.

@MatthewCaseres
Created October 19, 2021 17:27
Show Gist options
  • Select an option

  • Save MatthewCaseres/c7b0b791b456be199acd9e3bc4082549 to your computer and use it in GitHub Desktop.

Select an option

Save MatthewCaseres/c7b0b791b456be199acd9e3bc4082549 to your computer and use it in GitHub Desktop.
from dataclasses import dataclass
from typing import Literal
import numpy as np
from statistics import mean
np.random.seed(6420)
GradeType = Literal['A', 'B', '<=C']
@dataclass(frozen=True)
class Observation:
bull: bool
grade: GradeType
car: bool
lottery: bool
redington: bool
def simulateObservation() -> Observation:
bull = np.random.uniform() <= .5
grade: GradeType = np.random.choice(["A", "B", "<=C"], p=[0.6, 0.3, 0.1])
if bull:
if grade == "A":
car = np.random.uniform() <= .8
elif grade == "B":
car = np.random.uniform() <= .5
else:
car = np.random.uniform() <= .2
else:
if grade == "A":
car = np.random.uniform() <= .5
elif grade == "B":
car = np.random.uniform() <= .3
else:
car = np.random.uniform() <= .1
lottery = np.random.uniform() <= .001
if lottery:
redington = np.random.uniform() <= .99
else:
if car:
redington = np.random.uniform() <= .7
else:
redington = np.random.uniform() <= .2
return Observation(bull, grade, car, lottery, redington)
obs: list[Observation] = [simulateObservation() for _ in range(100_000)]
# filter out observations where she is not at redington
obs = [o for o in obs if o.redington]
print(mean(o.car for o in obs))
print(mean(o.lottery for o in obs))
print(mean(o.grade == "B" for o in obs))
print(mean(not o.bull for o in obs))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment