Skip to content

Commit

Permalink
Added BOOTcor option to ES.SE(). The standard error of ES can now be …
Browse files Browse the repository at this point in the history
…computed using all four options.
  • Loading branch information
chenx26 committed Jul 13, 2016
1 parent 394432b commit 0d38a5d
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 3 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export(EstimatorSE)
export(RES)
export(SD.IF)
export(SE.ES)
export(SE.ES.boot.cor)
export(SE.ES.boot.cor.xts)
export(SE.ES.boot.iid)
export(SE.ES.boot.iid.xts)
export(SE.ES.cor)
Expand Down
8 changes: 8 additions & 0 deletions R/ES_SE.R
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@
#' ES.SE(edhec, p=.95, method="historical",se.method = "IFcor")
#' h2o.shutdown(prompt=FALSE)
#'
#' # Now using iid bootstrapping
#' ES.SE(edhec, p=.95, method="historical",se.method = "BOOTiid")
#'
#'
#' # and with tsboot
#'
#' ES.SE(edhec, p=.95, method="historical",se.method = "BOOTcor")
#'
#' # now use Gaussian
#' ES.SE(edhec, p=.95, method="gaussian")
#'
Expand Down
64 changes: 62 additions & 2 deletions R/SE_ES.R
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ SE.ES.cor = function(x, d = 5, alpha.lasso = 0.5, keep = 1, alpha = 0.05){
#'
#' @examples
#' data(edhec)
#' SE.ES.iid.xts(edhec)
#' SE.ES.boot.iid.xts(edhec)
SE.ES.boot.iid.xts = function(x,...,alpha=0.05){
if (is.vector(x) || is.null(ncol(x)) || ncol(x) == 1) {
x <- as.numeric(x)
Expand Down Expand Up @@ -172,11 +172,71 @@ SE.ES.boot.iid = function(data, ...,alpha=0.05){
# each standard error is computed from boot.sim ESs
for(sim.iter in 1:boot.sim){
x=sample(data,N,replace = TRUE)
res.sd[sim.iter]=ES(x,alpha)
res.sd[sim.iter]=ES.hist(x,alpha)
}
res[run.iter]=sd(res.sd)
}
# return the ES of the boot.run standard errors
return(mean(res))
}

#' Wrapper function to compute the standard error(s) of the ES for an xts object
#' using time series bootstrapping
#'
#' The operation is performed column wise.
#'
#' @param x the xts object
#' @param alpha tail probability
#' @param ... other parameters
#'
#' @return standard error(s) of the xts object
#' @export
#'
#' @examples
#' data(edhec)
#' SE.ES.boot.cor.xts(edhec)
SE.ES.boot.cor.xts = function(x,...,alpha=0.05){
if (is.vector(x) || is.null(ncol(x)) || ncol(x) == 1) {
x <- as.numeric(x)
# if(na.rm) x <- na.omit(x)
return(SE.ES.boot.cor(x,...,alpha=alpha))
}
else {
x <- coredata(x)
# if(na.rm) x <- na.omit(x)
return(apply(x, 2, SE.ES.boot.cor,...=...,alpha=alpha))
}
}

#' Compute standard error of ES via bootstrapping for data (possibily serially correlated)
#'
#' @param data vector of data
#' @param ... parameters passed from upper calls
#' @param alpha tail probability
#' @param segment.length the length of the fixed block for bootstrapping
#'
#' @return SE of ES
#' @export
#'
#' @examples
#' SE.ES.boot.cor(rnorm(100), alpha=0.1)
SE.ES.boot.cor = function(data, ...,alpha=0.05,segment.length=length(data)/5){
args=list(...)
arg.names=names(args)
boot.sim=100
boot.run=100
N=length(data)
# check if the parameters are specified by the function call
if(("boot.sim" %in% arg.names) & (!is.null(args["boot.sim"]))) boot.sim=args["boot.sim"]
if(("boot.run" %in% arg.names) & (!is.null(args["boot.run"]))) boot.run=args["boot.run"]
res=rep(0,boot.run)
# compute boot.run standard errors
for(run.iter in 1:boot.run){
boot.res=tsboot(data, ES.hist, R = boot.sim, sim = "fixed", l = segment.length, alpha = alpha)
res[run.iter]=sd(boot.res$t)
}
# return the ES of the boot.run standard errors
return(mean(res))
}


8 changes: 8 additions & 0 deletions man/ES.SE.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions man/SE.ES.boot.cor.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions man/SE.ES.boot.cor.xts.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/SE.ES.boot.iid.xts.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0d38a5d

Please sign in to comment.