# [R] HAR-RV-CJ Moedel

cursethiscure caolan.harvey6 at mail.dcu.ie
Thu Jul 12 18:59:31 CEST 2012

```I am trying to write a loop to forecast realized volatility over successive
days for the purpose of VaR prediction using the HAR-RV-CJ model which is as
follows:

log(RV_t+1) = β_0 + β_CD log(CV_t) + β_CW log(CV_t-5) + β_CM
log(CV_t-22) + β_JD log(J_t + 1) + β_JW log(J_t-5 + 1) + β_JM log(J_t-22 +
1) + e_t

where RV is realized volatility, CV is continuous volatility and J is the
jump which is RV - CV, _t is subscript for time t, which is one day

basically I know how to compute ex post CV and J, and RV and have done in
another loop but need to forecast for half of my sample data to compare to
ex-post estimates of RV.

but I don't know  how to compute the weekly and monthly estimates _t-5 and
_t-22
weekly continuous volatility is given as:

log(CV_t-5) = 1/5 * ∑ _i=1 to 5 log(C_t-i)
and similar for monthly CV and weekly and Monthly J
which I think is:

cw = apply(embed((log(cv)), 5), 1, sum, na.rm=T)
cw = 1/5*(cw)
correct me if I am wrong please.

I think also the daily lagged CV is:

lcv = cv[-length(cv)]

Now if I can get the lagged variables correct, how do I run the above
regression so it loops over each successive day?

thanks, If i am missing any vital info please advise me. thanks

the loop for the data that I have used to compute RV and CV to get daily
statistics is:

dollar <- getdat("eurd")
#dollar = dollar[seq(1, nrow(dollar), by =3), ]
tim = strptime(dollar[,1], "%d/%m/%Y %H:%M");
dollar\$day = format(tim, "%Y-%m-%d");
##ddx <- row index for each day
ddx = split(seq.int(nrow(dollar)), dollar\$day);

dollarbns = data.frame(day=as.Date(names(ddx)), m=NA, rtn=NA, rv=NA, bv=NA,
zg=NA, lzg=NA, zj=NA, zmj=NA, lzm=NA, zgtp=NA, lzgtp=NA, zmjtp=NA, zjtp=NA,
lzmtp=NA);
for (i in seq_along(ddx)) {
##quotes for day i
x = dollar\$mid[ddx[[i]]];
n = length(x);
y = log(x[-1]/x[-n]);#log price difference
y2 = y^2
m = length(y);
z999 = (qnorm(1 - .001))
if (m>=260) {
##raw moments
rtn = sum(y) * 100
rv = sum(y*y);#realized variance [Y]
bv = (m/(m-1))*(pi/2)*(sum(abs(y[-1]*y[-m])));#bi-power vairiation
#mu_1^{-2}*{Y}^[1,1]
rj = (rv-bv)/rv
tmp = apply(embed(y, 4), 1, prod);#y{j}*y{j-1}*y{j-2}*y{j-3}
tp1 = apply(embed(y, 3), 1, prod)#y{j}*y{j-1}*y{j-2}
tpq = (mu43^-3)*((m^2)/(m-3))*(sum((abs(tp1))^(4/3)))

#mu_1^{-4}*{Y}^[1,1,1,1]
##jump test statistic (10)
theta = pi*pi/4 + pi - 5;#(6)

zg = sqrt(m) * (bv - rv) / sqrt(theta*qp); # difference test
lzg = sqrt(m) * ((log(bv)) - (log(rv))) / sqrt(theta*(qp/(bv^2))); # log
difference test
zmj = sqrt(m) * ((bv/rv) - 1)/(sqrt((theta)*(sapply((qp/(bv^2)),
function(x) max(x, 1))))) # relative (max) jump
zj =  sqrt(m) * ((bv/rv) - 1)/(sqrt(theta*(qp/(bv^2))))
lzm = sqrt(m) * ((log(bv)) - (log(rv))) /
(sqrt((theta)*(sapply((qp/(bv^2)), function(x) max(x, 1))))) # log max
# tpq bv
zgtp = sqrt(m) * (bv - rv) / sqrt(theta*tpq); # difference test
lzgtp = sqrt(m) * ((log(bv)) - (log(rv))) / sqrt(theta*(tpq/(bv^2))); #
log difference test
zmjtp = sqrt(m) * ((bv/rv) - 1)/(sqrt((theta)*(sapply((tpq/(bv^2)),
function(x) max(x, 1))))) # relative (max) jump
zjtp =  sqrt(m) * ((bv/rv) - 1)/(sqrt(theta*(tpq/(bv^2))))
lzmtp = sqrt(m) * ((log(bv)) - (log(rv))) /
(sqrt((theta)*(sapply((tpq/(bv^2)), function(x) max(x, 1))))) # log max
#  if (abs(zg)>z999 && abs(zj)>z999 && abs(lzg)>z999 && abs(zmj)>z999 &&
abs(lzm)>z999 && abs(zgtp)>z999 && abs(zjtp)>z999 && abs(lzgtp)>z999 &&
abs(zmjtp)>z999 && abs(lzmtp)>z999)
##store results for day i
dollarbns[i,-1] = c(m, rtn, rv, bv, zg, lzg, zmj, zj, lzm, zgtp, lzgtp,
zmjtp, zjtp, lzmtp);
}
}
this loops return the test statistics for how to test for presence of jumps
for my entire data set. I need to estimate the above HAR-RV-CJ so that it
estimates RV for say half the period i have to compare to ex-post RV.

I should add say I want to loop over T days one day at a time.

I have data for those T days but I need to compare my forecast with the
ex-post realized volatility.

--
View this message in context: http://r.789695.n4.nabble.com/HAR-RV-CJ-Moedel-tp4636326.html
Sent from the R help mailing list archive at Nabble.com.

```