简体   繁体   中英

How to initialize sample values in PyMC3?

I have a model with 4 priors and a custom likelihood. Everything works fine when running the sampling method with its default values and 1 chain. However, I need to change the initial values used by the algorithm as they come from research work that I am trying to replicate in PyMC.

I have tried passing the init value in the distribution directly or as a dictionary in the sampling method but they are not being used (the output is always the same no 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 with this? Thank you so much,

My code :

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)

Generally, initial values should have no significant effect on inference results, and if they did it would indicate there is something problematic about the sampling strategy/parameterization. They could have some effect on the efficiency of sampling, but the NUTS tuning phase should proceed to the point that tuned parameters are similar no matter the initial values .

Further, all the samples from tuning stage are discarded by default. So, unless one explicitly overrides this, the chains starting positions should also be independent of the initial values.

What does matter, and what can meaningfully be informed by domain expertise, are prior parameters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM