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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Jun 10 23:08:25 CEST 2015


Author: dacharya
Date: 2015-06-10 23:08:25 +0200 (Wed, 10 Jun 2015)
New Revision: 3670

Added:
   pkg/Dowd/R/FrechetES.R
   pkg/Dowd/R/FrechetVaR.R
   pkg/Dowd/man/FrechetES.Rd
   pkg/Dowd/man/FrechetVaR.Rd
Modified:
   pkg/Dowd/NAMESPACE
   pkg/Dowd/readme.txt
Log:
FrechetES and FrechetVaR: source and documentation

Modified: pkg/Dowd/NAMESPACE
===================================================================
--- pkg/Dowd/NAMESPACE	2015-06-10 18:40:35 UTC (rev 3669)
+++ pkg/Dowd/NAMESPACE	2015-06-10 21:08:25 UTC (rev 3670)
@@ -16,6 +16,8 @@
 export(ChristoffersenBacktestForUnconditionalCoverage)
 export(CornishFisherES)
 export(CornishFisherVaR)
+export(FrechetES)
+export(FrechetVaR)
 export(GaussianCopulaVaR)
 export(GumbelCopulaVaR)
 export(HSES)

Added: pkg/Dowd/R/FrechetES.R
===================================================================
--- pkg/Dowd/R/FrechetES.R	                        (rev 0)
+++ pkg/Dowd/R/FrechetES.R	2015-06-10 21:08:25 UTC (rev 3670)
@@ -0,0 +1,97 @@
+#' Frechet Expected Shortfall
+#'
+#' Plots the ES of a portfolio against confidence level assuming extreme losses
+#' are Frechet distributed, for specified confidence level and a given 
+#' holding period.
+#'
+#' Note that the long-right-hand tail is fitted to losses, not profits.
+#' 
+#'
+#' @param mu Location parameter for daily L/P
+#' @param sigma Scale parameter for daily L/P
+#' @param tail.index Tail index
+#' @param n Block size from which maxima are drawn
+#' @param cl Confidence level
+#' @param hp Holding period
+#' @return Value at Risk. If cl and hp are scalars, it returns scalar VaR. If cl
+#' is vector and hp is a scalar, or viceversa, returns vector of VaRs. If both 
+#' cl and hp are vectors, returns a matrix of VaRs.
+#' 
+#' @references Dowd, K. Measuring Market Risk, Wiley, 2007.
+#' 
+#' Embrechts, P., Kluppelberg, C. and Mikosch, T., Modelling Extremal Events for
+#' Insurance and Finance. Springer, Berlin, 1997, p. 324.
+#' 
+#' Reiss, R. D. and Thomas, M. Statistical Analysis of Extreme Values from 
+#' Insurance, Finance, Hydrology and Other Fields, Birkhaueser, Basel, 1997, 
+#' 15-18.
+#' 
+#' @author Dinesh Acharya
+#' @examples
+#' 
+#'    # Computes VaR assuming Frechet Distribution for given parameters
+#'    FrechetVaR(3.5, 2.3, 1.6, 10, .95, 30)
+#'
+#' @export
+FrechetES <- function(mu, sigma, tail.index, n, cl, hp){
+  
+  # Check that inputs have correct dimensions
+  if (!length(mu) == 1) {
+    stop("mu must be a scalar")
+  }
+  if (!length(sigma) == 1) {
+    stop("sigma must be a scalar")
+  }
+  if (!length(tail.index) == 1) {
+    stop("tail.index must be a scalar")
+  }
+  if (!is.vector(cl)) {
+    stop("cl must be a vector or a scalar")
+  }
+  if (!is.vector(hp)) {
+    stop("hp must be a vector or a scalar")
+  }
+  
+  # Change cl and hp to row vector and column vectors respectively
+  cl <- t(as.matrix(cl))
+  hp <- as.matrix(hp)
+  
+  # Check that parameters obey sign and value restrictions
+  if (sigma < 0) {
+    stop("Standard deviation must be non-negative")
+  }
+  if (min(tail.index) <= 0) {
+    stop("Tail index must be greater than 0")
+  }
+  if ( max(cl) >= 1){
+    stop("Confidence level(s) must be less than 1")
+  }
+  if ( min(cl) <= 0){
+    stop("Confidence level(s) must be greater than 0")
+  }
+  if ( min(cl) <= 0){
+    stop("Holding period(s) must be greater than 0")
+  }
+  
+  # VaR estimation
+  VaR <- mu * matrix(1, 1, length(cl)) - (sigma / tail.index) * 
+    (1 - ( - n * log(cl)) ^ ( - tail.index))
+  
+  # ES Estimation
+  number.slices <- 1000 # Number of slices into which tail is divided
+  cl0 <- cl # Initial confidence level
+  term <- VaR
+  
+  delta.cl <- (1 - cl) / number.slices # Increment to confidence level as each slice is taken
+  for (i in 1:(number.slices-1)) {
+    cl <- cl0 + i * delta.cl # Revised cl
+    term <- term + mu * matrix(1, 1, length(cl)) - (sigma / tail.index) * 
+      (1 - ( - n * log(cl)) ^ ( - tail.index))
+    # NB Frechet term
+  }
+  
+  y <- term / (number.slices - 1)
+  
+  return(y)
+  
+} 
\ No newline at end of file

Added: pkg/Dowd/R/FrechetVaR.R
===================================================================
--- pkg/Dowd/R/FrechetVaR.R	                        (rev 0)
+++ pkg/Dowd/R/FrechetVaR.R	2015-06-10 21:08:25 UTC (rev 3670)
@@ -0,0 +1,81 @@
+#' Frechet Value at Risk
+#'
+#' Plots the VaR of a portfolio against confidence level assuming extreme losses
+#' are Frechet distributed, for specified range of confidence level and a given 
+#' holding period.
+#'
+#' Note that the long-right-hand tail is fitted to losses, not profits.
+#' 
+#'
+#' @param mu Location parameter for daily L/P
+#' @param sigma Scale parameter for daily L/P
+#' @param tail.index Tail index
+#' @param n Block size from which maxima are drawn
+#' @param cl Confidence level
+#' @param hp Holding period
+#' @return Value at Risk. If cl and hp are scalars, it returns scalar VaR. If cl
+#' is vector and hp is a scalar, or viceversa, returns vector of VaRs. If both 
+#' cl and hp are vectors, returns a matrix of VaRs.
+#' 
+#' @references Dowd, K. Measuring Market Risk, Wiley, 2007.
+#' 
+#' Embrechts, P., Kluppelberg, C. and Mikosch, T., Modelling Extremal Events for
+#' Insurance and Finance. Springer, Berlin, 1997, p. 324.
+#' 
+#' Reiss, R. D. and Thomas, M. Statistical Analysis of Extreme Values from 
+#' Insurance, Finance, Hydrology and Other Fields, Birkhaueser, Basel, 1997, 
+#' 15-18.
+#' 
+#' @author Dinesh Acharya
+#' @examples
+#' 
+#'    # Computes VaR assuming Frechet Distribution for given parameters
+#'    FrechetVaR(3.5, 2.3, 1.6, 10, .95, 30)
+#'
+#' @export
+FrechetVaR <- function(mu, sigma, tail.index, n, cl, hp){
+  
+  # Check that inputs have correct dimensions
+  if (!length(mu) == 1) {
+    stop("mu must be a scalar")
+  }
+  if (!length(sigma) == 1) {
+    stop("sigma must be a scalar")
+  }
+  if (!length(tail.index) == 1) {
+    stop("tail.index must be a scalar")
+  }
+  if (!is.vector(cl)) {
+    stop("cl must be a vector or a scalar")
+  }
+  if (!is.vector(hp)) {
+    stop("hp must be a vector or a scalar")
+  }
+  
+  # Change cl and hp to row vector and column vectors respectively
+  cl <- t(as.matrix(cl))
+  hp <- as.matrix(hp)
+  
+  # Check that parameters obey sign and value restrictions
+  if (sigma < 0) {
+    stop("Standard deviation must be non-negative")
+  }
+  if (min(tail.index) <= 0) {
+    stop("Tail index must be greater than 0")
+  }
+  if ( max(cl) >= 1){
+    stop("Confidence level(s) must be less than 1")
+  }
+  if ( min(cl) <= 0){
+    stop("Confidence level(s) must be greater than 0")
+  }
+  if ( min(cl) <= 0){
+    stop("Holding period(s) must be greater than 0")
+  }
+  # VaR estimation
+  y <- mu * matrix(1, 1, length(cl)) - (sigma / tail.index) * 
+    (1 - ( - n * log(cl)) ^ ( - tail.index))
+  
+  return(y)
+  
+} 
\ No newline at end of file

Added: pkg/Dowd/man/FrechetES.Rd
===================================================================
--- pkg/Dowd/man/FrechetES.Rd	                        (rev 0)
+++ pkg/Dowd/man/FrechetES.Rd	2015-06-10 21:08:25 UTC (rev 3670)
@@ -0,0 +1,52 @@
+% Generated by roxygen2 (4.1.1): do not edit by hand
+% Please edit documentation in R/FrechetES.R
+\name{FrechetES}
+\alias{FrechetES}
+\title{Frechet Expected Shortfall}
+\usage{
+FrechetES(mu, sigma, tail.index, n, cl, hp)
+}
+\arguments{
+\item{mu}{Location parameter for daily L/P}
+
+\item{sigma}{Scale parameter for daily L/P}
+
+\item{tail.index}{Tail index}
+
+\item{n}{Block size from which maxima are drawn}
+
+\item{cl}{Confidence level}
+
+\item{hp}{Holding period}
+}
+\value{
+Value at Risk. If cl and hp are scalars, it returns scalar VaR. If cl
+is vector and hp is a scalar, or viceversa, returns vector of VaRs. If both
+cl and hp are vectors, returns a matrix of VaRs.
+}
+\description{
+Plots the ES of a portfolio against confidence level assuming extreme losses
+are Frechet distributed, for specified confidence level and a given
+holding period.
+}
+\details{
+Note that the long-right-hand tail is fitted to losses, not profits.
+}
+\examples{
+# Computes VaR assuming Frechet Distribution for given parameters
+   FrechetVaR(3.5, 2.3, 1.6, 10, .95, 30)
+}
+\author{
+Dinesh Acharya
+}
+\references{
+Dowd, K. Measuring Market Risk, Wiley, 2007.
+
+Embrechts, P., Kluppelberg, C. and Mikosch, T., Modelling Extremal Events for
+Insurance and Finance. Springer, Berlin, 1997, p. 324.
+
+Reiss, R. D. and Thomas, M. Statistical Analysis of Extreme Values from
+Insurance, Finance, Hydrology and Other Fields, Birkhaueser, Basel, 1997,
+15-18.
+}
+

Added: pkg/Dowd/man/FrechetVaR.Rd
===================================================================
--- pkg/Dowd/man/FrechetVaR.Rd	                        (rev 0)
+++ pkg/Dowd/man/FrechetVaR.Rd	2015-06-10 21:08:25 UTC (rev 3670)
@@ -0,0 +1,52 @@
+% Generated by roxygen2 (4.1.1): do not edit by hand
+% Please edit documentation in R/FrechetVaR.R
+\name{FrechetVaR}
+\alias{FrechetVaR}
+\title{Frechet Value at Risk}
+\usage{
+FrechetVaR(mu, sigma, tail.index, n, cl, hp)
+}
+\arguments{
+\item{mu}{Location parameter for daily L/P}
+
+\item{sigma}{Scale parameter for daily L/P}
+
+\item{tail.index}{Tail index}
+
+\item{n}{Block size from which maxima are drawn}
+
+\item{cl}{Confidence level}
+
+\item{hp}{Holding period}
+}
+\value{
+Value at Risk. If cl and hp are scalars, it returns scalar VaR. If cl
+is vector and hp is a scalar, or viceversa, returns vector of VaRs. If both
+cl and hp are vectors, returns a matrix of VaRs.
+}
+\description{
+Plots the VaR of a portfolio against confidence level assuming extreme losses
+are Frechet distributed, for specified range of confidence level and a given
+holding period.
+}
+\details{
+Note that the long-right-hand tail is fitted to losses, not profits.
+}
+\examples{
+# Computes VaR assuming Frechet Distribution for given parameters
+   FrechetVaR(3.5, 2.3, 1.6, 10, .95, 30)
+}
+\author{
+Dinesh Acharya
+}
+\references{
+Dowd, K. Measuring Market Risk, Wiley, 2007.
+
+Embrechts, P., Kluppelberg, C. and Mikosch, T., Modelling Extremal Events for
+Insurance and Finance. Springer, Berlin, 1997, p. 324.
+
+Reiss, R. D. and Thomas, M. Statistical Analysis of Extreme Values from
+Insurance, Finance, Hydrology and Other Fields, Birkhaueser, Basel, 1997,
+15-18.
+}
+

Modified: pkg/Dowd/readme.txt
===================================================================
--- pkg/Dowd/readme.txt	2015-06-10 18:40:35 UTC (rev 3669)
+++ pkg/Dowd/readme.txt	2015-06-10 21:08:25 UTC (rev 3670)
@@ -1,4 +1,11 @@
+#
+# General Notes for Modification: 
 #***************************************************************
+# FrechetVaR does not use hp and the remark about return value when it is vector is vaccuous.
+#***************************************************************
+# In Normal/t QQ Plots, dowd code does not work for matrices but the code contains parts that
+# work for matrices. some vectors like pvec are not defined anywhere in his code.
+#***************************************************************
 # Some error is present in GumbelCopulaVaR and needs correction
 #***************************************************************
 # Bootstrap is functional (but HSVaR still does not accept matrix P/L
@@ -6,7 +13,6 @@
 #***************************************************************
 # Jarque-Bera Test:
 # It has to be checked Probability of null (H0) or (H1).
-# 
 #***************************************************************
 # Christofferson Backtest for Independence:
 # VaR(excess_loss<=0)=[]; Does not make sense. It is still to be checked if it is as intended.



More information about the Returnanalytics-commits mailing list