[Returnanalytics-commits] r3766 - in pkg/Dowd: . R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Jul 2 20:04:35 CEST 2015


Author: dacharya
Date: 2015-07-02 20:04:34 +0200 (Thu, 02 Jul 2015)
New Revision: 3766

Added:
   pkg/Dowd/R/PCAVaR.R
   pkg/Dowd/man/PCAVaR.Rd
Modified:
   pkg/Dowd/NAMESPACE
Log:
PCAVaR added.

Modified: pkg/Dowd/NAMESPACE
===================================================================
--- pkg/Dowd/NAMESPACE	2015-06-30 08:49:02 UTC (rev 3765)
+++ pkg/Dowd/NAMESPACE	2015-07-02 18:04:34 UTC (rev 3766)
@@ -59,6 +59,7 @@
 export(LopezBacktest)
 export(MEFPlot)
 export(NormalQQPlot)
+export(PCAVaR)
 export(PickandsEstimator)
 export(PickandsPlot)
 export(ProductCopulaVaR)

Added: pkg/Dowd/R/PCAVaR.R
===================================================================
--- pkg/Dowd/R/PCAVaR.R	                        (rev 0)
+++ pkg/Dowd/R/PCAVaR.R	2015-07-02 18:04:34 UTC (rev 3766)
@@ -0,0 +1,52 @@
+#' Estimates VaR by principal components analysis
+#' 
+#' Estimates the VaR of a multi position portfolio by principal components analysis, using chosen number of principal components and a specified confidence level or range of confidence levels.
+#' 
+#' @param Ra Matrix return data set where each row is interpreted as a set of daily observations, and each column as the returns to each position in a portfolio
+#' @param position.data Position-size vector, giving amount invested in each position
+#' @param number.of.principal.components Chosen number of principal components
+#' @param cl Chosen confidence level
+#' @return VaR
+#' @references Dowd, K. Measuring Market Risk, Wiley, 2007.
+#'
+#' @author Dinesh Acharya
+#' @examples
+#' 
+#'    # Computes PCA VaR
+#'    
+#'    Ra <- rnorm(30)
+#'    KernelESEpanechinikovKernel(Ra, .95)
+#'
+#' @export
+PCAVaR <- function(Ra, position.data, number.of.principal.components, cl){
+  # Check that inputs have correct dimensions
+  return.data<-as.matrix(Ra)
+  m <- dim(return.data)[1]
+  n <- dim(return.data)[2]
+  if (min(m, n) == 1) {
+    stop("Input data set has insufficient dimensionality")
+  }
+  if (number.of.principal.components < 0) {
+    stop("Number of principal components must be positive")
+  }
+  if (number.of.principal.components > n) {
+    stop("Number of principal components cannot exceed number of positions")
+  }
+  # Check that dimensions of position data and return data match
+  if (n != length(position.data)) {
+    stop("Dimensions of return data and position data should be compatible")
+  }
+  
+  # Principal components estimation
+  a <- svd(return.data)  # SVD; provides U and V
+  index <- n - number.of.principal.components # Establishes how many zero terms on diagonal of S matrix
+  print(index)
+  S.diag <- sort(a$d, decreasing = TRUE)[1:number.of.principal.components] # Creates diagonal for S matrix
+  S.diag <- diag(S.diag)
+  S <- matrix(0, min(m, n), min(m, n))
+  S[1:number.of.principal.components,1:number.of.principal.components] <- S.diag # Creates S matrix with diagonal S.diag
+  synthetic.PandL.data <- a$u %*% S %*% t(a$v) %*% position.data
+  
+  y <- HSVaR(synthetic.PandL.data, cl)
+  return(y)
+}
\ No newline at end of file

Added: pkg/Dowd/man/PCAVaR.Rd
===================================================================
--- pkg/Dowd/man/PCAVaR.Rd	                        (rev 0)
+++ pkg/Dowd/man/PCAVaR.Rd	2015-07-02 18:04:34 UTC (rev 3766)
@@ -0,0 +1,36 @@
+% Generated by roxygen2 (4.1.1): do not edit by hand
+% Please edit documentation in R/PCAVaR.R
+\name{PCAVaR}
+\alias{PCAVaR}
+\title{Estimates VaR by principal components analysis}
+\usage{
+PCAVaR(Ra, position.data, number.of.principal.components, cl)
+}
+\arguments{
+\item{Ra}{Matrix return data set where each row is interpreted as a set of daily observations, and each column as the returns to each position in a portfolio}
+
+\item{position.data}{Position-size vector, giving amount invested in each position}
+
+\item{number.of.principal.components}{Chosen number of principal components}
+
+\item{cl}{Chosen confidence level}
+}
+\value{
+VaR
+}
+\description{
+Estimates the VaR of a multi position portfolio by principal components analysis, using chosen number of principal components and a specified confidence level or range of confidence levels.
+}
+\examples{
+# Computes PCA VaR
+
+   Ra <- rnorm(30)
+   KernelESEpanechinikovKernel(Ra, .95)
+}
+\author{
+Dinesh Acharya
+}
+\references{
+Dowd, K. Measuring Market Risk, Wiley, 2007.
+}
+



More information about the Returnanalytics-commits mailing list