-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathhypothesisTestingCER.r
333 lines (276 loc) · 9.27 KB
/
hypothesisTestingCER.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# hypothesisTestingCER.r R script for hypothesis testing examples in CER
# model
#
# author: Eric Zivot
# created: November 10, 2003
# update history
# July 28, 2011
# updated examples for summer 2011
#
# data for examples
# cerExample dataframe with month prices on Microsoft, Starbucks and SP500
# from June 1992 through October 2000. Price data taken from Yahoo
#
# R function used
#
# apply() apply function to rows or columns of matrix
# colnames() show column names
# dchisq() compute chi-square density
# dim() show matrix dimensions
# dt() compute Student's t density
# legend() add legend to plot
# log() compute natural log
# pchisq() compute chi-square CDF
# points() add points to plot
# pt() compute Student's t CDF
# qchisq() compute chi-square quantiles
# qt() compute Student's t quantiles
# read.csv Read csv file
# t.test Perform t-test
#
# R package zoo functions used
#
# diff() compute difference
# end() show ending date
# rollapply() apply function over rolling window
# start() show starting date
# zooreg() create zooreg object
#
# Example data sets
# cerExample data.frame contain price data on sbux, msft and sp500 from
# June 1992 - October 2000. Source data is in the Excel file cerExample.xls
# that can be downloaded from
#
# http://faculty.washington.edu/ezivot/econ424/424notes.htm
#
# load R packages
library("zoo")
library("TSA")
options(digits=4, width=70)
#
# Example data sets
# cerExample data.frame contain price data on sbux, msft and sp500 from
# June 1992 - October 2000. Source data is in the csv file cerExample.csv
# that can be downloaded from
#
# http://faculty.washington.edu/ezivot/econ424/424notes.htm
#
# 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 data
# panel function to put horizontal lines at zero in each panel
my.panel <- function(...) {
lines(...)
abline(h=0)
}
plot(returns.z, lwd=2, col="blue", panel=my.panel)
#
# chi-square distribution
#
?dchisq
# plot chi-square distribution for various degrees
# of freedom
xx = seq(from=0,to=20,length=200)
plot(xx,dchisq(xx,df=2),main="Chi-Square Distribution",
type="l", xlab="x", ylab="pdf",lwd=2,col="black")
points(xx,dchisq(xx,df=5),type="l",lwd=2,col="blue")
points(xx,dchisq(xx,df=10),type="l",lwd=2,col="orange")
legend(10,0.4,legend=c("df=2","df=5","df=10"),lty=c(1,1,1),
lwd=c(2,2,2), col=c("black","blue","orange"))
# plot Student's t distribution for various degrees
# of freedom
xx = seq(from=-5,to=5,length=200)
plot(xx,dt(xx,df=10),main="Student's t Distribution",
type="l", xlab="t", ylab="pdf",lwd=2,col="black")
points(xx,dt(xx,df=5),type="l",lwd=2,col="blue")
points(xx,dt(xx,df=1),type="l",lwd=2,col="orange")
points(xx,dnorm(xx),type="l",lwd=2,col="green")
legend(x="topleft",legend=c("N(0,1)","df=10","df=5","df=2"),
lty=c(1,1,1,1), lwd=c(2,2,2,2),
col=c("green","black","blue","orange"))
# compare quantiles of different t distributions
qnorm(c(0.01,0.05))
qt(c(0.01,0.05), df=10)
qt(c(0.01,0.05), df=5)
qt(c(0.01,0.05), df=2)
#
# Basic significance tests for CER Model
#
# construct test by brute force
nobs = dim(returns.z)[1]
muhat.vals = apply(returns.z, 2, mean)
muhat.vals
sigmahat.vals = apply(returns.z, 2, sd)
se.muhat = sigmahat.vals/sqrt(nobs)
se.muhat
t.stats = muhat.vals/se.muhat
abs(t.stats)
# compute 2-sided 5% critical values
cv.2sided = qt(0.975, df=nobs-1)
cv.2sided
abs(t.stats) > cv.2sided
# compute 2-sided p-values
2*(1-pt(abs(t.stats),df=nobs-1))
#
?t.test
# Test H0: mu = 0 for msft
t.test.msft = t.test(returns.z[,"msft"],
alternative="two.sided",
mu=0, conf.level=0.95)
class(t.test.msft)
t.test.msft
# Test H0: mu = 0 for sbux and sp500
t.test(returns.z[,"sbux"], alternative="two.sided",
mu=0, conf.level=0.95)
t.test(returns.z[,"sp500"], alternative="two.sided",
mu=0, conf.level=0.95)
#
# test for specific value
#
# Test H0: mu = 0.03 for msft
t.test(returns.z[,"msft"],
alternative="two.sided",
mu = 0.03, conf.level=0.95)
#
# test for sign
#
# Test H0: mu > 0 for msft
t.test(returns.z[,"msft"],
alternative="greater",
mu = 0, conf.level=0.95)
#
# Paired t-test for equality of means
#
# Test H0: mu_msft = mu_sbux vs. H1: mu_msft /= mu_sbux
t.test(x=returns.z[,"msft"],
y=returns.z[,"sbux"],
paired=T)
#
# test for normality of returns
#
par(mfrow=c(2,2))
hist(returns.z[,"sbux"],main="Starbucks monthly cc returns",
probability=T, ylab="cc return", col="slateblue1")
boxplot(returns.z[,"sbux"],outchar=T, ylab="cc return",
col="slateblue1")
plot(density(returns.z[,"sbux"]),type="l",xlab="cc return",
ylab="density estimate", main="Smoothed density",
lwd=2, col="slateblue1")
qqnorm(returns.z[,"sbux"], col="slateblue1")
qqline(coredata(returns.z[,"sbux"]))
par(mfrow=c(1,1))
sbux.skew = skewness(returns.z[,"sbux"])
sbux.ekurt= kurtosis(returns.z[,"sbux"]) # note: this is excess kurtosis
JB = nobs*(sbux.skew^2 + 0.25*sbux.ekurt^2)/6
JB
# compute p-value from chi-square 2 distn
p.value = 1 - pchisq(JB,df=2)
p.value
# use jarque.bera.test() function from tseries package
library(tseries)
jarque.bera.test(returns.z[,"sbux"])
#
# test for no autocorrelation in returns
#
sbux.acf = acf(returns.z[,"sbux"])
#
# diagnostics for constant parameters
#
#
# compute two sample t-test for equality of means
#
# Split sample into two equally sized pieces
# Test H0: E[r_sample1] = E[r_sample2]
t.test(x=returns.z[1:50,"msft"],
y=returns.z[51:100,"msft"],
paired=F)
# compute rolling means for sbux
# using function rollapply
# note: must use zoo objects
#
?rollapply
args(rollapply)
# compute rolling means over 24 month windows
roll.muhat = rollapply(returns.z[,"sbux"], width=24,
FUN=mean, align="right")
class(roll.muhat)
roll.muhat[1:5]
# plot rolling estimates with data
plot(merge(roll.muhat,returns.z[,"sbux"]), plot.type="single",
main="24 month rolling means for SBUX",ylab="returns",
lwd=c(2,2), col=c("blue","orange"))
abline(h=0)
legend(x="bottomleft",legend=c("Rolling mean","Monthly returns"),
lwd=c(2,2), col=c("blue","orange"))
# compute rolling standard deviations over 24 month windows
roll.sigmahat = rollapply(returns.z[,"sbux"],width=24,
FUN=sd, align="right")
roll.sigmahat[1:5]
# plot rolling estimates with data
plot(merge(roll.sigmahat,returns.z[,"sbux"]), plot.type="single",
main="24 month rolling SDs for SBUX", ylab="Returns",
lwd=c(2,2), col=c("blue","orange"))
abline(h=0)
legend(x="bottomleft",legend=c("Rolling SD","Monthly returns"),
lwd=c(2,2), col=c("blue","orange"))
# repeat analysis for sp500
# compute rolling means over 24 month windows
roll.muhat = rollapply(returns.z[,"sp500"], width=24,
FUN=mean, align="right")
class(roll.muhat)
roll.muhat[1:5]
# plot rolling estimates with data
plot(merge(roll.muhat,returns.z[,"sp500"]), plot.type="single",
main="24 month rolling means for SP500",ylab="returns",
lwd=c(2,2), col=c("blue","orange"))
abline(h=0)
legend(x="bottomleft",legend=c("Rolling mean","Monthly returns"),
lwd=c(2,2), col=c("blue","orange"))
# compute rolling standard deviations over 24 month windows
roll.sigmahat = rollapply(returns.z[,"sp500"],width=24,
FUN=sd, align="right")
roll.sigmahat[1:5]
# plot rolling estimates with data
plot(merge(roll.sigmahat,returns.z[,"sp500"]), plot.type="single",
main="24 month rolling SDs for SP500", ylab="Returns",
lwd=c(2,2), col=c("blue","orange"))
abline(h=0)
legend(x="bottomleft",legend=c("Rolling SD","Monthly returns"),
lwd=c(2,2), col=c("blue","orange"))
#
# compute rolling correlations
#
rhohat = function(x) {
cor(x)[1,2]
}
# compute rolling estimates b/w sp500 and sbux
roll.rhohat = rollapply(returns.z[,c("sp500","sbux")],
width=24,FUN=rhohat, by.column=FALSE,
align="right")
class(roll.rhohat)
roll.rhohat[1:5]
# plot rolling estimates
plot(roll.rhohat, main="Rolling Correlation b/w SP500 and SBUX",
lwd=2, col="blue", ylab="rho.hat")
abline(h=0)
# compute rolling correlations b/w sbux and msft
roll.rhohat = roll.rhohat = rollapply(returns.z[,c("sbux","msft")],
width=24,FUN=rhohat, by.column=FALSE,
align="right")
class(roll.rhohat)
roll.rhohat[1:5]
plot(roll.rhohat, main="Rolling Correlation b/w SBUX and MSFT",
lwd=2, col="blue", ylab="rho.hat")
abline(h=0)