Tietokoneharjoitus 6

Tehtävä 5

```# (a) Lasketaan tuotot

file<-"http://cc.oulu.fi/~jklemela/stafin/sp500-ndx100.txt"
sp500<-data[,1]
ndx100<-data[,2]

pituus<-length(sp500)
r.sp500<-sp500[2:pituus]/sp500[1:(pituus-1)]
plot(r.sp500,type="l")

r.ndx100<-ndx100[2:pituus]/ndx100[1:(pituus-1)]
plot(r.ndx100,type="l")

250*mean(r.sp500-1)
sqrt(250)*sd(r.sp500)
sqrt(250)*mean(r.sp500-1)/sd(r.sp500)

#[1] 0.09905956
#[1] 0.1837502
#[1] 0.5390991

250*mean(r.ndx100-1)
sqrt(250)*sd(r.ndx100)
sqrt(250)*mean(r.ndx100-1)/sd(r.ndx100)

[1] 0.1604425
[1] 0.2712226
[1] 0.5915528

# (c) Lasketaan gamma, jolle Markowitzin kriteeri suosittelee Nasdaqia.

gammat<-seq(0.1,10,0.1)

M.sp500<-matrix(0,length(gamma),1)
mu1<-mean(r.sp500-1)
sigma1<-sd(r.sp500)
for (i in 1:length(gammat)) M.sp500[i]<-mu1-gammat[i]/2*sigma1^2

M.ndx100<-matrix(0,length(gammat),1)
mu2<-mean(r.ndx100-1)
sigma2<-sd(r.ndx100)
for (i in 1:length(gammat)) M.ndx100[i]<-mu2-gammat[i]/2*sigma2^2

matplot(gammat,M.ndx100)

2*(mu1-mu2)/(sigma1^2-sigma2^2)

# [1] 3.084759

# (d) Lasketaan gamma, jolle utiliteetin odotusarvo suosittelee Nasdaqia.

gammat<-seq(2,4,0.01)
u1<-matrix(0,length(gammat),1)
u2<-matrix(0,length(gammat),1)
for (i in 1:length(gammat)){
gamma<-gammat[i]
uut<-r.sp500^(1-gamma)/(1-gamma)
u1[i]<-mean(uut)
uut<-r.ndx100^(1-gamma)/(1-gamma)
u2[i]<-mean(uut)
}
matplot(gammat,u2,type="l")