#--------------------------# # Loesungen UeBlatt 8 # #--------------------------# # Aufgabe 1 # # 1a) f = function(x,y) { w = sqrt(x^2+y^2) if(w==0) { res = 1 } # lim_{x->0} sin(x)/x = 1 else { res = sin(w)/(w) } return(res) } # 1b) x = seq(-20,20,by=0.2) y = x nx = length(x) ny = length(y) z = matrix( 0 , nrow=nx , ncol=ny ) for(i in 1:nx) { for(j in 1:ny) { z[i,j] = f(x[i],y[j]) } } persp(x,y,z) require(plot3D) persp3D(x,y,z) # 1c) contour(x,y,z) contour(x,y,z,nlevels=100) # Aufgabe 2 # # we can use the code from week8.txt: # log-Likelihood-Funktion fuer das ARCH(1)-Modell: logL = function( bsvol , w0 ) { w1 = 1 - w0 vol = sqrt(w0*bsvol^2+w1*ret^2) # man kann den Vektor ret benutzen, obwohl # der nicht als Argument uebergeben wurde # shift vol by 1 element: n = length(ret) vol = vol[-n] # remove last one vol = c(bsvol,vol) # add a new first one 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/SS2025/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: bsvol = seq( from=0.1/100 , to=4/100 , by=0.1/100 ) w0 = seq( from=0.05 , to=0.95 , by=0.05 ) 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] ) } } # let's look at the result: contour( bsvol , w0 , z ) contour( bsvol , w0 , z , nlevels = 50 ) contour( bsvol , w0 , z , nlevels = 50 , zlim=c(60000,80000) ) contour( bsvol , w0 , z , nlevels = 100 , zlim=c(60000,80000) ) # we take a more refined look at the region w0 = 0.5 to 0.9 # and bsvol = 0.5% to 1.5%: bsvol = seq( from=0.5/100 , to=1.5/100 , by=0.02/100 ) w0 = seq( from=0.5 , to=0.9 , by=0.01 ) 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] ) } } contour( bsvol , w0 , z , nlevels = 100 , zlim=c(60000,80000) ) contour( bsvol , w0 , z , nlevels = 100 , zlim=c(69000,70000) ) max(z) logL(sd(ret),0.69) sd(ret) # thus: Maximum approximately at bsvol = sd(ret) = 0.97% and w0 = 0.69 # same analysis for GE.txt data: # load GE.txt data and generate return vector ret: ge = read.table("C:/Users/detlef/OneDrive/hochschule/Vorlesungen/SS2025/Datenanalyse-mit-R/GE.txt",header=TRUE,sep=";") head(ge) summary(ge) # no NA's 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: bsvol = seq( from=0.1/100 , to=4/100 , by=0.1/100 ) w0 = seq( from=0.05 , to=0.95 , by=0.05 ) 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] ) } } # let's look at the result: contour( bsvol , w0 , z ) contour( bsvol , w0 , z , nlevels = 50 ) contour( bsvol , w0 , z , nlevels = 50 , zlim=c(40000,50000) ) # we take a more refined look at the region w0 = 0.5 to 0.9 # and bsvol = 1.0% to 2.0%: bsvol = seq( from=1.0/100 , to=2.0/100 , by=0.02/100 ) w0 = seq( from=0.5 , to=0.9 , by=0.01 ) 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] ) } } contour( bsvol , w0 , z , nlevels = 50 , zlim=c(40000,50000) ) contour( bsvol , w0 , z , nlevels = 100 , zlim=c(49500,50000) ) max(z) logL(sd(ret),0.68) sd(ret) # thus: Maximum approximately at bsvol = sd(ret) = 0.97% and w0 = 0.69