PyMC最简单的线性模型

时间:2014-03-07 00:30:14

标签: python statistics statsmodels pymc

假设我尝试使用以下数据估算简单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。

2 个答案:

答案 0 :(得分:3)

你需要定义一个可能性,试试这个:

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是它的预期值。

答案 1 :(得分:1)

您需要为可能性设置value=y_data, observed=True。另外,一个小问题,您不需要实例化Model对象。只需将您的节点(或对locals()的调用)传递给MCMC即可。