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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Jun 30 01:48:13 CEST 2015


Author: dacharya
Date: 2015-06-30 01:48:12 +0200 (Tue, 30 Jun 2015)
New Revision: 3756

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

Modified: pkg/Dowd/NAMESPACE
===================================================================
--- pkg/Dowd/NAMESPACE	2015-06-29 12:38:33 UTC (rev 3755)
+++ pkg/Dowd/NAMESPACE	2015-06-29 23:48:12 UTC (rev 3756)
@@ -49,6 +49,7 @@
 export(InsuranceVaR)
 export(JarqueBeraBacktest)
 export(KSTestStat)
+export(KernelVaRTriangleKernel)
 export(KuiperTestStat)
 export(LopezBacktest)
 export(MEFPlot)

Added: pkg/Dowd/R/KernelVaRTriangleKernel.R
===================================================================
--- pkg/Dowd/R/KernelVaRTriangleKernel.R	                        (rev 0)
+++ pkg/Dowd/R/KernelVaRTriangleKernel.R	2015-06-29 23:48:12 UTC (rev 3756)
@@ -0,0 +1,50 @@
+#' Calculates VaR using triangle kernel approach
+#' 
+#' The output consists of a scalar VaR for specified confidence level.
+#' 
+#' @param Ra Profit and Loss data set
+#' @param cl VaR confidence level
+#' @return Scalar VaR
+#' @references Dowd, K. Measuring Market Risk, Wiley, 2007.
+#'
+#' @author Dinesh Acharya
+#' @examples
+#' 
+#'    # VaR for specified confidence level using triangle kernel approach
+#'    Ra <- rnorm(30)
+#'    KernelVaRTriangleKernel(Ra, .95)
+#'
+#' @export
+KernelVaRTriangleKernel <- function(Ra, cl) {
+  PandL <- as.vector(Ra)
+  mu <- mean(PandL)
+  sigma <- sd(PandL)
+  
+  # Obtain pdf values
+  kernel.data <- density(PandL, kernel = "triangular", from = mu - 4 * sigma, to = mu + 4 * sigma, n = 1000)
+  kernel.pdf <- kernel.data$y
+  x.values <- kernel.data$x
+  delta.x <- x.values[2]-x.values[1]
+  n <- 1000 # = length(x.values)
+  
+  # Obtain cdf values
+  cdf <- double(n)
+  cdf[1] <- kernel.pdf[1] * delta.x
+  for (i in 2:n) {
+    cdf[i] <- kernel.pdf[i] * delta.x + cdf[i - 1]
+  }
+  plot(x.values, kernel.pdf, type="l", main = "Constructed Pdf")
+  
+  # Derivation of required percentile
+  cdf.indices.less.than.prob <- which(cdf<cl)
+  # Gives vector of indices for all cdf-values less than probability
+  max.cdf.index.less.than.prob <- length(cdf.indices.less.than.prob)
+  # Gives index of cdf-value just less than probability
+  lower.x.bound <- x.values[max.cdf.index.less.than.prob]
+  # Gives x-value just below specified probability
+  upper.x.bound <- x.values[max.cdf.index.less.than.prob + 1]
+  # Gives x-value just above specified probability
+  VaR <- (lower.x.bound + upper.x.bound) / 2 # Desired percentile, ie, answer.
+  return(VaR)
+  
+}
\ No newline at end of file

Added: pkg/Dowd/man/KernelVaRTriangleKernel.Rd
===================================================================
--- pkg/Dowd/man/KernelVaRTriangleKernel.Rd	                        (rev 0)
+++ pkg/Dowd/man/KernelVaRTriangleKernel.Rd	2015-06-29 23:48:12 UTC (rev 3756)
@@ -0,0 +1,31 @@
+% Generated by roxygen2 (4.1.1): do not edit by hand
+% Please edit documentation in R/KernelVaRTriangleKernel.R
+\name{KernelVaRTriangleKernel}
+\alias{KernelVaRTriangleKernel}
+\title{Calculates VaR using triangle kernel approach}
+\usage{
+KernelVaRTriangleKernel(Ra, cl)
+}
+\arguments{
+\item{Ra}{Profit and Loss data set}
+
+\item{cl}{VaR confidence level}
+}
+\value{
+Scalar VaR
+}
+\description{
+The output consists of a scalar VaR for specified confidence level.
+}
+\examples{
+# VaR for specified confidence level using triangle kernel approach
+   Ra <- rnorm(30)
+   KernelVaRTriangleKernel(Ra, .95)
+}
+\author{
+Dinesh Acharya
+}
+\references{
+Dowd, K. Measuring Market Risk, Wiley, 2007.
+}
+



More information about the Returnanalytics-commits mailing list