Hi All,
I am a newbie for using pymc3 and now in the early stage of transforming previous R MCMC code to python.
My problems are:
- Is it possible to use pandas functions inside of pymc context? Cause I have seen several post saying that it is not recommended to use.
- Running this code will result in dead kernel and it will use lots of memory. I don’t know why this will happen
Here are the main part of my code:
with model:
### prior for unknown parameters
# prior for lambda
BoundedNormal_lam = pm.Bound(pm.Normal, lower=lam_lbd, upper=lam_ubd)
lam = BoundedNormal_lam(‘lam’, mu=lam_mean, sd=lam_sd, shape = 8)
# prior for gamma
BoundedNormal_gamma = pm.Bound(pm.Normal, lower=0.2, upper=0.8)
gamma = BoundedNormal_gamma(‘gamma’, mu=0.5, sd=0.1)
# prior for beta
BoundedNormal_beta = pm.Bound(pm.Normal, lower=b_lbd, upper=b_ubd)
beta = BoundedNormal_beta(‘beta’, mu=b_mean, sd=b_sd, shape = 8)
# prior for v
v = pm.Uniform(‘v’, lower = 1e-2, upper =1e2)
### Get expected value of outcome
xxdat = tpdat
# decayed promotion: pde * lam_j^{t-l}
xxdat['ad'] = (xxdat['pde_norm'].values) * (lam[xxdat['chnl_cd'].values-1]**(xxdat['gap_nrx_promo'].values))
# Sum up of decayed promotion for each NRx + each channel: \sum_t pde * lam_j^{t-l}
yydat = xxdat.groupby(['spcl_st_cd','nrx_wk', 'chnl_cd', 'nrx_norm'], as_index= False).agg({'ad':np.sum})
yydat.rename(columns = {'ad': 'ad_sum'}, inplace = True)
yydat['ad_sum_r'] = yydat['ad_sum'].values ** gamma # Add adstock diminishing factor (\sum_t pde * lam_j^{t-l})^r
# normalize each channel's adstock
yydat['ad_norm'] = np.divide(yydat['ad_sum_r'],yydat.groupby('chnl_cd', as_index=False).agg({'ad_sum_r':np.mean})['ad_sum_r'][yydat['chnl_cd'].values-1])
# sum up adstock multiplying impact factor over channels
yydat['bx'] = beta[yydat['chnl_cd'].values -1]*(yydat['ad_norm'])
zzdat = yydat.groupby(['spcl_st_cd', 'nrx_wk', 'nrx_norm'], as_index = False).agg({'bx':np.sum})
# Get the final mu
mu = np.mean(zzdat['bx'])
### likelihood (sampling distribution) of observations
Y_obs = pm.Normal('Y_obs', mu=mu, sd = v, observed = zzdat['nrx_norm'])
Any suggestions are welcomed! Thanks in advance.
Best,
Fan