假设我试图用下面的数据来估计一个简单的y= m * x
问题的斜率:
x_data = np.array([0,1,2,3])
y_data = np.array([0,1,2,3])
斜率为1。但是,当我在pymc中运行这个时,我得到10
slope = pm.Uniform('slope', lower=0, upper=20)
@pm.deterministic
def y_gen(value=y_data, x=x_data, slope=slope, observed=True):
return slope * x
model = pm.Model([slope])
mcmc = pm.MCMC(model)
mcmc.sample(100000, 5000)
# This returns 10
final_guess = mcmc.trace('slope')[:].mean()
但应该是1!
注:以上为PYMC2。
最佳答案
你需要定义一个可能性,试试这个:
import pymc as pm
import numpy as np
x_data = np.linspace(0,1,100)
y_data = np.linspace(0,1,100)
slope = pm.Normal('slope', mu=0, tau=10**-2)
tau = pm.Uniform('tau', lower=0, upper=20)
@pm.deterministic
def y_gen(x=x_data, slope=slope):
return slope * x
like = pm.Normal('likelihood', mu=y_gen, tau=tau, observed=True, value=y_data)
model = pm.Model([slope, y_gen, like, tau])
mcmc = pm.MCMC(model)
mcmc.sample(100000, 5000)
# This returns 10
final_guess = mcmc.trace('slope')[:].mean()
它返回10,因为你只是从你的制服之前采样,10是它的预期值。