<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Hi,<div><br></div><div>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:</div><div><br></div><div>N.B. volatility.new is the edited code with the suggestion of using runVar instead of runSum, as suggested by James</div><div>N.B SPX.ohlc is an xts object of that contains OHLC data for the Bloomberg ticker "SPX Index"</div><div><br></div><div>> original.vol.code <- volatility(SPX.ohlc, calc="yang.zhang")</div><div>> new.vol.code <- volatility.new(SPX.ohlc, calc="yang.zhang")</div><div><div>> plot((new.vol.code-original.vol.code)*100/original.vol.code, main="% difference in volatility esitimates </div><div>+ when comparing new and old code")</div></div><div><img height="470" width="1113" apple-width="yes" apple-height="yes" id="8436658c-d7cb-44b6-8834-01bbdcbb92bc" src="cid:0CE13DA0-59D4-4FA9-9FC7-832D922281D9"></div><div><br></div><div>Some quite significant percentage differences getting up to approx. -10% viewing it as a histogram in terms of differences, you get...</div><div><br></div><div>> hist((new.vol.code-original.vol.code)*100/original.vol.code, breaks="Scott", main="Histogram of the same data")</div><div><br></div><div><img height="482" width="729" apple-width="yes" apple-height="yes" id="482cf89e-0c53-4ebf-93ef-6a6cec31aa8b" src="cid:B83E6732-2852-49E7-BF62-F210CA93E9DF"></div><div><br></div><div><br></div><div>Hopefully that gives some indication of the differences...</div><div><br></div><div>Hideyoshi</div><div><br></div><div><div><br class="Apple-interchange-newline"><blockquote type="cite"><div bgcolor="white" lang="EN-US" link="blue" vlink="purple"><div class="WordSection1"><div style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif; color: black; "><br>-------- Original Message --------</div><table class="MsoNormalTable" border="0" cellspacing="0" cellpadding="0"><tbody><tr><td nowrap="" valign="top" style="padding-top: 0cm; padding-right: 0cm; padding-bottom: 0cm; padding-left: 0cm; "><div style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif; color: black; text-align: right; "><b>Subject:</b></div></td><td style="padding-top: 0cm; padding-right: 0cm; padding-bottom: 0cm; padding-left: 0cm; "><div style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif; color: black; ">Re: [R-SIG-Finance] error in yang.zhang volatility{TTR}</div></td></tr><tr><td nowrap="" valign="top" style="padding-top: 0cm; padding-right: 0cm; padding-bottom: 0cm; padding-left: 0cm; "><div style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif; color: black; text-align: right; "><b>Date:</b></div></td><td style="padding-top: 0cm; padding-right: 0cm; padding-bottom: 0cm; padding-left: 0cm; "><div style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif; color: black; ">Mon, 21 May 2012 12:40:29 -0500</div></td></tr><tr><td nowrap="" valign="top" style="padding-top: 0cm; padding-right: 0cm; padding-bottom: 0cm; padding-left: 0cm; "><div style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif; color: black; text-align: right; "><b>From:</b></div></td><td style="padding-top: 0cm; padding-right: 0cm; padding-bottom: 0cm; padding-left: 0cm; "><div style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif; color: black; ">J Toll <a href="mailto:jctoll@gmail.com" style="color: blue; text-decoration: underline; "><jctoll@gmail.com></a></div></td></tr><tr><td nowrap="" valign="top" style="padding-top: 0cm; padding-right: 0cm; padding-bottom: 0cm; padding-left: 0cm; "><div style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif; color: black; text-align: right; "><b>To:</b></div></td><td style="padding-top: 0cm; padding-right: 0cm; padding-bottom: 0cm; padding-left: 0cm; "><div style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 12pt; font-family: 'Times New Roman', serif; color: black; "><a href="mailto:r-sig-finance@r-project.org" style="color: blue; text-decoration: underline; ">r-sig-finance@r-project.org</a></div></td></tr></tbody></table><div style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 12pt; font-size: 12pt; font-family: 'Times New Roman', serif; color: black; "> <br class="webkit-block-placeholder"></div><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; ">On Mon, May 21, 2012 at 11:32 AM, J Toll <a href="mailto:jctoll@gmail.com" style="color: blue; text-decoration: underline; "><jctoll@gmail.com></a> wrote:</pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; ">> Hi,</pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; ">> </pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; ">> I've been going through the code for the yang.zhang calculation method</pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; ">> of volatility in the TTR package and believe I've found the same error</pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; ">> as the one from last year on the Close to Close volatility estimator.</pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; ">> </pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; ">> <a href="https://stat.ethz.ch/pipermail/r-sig-finance/2011q2/007989.html" style="color: blue; text-decoration: underline; ">https://stat.ethz.ch/pipermail/r-sig-finance/2011q2/007989.html</a></pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; ">> </pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; ">> Like the previous error, one of the first clues that there was a</pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; ">> problem was an excessive number of NA values generated at the</pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; ">> beginning of the output (e.g. 40 NA's for n = 20).</pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; ">> </pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; ">> I believe the errors are in these two calculations:</pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; ">> </pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; ">> s2o <- N/(n - 1) * runSum((log(OHLC[, 1]/Cl1) - 1/n *</pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; ">> runSum(log(OHLC[, 1]/Cl1), n))^2, n)</pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; ">> s2c <- N/(n - 1) * runSum((log(OHLC[, 4]/OHLC[, 1]) -</pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; ">> 1/n * runSum(log(OHLC[, 4]/OHLC[, 1]), n))^2, n)</pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; ">> </pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; ">> Basically, s2o is just supposed to be the variance of the normalized open,</pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; ">> log(OHLC[, 1]/Cl1</pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; ">> </pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; ">> and s2c is the variance of the normalized close.</pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; ">> log(OHLC[, 4]/OHLC[, 1]</pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; ">> </pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; ">> The problem in both formula is, as in Close to Close, that you have</pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; ">> two nested runSum calculations being used to calculate variance. For</pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; ">> example, if n = 20, the first 20 values should be differenced against</pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; ">> the same mean. That's not happening here. The first value is being</pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; ">> differenced against the first mean, the second value is being</pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; ">> differenced against the second mean, and so on.</pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; ">> </pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; ">> As a simple fix, I would suggest these corrections:</pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; ">> </pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; ">> s2o <- N * runVar(log(OHLC[, 1] / Cl1), n)</pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; ">> s2c <- N * runVar(log(OHLC[, 4] / OHLC[, 1]), n)</pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; "> </pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; "> </pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; "><span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: yellow; ">Sorry, to get that to work, it should actually be:</span></pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; "><span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: yellow; "> </span></pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; "><span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: yellow; "> s2o <- N * runVar(log(OHLC[, 1] / Cl1), n = n)</span></pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; "><span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: yellow; "> s2c <- N * runVar(log(OHLC[, 4] / OHLC[, 1]), n = n)</span></pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; "><span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: yellow; "> </span></pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; "><span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: yellow; ">It needs to be "n = n", otherwise runVar confuses n for y.</span></pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; "> </pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; "> </pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; ">James</pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; "> </pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; ">_______________________________________________</pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; "><a href="mailto:R-SIG-Finance@r-project.org" style="color: blue; text-decoration: underline; ">R-SIG-Finance@r-project.org</a> mailing list</pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; "><a href="https://stat.ethz.ch/mailman/listinfo/r-sig-finance" style="color: blue; text-decoration: underline; ">https://stat.ethz.ch/mailman/listinfo/r-sig-finance</a></pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; ">-- Subscriber-posting only. If you want to post, subscribe first.</pre><pre style="margin-top: 0cm; margin-right: 0cm; margin-left: 0cm; margin-bottom: 0.0001pt; font-size: 10pt; font-family: 'Courier New'; color: black; ">-- Also note that this is not the r-help list where general R questions should go.</pre></div><br><hr><font color="Gray" face="Arial" size="1">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.</font></div></blockquote></div></div><div><div bgcolor="white" lang="EN-US" link="blue" vlink="purple"><font color="Gray" face="Arial" size="1"><br></font></div></div></body></html>