Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: calculate volatility in different ways


From   tzygmund mcfarlane <[email protected]>
To   [email protected]
Subject   Re: st: calculate volatility in different ways
Date   Tue, 26 Jul 2011 09:58:07 -0700

If you would like to compute variously smoothed squared returns:

/****************************************/
clear*
sysuse sp500, clear
tsset date

// generate returns
g ret = log(open/L.open)
g retsq = ret^2
su ret retsq

// EWMA filter (sq. returns)
scalar lambda =  0.95  // smoothing parameter
qui su retsq
local double var_retsq = r(Var)
tssmooth exponential  ewma_retsq = retsq, ///
	parms(`lambda') samp0(`var_retsq')
label variable ewma_retsq "Sq. Ret. (EWMA Filt.)"

// MA filter (sq. returns)
tssmooth ma ma_retsq = retsq, window(59 1 0)
label variable ma_retsq "Sq. Ret. (MA filt.)"

// MA filter (variance)
preserve
tempfile rollvar_ret
rolling ma_var = r(Var), window(60) saving(`rollvar_ret', replace): ///
	summarize ret, d
use `rollvar_ret', clear
rename end date
label variable ma_var "Filt. Ret. Variance"
save `rollvar_ret', replace
restore

merge 1:1 date using `rollvar_ret'
li *_retsq ma_var in 1/10
tsline *_retsq ma_var
/***************************************/


Note the virtually imperceptible difference between filtering the
variance and the squared returns.



On Tue, Jul 26, 2011 at 8:48 AM, Oliver Jones
<[email protected]> wrote:
> Hi,
> dose this help you?
>
> ********** Begin example ****************
> sysuse sp500, clear
> tsset date
>
> rolling Var = r(Var), window(60) step(1): summarize open, d
>
> list in 1/10
> ********** End example ****************
>
>
> Best
> Oliver
>
> Am 25.07.2011 13:00, schrieb Nadine R:
>>
>> Dear Statalisters!
>>
>> I have a new problem: I want to calculate the volatility of some stock
>> returns using Equally Weighted Moving Averages and Exponential
>> Weighted Moving Averages in Stata 11.0. I have daily return data for
>> the stocks. I did not use the standard deviation measures as I need
>> the volatilities for different periods (if I could calculate the
>> 60-day-volatilities for 10 years with this formula I would do it like
>> this).
>>
>> formula for Equally Weighted (for a 60-day-window: M=60):
>> variance at day t = 1/M * sum of the 60 preceding squared returns
>> variance at day 60 = 1/60 * Sum(from (return t=1)^2 to (return t=60)^2)
>> variance at day 61 = 1/60 * Sum(from (return t=2)^2 to (return t=61)^2)
>> ...
>>
>> I tried it with a formula like this:
>> generate var = 1/60 * sum((return[_n-60])^2 - (return[_n])^2)
>>
>> where the "-" should indicate a "until/till" but Stata interpreted it
>> as a minus.
>>
>> formula for the Exponential Weighted Moving Averages:
>> variance at day t = lambda*variance(t-1) + (1 - lambda)*squared
>> return(t-1)
>>
>> I tried it with a formula like this with lambda = 0.95 but it seems
>> that Stata does not calculate var(t=0) and then var(t=1) and therefore
>> the formula did not work:
>> generate var = 0.95*(var[_n-1]) + 0.05*return[_n]^2
>>
>> Thanks for yur help.
>>
>> Kind regards,
>>
>> Nadine
>> *
>> *   For searches and help try:
>> *   http://www.stata.com/help.cgi?search
>> *   http://www.stata.com/support/statalist/faq
>> *   http://www.ats.ucla.edu/stat/stata/
>
> --
> Universität Bielefeld
> Fakultät für Wirtschaftswissenschaften
> Lehrstuhl für Ökonometrie und Statistik
> - -
> Bielefeld University
> Faculty of Business Administration and Economics
> Chair of Econometrics and Statistics
> - -
> Raum / room:  V9-110
> Tel / phone:  +49 (0)521 106 4871
> ---
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/
>

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index