I have a dataset of stock returns, with daily returns (panel). I have successfully created running 3 year standard deviation of returns with the use of lags for my monthly data (different dataset). However, now I wish to do the same with daily stock returns. However, not every stock contains the same amount of observations in any 3 year period, some days might be missing (weekends, some months shorter than the other etc). So I cannot simply use 3 years (in days) of lags.
Does anyone have ideas?
This is what I do with monthly return data. Now I wish to do the same with daily return data. Here, permno is my stock identifier, mofd is my monthly date variable, and ret is my monthly return (per stock. SD is the standard deviation of returns.
// CALCULATE SOME RUNNING TOTALS OF RET AND RET^2 ///
by permno (mofd), sort: gen sum_ret = sum(ret)
by permno (mofd): gen sum_ret_sq = sum(ret^2)
// AND A RUNNING COUNT OF NON MISSING OBSERVATIONS
by permno (mofd): gen int n_obs = sum(!missing(ret))
// NOW CALCULATE RUNNING STANDARD DEVIATIONS OVER 3 YEAR PERIOD, 36 MONTHS
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
gen sd = sqrt(variance)
Dataex:
----------------------- copy starting from the next line -----------------------
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input double PERMNO long date double SHRCD str2 PRIMEXCH double(PRC RET SHROUT) 10000 9503 10 "Q" -2.5625 .c 3680 10000 9504 10 "Q" -2.5 -.024390242993831635 3680 10000 9505 10 "Q" -2.5 0 3680 10000 9506 10 "Q" -2.5 0 3680 10000 9509 10 "Q" -2.625 .05000000074505806 3680 10000 9510 10 "Q" -2.75 .0476190485060215 3680 10000 9511 10 "Q" -2.875 .04545454680919647 3680 10000 9512 10 "Q" -3 .043478261679410934 3680 10000 9513 10 "Q" -3 0 3680 10000 9516 10 "Q" -3 0 3680 10000 9517 10 "Q" -3 0 3680 10000 9518 10 "Q" -3 0 3680 10000 9519 10 "Q" -3.75 .25 3680 10000 9520 10 "Q" -4.1875 .11666666716337204 3680 10000 9523 10 "Q" -4.4375 .05970149114727974 3680 10000 9524 10 "Q" -4.4375 0 3680 10000 9525 10 "Q" -4.3125 -.028169013559818268 3680 10000 9526 10 "Q" -4.4375 .028985507786273956 3680 10000 9527 10 "Q" -4.375 -.014084506779909134 3680 10000 9530 10 "Q" -4.375 0 3680 10000 9531 10 "Q" -4.375 0 3680 10000 9532 10 "Q" -4.375 0 3680 10000 9533 10 "Q" -4.1875 -.04285714402794838 3680 10000 9534 10 "Q" -4.375 .04477611929178238 3680 10000 9537 10 "Q" -4.3125 -.014285714365541935 3680 10000 9538 10 "Q" -4.3125 0 3680 10000 9539 10 "Q" -4.21875 -.021739130839705467 3680 10000 9540 10 "Q" -4.21875 0 3680 10000 9541 10 "Q" -4.28125 .014814814552664757 3680 10000 9545 10 "Q" -4 -.0656934306025505 3680 10000 9546 10 "Q" -3.9375 -.015625 3680 10000 9547 10 "Q" -3.6875 -.0634920671582222 3680 10000 9548 10 "Q" -3.6875 0 3680 10000 9551 10 "Q" -3.625 -.016949152573943138 3680 10000 9552 10 "Q" -3.5625 -.017241379246115685 3680 10000 9553 10 "Q" -3.25 -.08771929889917374 3680 10000 9554 10 "Q" -3.25 0 3680 10000 9555 10 "Q" -3.25 0 3680 10000 9558 10 "Q" -3.25 0 3680 10000 9559 10 "Q" -3.4375 .057692307978868485 3680 10000 9560 10 "Q" -3.46875 .00909090880304575 3680 10000 9561 10 "Q" -3.875 .11711711436510086 3680 10000 9562 10 "Q" -4.4375 .14516128599643707 3680 10000 9565 10 "Q" -4.1875 -.056338027119636536 3680 10000 9566 10 "Q" -4.3125 .02985074557363987 3680 10000 9567 10 "Q" -4.3125 0 3680 10000 9568 10 "Q" -4.4375 .028985507786273956 3680 10000 9569 10 "Q" -4.4375 0 3680 10000 9572 10 "Q" -4.4375 0 3680 10000 9573 10 "Q" -4.4375 0 3680 10000 9574 10 "Q" -4.4375 0 3680 10000 9575 10 "Q" -4.4375 0 3680 10000 9576 10 "Q" -4.4375 0 3680 10000 9579 10 "Q" -4.4375 0 3680 10000 9580 10 "Q" -4.4375 0 3680 10000 9581 10 "Q" -4.4375 0 3680 10000 9582 10 "Q" -4.40625 -.007042253389954567 3680 10000 9586 10 "Q" -4.4375 .007092198356986046 3680 10000 9587 10 "Q" -4.3125 -.028169013559818268 3680 10000 9588 10 "Q" -4.3125 0 3680 10000 9589 10 "Q" -4.3125 0 3680 10000 9590 10 "Q" -4.3125 0 3680 10000 9593 10 "Q" -4.25 -.014492753893136978 3680 10000 9594 10 "Q" -4.125 -.029411764815449715 3680 10000 9595 10 "Q" -4.21875 .022727273404598236 3680 10000 9596 10 "Q" -4.15625 -.014814814552664757 3680 10000 9597 10 "Q" -4.125 -.007518797181546688 3680 10000 9600 10 "Q" -4.09375 -.007575757801532745 3680 10000 9601 10 "Q" -4.09375 0 3680 10000 9602 10 "Q" -4.09375 0 3680 10000 9603 10 "Q" -4.0625 -.007633587811142206 3680 10000 9604 10 "Q" -4.0625 0 3680 10000 9607 10 "Q" -4.0625 0 3680 10000 9608 10 "Q" -4.0625 0 3680 10000 9609 10 "Q" -4.0625 0 3680 10000 9610 10 "Q" -4.0625 0 3680 10000 9611 10 "Q" -4.0625 0 3680 10000 9614 10 "Q" -4.09375 .007692307699471712 3680 10000 9615 10 "Q" -4.0625 -.007633587811142206 3680 10000 9616 10 "Q" -4 -.015384615398943424 3793 10000 9617 10 "Q" -4 0 3793 10000 9618 10 "Q" -3.96875 -.0078125 3793 10000 9621 10 "Q" -3.890625 -.019685039296746254 3793 10000 9622 10 "Q" -3.8125 -.0200803205370903 3793 10000 9623 10 "Q" -3.75 -.016393441706895828 3793 10000 9624 10 "Q" -3.75 0 3793 10000 9625 10 "Q" -3.78125 .008333333767950535 3793 10000 9628 10 "Q" -4.21875 .11570248007774353 3793 10000 9629 10 "Q" -4.09375 -.029629629105329514 3793 10000 9630 10 "Q" -4.15625 .015267175622284412 3793 10000 9631 10 "Q" -4.03125 -.030075188726186752 3793 10000 9632 10 "Q" -3.96875 -.01550387591123581 3793 10000 9635 10 "Q" -3.875 -.023622047156095505 3793 10000 9636 10 "Q" -3.875 0 3793 10000 9637 10 "Q" -3.65625 -.05645161122083664 3793 10000 9638 10 "Q" -3.4375 -.05982905998826027 3793 10000 9639 10 "Q" -3.46875 .00909090880304575 3793 10000 9643 10 "Q" -3.46875 0 3793 10000 9644 10 "Q" -3.34375 -.036036036908626556 3793 10000 9645 10 "Q" -3.0625 -.08411215245723724 3793 end format %td date
0 Response to Lagged variables of daily data
Post a Comment