<!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">
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.<br>
<br>
volatility2 <- function (OHLC, n = 10, N = 260, calc = "close",
roundValue=FALSE, roundNum=2, normalize=FALSE, nonOHLC=FALSE, ...) <br>
{<br>
if (nonOHLC == FALSE) {<br>
<br>
OHLC <- try.xts(OHLC, error = as.matrix)<br>
calc <- match.arg(calc, c("close", "garman.klass",
"parkinson", <br>
"rogers.satchell"))<br>
if (calc == "close") {<br>
r <- ROC(OHLC[, 4], 1, ...)<br>
rBar <- runSum(r, n - 1)/(n - 1)<br>
s <- sqrt(N/(n - 2) * runSum((r - rBar)^2, n - 1))<br>
}<br>
if (calc == "garman.klass") {<br>
s <- sqrt(N/n * runSum(0.5 * log(OHLC[, 2]/OHLC[, 3])^2
- <br>
(2 * log(2) - 1) * log(OHLC[, 4]/OHLC[, 1])^2, n))<br>
}<br>
if (calc == "parkinson") {<br>
s <- sqrt(N/(4 * n * log(2)) * runSum(log(OHLC[,
2]/OHLC[, <br>
3])^2, n))<br>
}<br>
if (calc == "rogers.satchell") {<br>
s <- sqrt(N/n * runSum(log(OHLC[, 2]/OHLC[, 4]) *
log(OHLC[, <br>
2]/OHLC[, 1]) + log(OHLC[, 3]/OHLC[, 4]) * log(OHLC[, <br>
3]/OHLC[, 1]), n))<br>
}<br>
} else {<br>
#still using the OHLC object, since this is indeed a matrix<br>
# diff(log(OHLC)) provides the same output as ROC(x,1) so we should
be goog. Thanks to Murali Menon<br>
# for the suggested use of apply.rolling <br>
s <- apply.rolling(R = diff(log(OHLC)), width=n, FUN="sd",
na.pad=FALSE) * sqrt(N)<br>
}<br>
<br>
if (roundValue == TRUE) {<br>
s <- round(s, roundNum)<br>
}<br>
if (normalize == TRUE) {<br>
s <- s * 100<br>
}<br>
<br>
reclass(s, OHLC)<br>
}<br>
<br>
<br>
Cedrick Johnson wrote:
<blockquote cite="mid:4ABA9629.5020703@cedrickjohnson.com" type="cite"><font
size="-1"><font face="Calibri">Howdy-<br>
<br>
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):<br>
<br>
US2Y US5Y US10Y US30Y UKG5 UKG10 USSP2 USSP5 USSP10 USSP30<br>
2009-01-02 0.88 1.72 2.46 2.83 2.67 3.36 1.50 2.17 2.61 2.78<br>
2009-01-05 0.78 1.67 2.49 3.00 2.75 3.47 1.60 2.31 2.82 3.03<br>
2009-01-06 0.80 1.68 2.51 3.04 2.79 3.57 1.55 2.34 2.88 3.18<br>
2009-01-07 0.82 1.66 2.52 3.05 2.80 3.60 1.44 2.16 2.69 2.99<br>
....<br>
blahblah..<br>
<br>
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:<br>
<br>
volatility.matrix <- function(Matrix, n = 10, N = 252, pretty=TRUE,
...) {<br>
volm <- Matrix<br>
for(i in 1:ncol(Matrix)) {<br>
r <- ROC(Matrix[,i], 1,...)<br>
rBar <- runSum(r, n -1)/(n - 1)<br>
s <- sqrt(N/(n - 2) * runSum((r - rBar)^2, n - 1))<br>
#because i like to make it pretty<br>
volm[,i] <- round(s,2) * 100<br>
}<br>
volm <- na.omit(volm)<br>
reclass(volm, Matrix)<br>
}<br>
<br>
<br>
The Result:<br>
> volatility.matrix(Yields, n=30)<br>
US2Y US5Y US10Y US30Y UKG5 UKG10 USSP2 USSP5 USSP10 USSP30<br>
2009-03-24 116 110 73 45 54 57 67 65 60 51<br>
2009-03-25 115 112 75 46 51 55 67 65 60 51<br>
2009-03-26 114 110 74 44 50 55 65 64 60 50<br>
2009-03-27 114 110 74 44 50 55 67 64 60 51<br>
2009-03-30 111 104 68 39 52 55 67 63 58 51<br>
2009-03-31 107 100 68 39 51 55 67 63 58 50<br>
2009-04-01 107 100 67 38 51 54 67 62 57 49<br>
2009-04-02 107 100 67 37 58 57 65 61 56 48<br>
......<br>
<br>
What I get is a nice matrix of volatilities in a pretty format
(rounded, etc.)<br>
<br>
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.<br>
<br>
Regards,<br>
c<br>
<br>
<br>
</font></font>
<pre wrap="">
<hr size="4" width="90%">
_______________________________________________
<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>
<br>
</body>
</html>