使用 Python 的贝叶斯建模
Bayesian modelling with Python
我在 Python 中使用 pymc3
包进行贝叶斯建模。下面是我的代码
import pymc3 as pm
with pm.Model() as model :
mu = pm.Normal("mu", mu = 0, sigma = 1)
obs = pm.Normal("obs", mu = mu, sigma = 1, observed = np.random.randn(100))
model.logp({"mu": 0})
上面的logp
方法给出的结果是array(-149.24174903)
你能帮我理解这个数字指的是什么吗?它是对数似然函数吗?我也检查了下面但无法匹配这个数字
import scipy.stats
import numpy as np
np.log(scipy.stats.norm(0, 1).pdf(0)) ### -0.9189385332046727
logp
方法应该为您提供未归一化的对数后验,即贝叶斯规则的 (log) 分子。回想一下,后验与先验和似然的乘积成正比,对数后验与对数先验和对数似然之和成正比。 IE。如果你想重现 logp
的输出,你还必须考虑可能性,而不仅仅是先验。你可以这样检查:
import pymc3 as pm
import scipy.stats
import numpy as np
# declare observed data above to check later
data = np.random.randn(100)
# that's the parameter value for which you want the unnormalized log posterior density
fixed_mu = 0
with pm.Model() as model :
mu = pm.Normal("mu", mu=0, sigma=1)
obs = pm.Normal("obs", mu=mu, sigma=1, observed=data)
# unnormalized log posterior as given by pymc3
pm_log_posterior = model.logp({"mu": 0})
# log prior as given by scipy
np_log_prior = scipy.stats.norm(0, 1).logpdf(fixed_mu)
# log likelihood as given by scipy
np_log_likelihood = scipy.stats.norm(fixed_mu, 1).logpdf(data).sum()
# unnormalized posterior is the sum
np_log_posterior = np_log_likelihood + np_log_prior
现在np_log_posterior
和pm_log_posterior
应该是一样的了。
我在 Python 中使用 pymc3
包进行贝叶斯建模。下面是我的代码
import pymc3 as pm
with pm.Model() as model :
mu = pm.Normal("mu", mu = 0, sigma = 1)
obs = pm.Normal("obs", mu = mu, sigma = 1, observed = np.random.randn(100))
model.logp({"mu": 0})
上面的logp
方法给出的结果是array(-149.24174903)
你能帮我理解这个数字指的是什么吗?它是对数似然函数吗?我也检查了下面但无法匹配这个数字
import scipy.stats
import numpy as np
np.log(scipy.stats.norm(0, 1).pdf(0)) ### -0.9189385332046727
logp
方法应该为您提供未归一化的对数后验,即贝叶斯规则的 (log) 分子。回想一下,后验与先验和似然的乘积成正比,对数后验与对数先验和对数似然之和成正比。 IE。如果你想重现 logp
的输出,你还必须考虑可能性,而不仅仅是先验。你可以这样检查:
import pymc3 as pm
import scipy.stats
import numpy as np
# declare observed data above to check later
data = np.random.randn(100)
# that's the parameter value for which you want the unnormalized log posterior density
fixed_mu = 0
with pm.Model() as model :
mu = pm.Normal("mu", mu=0, sigma=1)
obs = pm.Normal("obs", mu=mu, sigma=1, observed=data)
# unnormalized log posterior as given by pymc3
pm_log_posterior = model.logp({"mu": 0})
# log prior as given by scipy
np_log_prior = scipy.stats.norm(0, 1).logpdf(fixed_mu)
# log likelihood as given by scipy
np_log_likelihood = scipy.stats.norm(fixed_mu, 1).logpdf(data).sum()
# unnormalized posterior is the sum
np_log_posterior = np_log_likelihood + np_log_prior
现在np_log_posterior
和pm_log_posterior
应该是一样的了。