Chi Square Plot
Purpose
To use chi square plot to check the bivariate normality between nifty and gold
> library(fPortfolio) > library(xts) > annualized.returns <- function(x) { + n <- length(x) + (x[n] - x[1])/x[1] + } |
> "qqchi2" <- function(da) { + if (!is.matrix(da)) + da = as.matrix(da) + nr = dim(da)[1] + nc = dim(da)[2] + cm = matrix(colMeans(da), 1, nc) + o = matrix(rep(1, nr), nr, 1) + dev = da - kronecker(o, cm) + dev = as.matrix(dev) + s = t(dev) %*% dev/(nr - 1) + si = solve(s) + d2 = sort(diag(dev %*% si %*% t(dev))) + prob = (c(1:nr) - 0.5)/nr + q1 = qchisq(prob, nc) + plot(q1, d2, xlab = "Quantile of chi-square", ylab = "d") + fit = lsfit(q1, d2) + fitted = d2 - fit$residuals + lines(q1, fitted) + rq = cor(q1, d2) + print("correlation coefficient:") + print(rq) + } |
> nifty.data <- read.csv("C:/Cauldron/Benchmark/PMS/Product/dsp/nifty_data.csv", + stringsAsFactors = FALSE, header = T) > nifty.data$trade.date <- as.Date(nifty.data$Date, format = "%d-%m-%Y") > nifty.ts <- as.timeSeries(nifty.data$Nifty, timeDate(nifty.data$trade.date)) > rownames(nifty.ts) <- nifty.data$trade.date > colnames(nifty.ts) <- "nifty" > gold.data <- read.csv("C:/Cauldron/Benchmark/PMS/Product/dsp/gold_data.csv", + stringsAsFactors = FALSE, header = T) > gold.data$trade.date <- as.Date(gold.data$Date, format = "%d-%m-%Y") > gold.data$gold <- gold.data$gold/28.3495231 > gold.ts <- as.timeSeries(gold.data$gold, timeDate(gold.data$trade.date)) > rownames(gold.ts) <- gold.data$trade.date > colnames(gold.ts) <- "gold" > rawdata <- merge(nifty.ts, gold.ts) > rawdata <- rawdata[which(!is.na(rawdata[, 1]) & !is.na(rawdata[, + 2])), ] > rawdata <- na.locf(rawdata) > workdata <- rawdata > dates <- as.Date(rownames(workdata)) > workdata.xts <- xts(workdata[, 1:2], dates) > workdata.ret.roll.y <- cbind(rollapply(workdata.xts[, 1], width = 252, + annualized.returns, align = "right"), rollapply(workdata.xts[, + 2], width = 252, annualized.returns, align = "right")) > workdata.ret.master <- workdata.ret.roll.y > workdata.ret.master <- as.timeSeries(coredata(workdata.ret.master), + timeDate(as.Date(index(workdata.ret.master)))) > head(workdata.ret.master) GMT nifty gold 1996-01-30 -0.2806292 0.2255165 1996-01-31 -0.2709415 0.2373204 1996-02-01 -0.2438914 0.2603405 1996-02-02 -0.1875951 0.2971077 1996-02-05 -0.1519827 0.3471034 1996-02-06 -0.1422257 0.3222130 |
Use mvoutlier package
> library(mvoutlier) > chisq.plot(workdata.ret.master[, c("nifty", "gold")], ask = F) $outliers NULL |
Correlation
> qqchi2(workdata.ret.master[, c("nifty", "gold")]) [1] "correlation coefficient:" [1] 0.994143 |
This is just meeting the critical condition.
Here are four graphs with one year roll, 3 yr cagr, roll 3yr, roll 5y
> par(mfrow = c(2, 2)) > rawdata <- as.timeSeries(coredata(ret.roll.1yr), timeDate(as.Date(index(ret.roll.1yr)))) > chisq.plot((rawdata[, 1:2]), ask = F) $outliers NULL > rawdata <- as.timeSeries(coredata(ret.roll.3yrcagr), timeDate(as.Date(index(ret.roll.3yrcagr)))) > chisq.plot((rawdata[, 1:2]), ask = F) $outliers NULL > rawdata <- as.timeSeries(coredata(ret.roll.3yr), timeDate(as.Date(index(ret.roll.3yr)))) > chisq.plot((rawdata[, 1:2]), ask = F) $outliers NULL > rawdata <- as.timeSeries(coredata(ret.roll.5yr), timeDate(as.Date(index(ret.roll.5yr)))) > chisq.plot((rawdata[, 1:2]), ask = F) $outliers NULL |