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:

- Why does the above code not work?
- How do I determine the upper for beta2?
- 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?
- 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.