-
Notifications
You must be signed in to change notification settings - Fork 1
/
Game Theory3.R
60 lines (45 loc) · 1.67 KB
/
Game Theory3.R
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
run.model <- function(time, switch) {
# Runs the Model
#
# Args:
# time: Number of iterations
# switch: Boolean of switching strategy
# Returns:
# WinRate (wins/time)
#
# number of wins counter
wins <- c(0)
for (i in 1:time) {
# get a random ordering of 1,2,3 - 3 represents the FERRARI
objs <- sample(1:3)
# since its randomly distriubted, KISS, we will always initially pick the first door
pick <- objs[1]
# reveal a goat-door: choose a SINGLE door that isn't the original pick and isn't the car, and remove it from the set
reveal <- objs[objs != pick & objs < 3] # reveal is copy of objs (WHERE obj != pick AND obj < 3)
reveal <- reveal[1] # need this line in case reveal is both 1 and 2
objs <- objs[objs != reveal] # objs is now a copy of itself, minus the single revealed goat-door
# implement strategy
if (switch) {
newPick <- objs[objs != pick]
} else {
newPick <- pick
}
# win or lose!
if (newPick == 3) {
wins = wins + 1
}
}
message ("Switch Strategy:", switch)
message ("Total Wins: ", wins)
message ("Out of ", time)
return(wins/time)
}
Now we can run the model (dr evil voice) 1 MILLION times and see what win rate we get for each strategy:
switchWinRate <- run.model(1e+06, TRUE) # Switch TRUE
switchWinRate
[1] 0.667036
Interesting! My hypothetical value was 1/2 for the switch strategy, but it is actually 2/3. Time to think about it some moar…
noSwitchWinRate <- run.model(1e+06, FALSE) # Switch FALSE
noSwitchWinRate
[1] 0.333072
NoSwitch strategy stayed at 1/3, which was the theoretical value.