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

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


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

Added:
   pkg/Dowd/R/KernelVaRNormalKernel.R
   pkg/Dowd/man/KernelVaRNormalKernel.Rd
Log:
KernelVaRNormalKernell added.

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



More information about the Returnanalytics-commits mailing list