Interpreting Rolling Yang–Zhang Volatility Estimates
Summary
The document explains how to interpret output from the Yang–Zhang volatility estimator in R’s TTR package. The window parameter specifies how many historical periods feed each estimate, so the output is a rolling series rather than one value for the entire dataset. Each non-missing value summarizes volatility over a different window ending at its corresponding observation.
The example uses a stock price history to show that an initial run of missing values appears while the first full window is being formed. With a window nearly as long as the available history, the series contains just one estimate. These counts illustrate the rolling-window behavior, though exact alignment and output counts depend on the input data and implementation. The note clarifies output interpretation; it does not assess the estimator’s statistical properties or compare it with other volatility measures.
Key ideas
- The estimator returns a rolling series when its window is shorter than the dataset.
- Each valid estimate summarizes the historical window associated with that observation.
- Initial values are missing until enough history is available to form a window.
- A window spanning nearly the full dataset can produce a single estimate.
Tags
Full text
# Understanding Yang-Zhang Volatility Estimator
# 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 by rbm (score 8, accepted)
https://quant.stackexchange.com/a/27746
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
```Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.