4

Say I try to estimate the slope of a simple y= m * x problem using the following data:

x_data = np.array([0,1,2,3])
y_data = np.array([0,1,2,3])

Clearly the slope is 1. However, when I run this in PyMC I get 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()

but it should be 1!

Note: The above is with PyMC2.

4

2 回答 2

3

You need to define a likelihood, try this:

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()

It returns 10 because you're just sampling from your uniform prior and 10 is the expected value of that.

于 2014-03-07T02:12:40.500 回答
1

您需要设置value=y_data, observed=True可能性。此外,还有一点,您不需要实例化 Model 对象。只需将您的节点(或对 locals() 的调用)传递给 MCMC。

于 2014-03-07T03:04:50.007 回答