-
Notifications
You must be signed in to change notification settings - Fork 17
/
bootStrap.r
287 lines (240 loc) · 7.34 KB
/
bootStrap.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# bootStrap.r script file for computing descriptive statistics
# author: Eric Zivot
# created: October 10, 2003
# update history:
# July 17, 2012
# updated for summer 2012
# November 2, 2009
#
# Remarks:
# 1. The R function boot() from the package boot may be used for bootping arbitrary
# functions applied to data.
#
# Example data sets
# cerExample.csv price data on sbux, msft and sp500 from
# June 1992 - October 2000
# can be downloaded from
#
# http://faculty.washington.edu/ezivot/econ424/424notes.htm
#
# R function used
#
# abline() add line to plot
# cbind() combine columns
# class() return class of object
# colnames() extract column names from data object
# cor() compute correlation matrix
# diff() compute differences
# exp() compute exponential
# function() create R function
# lag() compute lags
# length() compute length of data object
# library() load package
# mean() compute sample mean
# names() extract names from list
# par() set graphics parameters
# qnorm() compute normal quantile
# read.csv() read comma separated value files
# sd() compute sample standard deviation
# sqrt() compute square root
#
# Package boot functions used
#
# boot() implement boot for given statistic
# print.boot() print boot object
# plot.boot() plot bootstrap distribution
# boot.ci() compute bootstrap confidence intervals
#
# Package zoo functions used
#
# zoo() create zoo object
#
# load boot and zoo packages
options(digits=4, width=70)
library(boot)
library(zoo)
# read prices from csv file on class webpage
cerExample.df = read.csv(file="http://faculty.washington.edu//ezivot//econ424//cerExample.csv")
# create zooreg object - regularly spaced zoo object
cerExample.z = zooreg(data=as.matrix(cerExample.df), start=c(1992,6),
end=c(2000,10), frequency=12)
index(cerExample.z) = as.yearmon(index(cerExample.z))
cerExample.z = as.zoo(cerExample.z)
colnames(cerExample.z)
start(cerExample.z)
end(cerExample.z)
returns.z = diff(log(cerExample.z))
colnames(returns.z)
start(returns.z)
end(returns.z)
#
# plot prices and returns for Microsoft
#
par(mfrow=c(2,1))
plot(cerExample.z[,"msft"], col="blue", lwd=2, ylab="price",
main="Monthly Prices on MSFT")
plot(log(cerExample.z[,"msft"]),col="blue", lwd=2, ylab="log price")
par(mfrow=c(1,1))
plot(returns.z[,"msft"],ylab="cc return",
main="Monthly cc returns on Microsoft",
col="blue", lwd=2)
abline(h=0)
#
# put data in matrix
#
returns.mat = coredata(returns.z)
MSFT = returns.mat[,"msft"]
SBUX = returns.mat[,"sbux"]
SP500 = returns.mat[,"sp500"]
#
# compute estimates of mu, sigma for MSFT and rho for MSFT and SP500
#
muhat.MSFT = mean(MSFT)
sigmahat.MSFT = sd(MSFT)
rhohat.MSFT.SP500 = cor(returns.mat[,c("msft","sp500")])[1,2]
muhat.MSFT
sigmahat.MSFT
rhohat.MSFT.SP500
#
# brute force bootstrap: MSFT mean
#
muhat.MSFT = mean(MSFT)
sigmahat.MSFT = sd(MSFT)
se.muhat.MSFT = sigmahat.MSFT/sqrt(length(MSFT))
rbind(muhat.MSFT, se.muhat.MSFT)
# sample() function
# random permutations of the index vector 1:5
sample(5)
sample(5)
# random sample of size 5 from MSFT return with replacement
sample(MSFT, 5, replace=TRUE)
B = 999
muhat.boot = rep(0, B)
nobs = length(MSFT)
for (i in 1:B) {
boot.data = sample(MSFT, nobs, replace=TRUE)
muhat.boot[i] = mean(boot.data)
}
# bootstrap bias
mean(muhat.boot) - muhat.MSFT
# bootstrap SE
sd(muhat.boot)
# analytic SE
sigmahat.MSFT/sqrt(length(MSFT))
par(mfrow=c(1,2))
hist(muhat.boot, col="slateblue1")
abline(v=muhat.MSFT, col="white", lwd=2)
qqnorm(muhat.boot)
qqline(muhat.boot)
par(mfrow=c(1,1))
#
# boot mean estimates using R boot package function boot()
#
?boot
# note: boot requires user-supplied functions that take
# two arguments: data and an index. The index is created
# by the boot function and represents random resampling with
# replacement
# function for bootstrapping sample mean
mean.boot = function(x, idx) {
# arguments:
# x data to be resampled
# idx vector of scrambled indices created by boot() function
# value:
# ans mean value computed using resampled data
ans = mean(x[idx])
ans
}
MSFT.mean.boot = boot(MSFT, statistic = mean.boot, R=999)
class(MSFT.mean.boot)
names(MSFT.mean.boot)
# print, plot and qqnorm methods
MSFT.mean.boot
# compare boot SE with analytic SE
se.muhat.MSFT = sigmahat.MSFT/sqrt(length(MSFT))
se.muhat.MSFT
# plot bootstrap distribution and qq-plot against normal
plot(MSFT.mean.boot)
# compute bootstrap confidence intervals from normal approximation
# basic bootstrap method and percentile intervals
boot.ci(MSFT.mean.boot, conf = 0.95, type = c("norm","perc"))
# compare boot confidence intervals with analytic confidence interval
MSFT.lower = muhat.MSFT - 2*se.muhat.MSFT
MSFT.upper = muhat.MSFT + 2*se.muhat.MSFT
cbind(MSFT.lower,MSFT.upper)
#
# boostrap SD estimate
#
# function for bootstrapping sample standard deviation
sd.boot = function(x, idx) {
# arguments:
# x data to be resampled
# idx vector of scrambled indices created by boot() function
# value:
# ans sd value computed using resampled data
ans = sd(x[idx])
ans
}
MSFT.sd.boot = boot(MSFT, statistic = sd.boot, R=999)
MSFT.sd.boot
# compare boot SE with analytic SE
se.sigmahat.MSFT = sigmahat.MSFT/sqrt(2*length(MSFT))
se.sigmahat.MSFT
# plot bootstrap distribution
plot(MSFT.sd.boot)
# compute confidence intervals
boot.ci(MSFT.sd.boot, conf=0.95, type=c("norm", "basic", "perc"))
# compare boot confidence intervals with analytic confidence interval
MSFT.lower = sigmahat.MSFT - 2*se.sigmahat.MSFT
MSFT.upper = sigmahat.MSFT + 2*se.sigmahat.MSFT
cbind(MSFT.lower,MSFT.upper)
# bootstrap correlation
# function to compute correlation between 1st 2 variables in matrix
rho.boot = function(x.mat, idx) {
# x.mat n x 2 data matrix to be resampled
# idx vector of scrambled indices created by boot() function
# value:
# ans correlation value computed using resampled data
ans = cor(x.mat[idx,])[1,2]
ans
}
MSFT.SP500.cor.boot = boot(returns.mat[,c("msft","sp500")],
statistic=rho.boot, R = 999)
MSFT.SP500.cor.boot
# compare boot SE with analytic SE based on CLT
se.rhohat.MSFT.SP500 = (1 - rhohat.MSFT.SP500^2)/sqrt(length(MSFT))
se.rhohat.MSFT.SP500
# plot bootstrap distribution
plot(MSFT.SP500.cor.boot)
# bootstrap confidence intervals
boot.ci(MSFT.SP500.cor.boot, conf=0.95, type=c("norm", "basic", "perc"))
# boot estimate of normal distribution quantile
norm.quantile.boot = function(x, idx, p=0.05) {
# x.mat data to be resampled
# idx vector of scrambled indices created by boot() function
# p probability value for quantile
# value:
# ans normal quantile value computed using resampled data
q = mean(x[idx]) + sd(x[idx])*qnorm(p)
q
}
MSFT.q05.boot = boot(MSFT, statistic=norm.quantile.boot, R=999)
MSFT.q05.boot
plot(MSFT.q05.boot)
boot.ci(MSFT.q05.boot, conf=0.95, type=c("norm", "basic", "perc"))
# 5% Value-at-Risk
ValueAtRisk.boot = function(x, idx, p=0.05, w=100000) {
# x.mat data to be resampled
# idx vector of scrambled indices created by boot() function
# p probability value for VaR calculation
# w value of initial investment
# value:
# ans Value-at-Risk computed using resampled data
q = mean(x[idx]) + sd(x[idx])*qnorm(p)
VaR = (exp(q) - 1)*w
VaR
}
MSFT.VaR.boot = boot(MSFT, statistic = ValueAtRisk.boot, R=999)
MSFT.VaR.boot
boot.ci(MSFT.VaR.boot, conf=0.95, type=c("norm", "perc"))
plot(MSFT.VaR.boot)