Econ 424/Amath 462 Eric Zivot Summer 2013 Summer 2013 Updated: J uly 23, 2013 Eric Zivot 2006 Data for Examples returns.z 0 . 0 0 . 2 s b u x - 0 . 4 - 0 . 2 s 0 . 2 - 0 . 2 0 . 0 0 m s f t - 0 . 4 0 . 0 0 0 . 0 5 0 . 1 0 - 0 . 1 5 - 0 . 0 5 0 s p 5 0 0 Eric Zivot 2006 T = 100 months 1993 1994 1995 1996 1997 1998 1999 2000 Index Estimated Standard Errors > se.muhat = sigmahat.vals/sqrt(nobs) > rbind(muhat.vals,se.muhat) sbux msft sp500 p muhat.vals 0.0277 0.0275 0.01253 se.muhat 0.0135 0.0106 0.00378 i 2h i 2h l / ( b /2) > se.sigma2hat = sigma2hat.vals/sqrt(nobs/2) > rbind(sigma2hat.vals,se.sigma2hat) sbux msft sp500 sigma2hat.vals 0.01845 0.01141 0.00143 se.sigma2hat 0.00261 0.00161 0.00020 > se.sigmahat = sigmahat.vals/sqrt(2*nobs) g g / q ( ) > rbind(sigmahat.vals,se.sigmahat) sbux msft sp500 sigmahat.vals 0.1358 0.1068 0.0378 Eric Zivot 2006 g se.sigmahat 0.0096 0.0075 0.0026 R function sample() p () # random permutations of the index vector 1:5 > sample(5) [1] 1 3 2 5 4 > sample(5) [1] 4 2 3 5 1 # random sample of size 5 from MSFT return with replacement > sample(MSFT, 5, replace=TRUE) [1] -0.02904 0.12130 -0.01890 -0.15332 -0.14627 Eric Zivot 2006 Brute Force Bootstrap Same idea as Monte Carlo Simulation but instead of generating randomdata froman assumed distribution generating random data from an assumed distribution, you generate random data by sampling with replacement from the observed data # bootstrap distribution for > B = 999 # why use 999?
> muhat.boot = rep(0, B)
> nobs = length(MSFT) > for (i in 1:B) { > for (i in 1:B) { + boot.data = sample(MSFT, nobs, replace=TRUE) + muhat.boot[i] = mean(boot.data) } Eric Zivot 2006 } Brute Force Bootstrap # bootstrap bias > mean(muhat.boot) - muhat.MSFT [1] -0.0005643 # bootstrap SE Bootstrap SE is very close t l ti SE # p > sd(muhat.boot) [1] 0.01045 to analytic SE # analytic SE > sigmahat.MSFT/sqrt(length(MSFT)) [1] 0.01068 Eric Zivot 2006 Brute Force Bootstrap Brute Force Bootstrap Histogram of muhat.boot 2 0 0 0 5 Normal Q-Q Plot F r e q u e n c y 1 0 0 1 5 0 0 . 0 3 0 . 0 4 0 . 0 5 m p l e
Q u a n t i l e s F r e 0 5 0 0 . 0 0 0 . 0 1 0 . 0 2 S a m p l muhat.boot -0.01 0.01 0.03 0.05 0 -3 -2 -1 0 1 2 3 Theoretical Quantiles par(mfrow=c(1,2)) pa ( o c( , )) hist(muhat.boot, col="slateblue1") abline(v=muhat.MSFT, col="white", lwd=2) qqnorm(muhat.boot) li ( h t b t) Eric Zivot 2006 qqline(muhat.boot) par(mfrow=c(1,1)) R Package boot R Package boot Implements a variety of bootstrapping functions Background material is book by Davidson and Hinkley, Bootstrap Methods and Their A li ti C b id U i it P 1997 Application, Cambridge University Press, 1997. Main functions are: b t() b t t li d f ti boot() bootstrap user supplied function boot.ci() compute bootstrap confidence i t l interval Eric Zivot 2006 Example: Bootstrapping sample mean Example: Bootstrapping sample mean # function for bootstrapping sample mean mean.boot = function(x, idx) { mean.boot function(x, idx) { # arguments: # x data to be resampled # id ector of scrambled indices created # idx vector of scrambled indices created # by boot() function # value: # ans mean value computed using resampled # data ans = mean(x[idx]) ans mean(x[idx]) ans } Eric Zivot 2006 Example: Bootstrapping sample mean > MSFT.mean.boot = boot(MSFT, statistic = mean.boot, R=999) > class(MSFT.mean.boot) [1] "b t [1] "boot > MSFT.mean.boot Number of bootstrap samples ORDINARY NONPARAMETRIC BOOTSTRAP Call: Call: boot(data = MSFT, statistic = mean.boot, R = 999) Bootstrap Statistics : original bias std. error t1* 0.02756 -0.00013 0.01052 Eric Zivot 2006 Sample mean Bootstrap estimate of bias Bootstrap estimate of SE Histogram of t 0 4 0 0 . 0 5 0 . 0 6 s i t y 0 3 0 0 . 0 3 0 . 0 4 D e n s 1 0 2 0 0 . 0 1 0 . 0 2 t * 0 1 0 . 0 1 0 . 0 0 0 t* 0.00 0.04 -3 -1 1 2 3 - 0 Quantiles of Standard Normal Eric Zivot 2006 > plot(MSFT.mean.boot) Compare Bootstrap Statistics with Analytic Formulas ORDINARY NONPARAMETRIC BOOTSTRAP Call: boot(data = MSFT, statistic = mean.boot, R = 999) Bootstrap Statistics : original bias std. error t1* 0.02756 -0.00013 0.01052 t1 0.02756 0.00013 0.01052 # compare boot SE with analytic SE > se.muhat.MSFT = sigmahat.MSFT/sqrt(length(MSFT)) h t S > se.muhat.MSFT [1] 0.01068 Eric Zivot 2006 Bootstrap Confidence Intervals Bootstrap Confidence Intervals > boot ci(MSFT mean boot conf = 0 95 type = > boot.ci(MSFT.mean.boot, conf = 0.95, type = + c("norm","perc")) BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS Based on 999 bootstrap replicates CALL : boot.ci(boot.out = MSFT.mean.boot, conf = 0.95, type = c("norm", "perc")) c( norm , perc )) Intervals : Level Normal Percentile 95% ( 0 0071 0 0483 ) ( 0 0065 0 0471 ) 95% ( 0.0071, 0.0483 ) ( 0.0065, 0.0471 ) Calculations and Intervals on Original Scale Eric Zivot 2006 Example: Bootstrapping Sample SD Example: Bootstrapping Sample SD # function for bootstrapping sample standard deviation sd.boot = function(x, idx) { # arguments: # x data to be resampled # idx vector of scrambled indices created by # b t() f ti # boot() function # value: # ans sd value computed using resampled data ans = sd(x[idx]) ans = sd(x[idx]) ans } Eric Zivot 2006 Example: Bootstrapping Sample SD Example: Bootstrapping Sample SD > MSFT.sd.boot = boot(MSFT, statistic = sd.boot, R=999) > MSFT.sd.boot ORDINARY NONPARAMETRIC BOOTSTRAP Call: boot(data = MSFT, statistic = sd.boot, R = 999) Bootstrap Statistics : original bias std. error t1* 0.1068 -0.00145 0.01078 # compare boot SE with analytic SE based on CLT > se.sigmahat.MSFT = sigmahat.MSFT/sqrt(2*length(MSFT)) > se.sigmahat.MSFT Eric Zivot 2006 g [1] 0.00755 Histogram of t 4 0 0 . 1 3 0 . 1 4 t y 3 0 1 1 0 . 1 2 D e n s i t 2 0 9 0 . 1 0 0 . t * 0 1 0 0 . 0 8 0 . 0 9 t* 0.08 0.12 0 -3 -1 1 2 3 Quantiles of StandardNormal Eric Zivot 2006 > plot(MSFT.sd.boot) t Quantiles of Standard Normal Example: Boostrapping Normal VaR Example: Boostrapping Normal VaR ValueAtRisk.boot = function(x, idx, p=0.05, w=100000) { # t d t t b l d # x.mat data to be resampled # idx vector of scrambled indices created by # boot() function # p probability value for VaR calculation # p probability value for VaR calculation # w value of initial investment # value: # ans Value-at-Risk computed using resampled data # ans Value at Risk computed using resampled data q = mean(x[idx]) + sd(x[idx])*qnorm(p) VaR = (exp(q) - 1)*w VaR } Eric Zivot 2006 Example: Boostrapping Normal VaR Example: Boostrapping Normal VaR > MSFT.VaR.boot ORDINARY NONPARAMETRIC BOOTSTRAP C ll Call: boot(data = MSFT, statistic = ValueAtRisk.boot, R = 999) Bootstrap Statistics : original bias std. error t1* -13769.40 210.2801 1886.953 Sample VaR estimate Bootstrap SE Eric Zivot 2006 p Histogram of t 0 2 0 t y 0 . 0 0 0 - 1 2 0 0 0 D e n s i t 0 . 0 0 0 1 0 - 1 6 0 0 0 t * 0 0 0 0 0 0 - t* -20000 -14000 -8000 0 . 0 0 0 -3 -1 1 2 3 - 2 0 0 0 Quantiles of StandardNormal Eric Zivot 2006 > plot(MSFT.VaR.boot) t Quantiles of Standard Normal Example: Boostrapping Normal VaR Example: Boostrapping Normal VaR > boot.ci(MSFT.VaR.boot, conf=0.95, type=c("norm", "perc")) BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS Based on 999 bootstrap replicates CALL CALL : boot.ci(boot.out = MSFT.VaR.boot, conf = 0.95, type = c("norm", "perc")) Intervals : Level Normal Percentile 95% (-17678, -10281 ) (-17212, -10009 )
2 ( ) boot SE * * .025 .975 [ , ] q q Eric Zivot 2006
Applied Statistics: From Bivariate Through Multivariate Techniques Second Edition – Ebook PDF Version - Quickly download the ebook to explore the full content
Applied Statistics: From Bivariate Through Multivariate Techniques Second Edition – Ebook PDF Version - Quickly download the ebook to explore the full content