首页 > 解决方案 > 具有高斯先验的主持人给出 NaN

问题描述

我一直在使用 emcee 对我的参数进行采样,起初我之前的都是统一的

def logprior_BAO(theta):
A, B, C, D, epsilon, rd = theta
if A > 0 and B > 0 and C > 0 and D > 0 and epsilon > -5 and 146.96<=rd<=147.58:
    return 0.0
return -np.inf

它工作得很好。然后,我在高斯之前更改 rd,

def logprior_BAO(theta):
A, B, C, D, epsilon, rd = theta
#flat priors 
if not A > 0 and B > 0 and C > 0 and D > 0 and epsilon > -5:
    return -np.inf
#gaussian prior rd
mu = 147.27
sigma = 0.31
return np.log(1.0/(np.sqrt(2*np.pi)*sigma))-0.5*(rd-mu)**2/sigma**2

程序给了我这个错误

ValueError                                Traceback (most recent call last)
<ipython-input-9-b9ee20e97036> in <module>
      3 move = emcee.moves.StretchMove(a=a_parameter)
      4 sampler = emcee.EnsembleSampler(nwalker, ndims, logposterior,args=argslist, moves=move)
----> 5 sampler.run_mcmc(initial, nsteps, progress=True)

~\anaconda3\lib\site-packages\emcee\ensemble.py in run_mcmc(self, initial_state, nsteps, **kwargs)
    382 
    383         results = None
--> 384         for results in self.sample(initial_state, iterations=nsteps, **kwargs):
    385             pass
    386 

~\anaconda3\lib\site-packages\emcee\ensemble.py in sample(self, initial_state, log_prob0, rstate0, blobs0, iterations, tune, skip_initial_state_check, thin_by, thin, store, progress)
    283             state.blobs = blobs0
    284         if state.log_prob is None:
--> 285             state.log_prob, state.blobs = self.compute_log_prob(state.coords)
    286         if np.shape(state.log_prob) != (self.nwalkers,):
    287             raise ValueError("incompatible input dimensions")

~\anaconda3\lib\site-packages\emcee\ensemble.py in compute_log_prob(self, coords)
    454         # Check for log_prob returning NaN.
    455         if np.any(np.isnan(log_prob)):
--> 456             raise ValueError("Probability function returned NaN")
    457 
    458         return log_prob, blob

ValueError: Probability function returned NaN

谁能告诉我为什么会这样,以及如何解决?我会感谢你的回答,谢谢

标签: pythonstatisticsbayesianemcee

解决方案


推荐阅读