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

J Toll jctoll at gmail.com
Sat May 28 06:25:00 CEST 2011

```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!
> --
>

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))

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

```