Below is a sample of the dataset I am using as well as the bgmodel
The summary table was created from utilizing the below code
from lifetimes import BetaGeoFitter
from lifetimes.plotting import plot_period_transactions, plot_frequency_recency_matrix, plot_probability_alive_matrix, plot_history_alive
bgModel = BetaGeoFitter()
bgModel.fit(new_df['lifetime_orders'], new_df['days_last'], new_df['T'])
bgModel.summary
I am trying to get expected number of transactions a customer will do over a year, and the probability that the customer is still alive right now by utilzing the below code
def bgnbd_expectations(x, t_x, T, t):
r, alpha, a, b = bgModel.summary.iloc[:,0]
aa = r+x
bb = b+x
cc = a+b+x-1
z = t/(t+alpha+T)
terms = np.zeros(shape=(151))
terms[0]=1
for i in range(1,len(terms)):
terms[i] = terms[i-1]*(aa+i-1)*(bb+i-1)/((cc+i-1)*i)*z
f2_1 = sum(terms)
if x > 0:
rpos = 1
else:
rpos = 0
eyt = ((a+b+x-1)/(a-1))*(1-(((alpha+T)/(alpha+T+t))**(r+x))*f2_1)/(1+(rpos*(a/(b+x-1)))*((alpha+T)/(alpha+t_x))**(r+x))
prob = 1/(1+(rpos*(a/(b+x-1))*((alpha+T)/(alpha+t_x))**(r+x)))
return eyt, prob
This portion of the code sets up the probablity alive and expected transacations once I run this code I follow it up with
alive = []
nextyear = []
for i, row in new_df.iterrows():
ex, proAl = bgnbd_expectations(row.lifetime_orders, row.days_last, row.T, 52) #52 weeks in a year
alive.append(proAl)
nextyear.append(ex)
new_df['ProbabilityAlive'] = alive
new_df['ExpectedTransactions'] = nextyear
new_df.head()
To produce the output. HOWEVER for some reason the code either runs and the output is a long string that includes the column names from the sample dataset appended to the probability and expected transcation values or I get an Overflow error (OverflowError: (34, 'Result too large'))