[Uwgarp-commits] r122 - in pkg/GARPFRM: . R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Sat Mar 22 21:18:12 CET 2014
Author: rossbennett34
Date: 2014-03-22 21:18:12 +0100 (Sat, 22 Mar 2014)
New Revision: 122
Added:
pkg/GARPFRM/R/volatility.R
pkg/GARPFRM/man/rollCor.Rd
pkg/GARPFRM/man/rollCov.Rd
pkg/GARPFRM/man/rollSD.Rd
pkg/GARPFRM/man/rollSimpleVolatility.Rd
Modified:
pkg/GARPFRM/DESCRIPTION
pkg/GARPFRM/NAMESPACE
pkg/GARPFRM/R/rollFUN.R
Log:
Adding functions and documentation for estimating volatility, covariance, and correlation
Modified: pkg/GARPFRM/DESCRIPTION
===================================================================
--- pkg/GARPFRM/DESCRIPTION 2014-03-22 04:03:15 UTC (rev 121)
+++ pkg/GARPFRM/DESCRIPTION 2014-03-22 20:18:12 UTC (rev 122)
@@ -22,3 +22,4 @@
'monte_carlo.R'
'efficient_frontier.R'
'rollFUN.R'
+ 'volatility.R'
Modified: pkg/GARPFRM/NAMESPACE
===================================================================
--- pkg/GARPFRM/NAMESPACE 2014-03-22 04:03:15 UTC (rev 121)
+++ pkg/GARPFRM/NAMESPACE 2014-03-22 20:18:12 UTC (rev 122)
@@ -30,6 +30,11 @@
export(realizedCor)
export(realizedCov)
export(realizedVol)
+export(rollCor)
+export(rollCov)
+export(rollSD)
+export(rollSimpleVolatility)
+export(simpleVolatility)
export(tangentPortfolio)
export(uvGARCH)
S3method(countViolations,xts)
Modified: pkg/GARPFRM/R/rollFUN.R
===================================================================
--- pkg/GARPFRM/R/rollFUN.R 2014-03-22 04:03:15 UTC (rev 121)
+++ pkg/GARPFRM/R/rollFUN.R 2014-03-22 20:18:12 UTC (rev 122)
@@ -1,4 +1,20 @@
+
+#' Rolling Covariance Estimate
+#'
+#' This function calculates the covariance estimate of the returns of two
+#' assets over a rolling window
+#'
+#' @param R xts or zoo object of asset returns
+#' @param width width of rolling window
+#' @author Ross Bennett
+#' @seealso \code{\link{cov}}
+#' @examples
+#' data(crsp_weekly)
+#' R <- largecap_weekly[,1:2]
+#' tail(rollCov(R, 10))
+#' @export
rollCov <- function(R, width){
+ if(!inherits(R, c("xts", "zoo"))) stop("x must be an xts or zoo object")
n <- nrow(R)
out <- xts(vector("numeric", n), index(R))
for(i in width:n){
@@ -12,7 +28,22 @@
out
}
+#' Rolling Correlation Estimate
+#'
+#' This function calculates the correlation estimate of the returns of two
+#' assets over a rolling window
+#'
+#' @param R xts or zoo object of asset returns
+#' @param width width of rolling window
+#' @author Ross Bennett
+#' @seealso \code{\link{cor}}
+#' @examples
+#' data(crsp_weekly)
+#' R <- largecap_weekly[,1:2]
+#' tail(rollCor(R, 10))
+#' @export
rollCor <- function(R, width){
+ if(!inherits(R, c("xts", "zoo"))) stop("x must be an xts or zoo object")
n <- nrow(R)
out <- xts(vector("numeric", n), index(R))
for(i in width:n){
@@ -26,11 +57,16 @@
out
}
-rollSD <- function(R, width){
- n <- nrow(R)
- out <- xts(vector("numeric", n), index(R))
+# rollSD function for a univariate R
+.rollSD <- function(R, width){
+ # if(!inherits(R, c("xts", "zoo"))) stop("R must be an xts or zoo object")
+ # this function should generally not be called by the user and we will check
+ # for xts or zoo object in rollSD which calls .rollSD
+ n <- length(R)
+ # out <- xts(vector("numeric", n), index(R))
+ out <- vector("numeric", n)
for(i in width:n){
- tmpR <- R[(i-width+1):i,]
+ tmpR <- R[(i-width+1):i,1]
out[i] <- sd(tmpR[,1])
}
# pad with leading NA
@@ -39,3 +75,82 @@
}
out
}
+
+#' Rolling Standard Deviation Estimate
+#'
+#' This function calculates the standard deviation estimate of asset returns
+#' over a rolling window
+#'
+#' @param R xts or zoo object of asset returns
+#' @param width width of rolling window
+#' @author Ross Bennett
+#' @seealso \code{\link{sd}}
+#' @examples
+#' data(crsp_weekly)
+#' R <- largecap_weekly[,1:4]
+#' tail(rollSD(R, 10))
+#' @export
+rollSD <- function(R, width){
+ if(!inherits(R, c("xts", "zoo"))) stop("R must be an xts or zoo object")
+ if(ncol(R) == 1){
+ tmp <- .rollSD(R, width)
+ } else {
+ tmp <- matrix(0, nrow(R), ncol(R))
+ for(i in 1:ncol(R)){
+ tmp[,i] <- .rollSD(R[,i], width=width)
+ }
+ }
+ out <- xts(tmp, index(R))
+ colnames(out) <- colnames(R)
+ return(out)
+}
+
+# rolling simple volatility estimate for a univariate R
+.rollSimpleVolatility <- function(R, width){
+ n <- length(R)
+ out <- vector("numeric", n)
+ for(i in width:n){
+ tmpR <- R[(i-width+1):i,1]
+ out[i] <- .simpleVolatility(tmpR)
+ }
+ # pad with leading NA
+ for(i in 1:(width-1)){
+ out[i] <- NA
+ }
+ out
+}
+
+#' Rolling Simple Volatility Estimate
+#'
+#' This function calculates the simple volatility estimate of asset returns
+#' over a rolling window.
+#'
+#' The simple volatility of x is defined as
+#'
+#' \deqn{
+#' \sigma = \sqrt{\frac{1}{n} \sum_{i=1}^n x_i^2}
+#' }
+#'
+#' @param R xts or zoo object of asset returns
+#' @param width width of rolling window
+#' @author Ross Bennett
+#' @seealso \code{\link{sd}}, \code{\link{simpleVolatility}}
+#' @examples
+#' data(crsp_weekly)
+#' R <- largecap_weekly[,1:2]
+#' tail(rollSimpleVolatility(R, 10))
+#' @export
+rollSimpleVolatility <- function(R, width){
+ if(!inherits(R, c("xts", "zoo"))) stop("R must be an xts or zoo object")
+ if(ncol(R) == 1){
+ tmp <- .rollSimpleVolatility(R, width)
+ } else {
+ tmp <- matrix(0, nrow(R), ncol(R))
+ for(i in 1:ncol(R)){
+ tmp[,i] <- .rollSimpleVolatility(R[,i], width=width)
+ }
+ }
+ out <- xts(tmp, index(R))
+ colnames(out) <- colnames(R)
+ return(out)
+}
Added: pkg/GARPFRM/R/volatility.R
===================================================================
--- pkg/GARPFRM/R/volatility.R (rev 0)
+++ pkg/GARPFRM/R/volatility.R 2014-03-22 20:18:12 UTC (rev 122)
@@ -0,0 +1,45 @@
+
+#' Simple Volatility
+#'
+#' This function calculates the volatility of asset returns using a simplified
+#' equation.
+#'
+#' @details
+#' This function is different from \code{sd} in two ways.
+#' \itemize{
+#' \item \code{simpleVolatility} uses a denominator of \code{n}.
+#' \item \code{simpleVolatility} assumes the mean to be zero.
+#' }
+#' The simple volatility of x is defined as
+#'
+#' \deqn{
+#' \sigma = \sqrt{\frac{1}{n} \sum_{i=1}^n x_i^2}
+#' }
+#'
+#' @param R xts or zoo object of asset returns
+#' @author Ross Bennett
+#' @seealso \code{\link{sd}}
+#' @examples
+#' data(crsp_weekly)
+#' R <- largecap_weekly[,1:4]
+#' simpleVolatility(R[,1])
+#' simpleVolatility(R)
+#' @export
+simpleVolatility <- function(R){
+ if(!inherits(R, c("xts", "zoo"))) stop("R must be an xts or zoo object")
+ n <- nrow(R)
+ if(ncol(R) == 1){
+ out <- .simpleVolatility(R)
+ } else if(ncol(R) > 1){
+ out <- apply(R, 2, .simpleVolatility)
+ }
+ names(out) <- colnames(R)
+ return(out)
+}
+
+.simpleVolatility <- function(R){
+ n <- length(R)
+ out <- sqrt(sum(R^2) / n)
+ return(out)
+}
+
Added: pkg/GARPFRM/man/rollCor.Rd
===================================================================
--- pkg/GARPFRM/man/rollCor.Rd (rev 0)
+++ pkg/GARPFRM/man/rollCor.Rd 2014-03-22 20:18:12 UTC (rev 122)
@@ -0,0 +1,27 @@
+\name{rollCor}
+\alias{rollCor}
+\title{Rolling Correlation Estimate}
+\usage{
+ rollCor(R, width)
+}
+\arguments{
+ \item{R}{xts or zoo object of asset returns}
+
+ \item{width}{width of rolling window}
+}
+\description{
+ This function calculates the correlation estimate of the
+ returns of two assets over a rolling window
+}
+\examples{
+data(crsp_weekly)
+R <- largecap_weekly[,1:2]
+tail(rollCor(R, 10))
+}
+\author{
+ Ross Bennett
+}
+\seealso{
+ \code{\link{cor}}
+}
+
Added: pkg/GARPFRM/man/rollCov.Rd
===================================================================
--- pkg/GARPFRM/man/rollCov.Rd (rev 0)
+++ pkg/GARPFRM/man/rollCov.Rd 2014-03-22 20:18:12 UTC (rev 122)
@@ -0,0 +1,27 @@
+\name{rollCov}
+\alias{rollCov}
+\title{Rolling Covariance Estimate}
+\usage{
+ rollCov(R, width)
+}
+\arguments{
+ \item{R}{xts or zoo object of asset returns}
+
+ \item{width}{width of rolling window}
+}
+\description{
+ This function calculates the covariance estimate of the
+ returns of two assets over a rolling window
+}
+\examples{
+data(crsp_weekly)
+R <- largecap_weekly[,1:2]
+tail(rollCov(R, 10))
+}
+\author{
+ Ross Bennett
+}
+\seealso{
+ \code{\link{cov}}
+}
+
Added: pkg/GARPFRM/man/rollSD.Rd
===================================================================
--- pkg/GARPFRM/man/rollSD.Rd (rev 0)
+++ pkg/GARPFRM/man/rollSD.Rd 2014-03-22 20:18:12 UTC (rev 122)
@@ -0,0 +1,27 @@
+\name{rollSD}
+\alias{rollSD}
+\title{Rolling Standard Deviation Estimate}
+\usage{
+ rollSD(R, width)
+}
+\arguments{
+ \item{R}{xts or zoo object of asset returns}
+
+ \item{width}{width of rolling window}
+}
+\description{
+ This function calculates the standard deviation estimate
+ of asset returns over a rolling window
+}
+\examples{
+data(crsp_weekly)
+R <- largecap_weekly[,1:4]
+tail(rollSD(R, 10))
+}
+\author{
+ Ross Bennett
+}
+\seealso{
+ \code{\link{sd}}
+}
+
Added: pkg/GARPFRM/man/rollSimpleVolatility.Rd
===================================================================
--- pkg/GARPFRM/man/rollSimpleVolatility.Rd (rev 0)
+++ pkg/GARPFRM/man/rollSimpleVolatility.Rd 2014-03-22 20:18:12 UTC (rev 122)
@@ -0,0 +1,32 @@
+\name{rollSimpleVolatility}
+\alias{rollSimpleVolatility}
+\title{Rolling Simple Volatility Estimate}
+\usage{
+ rollSimpleVolatility(R, width)
+}
+\arguments{
+ \item{R}{xts or zoo object of asset returns}
+
+ \item{width}{width of rolling window}
+}
+\description{
+ This function calculates the simple volatility estimate
+ of asset returns over a rolling window.
+}
+\details{
+ The simple volatility of x is defined as
+
+ \deqn{ \sigma = \sqrt{\frac{1}{n} \sum_{i=1}^n x_i^2} }
+}
+\examples{
+data(crsp_weekly)
+R <- largecap_weekly[,1:2]
+tail(rollSimpleVolatility(R, 10))
+}
+\author{
+ Ross Bennett
+}
+\seealso{
+ \code{\link{sd}}, \code{\link{simpleVolatility}}
+}
+
More information about the Uwgarp-commits
mailing list