-
Notifications
You must be signed in to change notification settings - Fork 199
/
Copy pathcerModelExamples.r
676 lines (579 loc) · 18.5 KB
/
cerModelExamples.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
# cerModelExamples.r scripts for examples in the CER model chapter
#
# author: Eric Zivot
# created: January 30, 2002
# update History:
# July 26, 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
# abline draw line on graph
# acf compute sample autocovariances or autocorrelations
# args determine agruments of a function
# boxplot compute boxplot
# cbind combine data objects vertically
# class determine class of object
# colnames get column names from object
# cor compute sample correlation matrix
# density compute smoothed histogram
# dnorm compute density from normal distribution
# end get end date of time series
# help invoke help system
# hist compute histogram
# legend add legend to graph
# length compute column length of matrix
# library load R package
# mean compute sample mean
# names show names of object
# pairs all pairwise scatterplots
# par set graphics parameters
# plot generic plot function
# pnorm compute normal CDF
# points add points to a graph
# qqline add line to qq-plot
# qqnorm qq-plot against normal distribution
# quantile compute empirical quantiles
# range compute range = max - min
# rmvnorm generate multivariate normal random numbers
# rnorm generate normal random numbers
# seq generate sequence of numbers
# set.seed set random number seed
# sort sort data
# start get start date of time series
# sd compute sample standard deviation
# ts.plot time series plot
# var compute sample variance or covariance matrix
# ? invoke help system
#
# R Packages Used
# zoo
# PerformanceAnalytics
# mvtnorm
#
options(digits=4, width=70)
# load R packages
library("PerformanceAnalytics")
library("mvtnorm")
#library("zoo")
#library("TSA")
#
# 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)
# create cc returns from prices
returns.z = diff(log(cerExample.z))
colnames(returns.z)
start(returns.z)
end(returns.z)
head(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)
# graphically summarize empirical distribution
#
# put data in matrix and compute mean and standard deviation
#
returns.mat = coredata(returns.z)
mu.msft = mean(returns.mat[,"msft"])
sigma.msft = sd(returns.mat[,"msft"])
mu.msft
sigma.msft
par(mfrow=c(1,2))
plot(returns.z[,"msft"],ylab="returns",
main="",
col="blue", lwd=2)
abline(h=0)
hist(returns.mat[, "msft"], main="", xlab="returns", col="slateblue1")
par(mfrow=c(1,1))
par(mfrow=c(2,2))
hist(returns.mat[,"msft"], xlab="return",ylab="frequency",
main="Monthly cc returns on MSFT", col="slateblue1")
boxplot(returns.mat[,"msft"], col="slateblue1")
plot(density(returns.mat[,"msft"]),type="l",xlab="return",ylab="density",
lwd=2, col="slateblue1", main="smoothed densiy")
qqnorm(returns.mat[,"msft"], col="blue")
qqline(returns.mat[,"msft"])
par(mfrow=c(1,1))
#
# Illustrate simulation of random numbers using inverse CDF technique
#
xx = seq(from=-4, to=4, length=250)
plot(xx,pnorm(xx), type="l", ylab="u", xlab="x", lwd=3)
#
# simulate CER model calibrated to monthly returns on MSFT
#
mu = 0.03
sd.e = 0.10
nobs = 100
set.seed(111)
sim.e = rnorm(nobs, mean=0, sd=sd.e)
sim.ret = mu + sim.e
# plot simulated returns
par(mfrow=c(1,2))
ts.plot(sim.ret, main="",
xlab="months",ylab="return", lwd=2, col="blue")
abline(h=mu)
hist(sim.ret, main="", xlab="returns", col="slateblue1")
par(mfrow=c(1,1))
# graphically summarize empirical distribution of simulated data
par(mfrow=c(2,2))
hist(sim.ret, xlab="return",ylab="frequency",
main="Simulated returns from CER model", col="slateblue1")
boxplot(sim.ret, col="slateblue1")
plot(density(sim.ret),type="l",xlab="return",ylab="density",
col="slateblue1", lwd=2, main="smoothed densiy")
qqnorm(sim.ret, col="slateblue1")
qqline(sim.ret)
par(mfrow=c(1,1))
# compare msft data to simulated gaussian data
boxplot(returns.z[,"msft"], sim.ret, col="slateblue1", names=c("MSFT","GWN"))
# compute quantiles and compare to normal quantiles
pvals = c(0.01,0.05,0.25,0.5,0.75,0.95,0.99)
quantile(probs=pvals,sim.ret)
qnorm(p=pvals,mean=mu,sd=sd)
# compute descriptive statistics
mean(sim.ret)
sd(sim.ret)
skewness(sim.ret)
kurtosis(sim.ret)
# plot autocorrelations
tmp = acf(sim.ret)
#
# simulate random walk model with initial log Price = 1
#
mu = 0.03
sd.e = 0.10
nobs = 100
set.seed(111)
sim.e = rnorm(nobs, mean=0, sd=sd.e)
sim.p = 1 + mu*seq(nobs) + cumsum(sim.e)
sim.P = exp(sim.p)
par(mfrow=c(2,1))
ts.plot(sim.p, col="blue",lwd=2,
ylim=c(-2, 4), ylab="log price")
lines( (1+mu*seq(nobs)), lty="dotted", col="black", lwd=2)
lines(cumsum(sim.e), col="orange", lty="dashed", lwd=2)
abline(h=0)
legend(x="topleft",legend=c("p(t)","E[p(t)]","p(t)-E[p(t)]"),
lty=c("solid","dotted","dashed"), col=c("blue","black","orange"),
lwd=2, cex=c(0.75,0.75,0.75))
ts.plot(sim.P, lwd=2, col="blue", ylab="price")
par(mfrow=c(1,1))
#
# plot returns on msft, sbux and sp500
#
msft.ret = returns.z[,"msft"]
sbux.ret = returns.z[,"sbux"]
sp500.ret = returns.z[,"sp500"]
# panel function to put horizontal lines at zero in each panel
my.panel <- function(...) {
lines(...)
abline(h=0)
}
plot(returns.z, col="blue", lwd=2, main="", panel=my.panel)
plot(returns.z, plot.type="single", lwd=2, col=1:3)
legend(x="bottomleft", legend=colnames(returns.z), col=1:3, lwd=2)
abline(h=0)
pairs(returns.mat, col="blue")
# multivariate simulation
mu = c(0.03,0.03,0.01)
sig2.msft = 0.018
sig2.sbux = 0.011
sig2.sp500 = 0.001
sig.msft.sbux = 0.004
sig.msft.sp500 = 0.002
sig.sbux.sp500 = 0.002
Sigma = matrix(c(sig2.sbux, sig.msft.sbux, sig.sbux.sp500,
sig.msft.sbux, sig2.msft, sig.msft.sp500,
sig.sbux.sp500, sig.msft.sp500, sig2.sp500),
nrow=3, ncol=3, byrow=TRUE)
nobs = 100
set.seed(123)
returns.sim = rmvnorm(nobs, mean=mu, sigma=Sigma)
colnames(returns.sim) = c("sbux", "msft", "sp500")
plot(as.zoo(returns.sim), lwd=2, col="blue", main="",
ylab=c("sbux", "msft", "sp500"), panel=my.panel)
plot(as.zoo(returns.sim), lwd=2, main="",
ylab=c("sbux", "msft", "sp500"), plot.type="single",
col=c("black","red","green"))
abline(h=0)
legend(x="topleft", legend=colnames(returns.sim), col=1:3, lwd=2)
pairs(returns.sim, col="blue")
#
# plot pdf's for unbiased estimator with high variance, and for
# biased estimator with low variance
#
x.min = -3
x.max = 3
npts = 100
x.vals = seq(from=x.min,to=x.max,length=npts)
pdf1 = dnorm(x.vals,mean=0,sd=1)
pdf2 = dnorm(x.vals,mean=0.1,sd=0.25)
ylimits = range(pdf1,pdf2)
plot(x.vals,pdf1,ylim=ylimits,type="l",lty=1,ylab="pdf",
xlab="estimate value", lwd=2)
segments(x0=0, y0=0, x1=0, y1=dnorm(0), lwd=2)
lines(x.vals, pdf2, lty=2, lwd=2, col="red")
legend(x="topleft",legend=c("theta.hat 1","theta.hat 2"), lty=1:2,
lwd=c(2,2), col=c(1,2))
segments(x0=0.1, y0=0, x1=0.1, y1=dnorm(0.1, mean=0.1, sd=0.25), lwd=2, col="red")
#
# estimate parameters from CER model
#
options(digits=4)
muhat.vals = apply(returns.mat,2,mean)
muhat.vals
sigma2hat.vals = apply(returns.mat,2,var)
sigma2hat.vals
sigmahat.vals = apply(returns.mat,2,sd)
sigmahat.vals
cov.mat = var(returns.z)
cov.mat
cor.mat = cor(returns.z)
cor.mat
covhat.vals = cov.mat[lower.tri(cov.mat)]
rhohat.vals = cor.mat[lower.tri(cor.mat)]
names(covhat.vals) <- names(rhohat.vals) <-
c("sbux,msft","sbux,sp500","msft,sp500")
covhat.vals
rhohat.vals
# compute estimated standard error for mean
nobs = nrow(returns.mat)
nobs
se.muhat = sigmahat.vals/sqrt(nobs)
se.muhat
# compute t-ratios
muhat.vals/se.muhat
# evaluate sampling distribution of muhat
x.vals = seq(3, -3, length=100)
pdf1 = dnorm(x.vals)
pdf2 = dnorm(x.vals, sd=1/sqrt(10))
pdf3 = dnorm(x.vals, sd=1/sqrt(50))
ub = max(pdf1, pdf2, pdf3)
plot(x.vals, pdf1, ylim=c(0, ub), type="l", lwd=2)
lines(x.vals, pdf2, lwd=2, col=2, lty=2)
lines(x.vals, pdf3, lwd=2, col=3, lty=3)
legend(x="topleft", legend=c("pdf: T=1", "pdf: T=10", "pdf: T=50"),
lty=1:3, col=1:3, lwd=2)
# compute exact 95% confidence intervals
t.975 = qt(0.975, df=99)
mu.lower = muhat.vals - t.975*se.muhat
mu.upper = muhat.vals + t.975*se.muhat
mu.width = mu.upper - mu.lower
cbind(mu.lower,mu.upper,mu.width)
# compute approx 95% confidence intervals
mu.lower = muhat.vals - 2*se.muhat
mu.upper = muhat.vals + 2*se.muhat
mu.width = mu.upper - mu.lower
cbind(mu.lower,mu.upper,mu.width)
# compute estimated standard errors for variance and sd
se.sigma2hat = sigma2hat.vals/sqrt(nobs/2)
se.sigmahat = sigmahat.vals/sqrt(2*nobs)
se.sigma2hat
se.sigmahat
# compute approx 95% confidence intervals
sigma2.lower = sigma2hat.vals - 2*se.sigma2hat
sigma2.upper = sigma2hat.vals + 2*se.sigma2hat
sigma2.width = sigma2.upper - sigma2.lower
cbind(sigma2.lower,sigma2.upper,sigma2.width)
sigma.lower = sigmahat.vals - 2*se.sigmahat
sigma.upper = sigmahat.vals + 2*se.sigmahat
sigma.width = sigma.upper - sigma.lower
cbind(sigma.lower,sigma.upper,sigma.width)
# compute estimated standard errors for correlation
se.rhohat = (1-rhohat.vals^2)/sqrt(nobs)
se.rhohat
# compute approx 95% confidence intervals
rho.lower = rhohat.vals - 2*se.rhohat
rho.upper = rhohat.vals + 2*se.rhohat
rho.width = rho.upper - rho.lower
cbind(rho.lower,rho.upper,rho.width)
#
# Monte Carlo evaluation of unbiasedness
#
# generate 10 simulated samples from CER model and plot
# r = 0.05 + e, e ~ iid N(0, 0.10^2)
mu = 0.05
sd = 0.10
n.obs = 100
n.sim = 10
set.seed(111)
sim.ret = matrix(0, n.obs, n.sim)
for (sim in 1:n.sim) {
sim.ret[,sim] = rnorm(n.obs,mean=mu,sd=sd)
}
ts.plot(sim.ret,ylab="returns", col=1:10, lty=1:10)
abline(h=0)
plot(as.zoo(sim.ret), col="blue", lwd=2, main="", panel=my.panel)
# generate 1000 samples from CER and evaluate sampling properties of muhat
mu = 0.05
sd = 0.10
n.obs = 100
n.sim = 1000
set.seed(111)
sim.means = rep(0,n.sim) # initialize vectors
mu.lower = rep(0,n.sim)
mu.upper = rep(0,n.sim)
qt.975 = qt(0.975, nobs-1)
for (sim in 1:n.sim) {
sim.ret = rnorm(n.obs,mean=mu,sd=sd)
sim.means[sim] = mean(sim.ret)
se.muhat = sd(sim.ret)/sqrt(n.obs)
mu.lower[sim] = sim.means[sim]-qt.975*se.muhat
mu.upper[sim] = sim.means[sim]+qt.975*se.muhat
}
mean(sim.means)
sd(sim.means)
in.interval = mu >= mu.lower & mu <= mu.upper
sum(in.interval)/n.sim
hist(sim.means, col="slateblue1", ylim=c(0,40), main="", xlab="muhat", probability=T)
abline(v=mean(sim.means), col="white", lwd=4, lty=2)
# overlay normal curve
x.vals = seq(0.02, 0.08, length=100)
lines(x.vals, dnorm(x.vals, mean=mu, sd=sd/sqrt(100)), col="orange", lwd=2)
# compute expected value of estimates and bias
mean(sim.means)
mean(sim.means) - mu
sd(sim.means)
se.muhat["msft"]
mu = 0.05
sd = 0.10
n.obs = 100
n.sim = 1000
set.seed(111)
sim.means = rep(0,n.sim) # initialize vectors
sim.vars = rep(0,n.sim)
sim.sds = rep(0,n.sim)
for (sim in 1:n.sim) {
sim.ret = rnorm(n.obs,mean=mu,sd=sd)
sim.means[sim] = mean(sim.ret)
sim.vars[sim] = var(sim.ret)
sim.sds[sim] = sqrt(sim.vars[sim])
}
# compute expected value of estimates and bias
mean(sim.means)
mean(sim.means) - mu
mean(sim.vars)
mean(sim.vars) - sd^2
mean(sim.sds)
mean(sim.sds) - sd
par(mfrow=c(2,2))
hist(sim.means, col="slateblue1", xlab="mu hat values", main="mu hat")
abline(v=mean(sim.means), col="white", lwd=4, lty=2)
hist(sim.vars, col="slateblue1", xlab="sigma2 hat values", main="sigma2 hat")
abline(v=mean(sim.vars), col="white", lwd=4, lty=2)
hist(sim.sds, col="slateblue1", xlab="sigma hat values", main="sigma hat")
abline(v=mean(sim.sds), col="white", lwd=4, lty=2)
par(mfrow=c(1,1))
#
# compute Monte Carlo standard errors and compare with
# asymptotic formulas based on true values
#
sd(sim.means)
sd/sqrt(nobs)
sd(sim.vars)
sd^2/sqrt(nobs/2)
sd(sim.sds)
sd/sqrt(2*nobs)
#
# plot pdf for different sample sizes
#
x.min = -3
x.max = 3
npts = 100
x.vals = seq(from=x.min,to=x.max,length=npts)
pdf1 = dnorm(x.vals,mean=0,sd=1)
pdf2 = dnorm(x.vals,mean=0,sd=1/sqrt(10))
pdf3 = dnorm(x.vals,mean=0,sd=1/sqrt(50))
ylimits = range(pdf1,pdf2,pdf3)
plot(x.vals,pdf1,ylim=ylimits,type="l",lty=1,ylab="pdf",
lwd=2,xlab="estimate value")
lines(x.vals,pdf2,type="l", lty=1, col=2, lwd=2)
lines(x.vals,pdf3,type="l", lty=1, col=3, lwd=2)
legend(1.5,2.5,legend=c("pdf T=1","pdf T=10","pdf T=50"),
lty=rep(1,3),col=1:3, lwd=rep(2,3))
#
# LLN
#
nobs = 1000
# Draw iid N(0.03,0.11) values
set.seed(123)
z = rnorm(1000, mean=0.03, sd=0.11)
mu.hat.n = cumsum(z)/1:nobs
ts.plot(mu.hat.n, main="Consistency of Sample Mean from CER Model",
ylab="Sample Mean", xlab="Sample Size, T")
abline(h=0.03)
#
# Illustration of CLT for muhat, sigma2hat and sigmahat
#
# generate 1000 samples from CER and compute sample statistics
mu = 0.03
sd = 0.11
n.obs = 25
n.sim = 1000
set.seed(111)
sim.means = rep(0,n.sim) # initialize vectors
sim.vars = rep(0,n.sim)
sim.sds = rep(0,n.sim)
for (sim in 1:n.sim) {
sim.ret = rnorm(n.obs,mean=mu,sd=sd)
sim.means[sim] = mean(sim.ret)
sim.vars[sim] = var(sim.ret)
sim.sds[sim] = sqrt(sim.vars[sim])
}
# plot pdfs
par(mfrow=c(2,2))
hist(sim.means,xlab="mu hat", col="slateblue1")
abline(v=mu, lwd=2, col="white")
hist(sim.vars,xlab="sigma2 hat", col="slateblue1")
abline(v=sd^2, lwd=2, col="white")
hist(sim.sds,xlab="sigma hat", col="slateblue1")
abline(v=sd, lwd=2, col="white")
par(mfrow=c(1,1))
#
# use Monte Carlo to evaluate confidence interval coverage
#
# generate 1000 samples from CER and compute sample statistics
n.sim = 1000
set.seed(111)
mu.lower = rep(0,n.sim) # initialize vectors
mu.upper = rep(0,n.sim)
for (sim in 1:n.sim) {
sim.ret = rnorm(n.obs,mean=mu,sd=sd)
mu.hat = mean(sim.ret)
se.muhat = sd(sim.ret)/sqrt(n.obs)
mu.lower[sim] = mu.hat - 2*se.muhat
mu.upper[sim] = mu.hat + 2*se.muhat
}
in.interval = (mu >= mu.lower) & (mu <= mu.upper)
sum(in.interval)/n.sim
#
# plot actual data
#
plot(returns.z, lwd=c(2,2,2), col=c(1,2,5),
lty=c(1,1,1))
plot(returns.z, plot.type = "single", col=c(1,2,5), lwd=2)
abline(h=0)
legend(x="bottomleft", legend=c("SBUX","MSFT","SP500"),
lwd=c(2,2,2), col=c(1,2,5), lty=c(1,1,1))
#
# simulate data for three asset returns
#
nobs = 100
set.seed(123)
sim.e = rmvnorm(nobs,mean=rep(0,3),cov=cov.mat)
sim.ret = muhat.vals + sim.e
colIds(sim.ret) = paste(colIds(returns.mat),".sim",sep="")
# plot data and scatterplots
ts.plot(sim.ret,main="Simulated return data",
lty=rep(1,3),lwd=rep(2,3),col=c(1,2,5))
legend(0,-0.2,legend=colIds(sim.ret),
lty=rep(1,3),lwd=rep(2,3),col=c(1,2,5))
abline(h=0)
pairs(sim.ret)
# compare with actual data
ts.plot(returns.mat,main="Actual return data",
lty=rep(1,3),lwd=rep(2,3),col=c(1,2,5))
legend(0,-0.2,legend=colIds(sim.ret),
lty=rep(1,3),lwd=rep(2,3),col=c(1,2,5))
abline(h=0)
pairs(returns.mat)
# generate 1000 samples from CER and compute correlations
n.obs = 100
n.sim = 1000
set.seed(111)
sim.corrs = matrix(0,n.sim,3) # initialize vectors
colIds(sim.corrs) = c("sbux,msft","sbux,sp500","msft,sp500")
for (sim in 1:n.sim) {
sim.ret = rmvnorm(n.obs,mean=muhat.vals,sigma=cov.mat)
cor.mat = cor(sim.ret)
sim.corrs[sim,] = cor.mat[lower.tri(cor.mat)]
}
par(mfrow=c(2,2))
hist(sim.corrs[,1], xlab="rhohat(sbux,msft)", col="slateblue1",
main="sbux,msft")
abline(v=rhohat.vals[1], lwd=4, col="white")
hist(sim.corrs[,2], xlab="rhohat(sbux,sp500)", col="slateblue1",
main="sbux,sp500")
abline(v=rhohat.vals[2], lwd=4, col="white")
hist(sim.corrs[,3], xlab="rhohat(msft,sp500)", col="slateblue1",
main="msft,sp500")
abline(v=rhohat.vals[3], lwd=4, col="white")
par(mfrow=c(1,1))
# MC means and standard deviations
colMeans(sim.corrs)
colStdevs(sim.corrs)
se.rhohat
#
# Compute rolling mean and SD values: using rolling functions from zoo
#
class(returns.z)
colIds(returns.z)
start(returns.z)
end(returns.z)
MSFT.z = returns.z[,"msft"]
class(MSFT.z)
plot(MSFT.z,reference.grid=F)
# compute rolling means of width 24 months
roll.mean.24 = aggregateSeries(MSFT.z,moving=23,adj=1,FUN=mean)
roll.mean.24
plot(roll.mean.24,reference.grid=F,
main="Rolling 24 month mean estimates")
abline(h=muhat.vals["msft"])
plot(roll.mean.24,MSFT.z,reference.grid=F,
main="MSFT returns and rolling 24 month means")
abline(h=muhat.vals["msft"])
# compute rolling sds of width 24 months
roll.sd.24 = aggregateSeries(MSFT.z,moving=23,adj=1,FUN=sd)
roll.sd.24
plot(roll.sd.24,reference.grid=F,
main="Rolling 24 month SD estimates")
abline(h=sigmahat.vals["msft"])
plot(roll.sd.24,MSFT.z,reference.grid=F,
main="MSFT returns and rolling 24 month sds")
abline(h=muhat.vals["msft"])
#
# Estimate Quantiles and VaR from CER model
#
# use R function qnorm to compute quantiles from standard normal distribution
args(qnorm)
qhat.05 = muhat.vals + sigmahat.vals*qnorm(0.05)
qhat.05
qnorm(0.05,muhat.vals,sigmahat.vals)
W0 = 100000
VaRhat.05 = (exp(qhat.05)-1)*W0
VaRhat.05
# VaR example from slides
muhat = 0.02
sigmahat = 0.10
qhat.05 = muhat + sigmahat*qnorm(0.05)
W0 = 10000
VaRhat.05 = (exp(qhat.05)-1)*W0
VaRhat.05