FinAnalyticsSolutions1236 PDF
FinAnalyticsSolutions1236 PDF
Analytic Thinking
1.1
1.14 − 1
= 0.14 ∼ 14%
1
1
1.50
1.45
1.40
EUR prices
1.35
1.30
1.25
1.20
1 2 3 4 5 6
Index
> homeuser="<basedir>"
> library(ggplot2)
> x[x>1.3]
[1] 1024
> g(4,6)
[1] 4096
> g(4,y=7)
[1] 16384
2
> g(y=8,x=4)
[1] 65536
> g
function(x,y=5) { return(x^y) }
> x <- 1
> assign("x",2)
> x=3
> f <- function(x)
+ {
+ x=4
+ x
+ }
> f(x)
[1] 4
> x
[1] 3
> x = 3
> x
[1] 3
> f <- function(x)
+ {
+ x <<- 4
+ }
> f(x)
> x
[1] 4
> typeof(f)
[1] "closure"
> typeof(x)
[1] "double"
> call_type = 2
> if(call_type == 1){
+ str = "f(2)"
+ } else {
+ str = "g(2)"
+ }
> eval(parse(text=str))
3
[1] 32
> call_type = 2
> ifelse(call_type == 1,
+ eval(parse(text="f(2)")),
+ eval(parse(text="g(2)")))
[1] 32
> set.seed(1)
> vec = c(1:3)
> sapply(vec,rnorm)
[[1]]
[1] -0.6264538
[[2]]
[1] 0.1836433 -0.8356286
[[3]]
[1] 1.5952808 0.3295078 -0.8204684
> A = cbind(rep(x,length(y)),y)
> A
y
[1,] 4 -0.08004271
[2,] 4 0.08004271
[3,] 4 NA
[4,] 4 NA
[5,] 4 0.06899287
> B = rbind(rep(x,length(y)),y)
> B
> t(A) == B
> sum(t(A) == B)
[1] NA
4
> B[,4]
y
4 NA
> B[,-4]
[1] 4
> n <- 12
> z <- 1:n
> z
[1] 1 2 3 4 5 6 7 8 9 10 11 12
> z <-c(1:n)
> z <- vector(length = n)
> for(i in 1:n)
+ z[i] <- i
> z
[1] 1 2 3 4 5 6 7 8 9 10 11 12
, , 1
5
[2,] 2 4 6 8
, , 2
, , 3
[1] 2
> dim(arr2by4by3[1,c(-3,-4),])
[1] 2 3
> A <- arr2by4by3[1,c(-3,-4),]
> t(A)
[,1] [,2]
[1,] 1 3
[2,] 9 11
[3,] 17 19
6
> A <- arr2by4by3[1,c(-3,-4),]
> A
> A%*%t(A)
[,1] [,2]
[1,] 371 425
[2,] 425 491
> 1+9*9+17*17
[1] 371
> fh <- 0
> tryCatch({
+ #main block
+ fh <<- file("file1.txt",open="r")
+ }, warning = function(w){
+ #warning handler code
+ print(w)
+ fh <<- NA
+ }, error = function(e){
+ #error handling code
+ print(e)
+ fh <<- NA
+ }, finally = {
+ #cleanup code
+ })
<simpleWarning in file("file1.txt", open = "r"): cannot open file 'file1.txt': No such file
> fh
[1] NA
> options(digits=10)
> pi = 3.1415926535897932384626
> pi
[1] 3.141592654
> plot(density(rbinom(50,50,1/2)))
7
density.default(x = rbinom(50, 50, 1/2))
0.14
0.12
0.10
0.08
Density
0.06
0.04
0.02
0.00
15 20 25 30 35
N = 50 Bandwidth = 0.9214
> options(digits=6)
> set.seed(99)
> sample(10,replace = TRUE)
[1] 6 2 7 10 6 10 7 3 4 2
[1] "PCLN,UNP,IBM,MCD,PFE"
[1] "01"
[1] 4
8
x y fac
1 1 1 B
2 1 2 B
3 1 3 A
4 1 4 B
> d$fac
[1] B B A B C B B A A A
Levels: A B C
x y fac
1 1 1 B
2 1 2 B
3 1 3 A
4 1 4 B
> names(e)
x y factor
1 1 1 B
> typeof(e)
[1] "list"
> #setwd(paste(homeuser,"/FinAnalytics/ChapXI",sep=""))
> c(1,c(1,2),3,"A",c(4,5))
> list(1,c(1,2),3,"A",list(4,5))
[[1]]
[1] 1
[[2]]
[1] 1 2
9
[[3]]
[1] 3
[[4]]
[1] "A"
[[5]]
[[5]][[1]]
[1] 4
[[5]][[2]]
[1] 5
> l <- list(1,c(1,2),3,"A",list(4,5))
> l[2]
[[1]]
[1] 1 2
> l[[2]]
[1] 1 2
> e[[1]]
[1] 1 1 1 1 1 1 1 1 1 1
> e[[2]]
[1] 1 2 3 4 5 6 7 8 9 10
> e[[3]]
[1] B B A B C B B A A A
Levels: A B C
> obtainPrices <- function() {
+ A <- matrix(c("VRSN","UNP","HPQ","NSC", nrow=1))
+ B <- matrix(c(37.61, 125.62, 50.48, 50.44), nrow=1)
+ list(A,B)
+ }
> res <- obtainPrices()
> res[[1]]
[,1]
[1,] "VRSN"
[2,] "UNP"
[3,] "HPQ"
[4,] "NSC"
[5,] "1"
10
> res[[2]]
2.2
> x<-seq(-2,2,0.1)
> f<-function(x) if(x>=0 & x<=1) 2*x else 0.0
> fx<-lapply(x,f)
> fx<-unlist(fx)
Now plot.
> plot(x,fx)
2.0
1.5
1.0
fx
0.5
0.0
−2 −1 0 1 2
2.3
> x<-c(1:25)
> y<-x^2
Now plot.
> plot(x,y)
11
600
500
400
300
y
200
100
0
5 10 15 20 25
Financial Statistics
3.1 Expected value is given by
Variance is given by
12
b) Probability that server is operating less than 6 hours is
Z 0.25
P (X < 6/24) = P (X < 0.25) = 2x · dx = (0.25)2 − 02
0
Variance is given by
1 1 1
V ar(X) = E(X 2 ) − E 2 (X) = − =
2 4 4
13
6.1 Public Transport Boardings In this exercise we use our time series
analysis techniques to model and forecast the number of people who boarded
light rail trains and city buses in Denver, Colorado.
a) Load the boardings data with
> data(boardings)
and examine it’s structure with
> str(boardings)
Time-Series [1:68, 1:2] from 2001 to 2006: 12.5 12.6 12.5 12.5 12.4 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:2] "log.boardings" "log.price"
12.55
12.50
12.45
12.40
Time
14
> plot(boardings,col='blue')
> points(boardings,x=time(boardings),pch=as.vector(season(boardings)))
12.70
O
12.65
N
S F
M
12.60
S J
O A
S O S F M A
AM M
boardings
S N
12.55
A O O
O
N M N FM J
F
N F AMJ A F J D
JA A J
12.50
AM N
M J J
A M A J
J J J
D
J D
12.45
J D
JJ
M
D
12.40
Time
> acf(as.vector(boardings))
15
Series as.vector(boardings)
0.4
0.2
ACF
0.0
−0.2
5 10 15
Lag
16
Series as.vector(boardings)
0.4
0.2
Partial ACF
0.0
−0.2
5 10 15
Lag
Is the yearly periodicity apparent? We see the yearly lag significant at lag
12. What about AR lags? We see lags at 1 and 4.
f) Model the boardings data as ARIMA(4, 0, 3) × (1, 0, 0)12 . Note that the
Integrated part of the model is zero because we did not do any differencing for
trend removal. Which estimates are the most precise? For a precise estimate
we want to see a standard error that is small compared to the magnitude of
the coefficient. With this in mind, comparing the coefficient estimates and
standard errors we see the most precise are ar2, ar3, ar4, ma1, ma3, and
sar1. Which are the least precise? Comparing the coefficient estimates and
standard errors we see the least precise are ar1 and ma2.
> m.boardings <- arima(boardings,
+ order=c(4,0,3),
+ seasonal=list(order=c(1,0,0),period=12))
> m.boardings
Call:
arima(x = boardings, order = c(4, 0, 3), seasonal = list(order = c(1, 0, 0),
period = 12))
Coefficients:
ar1 ar2 ar3 ar4 ma1 ma2 ma3 sar1 intercept
17
0.108 0.595 0.69 -0.534 0.578 -0.059 -0.690 0.899 12.548
s.e. 0.388 0.253 0.14 0.286 0.399 0.443 0.312 0.040 0.079
> plot(residuals(m.boardings))
0.08
0.06
0.04
residuals(m.boardings)
0.02
0.00
−0.02
−0.06
Time
and a histogram
18
Histogram of residuals(m.boardings)
6
5
4
Frequency
3
2
1
0
residuals(m.boardings)
Are the residuals well-behaved? Are they reasonably normal? Are they rea-
sonably independent? We conclude from the histogram that the residuals are
reasonably well-behaved and normal. From the plot, the residuals ’look’ inde-
pendent, but we want some more evidence. We examine the auto-correlation
function of residuals
> acf(residuals(m.boardings))
19
Series residuals(m.boardings)
0.2
0.1
ACF
0.0
−0.1
−0.2
Lag
data: residuals(m.boardings)
W = 0.9722, p-value = 0.132
20
12.8
12.7
12.6
x
12.5
Time
6.2 CO2 Levels In this exercise we use our time series analysis techniques to
model and forcast the levels of CO2 in the atmosphere.
a) Load the data set with
> data(co2)
and plot it with
> plot(co2,col="blue")
21
380
375
370
co2
365
360
355
350
Time
22
380
window(co2, start = c(2001, 1))
375
370
365
Time
c) Define
> months = c('J','F','M','A','J','J','A','S','O','N','D')
23
AS
A AJ O
J J A
J S
380
M
J MA
window(co2, start = c(2001, 1))
AJ F
M
AJ M F
M F
JF A
375
J J N
J O
D F
J
S
370
D
D DJ
N
A N
365
N
O
O
S
Time
24
Series as.vector(co2)
0.8
0.6
0.4
ACF
0.2
0.0
−0.2
0 10 20 30 40
Lag
25
6
4
2
0
diff(co2)
−2
−4
−6
−8
Time
> acf(as.vector(series))
26
Series as.vector(series)
0.2
0.0
ACF
−0.2
−0.4
5 10 15 20
Lag
and
> pacf(as.vector(series))
27
Series as.vector(series)
0.2
0.0
Partial ACF
−0.2
−0.4
5 10 15 20
Lag
Call:
arima(x = co2, order = c(2, 1, 1), seasonal = list(order = c(0, 1, 1), period = 12))
Coefficients:
ar1 ar2 ma1 sma1
0.388 0.270 -1.000 -0.804
s.e. 0.089 0.093 0.061 0.116
28
i) Examine model diagnostics with
> tsdiag(m.co2)
Standardized Residuals
0 1 2 3
−2
Time
ACF of Residuals
0.1
−0.1
5 10 15 20
Lag
0.8
P−values
0.4
0.0
5 10 15 20
Number of lags
data: residuals(m.co2)
W = 0.9827, p-value = 0.0927
Describe the residuals. Are they well-behaved? Are they normal? Residuals
are reasonably well behaved and the Shapiro test affirms normality at the
0.05 level (though not at 0.10 level).
j) Plot predictions 48 months ahead starting in 2004 with 95% confidence in-
tervals with the command
> plot(m.co2,n1=c(2004,1),n.ahead=48,col='blue')
29
390
385
380
x
375
370
Time
6.3 Exchange Rates In this exercise we will analyze the volatility of the the
US dollar to Hong Kong dollar exchange rate.
a) Load the US dollar / Hong Kong dollar data frame.
> data(usd.hkd)
b) Examine the structure of the data frame.
> str(usd.hkd)
30
c) Extract the hkrate component and make it a time series.
> us.hk<-ts(usd.hkd$hkrate)
d) Plot the time series.
> plot(us.hk)
0.15
0.10
0.05
us.hk
0.00
−0.15 −0.10 −0.05
Time
1 6.760625e-04 1.000e+00
2 5.000000e-02 1.000e+00
3 5.000000e-02 1.000e+00
31
2 7 -1.354e+03 2.67e-05 3.18e-05 9.9e-04 2.0e+00 1.0e-04 1.60e+01
3 13 -1.363e+03 6.82e-03 1.10e-02 4.2e-01 2.0e+00 7.2e-02 1.60e+01
4 14 -1.365e+03 1.63e-03 1.95e-03 2.9e-01 2.0e+00 7.2e-02 3.34e-01
5 16 -1.370e+03 3.23e-03 4.34e-03 4.4e-01 2.0e+00 2.0e-01 2.74e-01
6 18 -1.377e+03 5.28e-03 4.52e-03 2.3e-01 9.2e-01 2.0e-01 2.65e-02
7 20 -1.378e+03 1.16e-03 1.14e-03 3.9e-02 2.0e+00 4.3e-02 1.61e+01
8 22 -1.382e+03 2.23e-03 2.33e-03 7.0e-02 2.0e+00 8.5e-02 7.87e+01
9 24 -1.392e+03 7.33e-03 6.04e-03 1.2e-01 2.0e+00 1.7e-01 1.25e+00
10 31 -1.392e+03 1.61e-04 5.86e-04 2.6e-06 7.8e+00 4.3e-06 1.56e-02
11 32 -1.392e+03 3.75e-05 3.01e-05 2.5e-06 2.0e+00 4.3e-06 2.54e-02
12 33 -1.392e+03 1.39e-06 1.07e-06 2.6e-06 2.0e+00 4.3e-06 3.28e-02
13 34 -1.392e+03 5.56e-08 7.96e-08 2.6e-06 2.0e+00 4.3e-06 3.22e-02
14 40 -1.392e+03 1.04e-04 1.52e-04 5.3e-03 2.0e+00 8.9e-03 3.21e-02
15 41 -1.392e+03 8.28e-06 7.43e-06 1.0e-03 0.0e+00 2.2e-03 7.43e-06
16 42 -1.392e+03 2.00e-06 1.32e-06 8.7e-04 0.0e+00 2.0e-03 1.32e-06
17 44 -1.392e+03 9.74e-06 1.05e-05 5.0e-03 7.2e-01 1.1e-02 1.56e-05
18 45 -1.392e+03 1.28e-05 1.34e-05 1.0e-02 4.1e-01 2.1e-02 1.50e-05
19 46 -1.392e+03 4.89e-06 7.79e-06 1.0e-02 3.9e-01 2.1e-02 8.62e-06
20 47 -1.392e+03 -1.96e-07 7.63e-07 3.6e-03 0.0e+00 7.2e-03 7.63e-07
Call:
garch(x = us.hk - mean(us.hk), order = c(1, 1), reltol = 1e-06)
Model:
GARCH(1,1)
Residuals:
Min 1Q Median 3Q Max
-7.7802 -0.3959 0.0251 0.4435 4.4744
Coefficient(s):
32
Estimate Std. Error t value Pr(>|t|)
a0 1.75e-05 5.28e-06 3.32 0.00089 ***
a1 2.47e-01 2.46e-02 10.01 < 2e-16 ***
b1 7.97e-01 1.51e-02 52.82 < 2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Diagnostic Tests:
Jarque Bera Test
data: Residuals
X-squared = 2530, df = 2, p-value <2e-16
Box-Ljung test
data: Squared.Residuals
X-squared = 1.169, df = 1, p-value = 0.28
0.008
0.006
0.004
0.002
0.000
time
33
h) Plot the model residuals with the command
> plot(residuals(m2),col="blue",main="Residuals")
Residuals
4
2
0
residuals(m2)
−2
−4
−6
−8
Time
Do they look normal? Why or why not? The residuals do not appear normal,
with several of the negative values being quite extreme. We expect the
Shapiro test to reject normality.
34
Histogram of residuals(m2)
60
Frequency
40
20
0
−8 −6 −4 −2 0 2 4
residuals(m2)
Is it similar to a normal distribution? Why or why not? The left tail of the
histogram is too thick, indicating negative values that are too extreme to be
normally distributed.
j) Test the model residuals for normality with the command
> shapiro.test(residuals(m2))
data: residuals(m2)
W = 0.855, p-value <2e-16
Is normality accepted or rejected? Does this agree with your thoughts on the
residual plot and histogram above? We see that with a Shapiro test pval of
nearly zero, the null hypothesis of normality is strongly rejected. This agrees
with our thoughts on the plot and histogram of the residuals.
35
> head(taro)
> taro<-taro[c(3,4,6,8,9,10,11,12,13,14)]
> taro$Expiration<-as.Date(taro$Expiration)
> taro$DataDate<-as.Date(taro$DataDate)
> taro$Price<-(taro$Bid+taro$Ask)/2
> taro$Maturity<-as.double(taro$Expiration-taro$DataDate)/365
> head(taro)
36
UnderlyingSymbol UnderlyingPrice OptionRoot Type Expiration
107 TARO 32.88 QTT020720C00035000 call 2002-07-20
DataDate Strike Last Bid Ask Price Maturity
107 2002-03-25 35 2.5 2.4 3.2 2.8 0.320548
> optsub<-subset(taro,Type=='call'
+ & Expiration=='2002-07-20'
+ & DataDate=='2002-03-25'
+ & Strike==35.0)
> optsub$Price
[1] 2.8
> bs<-function(type,S,K,sigma,t,r){
+ d1 <- (log(S/K) + (r+(sigma^2)/2)*t) / (sigma*sqrt(t))
+ d2 <- (log(S/K) + (r-(sigma^2)/2)*t) / (sigma*sqrt(t))
+ if (type=='call') val <- pnorm(d1)*S - pnorm(d2)*K*exp(-r*t)
+ else if (type=='put') val <- pnorm(-d2)*K*exp(-r*t) - pnorm(-d1)*S
+ val
+ }
> secantIV<-function(type,V,S,K,sigma0,sigma1,t,r){
+ newSigma <- sigma0 - (bs(type,S,K,sigma0,t,r)-V)*(sigma0-sigma1)/
+ (bs(type,S,K,sigma0,t,r) - bs(type,S,K,sigma1,t,r))
+ if( abs(newSigma)==Inf ) return(0.0)
+ if( abs(newSigma - sigma0) < .0001 ) return(newSigma)
+ else return(secantIV(type,V,S,K,newSigma,sigma0,t,r))
+ }
> secantIV(optsub$Type,
+ optsub$Price,
+ optsub$UnderlyingPrice,
+ optsub$Strike,0.5,1,
+ optsub$Maturity,0.05)
[1] 0.465278
37