Here is the code that we used in past for getting monthly idiovol from daily return data. Instead of simple standard deviation, i want it to generate ewma standard deviation like we did in #29 of this thread. The difference is now use for each month daily data within that month instead of recursive function in #29. The lambda should optimise in a range of 0.78 and 0.94.
Code:
gen mdate = mofd(date)
format mdate %tm
gen month = month(date)
gen year = year(date)
capture program drop one_stock_month
program define one_stock_month
capture regress rt mkt smb hml
if c(rc) == 0 {
predict resid, resid
summ resid
if `r(N)' >= 17 {
gen idio_vol = r(sd)
gen n_obs = e(N)
}
}
else if !inlist(c(rc), 2000, 2001) { // ERROR OTHER THAN INSUFFICIENT OBSERVATIONS
gen unexpected_error_code = c(rc)
assert 0
}
exit
end
//gen mdate = ym(year, month)
assert missing(mdate) == missing(month, year)
format mdate %tm
runby one_stock_month, by(stock mdate)
gen idiovol_m = idio_vol * sqrt(n_obs)
collapse(mean) idio_vol idiovol_m n_obs, by (stock_id stock mdate year month)

Comment