-
Notifications
You must be signed in to change notification settings - Fork 1
/
win_probability.html
256 lines (231 loc) · 6.54 KB
/
win_probability.html
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width">
<title> Win Probability </title>
<link rel="stylesheet" type="text/css" href="/css/basscss.min.css">
<script type="text/javascript" src="/js/jquery/jquery-2.1.4.min.js"></script>
<script type="text/javascript" src="/templates/templates.js"></script>
<script type="text/javascript" src="/templates/ga.js"></script>
<script type="text/javascript" src="/js/d3/d3.min.js"></script>
<script type="text/javascript" src="/js/plotly/plotly.min.js"></script>
<script type="text/javascript" src="/js/chancejs/chance.min.js"></script>
<link rel="shortcut icon" type="image/png" href="/favicon.png">
<script src="https://use.fontawesome.com/83536f4ef8.js"></script>
<meta name="twitter:card" content="summary" />
<meta name="twitter:site" content="@dannypage" />
<meta name="twitter:title" content="Win Probability Simulator" />
<meta name="twitter:description" content="Creating a better way to graph Win Probability." />
<meta name="twitter:image" content="/assets/images/win_prob_preview.png" />
</head>
<body>
<div id="navHeader"></div>
<section class="container">
<h1>Win Probability Simulator</h1>
<div class="clearfix">
<div class="sm-col sm-col-12 px1">
<form id="simulate">
<label for="homeGoals">Home Goals Per Game</label>
<input id="homeGoals" type="text" class="col-3 mb1 field" value=1.5>
<label for="awayGoals">Away Goals Per Game</label>
<input id="awayGoals" type="text" class="col-3 mb1 field" value=1.2>
<a type="button" class="button" onclick="step()">Simulate</a>
</form>
</div>
</div>
<div class="clearfix">
<h2>Win Margin (Estimated and Actual)</h2>
<div class="sm-col sm-col-12 ">
<div id="goalProbabilityChart" style="height: 300px; width: 100%;"></div>
</div>
<h2>Win Probability</h2>
<div class="sm-col sm-col-12 ">
<div id="winProbabilityChart" style="height: 300px; width: 100%;"></div>
</div>
</div>
</section>
<script>
var minutes = 0;
var score = [0];
var expected = [];
var winprob = [];
var expectedTrace, actualTrace;
var homeGPG, awayGPG;
function margin(home, away) {
return 0;
}
function simulate() {
var h_result = parseInt(chance.weighted([0,1],[1-(homeGPG/90),homeGPG/90]) ) || 0;
var a_result = parseInt(chance.weighted([0,1],[1-(awayGPG/90),awayGPG/90]) ) || 0;
var result = h_result-a_result;
return result;
}
function factorialize(num) {
if (num === 0 || num === 1)
return 1;
for (var i = num - 1; i >= 1; i--) {
num *= i;
}
return num;
}
function calcGoals(lambda, minutes) {
var goals = {};
var adjLambda = lambda-lambda*(minutes/90)
for (i=0;i<8;i++){
goals[i] = ((adjLambda**i)*(2.7182818**-adjLambda))/factorialize(i);
}
return goals;
}
function winProbability (homeChances, awayChances, homeScore) {
var prob = {
homeProb: 0,
awayProb: 0,
total: 0
};
for (i=0;i<10000;i++){
var h = parseInt( chance.weighted(Object.keys(homeChances),Object.values(homeChances)) );
var a = parseInt( chance.weighted(Object.keys(awayChances),Object.values(awayChances)) );
if ( (h + homeScore) > a) {
prob['homeProb'] += 1;
} else if (a > (h + homeScore) ) {
prob['awayProb'] += 1;
} else {
prob['homeProb'] += 1;
prob['awayProb'] += 1;
}
}
return (prob['homeProb']/(prob['homeProb']+prob['awayProb'])).toFixed(2);
}
function updateTraces() {
expectedTrace = {
y: expected,
name: 'Expected Final Score',
mode: 'lines',
line: {
color: 'gray',
dash: 'dot',
width: 2
}
};
actualTrace = {
y: score,
mode: 'lines',
name: 'Current Score',
line: {
color: 'black',
width: 3
}
};
winProbTrace = {
y: winprob,
mode: 'lines',
line: {
color: 'black'
}
}
}
var goalLayout = {
xaxis: {
range: [0, 91],
autotick: false,
tick0: 0,
dtick: 15,
gridwidth: 2
},
yaxis: {
range: [-5, 5],
hoverformat: '.2f',
title: 'Home Margin',
gridwidth: 2
},
margin: {
l: 40,
r: 40,
b: 40,
t: 20,
pad: 3
},
legend: {
x: 0.01,
y: 1,
bgcolor: 'rgba(0,0,0,0)'
}
};
var winLayout = {
xaxis: {
range: [0, 91],
autotick: false,
tick0: 0,
dtick: 15,
gridwidth: 2
},
yaxis: {
title: 'Home Win %',
range: [0, 1.01],
hoverformat: '.2f',
showgrid: true,
autotick: false,
tick0: 0,
dtick: 0.25,
gridwidth: 2
},
margin: {
l: 40,
r: 40,
b: 40,
t: 20,
pad: 3
},
legend: {
x: 0.01,
y: 1,
bgcolor: 'rgba(0,0,0,0)'
}
};
updateTraces();
Plotly.plot('goalProbabilityChart', [expectedTrace, actualTrace], goalLayout);
Plotly.plot('winProbabilityChart', [winProbTrace], winLayout);
function step() {
minutes = 0;
score = [0];
expected = [];
winprob = [];
homeGPG = parseFloat( $('#homeGoals').val() ) || 0;
awayGPG = parseFloat( $('#awayGoals').val() ) || 0;
expected.push(homeGPG-awayGPG);
var homeExpected = calcGoals(homeGPG, minutes);
var awayExpected = calcGoals(awayGPG, minutes);
winprob.push(winProbability(homeExpected, awayExpected, score[0]));
updateTraces();
var interval = setInterval(function() {
var minuteScore = simulate();
var currentScore = score[score.length-1];
score.push( currentScore + minuteScore );
expected.push ( expected[0]-expected[0]*(minutes/90)+score[score.length-1] );
var homeExpected = calcGoals(homeGPG, minutes);
var awayExpected = calcGoals(awayGPG, minutes);
winprob.push(winProbability(homeExpected, awayExpected, score[score.length-1]));
updateTraces();
Plotly.newPlot('goalProbabilityChart', [expectedTrace, actualTrace], goalLayout)
Plotly.newPlot('winProbabilityChart', [winProbTrace], winLayout)
minutes +=1;
if(minutes > 90) clearInterval(interval);
}, 100);
}
</script>
<script type="text/javascript">
$( document ).ready(function() {
//On Load
step();
//On Form Submit
$('#simulate').keyup(function(event) {
if(event.keyCode === 13) {
step();
}
});
});
</script>
<div id="navFooter"></div>
</body>
</html>