commodity classes & equity index

get started

load libraries

options(digits = 4, width = 70)
library(zoo)
library(tseries)
library(multicore)

load data

returns.df = read.csv(file = "~/Documents/annual/returnsCME.csv", sep = ";", 
    header = TRUE, stringsAsFactors = FALSE)
returns.df[is.na(returns.df)] = 0
returns.z = zooreg(data = returns.df[, -1], start = c(1992, 41), end = c(2011, 
    12), frequency = 52)
grains.z = returns.z[, c("corn", "oats", "wheat", "rice", "soybeans", "soymeal", 
    "soyoil")]
soft.z = returns.z[, c("cocoa", "coffee", "cotton", "sugar", "lumber", 
    "orange")]
live.z = returns.z[, c("feeder", "live", "pork", "hogs")]
metals.z = returns.z[, c("copper", "gold", "palladium", "platinum", "silver")]
energy.z = returns.z[, c("crude", "gasoline", "heating", "natural", "electricity")]
SP.prices = get.hist.quote(instrument = "^GSPC", start = "1992-10-02", 
    end = "2011-03-31", quote = "AdjClose", provider = "yahoo", origin = "1970-01-01", 
    compression = "w", retclass = "zoo")
SP.z = diff(log(SP.prices))
SP.z = zooreg(SP.z, start = c(1992, 41), frequency = 52)

load functions

wget.and.source <- function(url) {
    fname <- tempfile()
    download.file(url, fname, method = "wget")
    source(fname)
    unlink(fname)
}
wget.and.source("https://gist.github.com/bautheac/b2e8269868a881b66a69/raw/")

classes

build classes as equally weighted portfolios of corresponding commo

rollEWGrains = getRollEW(grains.z, 52)
rollEWSofts = getRollEW(soft.z, 52)
rollEWLives = getRollEW(live.z, 52)
rollEWMetals = getRollEW(metals.z, 52)
rollEWEnergy = getRollEW(energy.z, 52)

agregate classes

classesReturns.z = merge.zoo(rollEWGrains[, 1], rollEWSofts[, 1], rollEWLives[, 
    1], rollEWMetals[, 1], rollEWEnergy[, 1])
colnames(classesReturns.z) = c("grains", "softs", "lives", "metals", "energy")

plot classes

individual classes

plotEqualWeightPortfolio(data = rollEWGrains, commo = "grains", width = 52)

plot of chunk plot individual classes

plotEqualWeightPortfolio(data = rollEWSofts, commo = "softs", width = 52)

plot of chunk plot individual classes

plotEqualWeightPortfolio(data = rollEWLives, commo = "lives", width = 52)

plot of chunk plot individual classes

plotEqualWeightPortfolio(data = rollEWMetals, commo = "metals", width = 52)

plot of chunk plot individual classes

plotEqualWeightPortfolio(data = rollEWEnergy, commo = "energy", width = 52)

plot of chunk plot individual classes

all classes

title = "52-week rolling mu for commo classes"
par(mar = c(2, 4, 2, 1))
plot(classesReturns.z, plot.type = "single", main = title, ylab = "mu", 
    xlab = "", col = c("orange", "blue", "red", "green", "yellow"), lwd = 1)
abline(h = 0)
legend(x = "bottomleft", legend = c(colnames(classesReturns.z)), col = c("orange", 
    "blue", "red", "green", "yellow"), lwd = 2)

plot of chunk plot all classes

rolling correlations

comments about rolling correlations

The standard error is computed with analytic approximation here: \( (1-\widehat{\varphi}^2)/\sqrt{t} \) Bootsrap is out of my means. Computational power required!

rollRhosClasses.z = rollingRhos.fun(classesReturns.z, startYear = 1992, 
    startWeek = 41, width = 52)

pairs

pairs(rollRhosClasses.z)
##  [1] "grains,softs"  "grains,lives"  "grains,metals" "grains,energy"
##  [5] "softs,lives"   "softs,metals"  "softs,energy"  "lives,metals" 
##  [9] "lives,energy"  "metals,energy"

plot

plotAllRhos(data = rollRhosClasses.z)

plot of chunk plot rolling correaltions plot of chunk plot rolling correaltions plot of chunk plot rolling correaltions plot of chunk plot rolling correaltions plot of chunk plot rolling correaltions plot of chunk plot rolling correaltions plot of chunk plot rolling correaltions plot of chunk plot rolling correaltions plot of chunk plot rolling correaltions plot of chunk plot rolling correaltions

global portfolio

build commo portfolio as equally weighted portfolio of all classes

rollEWCommo.z = getRollEW(returns.z, 52)

agregate commo portfolio with SP500

commoSPReturns.z = merge.zoo(rollEWCommo.z[, -2], SP.z)
names(commoSPReturns.z) = c("commo", "SP500")

correlation

rollRhosCommoSP.z = rollingRhos.fun(commoSPReturns.z, startYear = 1992, 
    startWeek = 41, width = 52)

plot correlation

plotRollingRhos(data = rollRhosCommoSP.z, pair = "commo,SP500")

plot of chunk plot correlation commo & SP500

More

Returning to the data issue here. There are some very nice functions in a convenient R package for econometrics that allow to cumpute second, third and fourth moments betas. Such statistics give insights about diversification benefits of an asset/asset class. Obviously it would be interesting to compute such estimates and roll them over the period for each commodity, class and for the whole commodity market with respect to an equity index for instance. We could then assess the eventual loss of diversification potential post 2K for example. But I haven't been able to implement such analysis because of data limitation issues. These functions require data be sorted in a very precise fashion. Impossible with the material I have. In other words, for every data the code should be able to pull the quote for the equity index, eventually the commo index as well as the front futures for the observed commo. Data provider required…