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

J Toll jctoll at gmail.com
Mon May 21 18:32:48 CEST 2012


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)

I would welcome the input of someone with reference data that could
double-check this against a known value.

Thanks,


James



More information about the R-SIG-Finance mailing list