I have to plot autocorellation function of the rate of return which is given by the set of equations: $$ \left\{\begin{matrix} \varepsilon_t = \sigma_t e_t\\ \sigma_t=\sum_{k=1}^\infty \frac{B(p+k-1,q+1)}{B(p,q)}\frac{|\varepsilon_{t-k}|}{\lambda}\\ r_t = C + D\varepsilon_{t-1}+\varepsilon_t \end{matrix}\right. $$ where $e_t$ is random variable from Laplace distribution with $\mu=0, \lambda=\sqrt{1/2}$, $C, D, p, q$ are known constants.
The problem I face is that to plot autocorrelation defined by: $$ ACF(\tau)=\frac{E\big((|r_t|-\mu')(|r_{t+\tau}|-\mu')\big)}{\sigma^2} $$ (where $\sigma^2$ is a varance of $|r_t|$ and $\mu'$ is mean of $|r_t|$ after time $t$) I have the compute nominator explicitly or store some values in vectors (as I choosed to simulate the process in R).
So my code looks like:
library(smoothmest)
C <- 0.000608
D <- 0.103
p <- 5.41
q <- 0.597
k <- 1
N <- 200000
lambd = sqrt(1/2)
sigma <- 1
r <- vector(mode = "numeric", length = N)
rnew <- vector(mode = "numeric", length = N)
varepsilon <-vector(mode = "numeric", length = N)
varepsilon[1] = sigma * rdoublex(1, mu = 0, lambda = sqrt(1/2))
for(t in 2:N) {
sigma <- sigma + beta(p + k - 1, q + 1)/beta(p,q)/lambd * abs(varepsilon[t - k]);
varepsilon[t] <- sigma * rdoublex(1, mu = 0, lambda = sqrt(1/2));
r[t] <- C + D * varepsilon[t - 1] + varepsilon[t];
k = k + 1;
}
rprzes <- vector(mode = "numeric", length = N)
mu <- vector(mode = "numeric", length = N)
Auto <- vector(mode = "numeric", length = N)
tau = 500
for(i in 1:(N-tau)) {
rprzes[i] <- abs(r[i + tau])
}
for(j in 1:N){
mu[j] <- sum(abs(r[1:j]))/j
}
p1 <- vector(mode = "numeric", length = N)
p2 <- vector(mode = "numeric", length = N)
for(k in 1:N) {
p1[k] <- (abs(r[k]) - mu[k])
p2[k] <- abs(rprzes[k]) - mu[k]
}
for(l in 1:N){
Auto[l] <- mean(p1[l] * p2[l])/sd(abs(r))^2
}
plot(Auto,type = "h", main = "", xlab="", ylab="")
But unfortunately the resulting plot is totally different form what I expect, and as far as I know the autocorelation function should not be $<0$ anywhere, but it is.
Before implementing by hand I tried to use R functions acf and pacf, but the results were nowhere close to desired one and I had not insight on what is going on, in particular I could not change $\tau$ value.
Anyone can help? Thanks in advance.