Stockholms Univ., Statistiska Inst. Finansiell Statistik, GN, 7,5 hp, HT2008 Numeriska svar till övningar Nicklas Pettersson 1 Övningslektion6 Tidsserier, Beslutsteori 1.1 Kapitel 18 51) a) slumpmässig b) trend eller cykel c) säsong d) slumpmässig e) trend 53) year <- 1965:1989 TNP <- c(2257.8,2359.1,2421.5,2485.2,2569.6,2606.2,2607.6,2674.4, 2760.8,2783.4,2699.9,2753.7,2836.9,2961.9,3027.2,3060.4,3089.9, 3092.7,3165.1,3329.3,3414.1,3489.9,3581.6,3659.5,3709.8) plot(year,tnp); lines(year,tnp) 54) Samma som det sista värdet i MA-serien. by 1990 = 3650; 3; by 1991 = 3650; 3 ma3 <- lter(tnp,rep(1,3))/3 max(ma3,na.rm=t) 1
55) by 1990 = 3662; 261; by 1991 = 3718:135 q55 <- lm(tnp ~year) q55_1 <- q55$coe cients[1]+q55$coe cients[2]*1990 q55_2 <- q55$coe cients[1]+q55$coe cients[2]*1991 56) Stationär serie av första di erenser. by 1990 = 3784; 824; by 1991 = 3866; 751 di TNP <- TNP[-1] - TNP[1:24] q56_ <- arima(di TNP,c(1,0,0)) TNPhat <- TNP TNPhat <- c(tnphat,as.numeric(tnphat[25]+q56_$coef[2]+q56_$coef[1]* (TNPhat[25]-TNPhat[24]) )) TNPhat <- c(tnphat,as.numeric(tnphat[26]+q56_$coef[2]+q56_$coef[1]*(tnphat[26]- TNPhat[25]) )) TNPhat[26:27] 57) year <- 1965:1991 q5456 <- data.frame(c(tnp,rep(max(ma3,na.rm=t),2)),c(tnp,q55_1,q55_2),tnphat) plot(year[20:27],q5456[20:27,1],type="l",xlab="year",ylab="income",ylim=c(3000,max(q5456))) lines(year[20:27],q5456[20:27,2],lty=2) lines(year[20:27],q5456[20:27,3],lty=3) points(year[20:27],q5456[20:27,1]) points(year[20:27],q5456[20:27,2]) points(year[20:27],q5456[20:27,3]) legend(1984,3250,c("moving average","regression","autoregressive"),lty=c(1,2,3)) MA tar ingen hänsyn till trend och linjär regression är "för global" och tar ingen hänsyn till beroendet mellan observationerna. 2
1.2 Kapitel 21 2) Maximin-kriteriet ger 10h med maximalt 90poäng. 3) Minimax-kriteriet ger att 10h minimerar regret. H A L 0 30 40 30 5 10 20 10 10 0 0 0 4) a) P(Hj0)=0,2; P(Hj5)=0,2; P(Hj10)=0,4 b) P(0jH)=0,25; P(5jH)=0,25; P(10jH)=0,5 5) EMV (0h) = 41; EMV (5h) = 62; EMV (10h) = 82. 6) Konkav kurva ) Riskavert study 0h study 5h study 10h x 60 40 30 80 60 50 90 80 60 8) a) P(x) 0; 2 0; 5 0; 3 0; 2 0; 6 0; 2 0; 4 0; 5 0; 1 U(x) 9; 5 5 2 11 9; 5 7; 5 12 11 9; 5 E[U(x)] 5 9; 4 11; 25 Maximerar E[U(x)] för 10h. b) Riskavert, riskneutral och risktagare de nieras som U[E(x)] > E[U(x)]; U[E(x)] = E[U(x)] och U[E(x)] < E[U(x)]. För att pröva om de angivna nyttorna härrör från en riskavert person kan vi pröva om U[E(x)] > E[U(x)] verkligen är uppfyllt. I 8)a) har vi ju räknat fram 3
E[U(x)]. Vi kan notera att E[U(x)] =5 motsvarar U(x = 40), E[U(x)] =9:4 nästan motsvarar U(x = 60), samt E[U(x)] =11:25 nästan motsvarar U(x = 80). Vi kan då konstatera att eftersom U(x = 41) U(x = 40) (i detta fall kan med goda skäl antas att U(x = 41) > U(x = 40) eftersom kurvan verkar ständigt växande) så gäller att U[E(40)] = E[U(41)] ) U[E(41)] > E[U(41)], dvs riskaversion. Vi skulle också kunna försöka att skatta nyttofunktionen, t ex som U[E(x)] = + log(x) + U[E(x)] = + 1 x + 2 x 2 + U[E(x)] = + 1 x + 2 x 2 + 3 x 3 + och sedan utifrån denna skatta nyttor U[E(x)]. d Jag väljer att använda den tredje varianten där x, x 2 och x 3 ingår. Vi får då till exempel att U[E(40)] d = 5; 25 (dvs skapligt nära det faktiska 5) och U[E(41)] d = 5; 54. En annan variant är att interpolera (dvs att anta ett strikt linjärt samband) mellan de kända värdena av nyttofunktionen. Eftersom vi vet att U[E(40)] < U[E(41)] < U[E(60)] så kan vi interpolera U[E(41)] som U[E(40)] 9; 5 1 20 = 5; 225 > 5. 60 41 60 40 Nedan ges R-koden för att beräkna uppgifterna på kapitel 21. + U[E(60)] 41 40 60 40 = 5 19 20 + #q2 data <- c(60,40,30,80,60,50,90,80,60) q2data <- matrix(data,3,3) # put data in a matrix rownames(q2data) <- c("high","average","low") # set names to "rows" q2data <- t(q2data) # Rotate matrix so that rows become columns rownames(q2data) <- c("0 h","5 h","10 h") # set names to rows q2 <- NULL # De ne a vector q2 for (i in 1:3) { # Use a loop to calculate maximin for each row q2[i] <- min(q2data[i,]) } names(q2) <- c("0 h","5 h","10 h") q2 # Plot the result #q3 q3 <- matrix(na,3,3) # de ne an empty matrix for (i in 1:3) { # use loop to calculate minimax q3[,i] <- max(q2data[,i])-q2data[,i] } rownames(q3) <- c("0 h","5 h","10 h") # set row names colnames(q3) <- c("high","average","low") # set column names q3 # plot the result #q4 data2 <- c(0.2,0.5,0.3,0.2,0.6,0.2,0.4,0.5,0.1) 4
q4data <- matrix(data2,3,3) # Put data in a matrix rownames(q4data) <- c("high","average","low") # "row" names q4data <- t(q4data) # transpose matrix rownames(q4data) <- c("0 h","5 h","10 h") # row names q4a <- q4data[,1] # put the result in q4a q4a # plot result q4b <- NULL # add empty vector q4b for (i in 1:3) { # loop to calculate probability q4b[i] <- (q4data[i,1]/3) / (sum(q4data[,1])/3) } q4b # plot the result #q5 q5 <- rowsums(q2data*q4data) # calculate EMV q5 # plot the result #q6 score <- sort(unique(data)) # the unique x-values (sorted) are the score utility <- c(2,5,7.5,9.5,11,12) # put utilities in a vector plot(score,utility) # plot the result lines(score,utility) # add a line #q8 q8data <- matrix( c(data,data2,c(9.5,5,2,11,9.5,7.5,12,11,9.5),rep(na,9),rep(na,9)),9,5) # add data in a matrix colnames(q8data) <- c("x","p(x)","u(x)","e[u(x)]","u(e[x])") # set column names q8rowname <- NULL # add vector for rownames for (i in 1:3) { # loop to produce rownames, including both study length and outcome for (j in 1:3) { q8rowname[3*i+j-3] <- paste( rownames(q2data)[i], ",", colnames(q2data)[j] ) }} rownames(q8data) <- q8rowname # use q8rowname as rownames for q8data q8data # plot the result q8data[,4] <- q8data[,2]*q8data[,3] # calculate E[U(x)] and put it in column 4 q8data # plot the result q8 <- NULL # de ne vector q8 for (i in 1:3) { # loop to calculate U(E[]) q8[i] <- sum(q8data[(i*3-2):(i*3),4]) } names(q8) <- names(q2) # use names from q2 on q8 q8 # plot the result 5
# This is just an approximation to the utilityfunction so the values will di er somewhat xmatrix <- matrix(c(q8data[,1],q8data[,1]^2,q8data[,1]^3),c(9,3)) # de ne a matrix including x, x^2 and x^3 to use as explanatory variables model <- lm(q8data[,3]~xmatrix) # linear regression with U(E(x)) as dependent and xmatrix as independent variables q8data[,5] <- round( model$coe [1]+model$coe [2]*q8data[,1]+model$coe [3]*q8data[,1]^2+model$coe [4 # calculate U(E[]) at put it in q8data q8data # plot the result q8_ <- NULL # predicted values of U(E[]) for values 40, 60 and 80 using the estimated model q8_[1] <- round( model$coe [1]+model$coe [2]*40+model$coe [3]*40^2+model$coe [4]*40^3,2) q8_[2] <- round( model$coe [1]+model$coe [2]*60+model$coe [3]*60^2+model$coe [4]*60^3,2) q8_[3] <- round( model$coe [1]+model$coe [2]*80+model$coe [3]*80^2+model$coe [4]*80^3,2) q8 <- NULL # predicted values of U(E[]) for values 41, 62 and 82 using the estimated model q8 [1] <- round( model$coe [1]+model$coe [2]*q5[1]+model$coe [3]*q5[1]^2+model$coe [4]*q5[1]^3,2) q8 [2] <- round( model$coe [1]+model$coe [2]*q5[2]+model$coe [3]*q5[2]^2+model$coe [4]*q5[2]^3,2) q8 [3] <- round( model$coe [1]+model$coe [2]*q5[3]+model$coe [3]*q5[3]^2+model$coe [4]*q5[3]^3,2) q8matrix <- NULL # de ne matrix q8matrix <- rbind(q8,c(5,9.5,11),q8_,q8 ) # row bind the results in matrix rownames(q8matrix) <- c("e[u(x)], actual ","E[U(x)], x=c(40,60,80) ", "U^(E[x]), x=c(40,60,80) ", "U^(E[x]), x=c(41,62,82) ") # set row names q8matrix # plot the result 6