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

Hideyoshi Maeda hideyoshi.maeda at gmail.com
Tue May 22 04:20:21 CEST 2012


Hi,

Doing a quick comparison of the difference in code for the volatility estimate in using 'yang.zhang' as a calculation method, on S&P 500 data, going back from the beginning of 2001 till today (22 May 2012). These were the results from the following lines of code:

N.B. volatility.new is the edited code with the suggestion of using runVar instead  of runSum, as suggested by James
N.B SPX.ohlc is an xts object of that contains OHLC data for the Bloomberg ticker "SPX Index"

> original.vol.code <- volatility(SPX.ohlc, calc="yang.zhang")
> new.vol.code <- volatility.new(SPX.ohlc, calc="yang.zhang")
> plot((new.vol.code-original.vol.code)*100/original.vol.code, main="% difference in volatility esitimates 
+      when comparing new and old code")


Some quite significant percentage differences getting up to approx. -10% viewing it as a histogram in terms of differences, you get...

> hist((new.vol.code-original.vol.code)*100/original.vol.code, breaks="Scott", main="Histogram of the same data")




Hopefully that gives some indication of the differences...

Hideyoshi


> 
> -------- Original Message --------
> Subject:
> Re: [R-SIG-Finance] error in yang.zhang volatility{TTR}
> Date:
> Mon, 21 May 2012 12:40:29 -0500
> From:
> J Toll <jctoll at gmail.com>
> To:
> r-sig-finance at r-project.org
>  
> 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
>  
> _______________________________________________
> R-SIG-Finance at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions should go.
> 
> This email is confidential. If you have received it in error, please destroy this email and any/all attached documents immediately. Please also notify the sender. This message is not a solicitation and, without express information to the contrary, does not take into account the investment objectives (or otherwise) of each client, or prospective client. The information herein is for the private information of the recipient and is not to be construed as an offer to sell or solicitation to buy any interest in particular funds or securities which are referred to herein. Any simulated historic/statistical analyses that are provided in connection with explanations of the potential returns of the Product use simulated analysis and hypothetical circumstances to estimate how the Product may have performed prior to their actual existence. Neither the provision nor the content of this email creates any obligations or liability of White & Co. towards the users. White & Co. does not make any representations, warranties, express or implied, as to the quality, accuracy, timeliness, continued availability or completeness of the information contained herein, including, but not limited to, the contents of any private placement memorandum or fund fact sheet. White & Co. accepts no liability relating to direct or indirect losses, immaterial damage or consequential damage including loss of profit (even where White & Co. has been made aware of the possibility of such damages). This limitation of liability also holds for directors and employees of White & Co. The information in this email has been prepared on information deemed to be reliable. White & Co. Pty Ltd does not represent that this material is accurate, complete or current, and accepts no liability if it is not. White & Co, its subsidiaries and partners may have positions in - and buy or sell - any of the securities mentioned in this e-mail without notice. White & Co. Pty. Ltd. is regulated by the CFTC and ASIC.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20120522/122f59a4/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: vol estimate pct diff n10.png
Type: image/png
Size: 14423 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20120522/122f59a4/attachment.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: histogram.png
Type: image/png
Size: 5186 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20120522/122f59a4/attachment-0001.png>


More information about the R-SIG-Finance mailing list