phenance.com
Stock Data Pages | TCA League Tables | About

Analyzing Bitcoin in R - Part 14 - Copulas and extreme values, many asssets

Pub: 04.12.17

| By Anonymous

In Finance.

Tags: coding r bitcoin analysis .

We can also do the previous exercise for more than two assets:

returns <- cbind(btcnret, tslaret, nvdaret, amdret)
returns <- head(returns, -1)
colnames(returns) <- c('BitcoinUSD', 'TSLA', 'NVDA', 'AMD')
returns <- as.timeSeries(returns)
Matrix <- matrix(c(4, 3, 2, 1,
                   0, 3, 2, 1,
                   0, 0, 2, 1,
                   0, 0, 0, 1), 4, 4)
selectedCopula <- RVineCopSelect(pobs(as.matrix(returns)), Matrix = Matrix, familyset = NA)
selectedCopula
png(filename = "btc_plot_multistock_contour.png")
contour(selectedCopula)
dev.off()
png(filename = "btc_plot_multistock_returns.png")
pairs2(returns)
dev.off()
png(filename = "btc_plot_multistock_simulated.png")
pairs2(RVineSim(500, selectedCopula))
dev.off()

Bitcoin and Stocks Copula Contour Plot Bitcoin and Stocks Actual Returns Bitcoin and Stocks Simulated Probabilities

Some more diagnostics:

png(filename = "btc_plot_multistock_copuladata.png")
pairs(as.copuladata(pobs(as.ts(returns))))
dev.off()
TauMatrix(returns)
            BitcoinUSD        TSLA      NVDA        AMD
BitcoinUSD  1.00000000 -0.06810811 0.0609009 0.02918919
TSLA       -0.06810811  1.00000000 0.1776577 0.17189189
NVDA        0.06090090  0.17765766 1.0000000 0.29369369
AMD         0.02918919  0.17189189 0.2936937 1.00000000

Bitcoin and Stocks Copula Data

Probability of integral transforms:

RVM <- RVineStructureSelect(pobs(as.matrix(returns)))
pit <- RVinePIT(pobs(as.matrix(returns)), RVM)
png(filename = "btc_plot_multistock_pit.png")
pairs2(pit)
dev.off()
cor(pit, method = "kendall")
            [,1]         [,2]         [,3]         [,4]
[1,]  1.00000000 -0.042162162  0.029909910  0.021981982
[2,] -0.04216216  1.000000000 -0.047927928 -0.003963964
[3,]  0.02990991 -0.047927928  1.000000000 -0.002522523
[4,]  0.02198198 -0.003963964 -0.002522523  1.000000000

Bitcoin and Stocks PIT

n <- 300
simdata <- RVineSim(n, RVM)
outsimm <- matrix(NA, n, 4)
colnames(outsimm) <- colnames(returns)
for (i in 1:4) {
    outsimm[,i] <- qnorm(simdata[,i], mean(returns[,i]), sd(returns[,i]))
}
png(filename = "btc_plot_multistock_rvinesim.png")
pairs2(outsimm)
dev.off()

Bitcoin and Stocks RVineSim

Using this setup, we can get even a 10 percent allocation in the portfolio with three other stocks.

outsimm <- as.timeSeries(outsimm)
out <- minvariancePortfolio(outsimm, constraints = "LongOnly")
store <- out@portfolio@portfolio$weights
out <- maxratioPortfolio(outsimm, constraints = "LongOnly")
store <- rbind(store, out@portfolio@portfolio$weights)
rownames(store) <- c('CopulaMinVar', 'CopulaMaxRat')
store
             BitcoinUSD      TSLA      NVDA        AMD
CopulaMinVar 0.03054926 0.2283597 0.6663010 0.07479008
CopulaMaxRat 0.10565250 0.3449800 0.5493675 0.00000000

And that's it! Now you know how R works and what kind of analysis is possible on Bitcoin. This is just a start. There are tons of other perspectives you could take.

Back to the beginning of this series: Bitcoin analysis with R

You can also jump to each section directly from here: