Sleeping Beauty problem

From Rosetta Code
Revision as of 22:10, 8 May 2021 by Wherrera (talk | contribs) (New task and Python example)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Sleeping Beauty problem is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Background on the task

In decision theory, The Sleeping Beauty Problem is a problem in invented by Arnold Zoboff and first publicised on Usenet. The experimental subject, named Sleeping Beauty, agrees to an experiment as follows: Sleeping Beauty is volunteers to be put into a deep sleep on a Sunday. There is then a fair coin toss. If this coin toss comes up heads, Sleeping Beauty wakes once (on Monday) and asked to estimated the probability that the coin toss was heads. Her estimate is recorded and she is then put back to sleep for 2 days until Wednesday, at which time the experiment's results are tallied.

If instead the coin toss is tails, Sleeping Beauty wakes as before on Monday and asked to estimated the probability the coin toss was heads, but is then given a drug which makes her forget that she had been woken on Monday before being put back to sleep again, but then wakes only 1 day later, on Tuesday. She is then asked (on Tuesday) again to guess the probability that the coin toss was heads or tails. She is then put back to sleep and awakes as before 1 day later, on Wednesday.

Some decision makers have argued that since the coin toss was fair Sleeping Beaty should always estimate the probability of heads as 1/2, since she does not have any additional information. Others have disagreed, saying that if Sleeping Beauty knows the study design she also knows that she is twice as likely to wake up and be asked to estimate the coin flip on tails than on heads, so the estimate should be 1/3 heads.

Task

Given the above problem, create a Monte Carlo estimate of the actual results. The program should find the proportion of heads on waking and asking Sleeping Beauty for an estimate, as a percentage of the times Sleeping Beauty is asked the question.


Python <lang python>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 add only if tails, so do not add.
       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)

</lang>

Output:

Wakenings over 1000000 experiments: 1499765 Results of experiment: Sleeping Beauty should estimate a credence of: 0.333542254953276