Help with Multivariate SDE Timeseries

Well, since I cant get this model working even when the volatility process is independent, perhaps we can try a model that does work. This is a similar model but instead of variance being stochastic, log-variance follows an AR(1) process:

Y_t are the observed returns and V_t the latend variance process
Y_t=\sqrt{V_{t-1}}\epsilon_t^s
log(V_t)=\alpha_v+\beta_vlog(V_{t-1})+\sigma_v\epsilon_t^v

For independent epsilon, implementation is straightforward. The log-variance process can implemented either using the AR class or EurlerMaruyama.


import numpy as np
import matplotlib.pyplot as plt
import pymc3 as pm
from pymc3.distributions.timeseries import EulerMaruyama

returns = np.genfromtxt(pm.get_data("SP500.csv"))

def logV_sde(logV, sigmaV, beta, alpha):
    return alpha + beta * logV, sigmaV 

with pm.Model() as sp500_model:

    alpha=pm.Normal('alpha',0,sd=100)
    beta=pm.Normal('beta',0,sd=10)
    sigmaV=pm.InverseGamma('sigmaV',2.5,0.1)
    
    logV = pm.AR('logV', [alpha,beta], sd=sigmaV**.5,constant=True,shape=len(returns))
    #logV = EulerMaruyama('logV',1.0,logV_sde,[sigmaV, beta, alpha],shape=len(returns),testval=np.repeat(.01,len(returns)))
    volatility_process = pm.Deterministic('volatility_process', pm.math.exp(.5*logV))
    r = pm.Normal('r', mu=0,sd=volatility_process, observed=returns)
    
with sp500_model:
    trace = pm.sample(2000,chains=1,cores=1)
    
pm.traceplot(trace, [alpha,beta,sigmaV]);
    

fig, ax = plt.subplots(figsize=(14, 8))
ax.plot(returns)
ax.plot(trace['volatility_process',::5].T, 'r', alpha=.03);
ax.set(xlabel='time', ylabel='returns')
ax.legend(['S&P500', 'stoch vol']);

the results make sense, and the volatility trace is similar to what we see in the PyMC3 stochastic volatility example:

Using this model as starting point, I’d like to introduce a correlation \rho between \epsilon_t^s and \epsilon_t^v . I’ve tried a workaround discussed earlier in this thread but the introduction of a third random variable produces nonsensical results. I think the proper solution is define the multivariate posterior and pass in the observed returns. This I don’t know how to do

1 Like