FW: [R-sig-finance] correlation between two stock market indices

Ajay Shah ajayshah at mayin.org
Tue Aug 31 06:42:51 CEST 2004


You can go far with short R code fragments, such as :

     library(its)
     x1 <- priceIts(instrument=c("^ftse"), start="1998-01-01", quote = "Close")
     x2 <- priceIts(instrument=c("^gdax"), start="1998-01-01", quote = "Close")
     prices <- intersect(x1,x2)
     names(prices) <- c("FTSE","DAX")
     returns = 100*diff(log(prices))
     cor(returns)

which gives 

          FTSE       DAX
FTSE 1.0000000 0.7455468
DAX  0.7455468 1.0000000

so you see that returns on the FTSE and the DAX have had a correlation
of 0.7455 in the post-1998 period.

I think the above serves as a great demo of R and 'its' in action! :-)

The pitfalls:

  * Timezones matter greatly. If one market closes before another,
    then it will look like one is causing the other, if viewed through
    daily returns.

    So you have to either go 'in' and pick intervals when both markets
    are contemporaneously trading, or you have to zoom 'out' and pick
    fat intervals where the overlaps matter less.

    E.g. India and the US have exactly non-overlapping hours. I find
    it hard to meaningfully interpret correlations of contemporaneous
    or lagged returns of daily data. It makes more sense to discuss
    correlations of weekly data.

  * Be careful to intersect and then compute returns, as done
    above. Be careful to make returns as 100*diff(log(prices)), as
    done above.

  * R TODO:
    The code fragment above works for indexes but not for individual
    stocks, since priceIts does not know how to give us "adjusted
    closing prices". That is, when splits take place, the price gets
    clobbered, and the series generated by priceIts is useless. I
    would be very happy if people on this list are able to propose a
    solution. I think priceIts is fabulous but this is a major gap in
    functionality. Raw closing prices are next to useless since most
    finance starts with returns, and in order to make returns, we need
    adjusted closing prices.

  * Markets are quite efficient for individual products (e.g. index
    futures or stocks) and you don't generally have problems with
    time-series structure. But when you get to indexes, the process of
    making linear combinations of things that trade with different
    timestamps is known to induce suprious autocorrelations. Andy Lo
    and others have papers on this. This problem is particularly acute
    when an index contains illiquid products.

  * Even if you dealt with individual traded products, like stocks or
    index futures, you'd have the problem of time-varying
    correlations.  You can do rolling window correlations and they'll
    help.

    R TODO: 
    There isn't yet a bivariate ARCH implementation in CRAN or in the
    Debian packages.

    R TODO:
    There isn't yet an abstract engine doing rolling window estimation
    in R, where you get to define the estimator (e.g. as is the case
    with 'by' where you get to specify FUN).

-- 
Ajay Shah                                                   Consultant
ajayshah at mayin.org                      Department of Economic Affairs
http://www.mayin.org/ajayshah           Ministry of Finance, New Delhi



More information about the R-sig-finance mailing list