Skip to content

making sense of 100's of funds

2 messages · Adrian Trapletti, Gabor Grothendieck

#
My two cents come in the form of an R-script:

# Germany vs USA
dax <- get.hist.quote(instrument = "^gdaxi", start = "1980-01-01", quote 
= "Close")
sp <- get.hist.quote(instrument = "^gspc", start = "1980-01-01", quote = 
"Close")

# Synchronize data
x <- merge(dax, sp, all=F)

# Avoid synchronization problems due to trading in different time zones
dax <- embed(as.vector(diff(log(x[,1]))), 5)
dax <- apply(dax, 1, sum)

sp <- embed(as.vector(diff(log(x[,2]))), 5)
sp <- apply(sp, 1, sum)

# Compute running correlation with a quaterly window
cov <- apply(embed(sp*dax, 60), 1, mean)
s1 <- sqrt(apply(embed(dax^2, 60), 1, mean))
s2 <- sqrt(apply(embed(sp^2, 60), 1, mean))
cor <- cov/(s1*s2)
idx <- 1:length(cor)

# Plot running correlation over time and loess average
# Correlation increases over time which is not surprising
# with today's levels of globalization
plot(idx, cor, type="l")
lines(idx, predict(loess(cor~idx)), col="green")

# Plot running correlation vs running volatility and loess average
# Correlation is higher during volatile periods, i.e., during market 
corrections
plot(s1, cor)
lines(s1, predict(md <- loess(cor~s1)), col="green")

# Plot running correlation vs running volatility and loess average
# Correlation is higher during volatile periods, i.e., during market 
corrections
plot(s2, cor)
lines(s2, predict(loess(cor~s2)), col="green")

# Correlation increase over time is somewhat down-biased in the past few 
years
# as the past few years exhibited exceptionally low volatility
# Plot running correlation over time with a "bias-corrected" running 
correlation
# correcting for different levels of volatility
ccor <- pmin(1, cor+(cor-predict(md, s1)))

plot(idx, cor, type="l", ylim=c(-1, 1))
lines(idx, ccor, col="blue")
lines(idx, predict(loess(ccor~idx)), col="green")

 From my point of view, the only way to find true diversification is 
investing in different sectors such as bonds or real estate or investing 
in different strategies, e.g., hedge funds being long volatility (but 
also in these examples correlations increased in the past few years, and 
it is difficult to find true diversification).

Best
Adrian

  
    
#
Just wanted to point out that some of these calculations could be streamlined
using facilities in zoo and dyn.   In particular we use rollmean instead of
embed and apply and we can directly plot the zoo series using plot.zoo.
Also using
  p <- dyn$loess(...)
the result becomes a dyn object so that predict(p) retains its zoo class.
(dyn does not know anything about the modelling function and simply
assumes that the it, loess in this case, works like lm and this is sufficiently
true for this example.)

There may be some small difference between what we have below and the
original since I did this quickly and did not take care that it be absolutely
identical.

library(tseries)
library(dyn)

dax <- get.hist.quote("^gdaxi", start = "1980-01-01", quote = "Close")
sp  <- get.hist.quote("^gspc",  start = "1980-01-01", quote = "Close")

x <- merge(dax, sp, all = FALSE)

x5 <- rollmean(diff(log(x)), 5)

s1 <- sqrt(rollmean(x5[,1] ^2, 60))
s2 <- sqrt(rollmean(x5[,2] ^2, 60))
Cov <- rollmean(x5[,1] * x5[,2], 60)
Cor <- Cov / (s1 * s2)
tt <- unclass(time(Cov))

opar <- par(mfrow = c(2,2))

plot(Cor)
tt <- as.numeric(time(Cov))
lines(p <- predict(dyn$loess(Cor ~ tt)), col = "green")

plot(s1, Cor)
lines(s1, predict(md <- dyn$loess(Cor ~ s1)), col = "green")

plot(s2, Cor)
lines(s2, predict(dyn$loess(Cor ~ s2)), col = "green")

ccor <- pmin(Cor+(Cor-predict(md)), 1)
plot(merge(Cor, ccor, p), col = c(1, 3, 4), plot.type = "single")

par(opar)
On 8/22/07, Adrian Trapletti <a.trapletti at swissonline.ch> wrote: