-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutcome_gen.py
74 lines (61 loc) · 2.33 KB
/
outcome_gen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# -*- coding: utf-8 -*-
"""
Testing Monte Hall's paradox for three choices.
"""
import matplotlib.pyplot as plt
import random
import numpy as np
import sys
def keep_choice(trials,basket_size):
hist = []
for i in range(trials):
baskets = np.zeros((1,basket_size))#make baskets
baskets[:,random.randint(0,basket_size-1)] = 1#plant gold bullion
if baskets[:,random.randint(0,basket_size-1)] == True:#pick basket and record win/loss
hist.append(1)
else:
hist.append(0)
prob = np.sum(np.asarray(hist))/trials
return prob
def switch_choice(trials,basket_size):
hist = []
for i in range(trials):
baskets = np.zeros((1,basket_size))#make baskets
baskets[:,random.randint(0,basket_size-1)] = 1#plant gold bullion
pick = random.randint(0,basket_size-1) #pick basket
snakes = 0#Track number of snakes revealed.
snakes_coord = []#Track indices of revealed snakes.
for i in range(basket_size):#Revealing N-2 snakes
if i != pick and baskets[:,i] == False:#Ensure we skip our first choice and the bullion.
snakes_coord.append(i)
snakes+=1
if snakes == basket_size-2:#Break the loop when we unconvered enough snakes.
break
snakes_coord.append(pick)#We will switch our first choice anyway. Therefore we assume it has a snake.
for i in range(basket_size):#Switching choice.
if i in snakes_coord:
continue
else:
pick = i
if baskets[:,pick] == True:#open basket and record win/loss
hist.append(1)
else:
hist.append(0)
prob = np.sum(np.asarray(hist))/trials
return prob#Record outcome
'''
Program Begins Here
'''
prob_stay = []
prob_switch = []
trials = range(99)#Indicate number of trials.
baskets = 3#Indicate number of baskets.
for i in trials:
prob_stay.append(keep_choice(i+1,baskets))
prob_switch.append(switch_choice(i+1,baskets))
plt.plot(trials,prob_stay,marker="o",label='keep choice')
plt.plot(trials,prob_switch,marker="o",label='switch choice')
plt.xlabel('Number of trials')
plt.ylabel('Probability')
plt.legend()
plt.show()