-
Notifications
You must be signed in to change notification settings - Fork 3
/
football1.py
163 lines (131 loc) · 5.46 KB
/
football1.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
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
"""This file contains code for use with "Think Bayes",
by Allen B. Downey, available from greenteapress.com
Copyright 2014 Allen B. Downey
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
"""
from __future__ import print_function, division
import numpy
import thinkbayes2
import thinkplot
from scrape import scrape_team
class Football():
""" Represents hypotheses about a Football teams offense,
in terms of TDs per game and FGs per games
"""
def __init__(self, hypos):
self.TD = ScoreType(hypos[0])
self.FG = ScoreType(hypos[1])
def Update(self, data):
"""Update the child PMFs based on the data.
data = (time since last TD, time since last FG)
"""
self.score.Update(data[0])
self.TD.Update(data[0])
self.FG.Update(data[1])
def UpdateFG(self, delta_time):
"""Update the child PMF based on the data.
delta_time = time since last FG
"""
self.FG.Update(delta_time)
def UpdateTD(self, delta_time):
"""Update the child PMF based on the data.
delta_time = time since last TD
"""
self.TD.Update(delta_time)
def PredRemaining(self, rem_time, points_scored):
"""Plots the predictive distribution for final number of goals.
rem_time: remaining time in the game in minutes
points_scored: points already scored
"""
FGpredict = self.FG.PredRemaining(rem_time, 0)
TDpredict = self.TD.PredRemaining(rem_time, 0)
GoalTotal = FGpredict * 3 + TDpredict * 7
GoalTotal += points_scored
return GoalTotal
class ScoreType(thinkbayes2.Suite):
"""Represents hypotheses about the lambda parameter of a
Poisson Process to generate scores.
"""
def Likelihood(self, data, hypo):
"""Computes the likelihood of the data under the hypothesis.
hypo: hypothetical goal scoring rate in goals per game
data: time between goals in minutes
"""
x = data #time between goals in minutes
lam = hypo/60.0 #goals per minute
like = thinkbayes2.EvalExponentialPdf(x,lam) #evaluating for every value of lamda
return like
def PredRemaining(self, rem_time, score):
"""Plots the predictive distribution for final number of goals.
rem_time: remaining time in the game in minutes
score: number of goals already scored
"""
metapmf=thinkbayes2.Pmf() #PMF about PMFS. probabilities of pmf values
for lam, prob in self.Items(): #loop through probabilities of lamdas
#print(lam,prob)
lt = lam*rem_time/60
pmf=thinkbayes2.MakePoissonPmf(lt,20)
#thinkplot.Pdf(pmf,linewidth=1,alpha=0.2,color='purple')
metapmf[pmf]=prob
mix = thinkbayes2.MakeMixture(metapmf)
mix += score #shift by 2 because we've already seen 2
return mix
def constructPriors():
"""Constructs an even prior for both teams, and then
uses data from www.covers.com from the 2014 season to
update the priors
"""
eagles_url = "/pageLoader/pageLoader.aspx?page=/data/nfl/teams/pastresults/2014-2015/team7.html"
giants_url = "/pageLoader/pageLoader.aspx?page=/data/nfl/teams/pastresults/2014-2015/team8.html"
eagles = Football((numpy.linspace(0, 20, 201), numpy.linspace(0, 20, 201)))
giants = Football((numpy.linspace(0, 20, 201), numpy.linspace(0, 20, 201)))
eagles_data = scrape_team(eagles_url)
giants_data = scrape_team(giants_url)
last_time_FG_eagles = 0
last_time_TD_eagles = 0
for game in eagles_data:
last_time_FG_eagles += 60.0
last_time_TD_eagles += 60.0
for item in game:
if item[2] == "Eagles":
if item[1] == "FG":
rem_time = item[0]
inter_arrival = last_time_FG_eagles - rem_time
last_time_FG_eagles = rem_time
eagles.UpdateFG(inter_arrival)
if item[1] == "TD":
rem_time = item[0]
inter_arrival = last_time_TD_eagles - rem_time
last_time_TD_eagles = rem_time
eagles.UpdateTD(inter_arrival)
last_time_FG_giants = 0
last_time_TD_giants = 0
for game in giants_data:
last_time_FG_giants += 60.0
last_time_TD_giants += 60.0
for item in game:
if item[2] == "Giants":
if item[1] == "FG":
rem_time = item[0]
inter_arrival = last_time_FG_giants - rem_time
last_time_FG_giants = rem_time
giants.UpdateFG(inter_arrival)
if item[1] == "TD":
rem_time = item[0]
inter_arrival = last_time_TD_giants - rem_time
last_time_TD_giants = rem_time
giants.UpdateTD(inter_arrival)
return eagles, giants
def main():
"""Look at the October 12th, 2014 game between the Giants and the Eagles,
and predict the probabilities of each team winning.
"""
eagles, giants = constructPriors()
GoalTotalGiants = giants.PredRemaining(60, 0)
GoalTotalEagles = eagles.PredRemaining(60, 0)
print("Giants win", GoalTotalEagles.ProbLess(GoalTotal_giants))
print("Eagles win", GoalTotalGiants.ProbLess(GoalTotal_eagles))
print(GoalTotalEagles.MakeCdf().CredibleInterval(90))
print(GoalTotalGiants.MakeCdf().CredibleInterval(90))
if __name__ == '__main__':
main()