I tried replicating the stochastic vol example in the pymc3 documentation, but using a larger dataset.

NUTS was taking too long, so I tried ADVI.

from pandas_datareader import data
import pymc3 as pm
import pandas as pd

returns = data.get_data_yahoo('SPY', start='2008-5-1', end='2016-12-1')['Adj Close'].pct_change()

with pm.Model() as model:

    nu                 = pm.Exponential(name='nu', lam=1.0 / 10, testval=5.0)
    sigma              = pm.Exponential(name='sigma', lam=1.0 / .02, testval=0.1)
    s                  = pm.distributions.timeseries.GaussianRandomWalk(name='s', sd=sigma, shape=len(returns))
    volatility_process = pm.Deterministic(name='volatility_process', var=pm.math.exp(-2.0*s))
    r                  = pm.StudentT(name='r', nu=nu, mu=0.0, lam=1.0 / volatility_process, observed=returns)

    start   = pm.find_MAP()
    vparams = pm.variational.advi(start=start, n=5000)
    trace   = pm.variational.sample_vp(
        vparams = vparams,
        draws   = 10000

fix, ax = sns.plt.subplots()
ax.plot(returns.index, 1/np.exp(trace['s',::5].T), 'r', alpha=0.03)

Unfortunately, the results were not similar to what was in the documentation. It looked liked it was modeling a constant vol, rather than a stochastic vol which was what I was expecting.

Is this to be expected or did I do something wrong? Is ADVI appropriate in this situation, and if not, why is that?



It is important to be cognizant of the assumptions underlying ADVI. At the core, it is a mean-field approximation (at least how it's implemented in PyMC3) which means that correlations in the posterior are ignored. This is fine for some models but the stochastic volatility model has a highly correlated posterior. The intuition is that s_t ~ N(s_{t-1}, sd^2) will be very correlated with s_{t-1}. Thus, the mean-field assumption is strongly violated here which leads to a bad approximation.

NUTS, on the other hand, is very good at exploring a very correlated, high-dimensional distribution, that's why this model is such a strong example for its power.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.