Friday, December 9, 2016

programming - Understanding Yang-Zhang Volatility Estimator


I am using TTR in R and I am trying to understand the Yang Zhang volatility estimator (without drift). The following equations seem to imply a single value:


$$ \sigma = \sqrt{{\sigma_o^2}+k\sigma_c^2+(1-k)\sigma_{rs}^2} $$



$$\sigma_o^2 = \frac{1}{N-1}\sum_{i=1}^{N}ln{\frac{o_i}{c_{i-1}}^2}$$ $$\sigma_c^2 = \frac{1}{N-1}\sum_{i=1}^{N}ln{\frac{c_i}{o_{i-1}}^2}$$ $$\sigma_{rs}^2 = \frac{1}{N-1}\sum_{i=1}^{N}(ln{\frac{h_i}{c_i}})(ln{\frac{h_i}{o_i}}) + (ln{\frac{l_i}{c_i}})(ln{\frac{l_i}{o_i}})$$ $$k = \frac{0.34}{1.34 + \frac{N+1}{N-1}}$$


However when I run volatility(my_data, n = 100, calc = "yang.zhang") I get a vector with a bunch of NAs in front of it. What is my volatility estimate? Is it the last value in the data frame, if so what are the remaining values at the other data points? I apologize if this is trivial - but I cant seem to find anything in the TTR documentation.


Thank you!



Answer



The n=100 specifies the number of periods (rolling) for the vol estimate - see the original link https://web.archive.org/web/20100326215050/http://www.sitmo.com/eq/409 where the n is referred to as Number of historical prices used for the volatility estimate


An example in R:


library(TTR)
library(quantmod)

getSymbols("AAPL")

nrow(AAPL) # we have 2384 price points


vol <- volatility(AAPL, n = 100, calc = "yang.zhang")
# if n = 100, then we have 2384 - 100 = 2284 vol estimates for all the "100-day rolling period"
nrow(na.omit(vol))
# and we have 100 NA for the initial 100 periods
sum(is.na(vol))

# if n = 2383 then we have only a single vol estimae (i.e. massive 2383 period window)

vol2 <- volatility(AAPL, n = 2383, calc = "yang.zhang")
nrow(na.omit(vol2))
sum(is.na(vol2))

produces


[1] "AAPL"
> nrow(AAPL) # we have 2384 price points
[1] 2384
>
>

> vol <- volatility(AAPL, n = 100, calc = "yang.zhang")
> # if n = 100, then we have 2384 - 100 = 2284 vol estimates for all the "100-day rolling period"
> nrow(na.omit(vol))
[1] 2284
> # and we have 100 NA for the initial 100 periods
> sum(is.na(vol))
[1] 100
>
> # if n = 2383 then we have only a single vol estimae (i.e. massive 2383 period window)
> vol2 <- volatility(AAPL, n = 2383, calc = "yang.zhang")

> nrow(na.omit(vol2))
[1] 1
> sum(is.na(vol2))
[1] 2383

No comments:

Post a Comment

technique - How credible is wikipedia?

I understand that this question relates more to wikipedia than it does writing but... If I was going to use wikipedia for a source for a res...