Hello everybody!
I am fitting the stocastic volatility model with leverage from here.
log_returns = y2
class StochasticVolatilityProcess(pm.distributions.distribution.Continuous):
def __init__(self, mu=None, phi=None, sigmav=None,
*args, **kwargs):
super(StochasticVolatilityProcess, self).__init__(*args, **kwargs)
self.alpha = pm.Deterministic('alpha', (1. - phi) * mu)
self.phi = phi
self.sigmav = sigmav
self.init = pm.Normal.dist(mu=mu, sd=sigmav)
self.mean = tt.as_tensor_variable(0.)
def logp(self, x):
alpha = self.alpha
phi = self.phi
sigmav = self.sigmav
init = self.init
x_im1 = x[:-1]
x_i = x[1:]
innov_like = pm.Normal.dist(mu=alpha + phi * x_im1, sd=sigmav).logp(x_i)
return init.logp(x[0]) + tt.sum(innov_like)
def _repr_latex_(self, name=None, dist=None):
if dist is None:
dist = self
mu = dist.mu
sd = dist.sd
name = r'\text{%s}' % name
return r'${} \sim \text{{StochasticVolatilityProcess}}(\mathit{{mu}}={},~\mathit{{sd}}={})$'.format(name,
get_variable_name(mu), get_variable_name(sd))
def make_stochastic_volatility_model_with_leverage(model, log_returns):
mu = pm.Normal(name='mu', mu=0., sd=np.sqrt(25.))
phistar = pm.Beta(name='phistar', alpha=20., beta=1.5)
recsigmav2 = pm.Gamma(name='recsigmav2', alpha=2.5, beta=.25)
beta = pm.math.exp(pm.Deterministic('beta', .5 * mu))
phi = pm.Deterministic('phi', 2. * phistar - 1.)
sigmav = pm.Deterministic('sigmav', pm.math.sqrt(1. / recsigmav2))
rho = pm.Uniform(name='rho', lower=-1., upper=1.)
x = StochasticVolatilityProcess('x', mu, phi, sigmav, shape=len(log_returns) + 2)
y = pm.Normal(name='y',
mu=rho / sigmav * pm.math.exp(.5 * x[1:-1]) * (x[2:] - (1. - phi) * mu - phi * x[1:-1]),
sd=pm.math.sqrt(pm.math.exp(x[1:-1]) * (1 - rho * rho)),
observed=log_returns)
now I want to forecast x with pm.sample_posterior_predictive the next 10 steps. To see were the particle filter distribution is heading to next.
I believe it is done with a np.linespace vector array in form of (1, 10, num = 10),
but I do not know who to fit this into the pm.set_data() …
If somebody can explain it and show me the soluition I would be a real help.