def llik_fun(beta, df):
beta0 = beta[0]
beta1 = beta[1]
beta2 = beta[2]
df = df.assign(loglamb = beta0 + beta1 * df['RQ'] + beta2 * df['ATL'])
df = df.assign(lamb = np.exp(df['loglamb']))
ll = []
lamb = df['lamb']
FTD = df['FTD']
for i in range(0,len(df)):
if df.iloc[i,8] == 1:
ll.append(np.log(np.exp(-lamb[i]*FTD[i])))
else:
ll.append(np.log(lamb[i] * np.exp(-lamb[i]*FTD[i])))
df = df.assign(ll = ll)
averagell = np.sum(ll)/len(ll)
print(averagell)
llik = -np.sum(ll)
return llik
llik_fun([1.5, 0.1, 0.1], df)
This function with the given parameter values does yield the exact result that is given on my answer sheet. But when I am trying to minimize the function. It is not converging and giving back values that I do not expect at all. What is going wrong?
beta0_ini = -np.log(np.sum(df['FTD'])/len(df))
beta1_ini = 0
beta2_ini = 0
beta_ini = np.array([beta0_ini, beta1_ini, beta2_ini])
results = optimize.minimize(llik_fun, beta_ini, args = (df), options = options, method = 'L-BFGS-B', bounds = ( (-10, 10), (-10, 10), (0.00001, 100)))
print(results.x)
print(results.fun)
print(results.success)