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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Jul 2 20:17:26 CEST 2015


Author: dacharya
Date: 2015-07-02 20:17:26 +0200 (Thu, 02 Jul 2015)
New Revision: 3767

Added:
   pkg/Dowd/R/PCAES.R
   pkg/Dowd/man/PCAES.Rd
Modified:
   pkg/Dowd/NAMESPACE
Log:
PCAES (Principal Component Analysis ES) added.

Modified: pkg/Dowd/NAMESPACE
===================================================================
--- pkg/Dowd/NAMESPACE	2015-07-02 18:04:34 UTC (rev 3766)
+++ pkg/Dowd/NAMESPACE	2015-07-02 18:17:26 UTC (rev 3767)
@@ -59,6 +59,7 @@
 export(LopezBacktest)
 export(MEFPlot)
 export(NormalQQPlot)
+export(PCAES)
 export(PCAVaR)
 export(PickandsEstimator)
 export(PickandsPlot)

Added: pkg/Dowd/R/PCAES.R
===================================================================
--- pkg/Dowd/R/PCAES.R	                        (rev 0)
+++ pkg/Dowd/R/PCAES.R	2015-07-02 18:17:26 UTC (rev 3767)
@@ -0,0 +1,51 @@
+#' Estimates ES by principal components analysis
+#' 
+#' Estimates the ES 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 ES
+#' @references Dowd, K. Measuring Market Risk, Wiley, 2007.
+#'
+#' @author Dinesh Acharya
+#' @examples
+#' 
+#'    # Computes PCA ES
+#'    Ra <- matrix(rnorm(4*6),4,6)
+#'    position.data <- rnorm(6)
+#'    PCAES(Ra, position.data, 2, .95)
+#'
+#' @export
+PCAES <- 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
+  S.diag <- c(sort(a$d, decreasing = TRUE)[1:number.of.principal.components],
+              double(index)) # Creates diagonal for S matrix
+  S <- matrix(0, m, n)
+  diag(S) <- S.diag # Creates S matrix with diagonal S.diag
+  synthetic.PandL.data <- a$u %*% S * t(a$v) * position.data
+  
+  y <- HSES(synthetic.PandL.data, cl)
+  return(y)
+}
\ No newline at end of file

Added: pkg/Dowd/man/PCAES.Rd
===================================================================
--- pkg/Dowd/man/PCAES.Rd	                        (rev 0)
+++ pkg/Dowd/man/PCAES.Rd	2015-07-02 18:17:26 UTC (rev 3767)
@@ -0,0 +1,36 @@
+% Generated by roxygen2 (4.1.1): do not edit by hand
+% Please edit documentation in R/PCAES.R
+\name{PCAES}
+\alias{PCAES}
+\title{Estimates ES by principal components analysis}
+\usage{
+PCAES(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{
+ES
+}
+\description{
+Estimates the ES 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 ES
+   Ra <- matrix(rnorm(4*6),4,6)
+   position.data <- rnorm(6)
+   PCAES(Ra, position.data, 2, .95)
+}
+\author{
+Dinesh Acharya
+}
+\references{
+Dowd, K. Measuring Market Risk, Wiley, 2007.
+}
+



More information about the Returnanalytics-commits mailing list