First post, first STATA project. I'm hoping to find some help here. Thanks in advance. I've searched the forums and other answers but they didn't seem to work for me.
My objective is to calculate 36 month volatility from monthly stock returns.
Sample of my dataset:
permno date prc ret
10001 30sep1986 6.375 -.003076924
10001 31oct1986 6.625 .039215688
10001 28nov1986 7 .056603774
10001 31dec1986 7 .015
10001 30jan1987 6.75 -.035714287
10001 27feb1987 6.25 -.074074075
10001 31mar1987 6.375 .036800001
10001 30jun1987 5.875 .051428571
10001 31jul1987 6 .021276595
10001 31aug1987 6.5 .083333336
10001 30sep1987 6.25 -.022307692
10001 30oct1987 6.375 .02
10001 30nov1987 6.1875 -.029411765
10001 31dec1987 5.875 -.033535354
10001 29jan1988 6.25 .063829787
10001 29feb1988 6.75 .079999998
10001 31mar1988 6.125 -.0762963
10001 30jun1988 6.25 -.012038835
10001 31aug1988 6.625 .029126214
10001 31oct1988 6.625 .039215688
10001 30nov1988 6.625 0
Following this topic I am able to proces this code:
But with the next step when I generate variance, the results are very scrambled, a lot of missing results. This is the line I've used:
It's a very large dataset, I am computing this for all listed companies from 1960 to 2015.
What did I do wrong?
My objective is to calculate 36 month volatility from monthly stock returns.
Sample of my dataset:
permno date prc ret
10001 30sep1986 6.375 -.003076924
10001 31oct1986 6.625 .039215688
10001 28nov1986 7 .056603774
10001 31dec1986 7 .015
10001 30jan1987 6.75 -.035714287
10001 27feb1987 6.25 -.074074075
10001 31mar1987 6.375 .036800001
10001 30jun1987 5.875 .051428571
10001 31jul1987 6 .021276595
10001 31aug1987 6.5 .083333336
10001 30sep1987 6.25 -.022307692
10001 30oct1987 6.375 .02
10001 30nov1987 6.1875 -.029411765
10001 31dec1987 5.875 -.033535354
10001 29jan1988 6.25 .063829787
10001 29feb1988 6.75 .079999998
10001 31mar1988 6.125 -.0762963
10001 30jun1988 6.25 -.012038835
10001 31aug1988 6.625 .029126214
10001 31oct1988 6.625 .039215688
10001 30nov1988 6.625 0
Following this topic I am able to proces this code:
Code:
by permno (date): gen ret_sq = ret^2 by permno (date): sort: gen sum_ret_sq = sum(ret_sq) by permno (date): gen int n_obs = sum(!missing(ret)) gen monthdate = month(date) gen yeardate = year(date) gen int date2 = ym(yeardate, monthdate) format date2 %tm xtset permno date2
Code:
gen variance = (L1.sum_ret_sq - L37.sum_ret_sq)/(L1.n_obs-L37.n_obs) - ((L1.sum_ret - L37.sum_ret)/(L1.n_obs-L37.n_obs))^2
What did I do wrong?
Comment