I am trying to replicate a finance paper in which standard deviation is computed differently to the traditional way. Accordingly,
"To measure the volatility of a firm’s stock returns, we use a proxy, centered around zero rather than the rolling 3-month mean, for daily variation of returns computed as an annualized 3-month rolling sample standard deviation:
where t denotes month. How do I compute this metric? Do I use PROC STANDARD to make returns be centered around zero? and then compute rolling standard deviation?
Here is the link to the paper if you are interested:
https://scholar.harvard.edu/files/campbell/files/campbellhilscherszilagyi_jf2008.pdf
Standard deviation centered around zero instead of the mean ... the square of this found by the USS() function in a data step, or the USS option in PROC MEANS/PROC SUMMARY.
Calling @Rick_SAS
If you want exact that formula combine the USS function and the LAG1, LAG2, and LAG3 functions:
data Have;
input Y @@;
t = _N_;
datalines;
4 5 6 5 7 6 4 3 4 6 3 5 7 7 7 8 7 6 7
;
data Want;
set Have nobs=N;
Y1 = lag1(Y); Y2 = lag2(Y); Y3 = lag3(Y);
sigma3 = sqrt(252/(N-1) * uss(Y1, Y2, Y3));
run;
proc sgplot data=Want;
series x=t y=sigma3;
run;
Thanks. But doesnt it have to be calculated using daily returns? I think your code uses monthly standard deviation of past 3 months. This is also the part that I dont understand 😞
@somebody wrote:
Thanks. But doesnt it have to be calculated using daily returns? I think your code uses monthly standard deviation of past 3 months. This is also the part that I dont understand
I don't see why you say this. Nothing in Rick's code indicates that it uses monthly returns. Nothing indicates it uses daily returns. His answer is general, not specific to a given time period. If you need it to work on daily returns, you can do the calculations based on daily returns; if you want it to work on monthly returns, you can do the calculations on monthly returns.
Here is another paper doing the same thing ( I think they are different).
I guess the question is what are the r_k values and what is the range of the index k?
I suggest you talk to your manager/mentor/advisor and figure out what you want to compute. After you know what you want to compute, provide example data and we can help you compute it in SAS.
I think k indexes the trading days. So to compute this, we use all trading days within that past 3 months. I can use PROC EXPAND to compute rolling standard deviation. However, how can I tell SAS just look at the past 3 months rather than using previous 66 obs (assuming 22 trading days in a month)
@somebody wrote:
Here is another paper doing the same thing ( I think they are different).
To be just slightly obnoxious, if you think the formula here and in your first post are different then you need a serious refresher of mathematic symbiology.
(<values>) to the 1/2 power IS square root,
252* 1/(n-1) is the same as 252/(n-1)
I know that the formulas are the same. What I mean difference is r. In the latter, r are just normal returns, whereas in the former, I think r are standardized (de-meaned).
Hi Rick, I am going to calculate sigma following the above-mentioned paper in the posted question Campbell et al. (2008),
I have the attached code, can you look into this and tell me whether the 3 months rolling volatility/standard deviation calculated perfectly or not and how can I annualize it? I am attaching the code and data for you. If you think there is any other efficient way then you can post it. Thanks in advance.
> can you look into this and tell me whether the 3 months rolling volatility/standard deviation calculated perfectly or not and how can I annualize it?
You are asking the wrong person. Your computation uses hash objects and DATA step arrays, so you should talk to someone who is skilled in using those tools.
> how can I tell SAS just look at the past 3 months rather than using previous 66 obs (assuming 22 trading days in a month)
For each date in your data, use the INTNX function to compute the "3 month prior" date.
For example, the following DATA step tells you the beginning of the three-month window for three dates in 2019.
/*
Use the INTCK and INTNX functions:
https://blogs.sas.com/content/iml/2017/05/15/intck-intnx-intervals-sas.html
*/
data Trade3;
input TradeDate anydtdte12.;
BeginDate = intnx('month', TradeDate, -3, 'same');
format TradeDate BeginDate DATE9.;
datalines;
Mar 4, 2019
Apr 30, 2019
Dec 14, 2019
;
proc print; run;
Thanks. But how do I tell PROC EXPAND to use obs within the past 3 months? I would use the following code to compute rolling standard deviation
PROC EXPAND DATA=sigma OUT=sigma method=none;
convert r=r_moving_sum / TRANSFORMOUT= (MOVSTD 66);
by stock;
RUN;
This would work if we have all trading days. However, some stocks don't trade often. For example, stock XYZ trade 20 days over the past 3 months, say from 1 June to 31 August. So on 1st Sep, I would like to use only those 20 trading days. But the code above would include other trading days prior to 1 June. Can I restrict PROC EXPAND to use only the past 3 months? if so, How?
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.