Hello All,
Glad to become a member of Statalist. I am trying to use kernel regression to predict the variance of A based on the variance of B (measured as return squared). For the variance of B, at each time t, I first create around 10,000 states (i.e. 10,000 different variances of B) and then for each state I use kernel regression based on all the available actual data of variances A and B to predict the variance of A. This process then rolls over by adding one more pair of actual observations each time (like a rolling regression). I have written the codes (see below). It worked but far too slow - it took around 40 hours for the estimation. I know there are many Stata experts on the forum, so could anyone let me know how to speed up the loop please? I heard mata is much faster. However, unfortunately I have no idea about it and given the submission deadline of my thesis, I might not have enough time to learn it at the moment.
Any help would be immensely appreciated!!
quietly {
forvalues j = 300(1)1300 {
forvalues v = 0(1)9 {
gen vara`j'`v' = (ln((`j'+`v'/10+0.05)/aprice))^2
forvalues i = 1/`=_N' {
gen K2`j'`v'`i' = ((2*_pi)^(-1/2))*exp((-1/2)*((vara`j'`v'[`i']-actualvara[_n-1])/h[`i'])^2) in 1/`i'
gen sum2`j'`v'`i' = sum(K2`j'`v'`i')
gen w2`j'`v'`i' = K2`j'`v'`i'/sum2`j'`v'`i'[_N]
drop K2`j'`v'`i' sum2`j'`v'`i'
gen expectedvarb`j'`v'`i' = sum(w2`j'`v'`i'*actualvarb[_n-1])
replace vara`j'`v' = expectedvarb`j'`v'`i'[_N] if _n ==`i'
drop w2* expectedvarb*
}
}
}
}
Notes:
vara = variance of A
aprice = price of A
actualvara = actual variance of A
actualvarb = actual variance of B
expectedvarb = expected variance of B
Glad to become a member of Statalist. I am trying to use kernel regression to predict the variance of A based on the variance of B (measured as return squared). For the variance of B, at each time t, I first create around 10,000 states (i.e. 10,000 different variances of B) and then for each state I use kernel regression based on all the available actual data of variances A and B to predict the variance of A. This process then rolls over by adding one more pair of actual observations each time (like a rolling regression). I have written the codes (see below). It worked but far too slow - it took around 40 hours for the estimation. I know there are many Stata experts on the forum, so could anyone let me know how to speed up the loop please? I heard mata is much faster. However, unfortunately I have no idea about it and given the submission deadline of my thesis, I might not have enough time to learn it at the moment.
Any help would be immensely appreciated!!
quietly {
forvalues j = 300(1)1300 {
forvalues v = 0(1)9 {
gen vara`j'`v' = (ln((`j'+`v'/10+0.05)/aprice))^2
forvalues i = 1/`=_N' {
gen K2`j'`v'`i' = ((2*_pi)^(-1/2))*exp((-1/2)*((vara`j'`v'[`i']-actualvara[_n-1])/h[`i'])^2) in 1/`i'
gen sum2`j'`v'`i' = sum(K2`j'`v'`i')
gen w2`j'`v'`i' = K2`j'`v'`i'/sum2`j'`v'`i'[_N]
drop K2`j'`v'`i' sum2`j'`v'`i'
gen expectedvarb`j'`v'`i' = sum(w2`j'`v'`i'*actualvarb[_n-1])
replace vara`j'`v' = expectedvarb`j'`v'`i'[_N] if _n ==`i'
drop w2* expectedvarb*
}
}
}
}
Notes:
vara = variance of A
aprice = price of A
actualvara = actual variance of A
actualvarb = actual variance of B
expectedvarb = expected variance of B