[R-SIG-Finance] Excessive data needed for volatility{TTR} calculation?

Joshua Ulrich josh.m.ulrich at gmail.com
Sat May 28 14:13:49 CEST 2011


Hi James,

On Fri, May 27, 2011 at 11:25 PM, J Toll <jctoll at gmail.com> wrote:
> On Fri, May 27, 2011 at 10:39 PM, Joshua Ulrich <josh.m.ulrich at gmail.com> wrote:
>> I think your solution will work, but using 'n' instead of 'n-1'.  The
>> code below shows the same results using your solution and a formula
>> similar to the one found here (which I mis-interpreted when I
>> originally wrote the function):
>> http://web.archive.org/web/20081224134043/http://www.sitmo.com/eq/172
>>
>> set.seed(21)
>> N <- 260
>> n <- 100
>> r <- rnorm(n)/100
>> last(sqrt(N) * runSD(r, n))
>> sqrt(N/(n-1)*sum((r-mean(r))^2))
>>
>> Thanks!
>> --
>> Joshua Ulrich  |  FOSS Trading: www.fosstrading.com
>>
>
> Hi Joshua,
>
> Thanks for replying and confirming my suspicions. However, I'm curious
> why you would use 'n' rather than 'n-1'.  My thinking is that a 10-day
> volatility (n = 10) is calculated as the annualized standard deviation
> of 9 (n - 1) price returns (i.e. ln(p1/p0), ROC()).  The sample
> standard deviation of 9 price returns would be the sum of the squared
> deviations divided by 9 - 1, or n - 2.  Therefore, I believe your line
>
> sqrt(N / (n - 1) * sum((r - mean(r)) ^ 2))
>
> should actually be
>
> sqrt(N / (n - 2) * sum((r - mean(r)) ^ 2))
>
Actually, because the first return in the moving window would always
be NA, it should be:
sqrt(N/(n-2)*sum((r[-1]-mean(r[-1]))^2))

which yields the same result as:
last(sqrt(N) * runSD(r, n-1))

> I've been double-checking my work and went ahead and calculated 10 and
> 20-day vols by hand and I'm pretty sure
>
> s <- sqrt(N) * runSD(r, (n - 1))
>
> is correct, unless your defining 10-day volatility as 11 days of data
> and 10 price returns.  Please let me know otherwise. Thanks.
>
> James
>
After getting some sleep, it's clear that your initial solution (n-1)
is correct.

Your patch will be on R-forge shortly.  Many thanks again!

Best,
--
Joshua Ulrich  |  FOSS Trading: www.fosstrading.com



More information about the R-SIG-Finance mailing list