Hello,
I have a model with 4 priors and a custom likelihood. Everything works fine when running the sample method with its default values and 1 chain. However I need to change the initial values used by the algorithm as they come from a research work that I am trying to replicate in PyMC.
I have tried passing the initvalue in the distribution directly or as a dictionary in the sample method but they are not being used (the ouput is always the same not matter the initial value I pass and I can see from the trace for each parameter that it doesn’t start there).
Could you help me on this ? Thank you so much,
My code :
import math
from scipy.special import gammaln, betaln,loggamma, gamma
import numpy as np
import aesara.tensor as at
import arviz as az
N11 = N11.astype(np.float64)
E11 = E11.astype(np.float64)
with pm.Model() as model2:
def likelihood(theta,E11,N11):
Mix_p,alpha1,alpha2,beta1,beta2 = theta
const1=np.log(at.gamma(alpha1+N11)) - np.log(at.gamma(alpha1)) - np.log(at.gamma(N11+1))
const2=np.log(at.gamma(alpha2+N11)) - np.log(at.gamma(alpha2)) - np.log(at.gamma(N11+1))
LogL1=const1 - N11*np.log(1+beta1/E11) - alpha1*np.log(1+E11/beta1)
LogL2=const2 - N11*np.log(1+beta2/E11) - alpha2*np.log(1+E11/beta2)
return(np.log(Mix_p*np.exp(LogL1) + (1-Mix_p)*np.exp(LogL2)))
alpha1 = pm.Gamma('alpha1', alpha=1, beta=1)
beta1 = pm.Gamma('beta1', alpha=1, beta=1)
alpha2 = pm.Gamma('alpha2', alpha=1, beta=1)
beta2 = pm.Gamma('beta2', alpha=1, beta=1)
Mix_p =pm.Uniform('Mix_p',0,1,initval=0.333)
theta = (Mix_p,alpha1,alpha2,beta1,beta2)
like = pm.Potential('likelihood',likelihood(theta,E11,N11))
#initvals = {'Mix_p': 0.333, 'alpha1':0.2,'alpha2': 2, 'beta1': 0.1, 'beta2': 4}
with model2:
trace = pm.sample(chains=1,initvals=initvals)
pm.plot_posterior(trace)
az.plot_trace(trace)