# Heteroscedaticity modelling - Time series modelling

Greetings to you all. I’ve gone through the tutorial on https://www.youtube.com/watch?v=nwuU-KEKXhU&t=2s; by stan and the code below is the Garch(1,1) model.

data {
int<lower=0> T;
real r[T];
real<lower=0> sigma1;
}

parameters {
real mu;
real<lower=0> alpha0;
real<lower=0,upper=1> alpha1;
real<lower=0,upper=(1-alpha1)> beta1;
}

transformed parameters {
real<lower=0> sigma[T];
sigma[1] = sigma1;
for (t in 2:T)
sigma[t] = sqrt(
alpha0
+ alpha1 * pow(r[t-1] - mu, 2)
+ beta1 * pow(sigma[t-1], 2)
);
}

model {
// priors on
alpha0 ~ normal(0, 10);
alpha1 ~ normal(0, 10);
beta1 ~ normal(0, 10);
// likelihood
r ~ normal(mu, sigma);
}



I’ve tried to extend the model to a Garch(2,2) but seems not to be working as it is failing to initialize. The code below is what I have tried.

data {
int<lower=0> T;
real r[T];
real<lower=0> sigma1;
}

parameters {
real mu;
real<lower=0> alpha0;
real<lower=0,upper=1> alpha1;
real<lower=0,upper=1> alpha2;
real<lower=0,upper=(1-alpha1)> beta1;
real<lower=0,upper=(1-alpha2)> beta2;
}

transformed parameters {
real<lower=0> sigma[T];
sigma[1] = sigma1;
for (t in 3:T)
sigma[t] = sqrt(
alpha0
+ alpha1 * pow(r[t-1] - mu, 2)
+ alpha2 * pow(r[t-2] - mu, 2)
+ beta1 * pow(sigma[t-1], 2)
+ beta2 * pow(sigma[t-2], 2)
);
}

model {
// priors on
alpha0 ~ normal(0, 10);
alpha1 ~ normal(0, 10);
alpha2 ~ normal(0, 10);
beta1 ~ normal(0, 10);
beta2 ~ normal(0, 10);
// likelihood
r ~ normal(mu, sigma);
}



My questions are:

1. Why does the above code not work?
2. How do I determine the upper for beta2?
3. Is there any material you would suggest that can guide in the development of stan for time series models especially for arch and extensions of arch such as garch, egarach and the rest?
4. How would a Garch(2,3) or uneven number of p,q look like and where do i learn how to write such code?

I will be grateful for any guidance on this topic.

Hola,

Well to a garch model be stationary, the alpha and beta parameters go to be higher than 0 and if you add all of them the result has to be lower or equal than 1.

For a garch(22) the restrition is:

Aalpha0>0, apha1 > 0,alpha2 > 0, beta1 > 0 and beta2 > 0

Alpha1 + alpha2 + beta1 + beta2 < 1

Impose this restrictions in your code might be hard so you can impose stationarity with a weak informative prior to your parameters, like a beta(2,1) distribution

Hope it helps :)

3 Likes

For this you can use my varstan package :) you can do garch models. Or just tag me And I can help u with the code :).

For avoiding models illness do a Stochastic volatility model instead of a egarch.

1 Like

Thank you so much @asael_am, I will definitely check out your varstan package, looks like what I would need to work on. However, I more comfortable with pystan, as I am more used to python than r. Though I will try to brush up on my r. My question to you would be how did you get to be so good with time series analysis? I am trying to understand this area so that I can make my own solutions. Do you have material that you can suggest in this area? I am very grateful for your reply.

1 Like

Thank you @asael_am, this is a great start. I now see, all the coefficients must lie between 0 and 1, this has definitely helped. However, how to impose such restrictions is something I have to work on as I do not understand how to do that but thank you so much.

Hola,

Send me a mail to: asael_am@hotmail.com I can send you all the extra material that can help you out.

if your garch(2,2) model is:

$$\sigma_t = \alpha_0 + \sum_{i=1}^2 \alpha_i \epsilon_{t-i} + \sum_{i=1}^2 \beta_i \sigma^2{t-i}$$

Then declare alpha1, alpha2, beta1, and beta2 as a simplex vector (stan has that option) and that will give you all the constrains you to need without you struggle that much you can check Stan’s Simplex manual to learn about Simplex vectors in Stan.

Good luck,
Asael