#---------------------------# # Loesungen UeBlatt 11 # #---------------------------# --------------- # Aufgabe 1 # --------------- # we can use the code from week11.txt: ExpWeightedVol = function( d , ret ) { n = length(ret) vol = rep(0,n) volsq = rep(0,n) # vol^2 vol0sq = sd(ret)^2 # we put start vol to bsvol w = 1 - 1/d # the first step is special: volsq[1] = w * vol0sq + (1-w)*ret[1]^2 for(k in 2:n) { volsq[k] = w * volsq[k-1] + (1-w)*ret[k]^2 } vol = sqrt( volsq ) return(vol) } EquallyWeightedVol = function( d , ret ) { n = length(ret) vol = rep(0,n) # we use vectorized calculation logic: 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 # equally weighted vol: vol = sqrt( sum_dretsquared ) return(vol) } spx = read.table("C:/Users/detlef/OneDrive/hochschule/Vorlesungen/WS2223/Datenanalyse-mit-R/SPX.txt",header=TRUE,sep=";") spx = na.omit(spx) 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",main="SPX-returns") # since the time series is quite long, we compare the following # values of d: d = 120, d = 250 and d = 500 d = 120 ewvol = ExpWeightedVol( d , ret ) vol = EquallyWeightedVol( d , ret ) info = "exp weighted (in red) vs. equally weighted vol for SPX, d = " info = paste(info,d) plot(vol,type="l",main=info,ylim=c(0,4/100)) lines(ewvol,col="red") d = 250 ewvol = ExpWeightedVol( d , ret ) vol = EquallyWeightedVol( d , ret ) info = "exp weighted (in red) vs. equally weighted vol for SPX, d = " info = paste(info,d) plot(vol,type="l",main=info,ylim=c(0,4/100)) lines(ewvol,col="red") d = 500 ewvol = ExpWeightedVol( d , ret ) vol = EquallyWeightedVol( d , ret ) info = "exp weighted (in red) vs. equally weighted vol for SPX, d = " info = paste(info,d) plot(vol,type="l",main=info,ylim=c(0,4/100)) lines(ewvol,col="red") # same for GE-series: ge = read.table("C:/Users/detlef/OneDrive/hochschule/Vorlesungen/WS2223/Datenanalyse-mit-R/GE.txt",header=TRUE,sep=";") ge = na.omit(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",main="GE-returns") # since the time series is quite long, we compare the following # values of d: d = 120, d = 250 and d = 500 d = 120 ewvol = ExpWeightedVol( d , ret ) vol = EquallyWeightedVol( d , ret ) info = "exp weighted (in red) vs. equally weighted vol for GE, d = " info = paste(info,d) plot(vol,type="l",main=info,ylim=c(0,6/100)) lines(ewvol,col="red") d = 250 ewvol = ExpWeightedVol( d , ret ) vol = EquallyWeightedVol( d , ret ) info = "exp weighted (in red) vs. equally weighted vol for GE, d = " info = paste(info,d) plot(vol,type="l",main=info,ylim=c(0,6/100)) lines(ewvol,col="red") d = 500 ewvol = ExpWeightedVol( d , ret ) vol = EquallyWeightedVol( d , ret ) info = "exp weighted (in red) vs. equally weighted vol for GE, d = " info = paste(info,d) plot(vol,type="l",main=info,ylim=c(0,6/100)) lines(ewvol,col="red")