[R-SIG-Finance] error in yang.zhang volatility{TTR}

J Toll jctoll at gmail.com
Mon May 21 19:40:29 CEST 2012


On Mon, May 21, 2012 at 11:32 AM, J Toll <jctoll at gmail.com> wrote:
> Hi,
>
> I've been going through the code for the yang.zhang calculation method
> of volatility in the TTR package and believe I've found the same error
> as the one from last year on the Close to Close volatility estimator.
>
> https://stat.ethz.ch/pipermail/r-sig-finance/2011q2/007989.html
>
> Like the previous error, one of the first clues that there was a
> problem was an excessive number of NA values generated at the
> beginning of the output (e.g. 40 NA's for n = 20).
>
> I believe the errors are in these two calculations:
>
>        s2o <- N/(n - 1) * runSum((log(OHLC[, 1]/Cl1) - 1/n *
>            runSum(log(OHLC[, 1]/Cl1), n))^2, n)
>        s2c <- N/(n - 1) * runSum((log(OHLC[, 4]/OHLC[, 1]) -
>            1/n * runSum(log(OHLC[, 4]/OHLC[, 1]), n))^2, n)
>
> Basically, s2o is just supposed to be the variance of the normalized open,
>    log(OHLC[, 1]/Cl1
>
> and s2c is the variance of the normalized close.
>    log(OHLC[, 4]/OHLC[, 1]
>
> The problem in both formula is, as in Close to Close, that you have
> two nested runSum calculations being used to calculate variance.  For
> example, if n = 20, the first 20 values should be differenced against
> the same mean.  That's not happening here.  The first value is being
> differenced against the first mean, the second value is being
> differenced against the second mean, and so on.
>
> As a simple fix, I would suggest these corrections:
>
>        s2o <- N * runVar(log(OHLC[, 1] / Cl1), n)
>        s2c <- N * runVar(log(OHLC[, 4] / OHLC[, 1]), n)


Sorry, to get that to work, it should actually be:

        s2o <- N * runVar(log(OHLC[, 1] / Cl1), n = n)
        s2c <- N * runVar(log(OHLC[, 4] / OHLC[, 1]), n = n)

It needs to be "n = n", otherwise runVar confuses n for y.


James



More information about the R-SIG-Finance mailing list