Frage

Following along with this example to do pretty simple bayesian linear regression using PYMC3 (learning, I hope) I get the initial example to run but then try to use my own data and get :

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:

Which is suspect is due to my data range,, but it may well be that I don't understand the other parameters. Data and code is as follows: This should just run in an IPython notebook I hope. The lastqu should predict the Units, when all is said and done..

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

And the model code I tried is:

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);
War es hilfreich?

Lösung

This works:

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);

Again many thanks to @fonnesbeck !!

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top