R Prep

#rm(list=ls())
#install.packages("quantmod")
#install.packages("psych")
library(quantmod)
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(psych)

Data Acquisition

getSymbols(c("AAPL", "TSLA", "MSFT", "WMT"))
## [1] "AAPL" "TSLA" "MSFT" "WMT"
r.aapl <- dailyReturn(AAPL[,6])
r.tsla <- dailyReturn(TSLA[,6])
r.msft <- dailyReturn(MSFT[,6])
r.wmt  <- dailyReturn(WMT[,6])

r.all <- merge(r.aapl, r.tsla)
r.all <- merge(r.all, r.msft)
r.all <- merge(r.all, r.wmt)
r.all <- na.omit(r.all)

colnames(r.all) <- c("AAPL", "TSLA", "MSFT", "WMT")

Exploratory Data Analysis

TT <- dim(r.all)[1]
CVD <- 2395

describe(r.all)
##      vars    n mean   sd median trimmed  mad   min  max range  skew kurtosis se
## AAPL    1 3517    0 0.02      0       0 0.01 -0.13 0.12  0.25 -0.05     5.34  0
## TSLA    2 3517    0 0.04      0       0 0.03 -0.21 0.24  0.45  0.33     4.91  0
## MSFT    3 3517    0 0.02      0       0 0.01 -0.15 0.14  0.29  0.05     7.79  0
## WMT     4 3517    0 0.01      0       0 0.01 -0.11 0.12  0.23  0.09    16.46  0
describe(r.all[1:CVD-1,])
##      vars    n mean   sd median trimmed  mad   min  max range  skew kurtosis se
## AAPL    1 2394    0 0.02      0       0 0.01 -0.12 0.09  0.21 -0.22     4.62  0
## TSLA    2 2394    0 0.03      0       0 0.02 -0.19 0.24  0.44  0.51     6.29  0
## MSFT    3 2394    0 0.01      0       0 0.01 -0.11 0.10  0.22  0.08     6.57  0
## WMT     4 2394    0 0.01      0       0 0.01 -0.10 0.11  0.21  0.16    16.11  0
describe(r.all[CVD:TT,])
##      vars    n mean   sd median trimmed  mad   min  max range skew kurtosis se
## AAPL    1 1123    0 0.02      0       0 0.02 -0.13 0.12  0.25 0.12     4.99  0
## TSLA    2 1123    0 0.04      0       0 0.03 -0.21 0.20  0.41 0.13     2.97  0
## MSFT    3 1123    0 0.02      0       0 0.01 -0.15 0.14  0.29 0.01     6.78  0
## WMT     4 1123    0 0.01      0       0 0.01 -0.11 0.12  0.23 0.02    14.21  0
cor(r.all)
##           AAPL      TSLA      MSFT       WMT
## AAPL 1.0000000 0.3564952 0.5883106 0.2907337
## TSLA 0.3564952 1.0000000 0.3455202 0.1396734
## MSFT 0.5883106 0.3455202 1.0000000 0.3414911
## WMT  0.2907337 0.1396734 0.3414911 1.0000000
c1 <- cor(r.all[1:CVD-1,])
c2 <- cor(r.all[CVD:TT,])

Rolling Correlation Matrix Plots

# Assuming df is your data frame with N time series
# N is the number of time series, T is the length of each time series

# Parameters
H <- 100  # Number of days for rolling correlation
T <- dim(r.all)[1]
num_series <- ncol(r.all)  # Number of time series

# Calculate rolling correlations
correlations <- matrix(NA, nrow = T, ncol = choose(num_series, 2))

col_counter <- 1
for (i in 1:(num_series - 1)) {
  for (j in (i + 1):num_series) {
    ts1 <- r.all[, i]
    ts2 <- r.all[, j]
    rolling_cor <- rollapplyr(cbind(ts1, ts2), width = H, FUN = function(x) cor(x[,1], x[,2]), by.column = FALSE, align = "right")
    correlations[, col_counter] <- rolling_cor
    col_counter <- col_counter + 1
  }
}

# Plotting
plot_titles <- combn(colnames(r.all), 2, paste, collapse = " vs ")
plot_colors <- rainbow(choose(num_series, 2))

par(mfrow = c(3, 2))  # Adjust rows and columns as needed for your number of time series

for (k in 1:ncol(correlations)) {
  plot(correlations[, k], type = 'l', col = plot_colors[k], xlab = 'Time', ylab = 'Correlation', main = paste("Rolling correlation:", plot_titles[k]))
  legend("bottomright", legend = plot_titles[k], col = plot_colors[k], lty = 1)
}

# Reset plotting parameters
par(mfrow = c(1, 1))  # Reset to default plotting layout

Multivariate GARCH (Assume Independent Errors)

Constant Conditional Correlation Modelling

Dynamic Conditional Correlation Modelling

Applications