twiecki / pymc3_talk

Bayesian Analysis in PyMC3.
https://rawgithub.com/twiecki/pymc3_talk/master/bayesian_pymc3.slides.html#/
79 stars 23 forks source link

Update for newer version of PyMC3? #2

Closed jbwhit closed 8 years ago

jbwhit commented 8 years ago

I was working through your Jupyter notebook here: http://nbviewer.ipython.org/github/twiecki/pymc3_talk/blob/master/bayesian_pymc3.ipynb

And when I got to input [40]:

model_randomwalk = pm.Model()
with model_randomwalk:
    # std of random walk, best sampled in log space.
    sigma_alpha, log_sigma_alpha = model_randomwalk.TransformedVar(
                            'sigma_alpha', 
                            pm.Exponential.dist(1./.02, testval = .1), 
                            pm.logtransform
    )
    sigma_beta, log_sigma_beta = model_randomwalk.TransformedVar(
                            'sigma_beta', 
                            pm.Exponential.dist(1./.02, testval = .1),
                            pm.logtransform
    )

I ran into a lot of problems. First, it appears that TransformedVar is no longer a method of pm.Model(). I substituted simply Var in both places, but then it broke on pm.logtransform, so I changed that to pm.log, but that broke with the following error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-42-a7745240c5a6> in <module>()
      6                             'sigma_alpha',
      7                             pm.Exponential.dist(1./.02, testval = .1),
----> 8                             pm.log
      9 #                             pm.logtransform
     10     )

/Users/jonathan/miniconda2/envs/pymc3/lib/python3.5/site-packages/pymc3/model.py in Var(self, name, dist, data)
    239                     self.named_vars[v.name] = v
    240         else:
--> 241             var = ObservedRV(name=name, data=data, distribution=dist, model=self)
    242             self.observed_RVs.append(var)
    243             if var.missing_values:

/Users/jonathan/miniconda2/envs/pymc3/lib/python3.5/site-packages/pymc3/model.py in __init__(self, type, owner, index, name, data, distribution, model)
    500 
    501         if distribution is not None:
--> 502             data = as_tensor(data, name,model,distribution.dtype)
    503             self.missing_values = data.missing_values
    504 

/Users/jonathan/miniconda2/envs/pymc3/lib/python3.5/site-packages/pymc3/model.py in as_tensor(data, name, model, dtype)
    459 
    460 def as_tensor(data, name,model, dtype):
--> 461     data = pandas_to_array(data).astype(dtype)
    462 
    463     if hasattr(data, 'mask'):

TypeError: float() argument must be a string or a number, not 'Elemwise'

I was running this code by creating a new conda environment via the following command that I ran today:

conda create -n pymc3 python=3 matplotlib pandas seaborn scikit-learn numpy scipy patsy jupyter notebook 

source activate pymc3

pip install --process-dependency-links git+https://github.com/pymc-devs/pymc3

Versions:

pymc3: 3.0

Any update to this document would greatly help!

twiecki commented 8 years ago

Thanks. Indeed that's using old syntax. I wasn't aware people still looked at these slides. I'll try to find some time to update them. In the mean-time, the new version of that model can be found here: http://nbviewer.ipython.org/github/pymc-devs/pymc3/blob/master/pymc3/examples/stochastic_volatility.ipynb

jbwhit commented 8 years ago

Thanks for the quick response! The updated Stochastic Volatility Model you link to is great, but I was actually more interested in the section in the first link labeled "Regression slowly adapts to best fit current data" (and immediately above).

twiecki commented 8 years ago

Ah, right. That'd be great to port, I agree.

twiecki commented 8 years ago

Here you go: http://pymc-devs.github.io/pymc3/rolling_regression/

jbwhit commented 8 years ago

Thanks! I don't know where to put this, but I had to update that analysis:

#ERROR: prices = data.YahooDailyReader(symbols=['GLD', 'GDX'], end='2014-8-1').read().loc['Adj Close', :, :].iloc[:1000]
# replacement
prices = data.get_data_yahoo(symbols=['GLD', 'GDX'], end='2014-8-1').loc['Adj Close', :, :].iloc[:1000]

After making that change (in 2 places on that line), all of the rest of the analysis works as advertised!

twiecki commented 8 years ago

@jbwhit Can you try updating pandas-datareader? Might require a newer version.