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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Jun 30 02:31:18 CEST 2015


Author: dacharya
Date: 2015-06-30 02:31:18 +0200 (Tue, 30 Jun 2015)
New Revision: 3757

Added:
   pkg/Dowd/R/KernelVaREpanechinikovKernel.R
   pkg/Dowd/man/KernelVaREpanechinikovKernel.Rd
Log:
KernelVaREpanechinikovKernel added.

Added: pkg/Dowd/R/KernelVaREpanechinikovKernel.R
===================================================================
--- pkg/Dowd/R/KernelVaREpanechinikovKernel.R	                        (rev 0)
+++ pkg/Dowd/R/KernelVaREpanechinikovKernel.R	2015-06-30 00:31:18 UTC (rev 3757)
@@ -0,0 +1,50 @@
+#' Calculates VaR using epanechinikov 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 epanechinikov kernel approach
+#'    Ra <- rnorm(30)
+#'    KernelVaREpanechinikovKernel(Ra, .95)
+#'
+#' @export
+KernelVaREpanechinikovKernel <- function(Ra, cl) {
+  PandL <- as.vector(Ra)
+  mu <- mean(PandL)
+  sigma <- sd(PandL)
+  
+  # Obtain pdf values
+  kernel.data <- density(PandL, kernel = "epanechinikov", from = mu - 4 * sigma, to = mu + 4 * sigma, n = 1000, bw = "nrd")
+  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/KernelVaREpanechinikovKernel.Rd
===================================================================
--- pkg/Dowd/man/KernelVaREpanechinikovKernel.Rd	                        (rev 0)
+++ pkg/Dowd/man/KernelVaREpanechinikovKernel.Rd	2015-06-30 00:31:18 UTC (rev 3757)
@@ -0,0 +1,31 @@
+% Generated by roxygen2 (4.1.1): do not edit by hand
+% Please edit documentation in R/KernelVaREpanechinikovKernel.R
+\name{KernelVaREpanechinikovKernel}
+\alias{KernelVaREpanechinikovKernel}
+\title{Calculates VaR using epanechinikov kernel approach}
+\usage{
+KernelVaREpanechinikovKernel(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 epanechinikov kernel approach
+   Ra <- rnorm(30)
+   KernelVaREpanechinikovKernel(Ra, .95)
+}
+\author{
+Dinesh Acharya
+}
+\references{
+Dowd, K. Measuring Market Risk, Wiley, 2007.
+}
+



More information about the Returnanalytics-commits mailing list