I have a model that generates a rate for an exponential distribution for each row in my data and I am trying to implement a customized likelihood function based on the observed value.
Here is my code:
theta_H = [x for x in range(B_Home_final_data.shape[0])]
theta_A = [x for x in range(B_Away_final_data.shape[0])]
mu_H_game = [x for x in range(B_Home_final_data.shape[0])]
mu_A_game = [x for x in range(B_Away_final_data.shape[0])]
for index, row in B_Home_final_data.iterrows():
theta_H[index] = weekly_attack[int(row[“Offense”])][int(row[“Week”]-1)]*beta_HA + weekly_defense[int(row[“Defense”])][int(row[“Week”]-1)]*beta_AD + beta_HFA + Home_intercept
mu_H_game[index] = theta_H[index]*rho_list[int(row[“Rho_Params”])]extra_booster[int(row[“Extra_Params”])] + H_time_boosterrow[“Time_List”]
for index, row in B_Away_final_data.iterrows():
theta_A[index] = weekly_attack[int(row["Offense"])][int(row["Week"]-1)]*beta_AA + weekly_defense[int(row["Defense"])][int(row["Week"]-1)]*beta_HD + Away_intercept
mu_A_game[index] = np.exp(theta_A[index])*rho_list[int(row["Rho_Params"])]*extra_booster[int(row["Extra_Params"])] + A_time_booster*row["Time_List"]
#Truncated Exponential Likelihood
def exp_trunc_LL(rate, time):
arr = np.array([np.exp(x[0]*x[1]) if x[1] < 0 else x[0]*np.exp(-1*x[0]*x[1]) if x[1] > 0 else np.exp(-1*x[0]) for x in np.column_stack((rate,time))])
return sum(arr)
like_H = pm.Potential('like_H', exp_trunc_LL(mu_H_game, B_Home_final_data["Home_Exp"].values))
like_A = pm.Potential('like_A', exp_trunc_LL(mu_A_game, B_Away_final_data["Away_Exp"].values))