Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,34 @@
from random import choice
def sleeping_beauty_experiment(repetitions):
"""
Run the Sleeping Beauty Problem experiment `repetitions` times, checking to see
how often we had heads on waking Sleeping Beauty.
"""
gotheadsonwaking = 0
wakenings = 0
for _ in range(repetitions):
coin_result = choice(["heads", "tails"])
# On Monday, we check if we got heads.
wakenings += 1
if coin_result == "heads":
gotheadsonwaking += 1
# If tails, we do this again, but of course we will not add as if it was heads..
if coin_result == "tails":
wakenings += 1
if coin_result == "heads":
gotheadsonwaking += 1 # never done
# Show the number of times she was wakened.
print("Wakenings over", repetitions, "experiments:", wakenings)
# Return the number of correct bets SB made out of the total number
# of times she is awoken over all the experiments with that bet.
return gotheadsonwaking / wakenings
CREDENCE = sleeping_beauty_experiment(1_000_000)
print("Results of experiment: Sleeping Beauty should estimate a credence of:", CREDENCE)

View file

@ -0,0 +1,59 @@
'''Sleeping Beauty Problem'''
from random import choice
from itertools import repeat
from functools import reduce
# experiment :: (Int, Int) -> IO (Int, Int)
def experiment(headsWakings):
'''A pair of counts updated by a coin flip.
'''
heads, wakings = headsWakings
return (
1 + heads, 1 + wakings
) if "h" == choice(["h", "t"]) else (
heads, 2 + wakings
)
# ------------------------- TEST -------------------------
# main :: IO ()
def main():
'''Observed results from one million runs.'''
n = 1_000_000
heads, wakes = applyN(n)(
experiment
)(
(0, 0)
)
print(
f'{wakes} wakenings over {n} experiments.\n'
)
print('Sleeping Beauty should estimate credence')
print(f'at around {round(heads/wakes, 3)}')
# ----------------------- GENERIC ------------------------
# applyN :: Int -> (a -> a) -> a -> a
def applyN(n):
'''n applications of f.
(Church numeral n).
'''
def go(f):
def ga(a, g):
return g(a)
def fn(x):
return reduce(ga, repeat(f, n), x)
return fn
return go
# MAIN ---
if __name__ == '__main__':
main()