#---------------------------# # Loesungen UeBlatt 10 # #---------------------------# --------------- # Aufgabe 1 # --------------- # we can use the code from week10.txt: # log-Likelihood-Funktion fuer das ARCH(d)-Modell: logL = function( bsvol , w0 , d ) { w1 = 1 - w0 n = length(ret) cumsum_retsquared = cumsum(ret^2) # remove last d elements: cumsum_retsquared_shifted = cumsum_retsquared[-((n-d+1):n)] # fill in d zeros at the first d places: cumsum_retsquared_shifted = c(rep(0,d),cumsum_retsquared_shifted) sum_dretsquared = cumsum_retsquared - cumsum_retsquared_shifted # now we have to divide this sum by d, but the first d elements # are special: divisor1 = 1:d divisor2 = rep(d,n-d) divisor = c(divisor1,divisor2) sum_dretsquared = sum_dretsquared/divisor # now we can calculate ARCH(d)-vol: vol = sqrt( w0*bsvol^2 + w1*sum_dretsquared ) # shift vol-vector by 1 element ( there is a vol(t_{k-1}) in # the likelihood-function, not a vol(t_k) ) vol = vol[-n] # remove last one vol = c(sqrt(w0*bsvol^2),vol) # add a new first one # the actual calculation: terms = log(vol) + 1/2*ret^2/vol^2 result = -sum(terms) return(result) } # load SPX.txt data and generate return vector ret: spx = read.table("C:/Users/detlef/OneDrive/hochschule/Vorlesungen/WS2223/Datenanalyse-mit-R/SPX.txt",header=TRUE,sep=";") head(spx) summary(spx) # no NA's S = spx[,2] n = length(S) ret = rep(0,n) for(i in 2:n) { ret[i] = (S[i]-S[i-1])/S[i-1] } plot(ret,type="l") # returns look ok # compute spx-logL and make setup for contour-plot: # first, we fix bsvol to its natural value: bsvol = sd(ret) bsvol # we make a contour-plot in the (w0,d)-plane: w0 = seq( from=0.05 , to=0.95 , by=0.05 ) dd = 1:40 nw = length(w0) nd = length(dd) # matrix z = logL for contour-plot: z = matrix( 0 , nrow=nd , ncol=nw ) for(i in 1:nd) { for(j in 1:nw ) { z[i,j] = logL( bsvol , w0[j] , dd[i] ) } } # let's look at the result: contour( dd , w0 , z ) contour( dd , w0 , z , nlevels = 50 ) contour( dd , w0 , z , nlevels = 80 , zlim=c(70000,72000) ) # apparently w0 less than 0.3 and d between 8 and 28: w0 = seq( from=0.01 , to= 0.3 , by=0.005 ) dd = 8:28 # reevaluate z = logL with the above code: nw = length(w0) nd = length(dd) z = matrix( 0 , nrow=nd , ncol=nw ) for(i in 1:nd) { for(j in 1:nw ) { z[i,j] = logL( bsvol , w0[j] , dd[i] ) } } contour( dd , w0 , z , nlevels = 100 , zlim=c(70000,72000) ) contour( dd , w0 , z , nlevels = 200 , zlim=c(71000,72000) ) # -> d = 14 and w0 around 0.15 max(z) logL( bsvol=sd(ret) , w0=0.15 , d=14 ) # das passt.. # some more pictures: persp( dd , w0 , z ) require(plot3D) persp3D( dd , w0 , z ) # finally we check for bsvol: w0 = seq( from=0.01 , to=0.3 , by=0.005 ) bsvol = seq( from=0.1/100 , to=2/100 , by=0.05/100 ) nbs = length(bsvol) nw = length(w0) z = matrix( 0 , nrow=nbs , ncol=nw ) for(i in 1:nbs) { for(j in 1:nw ) { z[i,j]=logL( bsvol[i] , w0[j] , d=14 ) } } contour( bsvol , w0 , z ) contour( bsvol , w0 , z , nlevels = 50) contour( bsvol , w0 , z , nlevels = 100 , zlim=c(70000,72000) ) contour( bsvol , w0 , z , nlevels = 200 , zlim=c(71000,72000) ) # thus: Maximum at approximately logL( bsvol=sd(ret) , w0=0.15 , d=14 ) max(z) # okay.. sd(ret) # same analysis for GE.txt data: # load GE.txt data and generate return vector ret: ge = read.table("C:/Users/detlef/OneDrive/hochschule/Vorlesungen/WS2223/Datenanalyse-mit-R/GE.txt",header=TRUE,sep=";") head(ge) summary(ge) S = ge[,2] n = length(S) ret = rep(0,n) for(i in 2:n) { ret[i] = (S[i]-S[i-1])/S[i-1] } plot(ret,type="l") # returns look ok # compute GE-logL and make setup for contour-plot: # first, we fix bsvol to its natural value: bsvol = sd(ret) bsvol # we make a contour-plot in the (w0,d)-plane: w0 = seq( from=0.05 , to=0.95 , by=0.05 ) dd = 1:40 nw = length(w0) nd = length(dd) # matrix z = logL for contour-plot: z = matrix( 0 , nrow=nd , ncol=nw ) for(i in 1:nd) { for(j in 1:nw ) { z[i,j] = logL( bsvol , w0[j] , dd[i] ) } } # let's look at the result: contour( dd , w0 , z ) contour( dd , w0 , z , nlevels = 50 ) contour( dd , w0 , z , nlevels = 70 , zlim=c(50000,51000) ) # apparently w0 less than 0.3 and d between 10 and 50: w0 = seq( from=0.01 , to= 0.3 , by=0.005 ) dd = 10:50 # reevaluate z = logL with the above code: nw = length(w0) nd = length(dd) z = matrix( 0 , nrow=nd , ncol=nw ) for(i in 1:nd) { for(j in 1:nw ) { z[i,j] = logL( bsvol , w0[j] , dd[i] ) } } contour( dd , w0 , z , nlevels = 70 , zlim=c(50000,51000) ) contour( dd , w0 , z , nlevels = 100 , zlim=c(50500,51000) ) # -> d = 29 and w0 around 0.15 max(z) logL( bsvol=sd(ret) , w0=0.15 , d=29 ) # das passt.. # some more pictures: persp( dd , w0 , z ) persp3D( dd , w0 , z ) # finally we check for bsvol: w0 = seq( from=0.01 , to=0.3 , by=0.005 ) bsvol = seq( from=0.5/100 , to=2.5/100 , by=0.05/100 ) nbs = length(bsvol) nw = length(w0) z = matrix( 0 , nrow=nbs , ncol=nw ) for(i in 1:nbs) { for(j in 1:nw ) { z[i,j]=logL( bsvol[i] , w0[j] , d=29 ) } } contour( bsvol , w0 , z ) contour( bsvol , w0 , z , nlevels = 50) contour( bsvol , w0 , z , nlevels = 100 , zlim=c(50500,51000) ) # thus: Maximum at approximately logL( bsvol=sd(ret) , w0=0.15 , d=29 ) max(z) sd(ret)