私は最近、長い間pymc3
独占的に使用emcee
した後、学習を開始しましたが、概念的な問題に直面しています。
Hogg の Fitting a model to data の第 7 章で練習しています。これには、任意の 2 次元の不確実性を持つ直線への 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 の測定値 ( mu
s) とそれに関連する測定の不確実性 (y_err
と)を渡そうとしていx_err
ます。しかし、それは 2 番目のtau
引数が気に入らないようです。
何か案は?これは可能でなければならない
ありがとう