joshuaulrich / TTR

Technical analysis and other functions to construct technical trading rules with R
GNU General Public License v2.0
330 stars 103 forks source link

TTR::volatility seems wrong in the default case #13

Open ivannp opened 9 years ago

ivannp commented 9 years ago
if( isTRUE(mean0) ) {
      # This is an alternative SD calculation using an effective mean of 0
      s <- sqrt(N) * sqrt(runSum(r^2, n-1) / (n-2))
    } else {
      # This is the standard SD calculation using the sample mean
      s <- sqrt(N) * runSD(r, n-1)
    }

runSD(r, n-1) - why do we compute the SD using n-1 bars? runSD already computes the SD of the mean (the default for the "sample" parameter is TRUE). I suspect that the formula in the mean0=TRUE case has similar issue - the n-2 looks suspicious.

joshuaulrich commented 9 years ago

Thanks for the report. I certainly agree that the standard calculation with n-1 observations looks odd... and the mean0 calculation was based on that, which is probably why the n-2 is there (subtract one more observation).

I used this site when coding these volatility functions.

ivannp commented 9 years ago

Hmm, looks like the site has the equation in this manner, since N is the number of CLOSING observations. For N closes, we have N-1 returns and so on. I guess that's one approach, but is it consistent for the other estimators (i.e. Yang-Zhang)? There I think we take N returns (or ratios).

braverock commented 9 years ago

see

?var

which says:

"The denominator n - 1 is used which gives an unbiased estimator of the (co)variance for i.i.d. observations."

Obviously, this applies to volatility as well as variance.

On 04/27/2015 10:41 PM, Ivan Popivanov wrote:

Hmm, looks like the site has the equation in this manner, since N is the number of CLOSING observations. For N closes, we have N-1 returns and so on. I guess that's one approach, but is it consistent for the other estimators (i.e. Yang-Zhang)? There I think we take N returns (or ratios).

— Reply to this email directly or view it on GitHub https://github.com/joshuaulrich/TTR/issues/13#issuecomment-96899920.

Brian G. Peterson http://braverock.com/brian/ Ph: 773-459-4973 IM: bgpbraverock