我想估計一個簡單的線性函數的參數和伽馬分佈的噪音項來自數據。 (注意:這是一個後續問題https://stats.stackexchange.com/questions/88676/regression-with-unidirectional-noise,但簡化和更具體的實現)。說我有產生我的觀測數據如下:迴歸「單向」噪音
import numpy as np
np.random.seed(0)
size = 200
true_intercept = 1
true_slope = 2
# Generate observed data
x_ = np.linspace(0, 1, size)
true_regression_line = true_intercept + true_slope * x_ # y = a + b*x
noise_ = np.random.gamma(shape=1.0, scale=1.0, size=size)
y_ = true_regression_line + noise_
看起來如下:
我試着估計使用pymc如下這些參數:
from pymc import Normal, Gamma, Uniform, Model, MAP
# Define priors
intercept = Normal('intercept', 0, tau=0.1)
slope = Normal('slope', 0, tau=0.1)
alpha = Uniform('alpha', 0, 2)
beta = Uniform('beta', 0, 2)
noise = Gamma('noise', alpha=alpha, beta=beta, size=size)
# Give likelihood > 0 to models where the regression line becomes larger than
# any of the datapoint
y = Normal('y', mu=intercept + slope * x_ + noise, tau=100,
observed=True, value=y_)
# Perform MAP fit of model
model = Model([alpha, beta, intercept, slope, noise])
map_ = MAP(model)
map_.fit()
然而,這給了我估計遠離真實值:
- Interc EPT:真:1.000,預計:3.281
- 坡:真:2.000,預計:-3.400
上午我錯誤地做一些事情?
我發現這主要是MAP.fit()中使用的優化器的一個問題。 – frisbee