with pm.Model() as model:
#Goal Hyperpriors Parameters
beta_attackG_bar = pm.Normal('G_betaA_bar', 1, 1)
beta_defenseG_bar = pm.Normal('G_betaD_bar', 0, 1)
sigmaG_Abar = pm.Exponential('G_sigma_Abar',1)
sigmaG_Dbar = pm.Exponential('G_sigma_Dbar',1)
#Shot Hyperpriors Parameters
beta_attackS_bar = pm.Normal('S_betaA_bar', 1, 1)
beta_defenseS_bar = pm.Normal('S_betaD_bar', 0, 1)
sigmaS_Abar = pm.Exponential('S_sigma_Abar',1)
sigmaS_Dbar = pm.Exponential('S_sigma_Dbar',1)
#Goal Priors
beta_attackG = pm.Normal('betaAG', beta_attackG_bar, sigma = sigmaG_Abar, shape = team_number)
beta_defenseG = pm.Normal('betaDG', beta_defenseG_bar, sigma = sigmaG_Dbar, shape = team_number)
beta_HAG = pm.Normal('HAG', mu = 0, sigma = 1)
#Shot Priors
beta_attackS = pm.Normal('betaAS', beta_attackS_bar, sigma = sigmaG_Abar, shape = team_number)
beta_defenseS = pm.Normal('betaDS', beta_defenseS_bar, sigma = sigmaG_Dbar, shape = team_number)
beta_HAS = pm.Normal('HAS', mu = 0, sigma = 1)
#Goal Parameter Regression Priors
coef_HAG = pm.Normal('beta_HAG', mu = 0, sigma = 3)
coef_HDG = pm.Normal('beta_HDG', mu = 0, sigma = 3)
coef_AAG = pm.Normal('beta_AAG', mu = 0, sigma = 3)
coef_ADG = pm.Normal('beta_ADG', mu = 0, sigma = 3)
coef_HGI = pm.Normal('beta_HGI', mu = 0, sigma = 1)
coef_AGI = pm.Normal('beta_AGI', mu = 0, sigma = 1)
#Shot Parameter Regression Priors
coef_HAS = pm.Normal('beta_HAS', mu = 0, sigma = 3)
coef_HDS = pm.Normal('beta_HDS', mu = 0, sigma = 3)
coef_AAS = pm.Normal('beta_AAS', mu = 0, sigma = 3)
coef_ADS = pm.Normal('beta_ADS', mu = 0, sigma = 3)
coef_ASI = pm.Normal('beta_ASI', mu = 0, sigma = 1)
coef_HSI = pm.Normal('beta_HSI', mu = 0, sigma = 1)
#Shot + Goal Parameter Regression Priors
coef_T_HG = pm.Normal('coef_T_HG', mu = 0, sigma = 3)
coef_T_AG = pm.Normal('coef_T_AG', mu = 0, sigma = 3)
coef_T_HS = pm.Normal('coef_T_HS', mu = 0, sigma = 3)
coef_T_AS = pm.Normal('coef_T_AS', mu = 0, sigma = 3)
coef_HI = pm.Normal('coef_T_HI', mu = 0, sigma = 1)
coef_AI = pm.Normal('coef_T_AI', mu = 0, sigma = 1)
#Game Simulation Parameters
Bounded_StudentT = pm.Bound(pm.StudentT, lower = 0.0)
rho_10 = Bounded_StudentT("rho_10", nu = 1, sigma = 1.5)
rho_01 = Bounded_StudentT("rho_01", nu = 1, sigma = 1.5)
rho_11 = Bounded_StudentT("rho_11", nu = 1, sigma = 1.5)
rho_21 = Bounded_StudentT("rho_21", nu = 1, sigma = 1.5)
rho_12 = Bounded_StudentT("rho_12", nu = 1, sigma = 1.5)
rho_xx = Bounded_StudentT("rho_22", nu = 1, sigma = 1.5)
rho_x0 = Bounded_StudentT("rho_x0", nu = 1, sigma = 1.5)
rho_0x = Bounded_StudentT("rho_0x", nu = 1, sigma = 1.5)
half_booster = Bounded_StudentT("half_booster", nu = 1, sigma = 1.5)
full_booster = Bounded_StudentT("full_booster", nu = 1, sigma = 1.5)
H_time_booster = pm.Uniform("H_time_booster", lower = 0, upper = 1)
A_time_booster = pm.Uniform("A_time_booster", lower = 0, upper = 1)
for index, row in goal_model_data.iterrows():
#Goal Regression Parameter
thetaHG = coef_HAG*beta_attackG[row["H_team_A"]] + coef_ADG*beta_defenseG[row["A_team_D"]] + beta_HAG + coef_HGI
thetaAG = coef_AAG*beta_attackG[row["A_team_A"]] + coef_HDG*beta_defenseG[row["H_team_D"]] + coef_AGI
#Shot Regression Parameter
thetaHS = coef_HAS*beta_attackS[row["H_team_A"]] + coef_ADS*beta_defenseS[row["A_team_D"]] + beta_HAS + coef_HSI
thetaAS = coef_AAS*beta_attackS[row["A_team_A"]] + coef_HDS*beta_defenseS[row["H_team_D"]] + coef_ASI
Home_Shots = pm.Poisson("Home_Shots", mu = np.exp(thetaHS), observed = row["HST"])
Away_Shots = pm.Poisson("Away_Shots", mu = np.exp(thetaAS), observed = row["AST"])
#Goal + Shot Final Pre-Game Average Goal Parameter
final_thetaH = pm.Deterministic("final_thetaH", coef_T_HG*thetaHG + coef_T_HS*thetaHS + coef_HI)
final_thetaA = pm.Deterministic("final_thetaA", coef_T_AG*thetaAG + coef_T_AS*thetaAS + coef_AI)
#Check if Home Team scored and initialize relevant variables accordingly: Home Total Goals (HTG) and Home Goal Times (HT_Times)
if '' not in row["HT_GT"]:
HTG = len(row["HT_GT"])
HT_Times = [int(x)/90 for x in row["HT_GT"]]
else:
HTG = -1
#Check if Away Team scored and initialize relevant variables accordingly: Away Total Goals (ATG) and Away Goal Times (AT_Times)
if '' not in row["AT_GT"]:
ATG = len(row["AT_GT"])
AT_Times = [int(x)/90 for x in row["AT_GT"]]
else:
ATG = -1
#Initialize Home Goals, Away Goals, and Score State
H = 0
A = 0
score = 0
if HTG + ATG > 0:
while H < HTG - 1 or A < ATG - 1:
#Assign Goal time > 1 if no more goals to be scored or no goals scored in game
if H == HTG -1 or HTG == -1:
temp_H_time = 1.1
else:
temp_H_time = HT_Times[H]
if A == ATG -1 or HTG == -1:
temp_A_time = 1.1
else:
temp_A_time = AT_Times[A]
#Boolean Variables to determine who scored
H_signal = (temp_H_time < temp_A_time or temp_H_time == temp_A_time)
A_signal = not H_signal
H_time = temp_H_time
A_time = temp_A_time
#Boolean Variables to determine if goal in 45th minute or 90th minute
half_signal = (H_signal*H_time == .5) or (A_signal*A_time == .5)
full_signal = (H_signal*H_time == 1) or (A_signal*A_time == 1)
if H + A == 0:
final_lambda = pm.math.switch(H_signal, final_thetaH, final_thetaA)\
*pm.math.switch(half_signal, half_booster, 1)\
*pm.math.switch(full_signal, full_booster, 1)
elif score == 1:
final_lambda = pm.math.switch(H_signal, final_thetaH, final_thetaA)\
*pm.math.switch(half_signal, half_booster, 1)\
*pm.math.switch(full_signal, full_booster, 1)\
*pm.match.switch(H >= 2, rho_21, rho_10)
elif score == -1:
final_lambda = pm.math.switch(H_signal, final_thetaH, final_thetaA)\
*pm.math.switch(half_signal, half_booster, 1)\
*pm.math.switch(full_signal, full_booster, 1)\
*pm.math.switch(A >= 2, rho_12, rho_01)
elif score == 0:
final_lambda = pm.math.switch(H_signal, final_thetaH, final_thetaA)\
*pm.math.switch(half_signal, half_booster, 1)\
*pm.math.switch(full_signal, full_booster, 1)\
*pm.math.switch(H >= 2, rho_xx, rho_11)
elif score >= 2:
final_lambda = pm.math.switch(H_signal, final_thetaH, final_thetaA)\
*pm.math.switch(half_signal, half_booster, 1)\
*pm.math.switch(full_signal, full_booster, 1)\
*rho_x0
elif score <= -2:
final_lambda = pm.math.switch(H_signal, final_thetaH, final_thetaA)\
*pm.math.switch(half_signal, half_booster, 1)\
*pm.math.switch(full_signal, full_booster, 1)\
*rho_0x
if H_signal:
data = H_time
H += 1
final_time_adjusted_lambda = final_lambda + data*H_time_booster
else:
data = A_time
A += 1
final_time_adjusted_lambda = final_lambda + data*A_time_booster
score = H - A
current_mu = pm.Deterministic("current_mu", np.exp(final_time_adjusted_lambda))
Y = pm.Exponential(lam = current_mu, observed = data)
else:
Y = pm.Exponential(lam = np.exp(final_thetaH + final_thetaA), observed = 0.0)
I am getting “Cannot cast True or False as a tensor variable. Please use np.array(True) or np.array(False) if you need these constants. This error might be caused by using the == operator on Variables. v == w does not do what you think it does, use theano.tensor.eq(v, w) instead.” as an error on the following line:
pm.math.switch(H_signal, final_thetaH, final_thetaA)
*pm.math.switch(half_signal, half_booster, 1)
*pm.math.switch(full_signal, full_booster, 1)
I am not sure what the issue is or how to fix it. H_signal, half_signal, and full_signal are all boolean variables used to trigger when to multiply final_thetaH or final_thetaA by a relevant parameter.