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()
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
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
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()
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:
- Introduction to Bitcoin analysis with R
- Retrieving Bitcoin transaction data
- Part 2 - Reading the bitcoin data in to R
- Using the xts package and dates
- Using xts to summarize Bitcoin transaction data
- Setting up Bitcoin data in OHLC format
- Charting Bitcoin data
- Prliminary return analysis with plots
- Preliminary return analysis with rolling windows
- Technical analysis plots of Bitcoin
- Bitcoin's future price path
- Evaluating a portfolio
- Evaluating a stock portfolio
- Copulas and extreme values with Bitcoin
- Copulas and extreme value, many assets