-
Notifications
You must be signed in to change notification settings - Fork 0
/
Martingale_Monte_Carlo.jl
183 lines (166 loc) · 5.58 KB
/
Martingale_Monte_Carlo.jl
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
using Distributions
using Gadfly
using DataFrames
# Single game function with an uncertain outcome considering a house edge of 1.5 percent
function rollDice()
rnd = rand(1:1000)
if rnd < 516
return false
else
return true
end
end
# Wagering function of variable wager multiplier until wager count or all funds lost
function Martingale(multiplier, funds, initialWager, count)
global bustsCount
global profitsCount
counter = 1
wager = initialWager
value = funds
prevWager = "win"
while counter <= count
if prevWager == "win"
wager = initialWager
if rollDice()
value += wager
prevWager = "win"
else
value -= wager
prevWager = "loss"
if value <= 0
bustsCount += 1
break
end
end
elseif prevWager == "loss"
wager *= multiplier
if (value - wager) < 0
wager = value
end
if rollDice()
value += wager
prevWager = "win"
else
value -= wager
prevWager = "loss"
if value <= 0
bustsCount += 1
break
end
end
end
counter += 1
end
if value > funds
profitsCount += 1
end
end
# Wagering function of constant wager multiplier until wager count or all funds lost
function bettor(funds=20000, initialWager=100, count=1000)
counter = 1
wager = initialWager
value = funds
prevWager = "win"
while counter <= count
if prevWager == "win"
wager = initialWager
if rollDice()
value += wager
prevWager = "win"
push!(fundsArr, value)
else
value -= wager
prevWager = "loss"
push!(fundsArr, value)
if value <= 0
break
end
end
elseif prevWager == "loss"
wager *= multiplier
if (value - wager) < 0
wager = value
end
if rollDice()
value += wager
prevWager = "win"
push!(fundsArr, value)
else
value -= wager
prevWager = "loss"
push!(fundsArr, value)
if value <= 0
break
end
end
end
counter += 1
end
println("Total Wager Count: ", counter)
println("Initial Funds: ", funds)
println("Terminal Value: ", value)
global mdf=DataFrame(A=[1:length(fundsArr)], B=[fundsArr])
end
# multiplier = 1
srand(10)
global fundsArr = Float64[]
global multiplier = 1
bettor()
plot(layer(yintercept=[mdf[end,:B]], Geom.hline(color="brown", size=.5mm)),
layer(yintercept=[mdf[1,:B]], Geom.hline(color="orange", size=.5mm)),
layer(x=mdf[:A], y=mdf[:B], Geom.line),
Guide.xlabel("Wager Count"), Guide.ylabel("Value", orientation=:vertical),
Theme(line_width=.5mm, default_color=color("darkgreen")))
# multiplier = 1.5
srand(10)
global fundsArr = Float64[]
global multiplier = 1.5
bettor()
plot(layer(yintercept=[mdf[end,:B]], Geom.hline(color="brown", size=.5mm)),
layer(yintercept=[mdf[1,:B]], Geom.hline(color="orange", size=.5mm)),
layer(x=mdf[:A], y=mdf[:B], Geom.line),
Guide.xlabel("Wager Count"), Guide.ylabel("Value", orientation=:vertical),
Theme(line_width=.5mm, default_color=color("darkgreen")))
# multiplier = 2
srand(10)
global fundsArr = Float64[]
global multiplier = 2
bettor()
plot(layer(yintercept=[mdf[end,:B]], Geom.hline(color="brown", size=.5mm)),
layer(yintercept=[mdf[1,:B]], Geom.hline(color="orange", size=.5mm)),
layer(x=mdf[:A], y=mdf[:B], Geom.line),
Guide.xlabel("Wager Count"), Guide.ylabel("Value", orientation=:vertical),
Theme(line_width=.5mm, default_color=color("darkgreen")))
# Risk-return trade off
global profArr = Float64[]
global bustArr = Float64[]
global multArr = Float64[]
srand(10)
for mult = 1:0.05:2.5 # trying a range of wager multiplier from 1 to 2.5 with step of 0.05
bustsCount = 0
profitsCount = 0
sampleSize = 10000
currentCount = 1
while currentCount <= sampleSize
Martingale(mult, 10000, 100, 200)
currentCount += 1
end
push!(multArr, mult)
push!(profArr, 100*profitsCount/sampleSize) # Average number of samples where bettor gained profit
push!(bustArr, 100*bustsCount/sampleSize) # Average number of samples where bettor lost his funds
end
bustDf=DataFrame(A=[multArr], B=[bustArr], Metric="Broke")
profDf=DataFrame(A=[multArr], B=[profArr], Metric="Profit")
df= vcat(bustDf, profDf)
minProf = 50 # min Return set by bettor
maxBust = 25 # max Risk set by bettor
plot(layer(xintercept=[minimum(profDf[profDf[:B] .>= minProf, :A]), maximum(profDf[profDf[:B] .>= minProf, :A])],
Geom.vline(color="orange", size=.1mm)),
layer(xintercept=[profDf[profDf[:B] .== maximum(profDf[:B]), :A]], Geom.vline(color="cyan", size=.1mm)),
layer(yintercept=[minimum(profDf[profDf[:B] .>= minProf, :B]), maximum(bustDf[bustDf[:B] .<= maxBust, :B])],
Geom.hline(color="orange", size=.1mm)),
layer(xintercept=[maximum(bustDf[bustDf[:B] .<= maxBust, :A])], Geom.vline(color="magenta", size=.1mm)),
layer(df, x=df[:A], y=df[:B], color="Metric", Geom.line, Theme(line_width=.8mm)),
Guide.xlabel("Multiplier"),
Guide.ylabel("Rate %", orientation=:vertical),
Scale.discrete_color_manual("brown","darkgreen"))