【问题标题】:PyMC multiple linear regressionsPyMC 多元线性回归
【发布时间】:2014-05-20 09:21:56
【问题描述】:

我正在尝试拟合共享相同截距的几条线。

import numpy as np
import pymc

# Observations
a_actual = np.array([[2., 5., 7.]]).T
b_actual = 3.
t = np.arange(100)
obs = np.random.normal(a_actual * t + b_actual)


# PyMC Model
def model_linear():
    b = pymc.Uniform('b', value=1., lower=0, upper=200)

    a = []
    s = []
    r = []
    for i in range(len(a_actual)):
        s.append(pymc.Uniform('sigma_{}'.format(i), value=1., lower=0, upper=100))
        a.append(pymc.Uniform('a_{}'.format(i), value=1., lower=0, upper=200))
        r.append(pymc.Normal('r_{}'.format(i), mu=a[i] * t + b, tau=1/s[i]**2, value=obs[i], observed=True))

    return [pymc.Container(a), b, pymc.Container(s), pymc.Container(r)]

model = pymc.Model(model_linear())
map = pymc.MAP(model)
map.fit()
map.revert_to_max()

计算的 MAP 估计值与实际值相差甚远。这些值对sigmas 和a 的下限和上限、a 的实际值(例如a = [.2, .5, .7] 会给我很好的估计)或进行回归的行数也非常敏感开。

这是执行线性回归的正确方法吗?

ps:我尝试对 sigma 使用指数先验分布,但结果并不好。

【问题讨论】:

  • 你不能只运行 3 个单独的线性回归吗?
  • 我不能,因为每个观察都需要共享相同的截距 (b)。我的真实模型比这更复杂;这是我的问题的简化版本。

标签: python linear-regression pymc


【解决方案1】:

我认为使用 MAP 可能不是您的最佳选择。如果您能够进行适当的采样,请考虑将示例代码的最后 3 行替换为

MCMClinear = pymc.MCMC( model)
MCMClinear.sample(10000,burn=5000,thin=5)
linear_output=MCMClinear.stats()

为此打印linear_output 可以为参数提供非常准确的推断。

【讨论】:

  • 是的,MCMC 采样工作正常,结果良好。我只是想知道为什么 MAP 估计对变量界限或对简单模型的实际观察值如此敏感。使用 lmfit 库(它也是基于 scipy 的优化包)对于同样的问题给出了很好的结果。
猜你喜欢
  • 2013-05-01
  • 2010-11-23
  • 2013-07-17
  • 1970-01-01
  • 2016-10-15
  • 2019-07-04
  • 1970-01-01
  • 1970-01-01
  • 2015-01-17
相关资源
最近更新 更多