【发布时间】:2016-09-24 14:07:56
【问题描述】:
在专门使用emcee 多年后,我最近开始学习pymc3,但我遇到了一些概念问题。
我正在练习Hogg's Fitting a model to data 的第 7 章。这涉及到具有任意二维不确定性的直线的 mcmc 拟合。我在emcee 中很容易做到这一点,但是pymc 给我带来了一些问题。
它本质上归结为使用多元高斯似然。
这是我目前所拥有的。
from pymc3 import *
import numpy as np
import matplotlib.pyplot as plt
size = 200
true_intercept = 1
true_slope = 2
true_x = np.linspace(0, 1, size)
# y = a + b*x
true_regression_line = true_intercept + true_slope * true_x
# add noise
# here the errors are all the same but the real world they are usually not!
std_y, std_x = 0.1, 0.1
y = true_regression_line + np.random.normal(scale=std_y, size=size)
x = true_x + np.random.normal(scale=std_x, size=size)
y_err = np.ones_like(y) * std_y
x_err = np.ones_like(x) * std_x
data = dict(x=x, y=y)
with Model() as model: # model specifications in PyMC3 are wrapped in a with-statement
# Define priors
intercept = Normal('Intercept', 0, sd=20)
gradient = Normal('gradient', 0, sd=20)
# Define likelihood
likelihood = MvNormal('y', mu=intercept + gradient * x,
tau=1./(np.stack((y_err, x_err))**2.), observed=y)
# start the mcmc!
start = find_MAP() # Find starting value by optimization
step = NUTS(scaling=start) # Instantiate MCMC sampling algorithm
trace = sample(2000, step, start=start, progressbar=False) # draw 2000 posterior samples using NUTS sampling
这会引发错误:LinAlgError: Last 2 dimensions of the array must be square
所以我试图通过MvNormal x 和 y 的测量值 (mus) 及其相关的测量不确定性(y_err 和 x_err)。但它似乎不喜欢 2d tau 参数。
有什么想法吗?这一定是可能的
谢谢
【问题讨论】:
-
您是否尝试进行线性回归,包括模型中
x和y的测量误差? -
是:二维不确定性
标签: python statistics pymc3 mcmc emcee