[Returnanalytics-commits] r3688 - pkg/Dowd/R
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Mon Jun 22 14:34:45 CEST 2015
Author: dacharya
Date: 2015-06-22 14:34:44 +0200 (Mon, 22 Jun 2015)
New Revision: 3688
Modified:
pkg/Dowd/R/BootstrapES.R
pkg/Dowd/R/BootstrapESConfInterval.R
pkg/Dowd/R/BootstrapESFigure.R
pkg/Dowd/R/BootstrapVaR.R
pkg/Dowd/R/BootstrapVaRConfInterval.R
pkg/Dowd/R/BootstrapVaRFigure.R
Log:
Import bootstrap added.
Modified: pkg/Dowd/R/BootstrapES.R
===================================================================
--- pkg/Dowd/R/BootstrapES.R 2015-06-22 12:09:17 UTC (rev 3687)
+++ pkg/Dowd/R/BootstrapES.R 2015-06-22 12:34:44 UTC (rev 3688)
@@ -1,59 +1,61 @@
-#' Bootstrapped ES for specified confidence level
-#'
-#' Estimates the bootstrapped ES for confidence level and holding period
-#' implied by data frequency.
-#'
-#' @param Ra Vector corresponding to profit and loss distribution
-#' @param number.resamples Number of samples to be taken in bootstrap procedure
-#' @return cl Number corresponding to Expected Shortfall confidence level
-#'
-#' @references Dowd, K. Measuring Market Risk, Wiley, 2007.
-#'
-#'
-#' @author Dinesh Acharya
-#' @examples
-#'
-#' # Estimates bootstrapped ES for given parameters
-#' a <- rnorm(100) # generate a random profit/loss vector
-#' BootstrapVaR(a, 50, 0.95)
-#'
-#' @export
-BootstrapES <- function(Ra, number.resamples, cl){
-
- if (nargs() < 3){
- error("Too few arguments")
- }
- if (nargs() > 3){
- error("Too many arguments")
- }
-
- profit.loss.data <- as.vector(Ra)
- # Preprocess data
- unsorted.loss.data <- -profit.loss.data
- losses.data <- sort(unsorted.loss.data)
- n <- length(losses.data)
-
- # Check that inputs have correct dimensions
- if (length(cl) != 1) {
- error("Confidence level must be a scalar")
- }
- if (length(number.resamples) != 1){
- error("Number of resamples must be a scalar");
- }
-
- # Check that inputs obey sign and value restrictions
- if (cl >= 1){
- stop("Confidence level must be less that 1")
- }
- if (cl <= 0){
- stop("Confidence level must be at least 0")
- }
- if (number.resamples <= 0){
- stop("Number of resamples must be at least 0")
- }
-
- # ES estimation
- es <- bootstrap(losses.data, number.resamples, HSES, cl)$thetastar
- y <- mean(es)
- return (y)
+#' Bootstrapped ES for specified confidence level
+#'
+#' Estimates the bootstrapped ES for confidence level and holding period
+#' implied by data frequency.
+#'
+#' @param Ra Vector corresponding to profit and loss distribution
+#' @param number.resamples Number of samples to be taken in bootstrap procedure
+#' @return cl Number corresponding to Expected Shortfall confidence level
+#'
+#' @references Dowd, K. Measuring Market Risk, Wiley, 2007.
+#'
+#'
+#' @author Dinesh Acharya
+#' @examples
+#'
+#' # Estimates bootstrapped ES for given parameters
+#' a <- rnorm(100) # generate a random profit/loss vector
+#' BootstrapVaR(a, 50, 0.95)
+#'
+#' @import bootstrap
+#'
+#' @export
+BootstrapES <- function(Ra, number.resamples, cl){
+
+ if (nargs() < 3){
+ error("Too few arguments")
+ }
+ if (nargs() > 3){
+ error("Too many arguments")
+ }
+
+ profit.loss.data <- as.vector(Ra)
+ # Preprocess data
+ unsorted.loss.data <- -profit.loss.data
+ losses.data <- sort(unsorted.loss.data)
+ n <- length(losses.data)
+
+ # Check that inputs have correct dimensions
+ if (length(cl) != 1) {
+ error("Confidence level must be a scalar")
+ }
+ if (length(number.resamples) != 1){
+ error("Number of resamples must be a scalar");
+ }
+
+ # Check that inputs obey sign and value restrictions
+ if (cl >= 1){
+ stop("Confidence level must be less that 1")
+ }
+ if (cl <= 0){
+ stop("Confidence level must be at least 0")
+ }
+ if (number.resamples <= 0){
+ stop("Number of resamples must be at least 0")
+ }
+
+ # ES estimation
+ es <- bootstrap(losses.data, number.resamples, HSES, cl)$thetastar
+ y <- mean(es)
+ return (y)
}
\ No newline at end of file
Modified: pkg/Dowd/R/BootstrapESConfInterval.R
===================================================================
--- pkg/Dowd/R/BootstrapESConfInterval.R 2015-06-22 12:09:17 UTC (rev 3687)
+++ pkg/Dowd/R/BootstrapESConfInterval.R 2015-06-22 12:34:44 UTC (rev 3688)
@@ -1,64 +1,66 @@
-#' Bootstrapped ES Confidence Interval
-#'
-#' Estimates the 90% confidence interval for bootstrapped ES, for confidence
-#' level and holding period implied by data frequency.
-#'
-#' @param Ra Vector corresponding to profit and loss distribution
-#' @param number.resample Number of samples to be taken in bootstrap procedure
-#' @param cl Number corresponding to Expected Shortfall confidence level
-#' @return 90% Confidence interval for bootstrapped ES
-#'
-#' @references Dowd, K. Measuring Market Risk, Wiley, 2007.
-#'
-#'
-#' @author Dinesh Acharya
-#' @examples
-#'
-#' # To be modified with appropriate data.
-#' # Estimates 90% confidence interval for bootstrapped ES for 95%
-#' # confidence interval
-#' Ra <- rnorm(1000)
-#' BootstrapESConfInterval(Ra, 50, 0.95)
-#'
-#' @export
-BootstrapESConfInterval <- function(Ra, number.resamples, cl){
-
- # Determine if there are three arguments
- if (nargs() < 3){
- stop("Too few arguments")
- }
- if (nargs() > 3){
- stop("Too many arguments")
- }
-
- profit.loss.data <- as.vector(Ra)
-
- # Preprocess data
- unsorted.loss.data <- -profit.loss.data # Derives L/P data from input P/L data
- losses.data <- sort(unsorted.loss.data) # Puts losses in ascending order
- n <- length(losses.data)
-
- # Check that inputs have correct dimensions
- if (is.vector(cl) & (length(cl) != 1) ) {
- error("Confidence level must be a scalar")
- }
- if (length(number.resamples) != 1) {
- error("Number of resamples must be a scalar")
- }
- # Check that inputs obey sign and value restrictions
- if (cl >= 1){
- stop("Confidence level must be less that 1")
- }
- if (cl <= 0){
- stop("Confidence level must be at least 0")
- }
- if (number.resamples <= 0){
- stop("Number of resamples must be at least 0")
- }
-
- # ES estimation
- es <- bootstrap(losses.data, number.resamples, HSES, cl)[1]
- y <- quantile(es, c(.05, .95))
- return(y)
-
+#' Bootstrapped ES Confidence Interval
+#'
+#' Estimates the 90% confidence interval for bootstrapped ES, for confidence
+#' level and holding period implied by data frequency.
+#'
+#' @param Ra Vector corresponding to profit and loss distribution
+#' @param number.resample Number of samples to be taken in bootstrap procedure
+#' @param cl Number corresponding to Expected Shortfall confidence level
+#' @return 90% Confidence interval for bootstrapped ES
+#'
+#' @references Dowd, K. Measuring Market Risk, Wiley, 2007.
+#'
+#'
+#' @author Dinesh Acharya
+#' @examples
+#'
+#' # To be modified with appropriate data.
+#' # Estimates 90% confidence interval for bootstrapped ES for 95%
+#' # confidence interval
+#' Ra <- rnorm(1000)
+#' BootstrapESConfInterval(Ra, 50, 0.95)
+#'
+#' @import bootstrap
+#'
+#' @export
+BootstrapESConfInterval <- function(Ra, number.resamples, cl){
+
+ # Determine if there are three arguments
+ if (nargs() < 3){
+ stop("Too few arguments")
+ }
+ if (nargs() > 3){
+ stop("Too many arguments")
+ }
+
+ profit.loss.data <- as.vector(Ra)
+
+ # Preprocess data
+ unsorted.loss.data <- -profit.loss.data # Derives L/P data from input P/L data
+ losses.data <- sort(unsorted.loss.data) # Puts losses in ascending order
+ n <- length(losses.data)
+
+ # Check that inputs have correct dimensions
+ if (is.vector(cl) & (length(cl) != 1) ) {
+ error("Confidence level must be a scalar")
+ }
+ if (length(number.resamples) != 1) {
+ error("Number of resamples must be a scalar")
+ }
+ # Check that inputs obey sign and value restrictions
+ if (cl >= 1){
+ stop("Confidence level must be less that 1")
+ }
+ if (cl <= 0){
+ stop("Confidence level must be at least 0")
+ }
+ if (number.resamples <= 0){
+ stop("Number of resamples must be at least 0")
+ }
+
+ # ES estimation
+ es <- bootstrap(losses.data, number.resamples, HSES, cl)[1]
+ y <- quantile(es, c(.05, .95))
+ return(y)
+
}
\ No newline at end of file
Modified: pkg/Dowd/R/BootstrapESFigure.R
===================================================================
--- pkg/Dowd/R/BootstrapESFigure.R 2015-06-22 12:09:17 UTC (rev 3687)
+++ pkg/Dowd/R/BootstrapESFigure.R 2015-06-22 12:34:44 UTC (rev 3688)
@@ -1,70 +1,72 @@
-#' Plots figure of bootstrapped ES
-#'
-#' Plots figure for the bootstrapped ES, for confidence
-#' level and holding period implied by data frequency.
-#'
-#' @param Ra Vector corresponding to profit and loss distribution
-#' @param number.resample Number of samples to be taken in bootstrap procedure
-#' @param cl Number corresponding to Expected Shortfall confidence level
-#'
-#' @references Dowd, K. Measuring Market Risk, Wiley, 2007.
-#'
-#'
-#' @author Dinesh Acharya
-#' @examples
-#'
-#' # To be modified with appropriate data.
-#' # Estimates 90% confidence interval for bootstrapped ES for 95%
-#' # confidence interval
-#' Ra <- rnorm(1000)
-#' BootstrapESFigure(Ra, 500, 0.95)
-#'
-#' @export
-BootstrapESFigure <- function(Ra, number.resamples, cl){
-
- # Determine if there are three arguments
- if (nargs() < 3){
- stop("Too few arguments")
- }
- if (nargs() > 3){
- stop("Too many arguments")
- }
-
- profit.loss.data <- as.vector(Ra)
-
- # Preprocess data
- unsorted.loss.data <- -profit.loss.data # Derives L/P data from input P/L data
- losses.data <- sort(unsorted.loss.data) # Puts losses in ascending order
- n <- length(losses.data)
-
- # Check that inputs have correct dimensions
- if (is.vector(cl) & (length(cl) != 1) ) {
- error("Confidence level must be a scalar")
- }
- if (length(number.resamples) != 1) {
- error("Number of resamples must be a scalar")
- }
- # Check that inputs obey sign and value restrictions
- if (cl >= 1){
- stop("Confidence level must be less that 1")
- }
- if (cl <= 0){
- stop("Confidence level must be at least 0")
- }
- if (number.resamples <= 0){
- stop("Number of resamples must be at least 0")
- }
-
- # ES Estimation
- es <- bootstrap(losses.data, number.resamples, HSES, cl)$thetastar
- mean.es <- mean(es)
- std.es <- sd(es)
- min.es <- min(es)
- max.es <- max(es)
- ninety.five.perc.conf.interval <- quantile(es, c(.05, .95))
-
- # Histogram
- cl.for.label <- 100*cl
- hist(es, 30, xlab="ES", ylab="Frequency", main=paste("Bootstrapped Historical Simulation ES at", cl, "% Confidence Level"))
-
+#' Plots figure of bootstrapped ES
+#'
+#' Plots figure for the bootstrapped ES, for confidence
+#' level and holding period implied by data frequency.
+#'
+#' @param Ra Vector corresponding to profit and loss distribution
+#' @param number.resample Number of samples to be taken in bootstrap procedure
+#' @param cl Number corresponding to Expected Shortfall confidence level
+#'
+#' @references Dowd, K. Measuring Market Risk, Wiley, 2007.
+#'
+#'
+#' @author Dinesh Acharya
+#' @examples
+#'
+#' # To be modified with appropriate data.
+#' # Estimates 90% confidence interval for bootstrapped ES for 95%
+#' # confidence interval
+#' Ra <- rnorm(1000)
+#' BootstrapESFigure(Ra, 500, 0.95)
+#'
+#' @import bootstrap
+#'
+#' @export
+BootstrapESFigure <- function(Ra, number.resamples, cl){
+
+ # Determine if there are three arguments
+ if (nargs() < 3){
+ stop("Too few arguments")
+ }
+ if (nargs() > 3){
+ stop("Too many arguments")
+ }
+
+ profit.loss.data <- as.vector(Ra)
+
+ # Preprocess data
+ unsorted.loss.data <- -profit.loss.data # Derives L/P data from input P/L data
+ losses.data <- sort(unsorted.loss.data) # Puts losses in ascending order
+ n <- length(losses.data)
+
+ # Check that inputs have correct dimensions
+ if (is.vector(cl) & (length(cl) != 1) ) {
+ error("Confidence level must be a scalar")
+ }
+ if (length(number.resamples) != 1) {
+ error("Number of resamples must be a scalar")
+ }
+ # Check that inputs obey sign and value restrictions
+ if (cl >= 1){
+ stop("Confidence level must be less that 1")
+ }
+ if (cl <= 0){
+ stop("Confidence level must be at least 0")
+ }
+ if (number.resamples <= 0){
+ stop("Number of resamples must be at least 0")
+ }
+
+ # ES Estimation
+ es <- bootstrap(losses.data, number.resamples, HSES, cl)$thetastar
+ mean.es <- mean(es)
+ std.es <- sd(es)
+ min.es <- min(es)
+ max.es <- max(es)
+ ninety.five.perc.conf.interval <- quantile(es, c(.05, .95))
+
+ # Histogram
+ cl.for.label <- 100*cl
+ hist(es, 30, xlab="ES", ylab="Frequency", main=paste("Bootstrapped Historical Simulation ES at", cl, "% Confidence Level"))
+
}
\ No newline at end of file
Modified: pkg/Dowd/R/BootstrapVaR.R
===================================================================
--- pkg/Dowd/R/BootstrapVaR.R 2015-06-22 12:09:17 UTC (rev 3687)
+++ pkg/Dowd/R/BootstrapVaR.R 2015-06-22 12:34:44 UTC (rev 3688)
@@ -17,6 +17,8 @@
#' a <- rnorm(100) # generate a random profit/loss vector
#' BootstrapES(a, 50, 0.95)
#'
+#' @import bootstrap
+#'
#' @export
BootstrapVaR <- function(Ra, number.resamples, cl){
Modified: pkg/Dowd/R/BootstrapVaRConfInterval.R
===================================================================
--- pkg/Dowd/R/BootstrapVaRConfInterval.R 2015-06-22 12:09:17 UTC (rev 3687)
+++ pkg/Dowd/R/BootstrapVaRConfInterval.R 2015-06-22 12:34:44 UTC (rev 3688)
@@ -20,6 +20,8 @@
#' Ra <- rnorm(1000)
#' BootstrapVaRConfInterval(Ra, 500, 0.95)
#'
+#' @import bootstrap
+#'
#' @export
BootstrapVaRConfInterval <- function(Ra, number.resamples, cl){
Modified: pkg/Dowd/R/BootstrapVaRFigure.R
===================================================================
--- pkg/Dowd/R/BootstrapVaRFigure.R 2015-06-22 12:09:17 UTC (rev 3687)
+++ pkg/Dowd/R/BootstrapVaRFigure.R 2015-06-22 12:34:44 UTC (rev 3688)
@@ -1,70 +1,71 @@
-#' Plots figure of bootstrapped VaR
-#'
-#' Plots figure for the bootstrapped VaR, for confidence
-#' level and holding period implied by data frequency.
-#'
-#' @param Ra Vector corresponding to profit and loss distribution
-#' @param number.sample Number of samples to be taken in bootstrap procedure
-#' @param cl Number corresponding to Value at Risk confidence level
-#'
-#' @references Dowd, K. Measuring Market Risk, Wiley, 2007.
-#'
-#'
-#' @author Dinesh Acharya
-#' @examples
-#'
-#' # To be modified with appropriate data.
-#' # Estimates 90% confidence interval for bootstrapped VaR for 95%
-#' # confidence interval
-#' Ra <- rnorm(1000)
-#' BootstrapESFigure(Ra, 500, 0.95)
-#'
-#' @export
-BootstrapVaRFigure <- function(Ra, number.resamples, cl){
-
- # Determine if there are three arguments
- if (nargs() < 3){
- stop("Too few arguments")
- }
- if (nargs() > 3){
- stop("Too many arguments")
- }
-
- profit.loss.data <- as.vector(Ra)
-
- # Preprocess data
- unsorted.loss.data <- -profit.loss.data # Derives L/P data from input P/L data
- losses.data <- sort(unsorted.loss.data) # Puts losses in ascending order
- n <- length(losses.data)
-
- # Check that inputs have correct dimensions
- if (is.vector(cl) & (length(cl) != 1) ) {
- error("Confidence level must be a scalar")
- }
- if (length(number.resamples) != 1) {
- error("Number of resamples must be a scalar")
- }
- # Check that inputs obey sign and value restrictions
- if (cl >= 1){
- stop("Confidence level must be less that 1")
- }
- if (cl <= 0){
- stop("Confidence level must be at least 0")
- }
- if (number.resamples <= 0){
- stop("Number of resamples must be at least 0")
- }
-
- # ES Estimation
- VaR <- bootstrap(losses.data, number.resamples, HSVaR, cl)$thetastar
- mean.VaR <- mean(VaR)
- std.VaR <- sd(VaR)
- min.VaR <- min(VaR)
- max.VaR <- max(VaR)
- ninety.five.perc.conf.interval <- quantile(VaR, c(.05, .95))
-
- # Histogram
- cl.for.label <- 100*cl
- hist(VaR, 30, xlab="VaR", ylab="Frequency", main=paste("Bootstrapped Historical Simulation VaR at", cl, "% Confidence Level"))
-
+#' Plots figure of bootstrapped VaR
+#'
+#' Plots figure for the bootstrapped VaR, for confidence
+#' level and holding period implied by data frequency.
+#'
+#' @param Ra Vector corresponding to profit and loss distribution
+#' @param number.sample Number of samples to be taken in bootstrap procedure
+#' @param cl Number corresponding to Value at Risk confidence level
+#'
+#' @references Dowd, K. Measuring Market Risk, Wiley, 2007.
+#'
+#' @author Dinesh Acharya
+#' @examples
+#'
+#' # To be modified with appropriate data.
+#' # Estimates 90% confidence interval for bootstrapped VaR for 95%
+#' # confidence interval
+#' Ra <- rnorm(1000)
+#' BootstrapESFigure(Ra, 500, 0.95)
+#'
+#' @import bootstrap
+#'
+#' @export
+BootstrapVaRFigure <- function(Ra, number.resamples, cl){
+
+ # Determine if there are three arguments
+ if (nargs() < 3){
+ stop("Too few arguments")
+ }
+ if (nargs() > 3){
+ stop("Too many arguments")
+ }
+
+ profit.loss.data <- as.vector(Ra)
+
+ # Preprocess data
+ unsorted.loss.data <- -profit.loss.data # Derives L/P data from input P/L data
+ losses.data <- sort(unsorted.loss.data) # Puts losses in ascending order
+ n <- length(losses.data)
+
+ # Check that inputs have correct dimensions
+ if (is.vector(cl) & (length(cl) != 1) ) {
+ error("Confidence level must be a scalar")
+ }
+ if (length(number.resamples) != 1) {
+ error("Number of resamples must be a scalar")
+ }
+ # Check that inputs obey sign and value restrictions
+ if (cl >= 1){
+ stop("Confidence level must be less that 1")
+ }
+ if (cl <= 0){
+ stop("Confidence level must be at least 0")
+ }
+ if (number.resamples <= 0){
+ stop("Number of resamples must be at least 0")
+ }
+
+ # ES Estimation
+ VaR <- bootstrap(losses.data, number.resamples, HSVaR, cl)$thetastar
+ mean.VaR <- mean(VaR)
+ std.VaR <- sd(VaR)
+ min.VaR <- min(VaR)
+ max.VaR <- max(VaR)
+ ninety.five.perc.conf.interval <- quantile(VaR, c(.05, .95))
+
+ # Histogram
+ cl.for.label <- 100*cl
+ hist(VaR, 30, xlab="VaR", ylab="Frequency", main=paste("Bootstrapped Historical Simulation VaR at", cl, "% Confidence Level"))
+
}
\ No newline at end of file
More information about the Returnanalytics-commits
mailing list