<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Hi Josh-<br>
<br>
No worries! The approach you outlined below works out well and I think
would be a great addition to TTR.<br>
<br>
The exercise has definitely taught me a few new things in R that will
greatly clean up my scripts! Thanks everyone!<br>
<br>
-cedrick<br>
<br>
Joshua Ulrich wrote:
<blockquote
 cite="mid:8cca69990909281008k22c7a1aeg79e4e124c62488bd@mail.gmail.com"
 type="cite">
  <pre wrap="">Hi Cedrick,

Sorry for the slow response.  How about this quick change to
TTR::volatility() and the code below?  I agree that OHLC should be
allowed to be univariate in the case of calc="close".  I'm hesitant to
add the other logic though, since the same results can be achieved
with an external call to apply() -- see below.

volatility3 &lt;-
function (OHLC, n = 10, N = 260, calc = "close", ...)
{
&lt;snip&gt;
    if (calc == "close") {
        # Add univariate case for calc="close"
        if (NCOL(OHLC) == 1) {
            r &lt;- ROC(OHLC, 1, ...)
        } else {
            r &lt;- ROC(OHLC[, 4], 1, ...)
        }
        rBar &lt;- runSum(r, n - 1)/(n - 1)
        s &lt;- sqrt(N/(n - 2) * runSum((r - rBar)^2, n - 1))
    }
&lt;snip&gt;
    reclass(s, OHLC)
}

Then you can get pretty volatilities via a command like:
  </pre>
  <blockquote type="cite">
    <pre wrap="">require(xts)
data(sample_matrix)
# Pretty volatility (note "v" will always be a matrix, even if OHLC is xts)
v &lt;- round(apply(sample_matrix, 2, volatility3)*100,0)
tail(v)
    </pre>
  </blockquote>
  <pre wrap=""><!---->           Open High Low Close
2007-06-25    6    5   5     6
2007-06-26    6    6   6     6
2007-06-27    6    6   6     6
2007-06-28    6    5   6     5
2007-06-29    5    5   6     5
2007-06-30    5    5   6     5
  </pre>
  <blockquote type="cite">
    <pre wrap="">tail( volatility3(sample_matrix)*100 )
    </pre>
  </blockquote>
  <pre wrap=""><!---->               [,1]
2007-06-25 5.656552
2007-06-26 5.729933
2007-06-27 5.732217
2007-06-28 4.771530
2007-06-29 4.764216
2007-06-30 4.818589

HTH,
Josh
--
<a class="moz-txt-link-freetext" href="http://www.fosstrading.com">http://www.fosstrading.com</a>



On Mon, Sep 28, 2009 at 9:34 AM, Cedrick Johnson
<a class="moz-txt-link-rfc2396E" href="mailto:cedrick@cedrickjohnson.com">&lt;cedrick@cedrickjohnson.com&gt;</a> wrote:
  </pre>
  <blockquote type="cite">
    <pre wrap="">As promised, here's my attempt at incorporating it into the original
volatility() function (for the sake of testing, I've renamed the new
function volatility2(..). Thanks to Murali for the suggestion to use
apply.rolling, I've added in a few new variables in the function definition.
So far it seems to work well here.

volatility2 &lt;- function (OHLC, n = 10, N = 260, calc = "close",
roundValue=FALSE, roundNum=2, normalize=FALSE, nonOHLC=FALSE, ...)
{
&nbsp;&nbsp;&nbsp; if (nonOHLC == FALSE) {

&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; OHLC &lt;- try.xts(OHLC, error = as.matrix)
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; calc &lt;- match.arg(calc, c("close", "garman.klass", "parkinson",
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "rogers.satchell"))
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (calc == "close") {
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;r &lt;- ROC(OHLC[, 4], 1, ...)
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rBar &lt;- runSum(r, n - 1)/(n - 1)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;s &lt;- sqrt(N/(n - 2) * runSum((r - rBar)^2, n - 1))
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (calc == "garman.klass") {
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s &lt;- sqrt(N/n * runSum(0.5 * log(OHLC[, 2]/OHLC[, 3])^2 -
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (2 * log(2) - 1) * log(OHLC[, 4]/OHLC[, 1])^2, n))
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (calc == "parkinson") {
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s &lt;- sqrt(N/(4 * n * log(2)) * runSum(log(OHLC[, 2]/OHLC[,
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3])^2, n))
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (calc == "rogers.satchell") {
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s &lt;- sqrt(N/n * runSum(log(OHLC[, 2]/OHLC[, 4]) * log(OHLC[,
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2]/OHLC[, 1]) + log(OHLC[, 3]/OHLC[, 4]) * log(OHLC[,
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3]/OHLC[, 1]), n))
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; } else {
&nbsp;&nbsp;&nbsp; #still using the OHLC object, since this is indeed a matrix
&nbsp;&nbsp;&nbsp; # diff(log(OHLC)) provides the same output as ROC(x,1) so we should be
goog. Thanks to Murali Menon
&nbsp;&nbsp;&nbsp; # for the suggested use of apply.rolling
&nbsp;&nbsp;&nbsp; s &lt;- apply.rolling(R = diff(log(OHLC)), width=n, FUN="sd", na.pad=FALSE)
* sqrt(N)
&nbsp;&nbsp;&nbsp; }

&nbsp;&nbsp;&nbsp; if (roundValue == TRUE) {
&nbsp;&nbsp;&nbsp; s &lt;- round(s, roundNum)
&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; if (normalize == TRUE) {
&nbsp;&nbsp;&nbsp; s &lt;- s * 100
&nbsp;&nbsp;&nbsp; }

&nbsp;&nbsp;&nbsp; reclass(s, OHLC)
}


Cedrick Johnson wrote:

Howdy-

I came up with some code that has served me well, perhaps if anyone runs
into the problem of needing to calculate historical volatility(close) on a
non-OHLC series (or a bunch of "univariate" series stored in a matrix):

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; US2Y US5Y US10Y US30Y UKG5 UKG10 USSP2 USSP5 USSP10 USSP30
2009-01-02 0.88 1.72&nbsp; 2.46&nbsp; 2.83 2.67&nbsp; 3.36&nbsp; 1.50&nbsp; 2.17&nbsp;&nbsp; 2.61&nbsp;&nbsp; 2.78
2009-01-05 0.78 1.67&nbsp; 2.49&nbsp; 3.00 2.75&nbsp; 3.47&nbsp; 1.60&nbsp; 2.31&nbsp;&nbsp; 2.82&nbsp;&nbsp; 3.03
2009-01-06 0.80 1.68&nbsp; 2.51&nbsp; 3.04 2.79&nbsp; 3.57&nbsp; 1.55&nbsp; 2.34&nbsp;&nbsp; 2.88&nbsp;&nbsp; 3.18
2009-01-07 0.82 1.66&nbsp; 2.52&nbsp; 3.05 2.80&nbsp; 3.60&nbsp; 1.44&nbsp; 2.16&nbsp;&nbsp; 2.69&nbsp;&nbsp; 2.99
....
blahblah..

I adapted the code to use 'close' from Josh Ulrich's volatility function
(since all of these are closing prices), and here's my result:

volatility.matrix &lt;- function(Matrix, n = 10, N = 252, pretty=TRUE, ...) {
&nbsp;&nbsp;&nbsp; volm &lt;- Matrix
&nbsp;&nbsp;&nbsp; for(i in 1:ncol(Matrix)) {
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; r &lt;- ROC(Matrix[,i], 1,...)
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; rBar &lt;- runSum(r, n -1)/(n - 1)
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; s &lt;- sqrt(N/(n - 2) * runSum((r - rBar)^2, n - 1))
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; #because i like to make it pretty
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; volm[,i] &lt;- round(s,2) * 100
&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; volm &lt;- na.omit(volm)
&nbsp;&nbsp;&nbsp; reclass(volm, Matrix)
}


The Result:
    </pre>
    <blockquote type="cite">
      <pre wrap="">volatility.matrix(Yields, n=30)
      </pre>
    </blockquote>
    <pre wrap="">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; US2Y US5Y US10Y US30Y UKG5 UKG10 USSP2 USSP5 USSP10 USSP30
2009-03-24&nbsp; 116&nbsp; 110&nbsp;&nbsp;&nbsp; 73&nbsp;&nbsp;&nbsp; 45&nbsp;&nbsp; 54&nbsp;&nbsp;&nbsp; 57&nbsp;&nbsp;&nbsp; 67&nbsp;&nbsp;&nbsp; 65&nbsp;&nbsp;&nbsp;&nbsp; 60&nbsp;&nbsp;&nbsp;&nbsp; 51
2009-03-25&nbsp; 115&nbsp; 112&nbsp;&nbsp;&nbsp; 75&nbsp;&nbsp;&nbsp; 46&nbsp;&nbsp; 51&nbsp;&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&nbsp;&nbsp;&nbsp;&nbsp; 49
2009-04-02&nbsp; 107&nbsp; 100&nbsp;&nbsp;&nbsp; 67&nbsp;&nbsp;&nbsp; 37&nbsp;&nbsp; 58&nbsp;&nbsp;&nbsp; 57&nbsp;&nbsp;&nbsp; 65&nbsp;&nbsp;&nbsp; 61&nbsp;&nbsp;&nbsp;&nbsp; 56&nbsp;&nbsp;&nbsp;&nbsp; 48
......

What I get is a nice matrix of volatilities in a pretty format (rounded,
etc.)

I'll give it a shot tonight trying to implement it into the current
volatility function, however I'm sure the code that I have here could be
done better.

Regards,
c


________________________________
_______________________________________________
<a class="moz-txt-link-abbreviated" href="mailto:R-SIG-Finance@stat.math.ethz.ch">R-SIG-Finance@stat.math.ethz.ch</a> mailing list
<a class="moz-txt-link-freetext" href="https://stat.ethz.ch/mailman/listinfo/r-sig-finance">https://stat.ethz.ch/mailman/listinfo/r-sig-finance</a>
-- Subscriber-posting only.
-- If you want to post, subscribe first.

_______________________________________________
<a class="moz-txt-link-abbreviated" href="mailto:R-SIG-Finance@stat.math.ethz.ch">R-SIG-Finance@stat.math.ethz.ch</a> mailing list
<a class="moz-txt-link-freetext" href="https://stat.ethz.ch/mailman/listinfo/r-sig-finance">https://stat.ethz.ch/mailman/listinfo/r-sig-finance</a>
-- Subscriber-posting only.
-- If you want to post, subscribe first.

    </pre>
  </blockquote>
</blockquote>
<br>
</body>
</html>