<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body bgcolor="#ffffff" text="#000000">
<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>
</body>
</html>