Pymc线性回归起始问题(缩放输入参数?)

时间:2014-02-12 22:59:45

标签: python pandas bayesian pymc

跟着this example使用PYMC3进行非常简单的贝叶斯线性回归(学习,我希望)我得到了最初的例子,然后尝试使用我自己的数据并得到:

ValueError: Optimization error: max, logp or dlogp at max have non-finite values. 
Some values may be outside of distribution support. max: {'alpha': array(50000.0), 
'beta': array(50000.0), 'sigma': array(25000.0)} logp: array(nan) dlogp: array([ nan,
nan,  nan])Check that 1) you don't have hierarchical parameters, these will lead to 
points with infinite density. 2) your distribution logp's are properly specified. 
Specific issues:

怀疑是由于我的数据范围,但很可能是我不理解其他参数。数据和代码如下:这应该只是在我希望的IPython笔记本中运行。当完成所有操作时,lastqu应该预测单位。

import pandas as pd
import io
content2 = '''\
Units   lastqu
2000-12-31   19391   NaN
2001-12-31   35068   5925
2002-12-31   39279   8063
2003-12-31   47517   9473
2004-12-31   51439   11226
2005-12-31   59674   11667
2006-12-31   58664   14016
2007-12-31   55698   13186
2008-12-31   42235   11343
2009-12-31   40478   7867
2010-12-31   38722   8114
2011-12-31   36965   8361
2012-12-31   39132   8608
2013-12-31   43160   9016
2014-12-31   NaN     9785
'''
df2 = pd.read_table(io.BytesIO(content2))
#make sure that the columns are int, it is all a DataFrame
df2['Units']=df2['Units'][:-1].astype('int')
df2['lastqu']=df2['lastqu'][1:].astype('int')
df2

我尝试过的模型代码是:

import pymc as pm
#import numpy as np
x=df2['lastqu']               <<<< my best guess as to how to specify my data
y=df2['Units']
trace = None
with pm.Model() as model:
    alpha = pm.Normal('alpha', mu=0, sd=20)
    beta = pm.Normal('beta', mu=0, sd=20)
    sigma = pm.Uniform('sigma', lower=0, upper=50000)

    y_est = alpha + beta * x

    likelihood = pm.Normal('y', mu=y_est, sd=sigma, observed=y)

    start = pm.find_MAP()
    step = pm.NUTS(state=start)
    trace = pm.sample(2000, step, start=start, progressbar=False)

    pm.traceplot(trace);

1 个答案:

答案 0 :(得分:1)

这有效:

df2=df2[1:-1]          <<<< gets rid of NaN from example data
df2
%matplotlib inline
import pymc as pm
#import numpy as np
x=df2['lastqu']               <<<< my best guess as to how to specify my data
y=df2['Units']
trace = None
with pm.Model() as model:
    alpha = pm.Normal('alpha', mu=0, sd=20)
    beta = pm.Normal('beta', mu=0, sd=20)
    sigma = pm.Uniform('sigma', lower=0, upper=50000)

    y_est = alpha + beta * x

    likelihood = pm.Normal('y', mu=y_est, sd=sigma, observed=y)

    start = pm.find_MAP()
    step = pm.NUTS(state=start)
    trace = pm.sample(2000, step, start=start, progressbar=False)

    pm.traceplot(trace);

再次感谢@fonnesbeck !!