Tecnológico de Monterrey
Doctor Teófilo Ozuna
Alumna:
Avril Lobato Delgado A00833113
Librerias
library(ggplot2)
library(dplyr)
library(tidyr)
library(lubridate)
library(purrr)
library(plotly)
library(forecast)
library(readxl)
library(DataExplorer)
library(dplyr)
library(ggplot2)
library(tm)
library(cluster)
library(factoextra)
library(gridExtra)
library(purrr)
library(pROC)
library(rpart)
library(rpart.plot)
library(e1071)
library(ggpubr)
library(dlookr)
library(zoo)
library(caret)
library(stats)
library(tseries)
library(readr)
library(vars)
library(syuzhet)
library(kableExtra)
library(plotly)
library(scales)
library(knitr)
library(rmgarch)
library(devtools)
library(openxlsx)
library(relaimpo)
library(stargazer)
library(RColorBrewer)
library(PerformanceAnalytics)
library(ConnectednessApproach)
library(readr)
library(tseries)
library(forecast)
library(urca)
library(fGarch)
library(MTS)
library(MASS)
library(nortest)
library(outliers)
library(moments)
library(FinTS)
library(WeightedPortTest)
# Function for summary statistics
summary_stats <- function(data) {
return(data.frame(
Mean = mean(data, na.rm = TRUE),
StdDev = sd(data, na.rm = TRUE),
Min = min(data, na.rm = TRUE),
P25 = quantile(data, 0.25, na.rm = TRUE),
P50 = median(data, na.rm = TRUE),
P75 = quantile(data, 0.75, na.rm = TRUE),
Max = max(data, na.rm = TRUE),
Skewness = skewness(data, na.rm = TRUE),
Kurtosis = kurtosis(data, na.rm = TRUE)
))
}
# Calculate statistics and tests for each index
results <- data.frame(
Test = c(
"Mean", "Standard Deviation", "Minimum", "25th Percentile", "Median",
"75th Percentile", "Maximum", "Skewness", "Kurtosis", "Jarque-Bera (p-value)",
"ADF Test (p-value)", "KPSS Test (p-value)", "PP Test (p-value)", "ERS Test",
"ARCH Test (p-value)", "Weighted Portmanteau Test (p-value)", "Q2(20) Test (p-value)",
"Outlier detection (Chen and Liu)","Correlation Matrix and R-squared"
),
AI_INDEX = NA, Fintech_Index = NA, Blockchain_Index = NA
)
# Populate the table with summary stats
stats_ai <- summary_stats(data_selected$AI_INDEX)
stats_fintech <- summary_stats(data_selected$Fintech_Index)
stats_blockchain <- summary_stats(data_selected$Blockchain_Index)
results$AI_INDEX[1:9] <- as.numeric(stats_ai)
results$Fintech_Index[1:9] <- as.numeric(stats_fintech)
results$Blockchain_Index[1:9] <- as.numeric(stats_blockchain)
# Normality and stationarity tests
results$AI_INDEX[10] <- jarque.bera.test(data_selected$AI_INDEX)$p.value
results$Fintech_Index[10] <- jarque.bera.test(data_selected$Fintech_Index)$p.value
results$Blockchain_Index[10] <- jarque.bera.test(data_selected$Blockchain_Index)$p.value
results$AI_INDEX[11] <- adf.test(data_selected$AI_INDEX)$p.value
results$Fintech_Index[11] <- adf.test(data_selected$Fintech_Index)$p.value
results$Blockchain_Index[11] <- adf.test(data_selected$Blockchain_Index)$p.value
results$AI_INDEX[12] <- kpss.test(data_selected$AI_INDEX)$p.value
results$Fintech_Index[12] <- kpss.test(data_selected$Fintech_Index)$p.value
results$Blockchain_Index[12] <- kpss.test(data_selected$Blockchain_Index)$p.value
results$AI_INDEX[13] <- pp.test(data_selected$AI_INDEX)$p.value
results$Fintech_Index[13] <- pp.test(data_selected$Fintech_Index)$p.value
results$Blockchain_Index[13] <- pp.test(data_selected$Blockchain_Index)$p.value
results$AI_INDEX[14] <- ur.ers(data_selected$AI_INDEX, type = "DF-GLS")@teststat
results$Fintech_Index[14] <- ur.ers(data_selected$Fintech_Index, type = "DF-GLS")@teststat
results$Blockchain_Index[14] <- ur.ers(data_selected$Blockchain_Index, type = "DF-GLS")@teststat
# ARCH test
results$AI_INDEX[15] <- ArchTest(data_selected$AI_INDEX)$p.value
results$Fintech_Index[15] <- ArchTest(data_selected$Fintech_Index)$p.value
results$Blockchain_Index[15] <- ArchTest(data_selected$Blockchain_Index)$p.value
# Portmanteau test
results$portmanteau_ai <- Weighted.Box.test(data_selected$AI_INDEX)$p.value
results$portmanteau_fintech <- Weighted.Box.test(data_selected$Fintech_Index)$p.value
results$portmanteau_blockchain <- Weighted.Box.test(data_selected$Blockchain_Index)$p.value
# Q2(20) test
results$AI_INDEX[17] <- Box.test(data_selected$AI_INDEX^2, lag = 20, type = "Ljung-Box")$p.value
results$Fintech_Index[17] <- Box.test(data_selected$Fintech_Index^2, lag = 20, type = "Ljung-Box")$p.value
results$Blockchain_Index[17] <- Box.test(data_selected$Blockchain_Index^2, lag = 20, type = "Ljung-Box")$p.value
# Outlier detection (Chen and Liu)
results$outliers_ai <- sum(scores(data_selected$AI_INDEX, type = "chisq", prob = 0.05) != 0)
results$outliers_fintech <- sum(scores(data_selected$Fintech_Index, type = "chisq", prob = 0.05) != 0)
results$outliers_blockchain <- sum(scores(data_selected$Blockchain_Index, type = "chisq", prob = 0.05) != 0)
# Correlation Matrix and R-squared
cor_matrix <- cor(data_selected[, -1], use = "complete.obs")
r_squared <- cor_matrix^2
print(results)
## Test AI_INDEX Fintech_Index
## 1 Mean 7.284978e-04 2.514532e-04
## 2 Standard Deviation 5.997773e-03 1.886246e-03
## 3 Minimum -1.803901e-02 -6.887190e-03
## 4 25th Percentile -2.658939e-03 -9.810085e-04
## 5 Median 1.024079e-03 2.999540e-04
## 6 75th Percentile 5.001330e-03 1.530050e-03
## 7 Maximum 1.653343e-02 5.406403e-03
## 8 Skewness -3.676255e-01 -6.215113e-02
## 9 Kurtosis 3.031592e+00 3.431835e+00
## 10 Jarque-Bera (p-value) 8.905365e-11 1.789472e-04
## 11 ADF Test (p-value) 1.000000e-02 1.000000e-02
## 12 KPSS Test (p-value) 1.000000e-01 1.000000e-01
## 13 PP Test (p-value) 1.000000e-02 1.000000e-02
## 14 ERS Test -8.226890e+00 -1.240032e+01
## 15 ARCH Test (p-value) 0.000000e+00 0.000000e+00
## 16 Weighted Portmanteau Test (p-value) NA NA
## 17 Q2(20) Test (p-value) 0.000000e+00 0.000000e+00
## 18 Outlier detection (Chen and Liu) NA NA
## 19 Correlation Matrix and R-squared NA NA
## Blockchain_Index portmanteau_ai portmanteau_fintech portmanteau_blockchain
## 1 4.414805e-04 2.791471e-321 0 0
## 2 5.269908e-03 2.791471e-321 0 0
## 3 -2.425996e-02 2.791471e-321 0 0
## 4 -2.095878e-03 2.791471e-321 0 0
## 5 7.566070e-04 2.791471e-321 0 0
## 6 3.584238e-03 2.791471e-321 0 0
## 7 2.091050e-02 2.791471e-321 0 0
## 8 -4.398032e-01 2.791471e-321 0 0
## 9 5.179150e+00 2.791471e-321 0 0
## 10 0.000000e+00 2.791471e-321 0 0
## 11 1.000000e-02 2.791471e-321 0 0
## 12 1.000000e-01 2.791471e-321 0 0
## 13 1.000000e-02 2.791471e-321 0 0
## 14 -1.321070e+01 2.791471e-321 0 0
## 15 0.000000e+00 2.791471e-321 0 0
## 16 NA 2.791471e-321 0 0
## 17 0.000000e+00 2.791471e-321 0 0
## 18 NA 2.791471e-321 0 0
## 19 NA 2.791471e-321 0 0
## outliers_ai outliers_fintech outliers_blockchain
## 1 1890 1974 1904
## 2 1890 1974 1904
## 3 1890 1974 1904
## 4 1890 1974 1904
## 5 1890 1974 1904
## 6 1890 1974 1904
## 7 1890 1974 1904
## 8 1890 1974 1904
## 9 1890 1974 1904
## 10 1890 1974 1904
## 11 1890 1974 1904
## 12 1890 1974 1904
## 13 1890 1974 1904
## 14 1890 1974 1904
## 15 1890 1974 1904
## 16 1890 1974 1904
## 17 1890 1974 1904
## 18 1890 1974 1904
## 19 1890 1974 1904
# Convertir la columna `Date` a tipo Date si está en formato "dd/mm/yyyy"
index_alt$Date <- as.Date(index_alt$Date, format = "%d/%m/%Y")
# Mostrar estructura y resumen
str(index_alt)
## spc_tbl_ [2,051 × 12] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
## $ Date : Date[1:2051], format: "2019-01-08" "2019-01-09" ...
## $ AI_INDEX : num [1:2051] 0.00865 0.00858 0.00851 0.00844 0.00836 ...
## $ Cumulative return...3: num [1:2051] 101 102 103 103 104 ...
## $ Fintech_Index : num [1:2051] -0.000675 -0.000675 -0.000676 -0.000676 -0.000677 ...
## $ Cumulative return...5: num [1:2051] 99.9 99.9 99.8 99.7 99.7 ...
## $ Blockchain_Index : num [1:2051] 0.00205 0.00204 0.00204 0.00203 0.00203 ...
## $ Cumulative return...7: num [1:2051] 100 100 101 101 101 ...
## $ Nasdaq Composite : num [1:2051] 1005 1010 1015 1020 1025 ...
## $ Dax 40 : num [1:2051] 1002 1003 1005 1006 1008 ...
## $ Dow Jones Industrial : num [1:2051] 1003 1007 1010 1014 1017 ...
## $ KFTX Index : num [1:2051] 1012 1023 1029 1028 1027 ...
## $ S&P Financials Index : num [1:2051] 1003 1005 1006 1008 1011 ...
## - attr(*, "spec")=
## .. cols(
## .. Date = col_character(),
## .. AI_INDEX = col_double(),
## .. `Cumulative return...3` = col_double(),
## .. Fintech_Index = col_double(),
## .. `Cumulative return...5` = col_double(),
## .. Blockchain_Index = col_double(),
## .. `Cumulative return...7` = col_double(),
## .. `Nasdaq Composite` = col_double(),
## .. `Dax 40` = col_double(),
## .. `Dow Jones Industrial` = col_double(),
## .. `KFTX Index` = col_double(),
## .. `S&P Financials Index` = col_double()
## .. )
## - attr(*, "problems")=<externalptr>
## Date AI_INDEX Cumulative return...3
## Min. :2019-01-08 Min. :-0.0180390 Min. :100.9
## 1st Qu.:2020-06-03 1st Qu.:-0.0026589 1st Qu.:206.3
## Median :2021-10-29 Median : 0.0010241 Median :275.6
## Mean :2021-10-29 Mean : 0.0007285 Mean :270.1
## 3rd Qu.:2023-03-25 3rd Qu.: 0.0050013 3rd Qu.:332.3
## Max. :2024-08-19 Max. : 0.0165334 Max. :439.8
## Fintech_Index Cumulative return...5 Blockchain_Index
## Min. :-0.0068872 Min. : 91.42 Min. :-0.0242600
## 1st Qu.:-0.0009810 1st Qu.:105.21 1st Qu.:-0.0020959
## Median : 0.0003000 Median :116.09 Median : 0.0007566
## Mean : 0.0002515 Mean :120.99 Mean : 0.0004415
## 3rd Qu.: 0.0015301 3rd Qu.:127.90 3rd Qu.: 0.0035842
## Max. : 0.0054064 Max. :173.73 Max. : 0.0209105
## Cumulative return...7 Nasdaq Composite Dax 40 Dow Jones Industrial
## Min. : 95.04 Min. :1005 Min. : 829.2 Min. : 818.2
## 1st Qu.:136.66 1st Qu.:1435 1st Qu.:1184.8 1st Qu.:1186.1
## Median :168.18 Median :1810 Median :1320.5 Median :1409.5
## Mean :176.21 Mean :1798 Mean :1329.6 Mean :1357.7
## 3rd Qu.:220.23 3rd Qu.:2098 3rd Qu.:1459.5 3rd Qu.:1482.5
## Max. :276.64 Max. :2723 Max. :1743.4 Max. :1757.1
## KFTX Index S&P Financials Index
## Min. : 881.8 Min. : 727.6
## 1st Qu.:1289.6 1st Qu.:1153.5
## Median :1404.2 Median :1371.1
## Mean :1466.7 Mean :1348.0
## 3rd Qu.:1680.7 3rd Qu.:1537.3
## Max. :1925.3 Max. :1811.6
# Convertir a objeto zoo
data_selected <- index_alt[, c("Date","AI_INDEX", "Fintech_Index", "Blockchain_Index")]
data_matrix <- as.matrix(data_selected)
data_zoo <- zoo(data_selected[, -1], order.by = data_selected$Date)
# Instalar y cargar el paquete ConnectednessApproach, si no está ya instalado
if (!requireNamespace("ConnectednessApproach", quietly = TRUE)) {
if (!requireNamespace("devtools", quietly = TRUE)) {
install.packages("devtools")
}
devtools::install_github("GabauerDavid/ConnectednessApproach")
}
library(ConnectednessApproach)
# Configuración de parámetros para TVPVAR
nlag <- 2 # Orden del rezago
lambda <- c(0.99, 0.99) # Valores de decaimiento para el filtro
# Crear la lista de configuración necesaria
config <- list(l = lambda, nlag = nlag)
# Estimar el modelo TVP-VAR con los parámetros especificados
tvpvar_model <- TVPVAR(data_zoo, configuration = config)
partition = c(pi+0.00001, pi/5, 0)
dca = ConnectednessApproach(data_zoo,
model="TVP-VAR",
connectedness="Frequency",
nlag=1,
nfore=100,
window.size=200,
VAR_config=list(TVPVAR=list(kappa1=0.99, kappa2=0.99, prior="BayesPrior")),
Connectedness_config = list(
FrequencyConnectedness=list(partition=partition, generalized=TRUE, scenario="ABS")
))
AI_INDEX.Total | Fintech_Index.Total | Blockchain_Index.Total | FROM.Total | AI_INDEX.1-5 | Fintech_Index.1-5 | Blockchain_Index.1-5 | FROM.1-5 | AI_INDEX.5-Inf | Fintech_Index.5-Inf | Blockchain_Index.5-Inf | FROM.5-Inf | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
AI_INDEX | 79.76 | 6.87 | 13.37 | 20.24 | 10.44 | 0.87 | 1.13 | 2.00 | 69.32 | 6.00 | 12.24 | 18.24 |
Fintech_Index | 8.56 | 84.40 | 7.04 | 15.60 | 0.75 | 10.89 | 0.53 | 1.28 | 7.81 | 73.51 | 6.51 | 14.32 |
Blockchain_Index | 15.46 | 5.78 | 78.76 | 21.24 | 1.24 | 0.69 | 10.50 | 1.93 | 14.22 | 5.08 | 68.26 | 19.31 |
TO | 24.02 | 12.64 | 20.41 | 57.07 | 1.99 | 1.56 | 1.65 | 5.20 | 22.03 | 11.08 | 18.76 | 51.87 |
Inc.Own | 103.79 | 97.04 | 99.17 | cTCI/TCI | 12.43 | 12.44 | 12.15 | cTCI/TCI | 91.35 | 84.60 | 87.02 | cTCI/TCI |
Net | 3.79 | -2.96 | -0.83 | 28.54/19.02 | -0.01 | 0.28 | -0.27 | 2.60/1.73 | 3.79 | -3.24 | -0.55 | 25.94/17.29 |
NPDC | 2.00 | 0.00 | 1.00 | 1.00 | 2.00 | 0.00 | 2.00 | 0.00 | 1.00 |
data_zoo2 <- zoo(data_selected[, c("AI_INDEX", "Fintech_Index", "Blockchain_Index")],
order.by = data_selected$Date)
# Configuración de parámetros para TVPVAR
nlag <- 2 # Orden del rezago
lambda <- c(0.99, 0.99) # Valores de decaimiento para el filtro
# Crear la lista de configuración necesaria
config <- list(l = lambda, nlag = nlag)
# Estimar el modelo TVP-VAR con los parámetros especificados
tvpvar_model <- TVPVAR(data_zoo2, configuration = config)
dca = ConnectednessApproach(data_zoo2,
nlag=1,
nfore=10,
model="TVP-VAR",
connectedness="Time",
window.size=200,
VAR_config=list(TVPVAR=list(kappa1=0.99, kappa2=0.99, prior="BayesPrior")))
AI_INDEX | Fintech_Index | Blockchain_Index | FROM | |
---|---|---|---|---|
AI_INDEX | 81.70 | 6.65 | 11.65 | 18.30 |
Fintech_Index | 7.31 | 86.71 | 5.98 | 13.29 |
Blockchain_Index | 11.98 | 5.17 | 82.86 | 17.14 |
TO | 19.29 | 11.82 | 17.63 | 48.73 |
Inc.Own | 100.99 | 98.53 | 100.48 | cTCI/TCI |
NET | 0.99 | -1.47 | 0.48 | 24.37/16.24 |
NPT | 2.00 | 0.00 | 1.00 |
# CONNECTEDNESS DECOMPOSITION
int = InternalConnectedness(dca, groups=list("Group1"=c(1), "Group2"=c(2), "Group3"=c(3)))
ext = ExternalConnectedness(dca, groups=list("Group1"=c(1), "Group2"=c(2), "Group3"=c(3)))
kable(int$TABLE)
AI_INDEX | Fintech_Index | Blockchain_Index | FROM | |
---|---|---|---|---|
AI_INDEX | 81.70 | 0.00 | 0.00 | 0.00 |
Fintech_Index | 0.00 | 86.71 | 0.00 | 0.00 |
Blockchain_Index | 0.00 | 0.00 | 82.86 | 0.00 |
TO | 0.00 | 0.00 | 0.00 | 0.00 |
Inc.Own | 81.70 | 86.71 | 82.86 | cTCI/TCI |
NET | 0.00 | 0.00 | 0.00 | 0.00/0.00 |
NPT | 0.00 | 0.00 | 0.00 |
AI_INDEX | Fintech_Index | Blockchain_Index | FROM | |
---|---|---|---|---|
AI_INDEX | 0.00 | 6.65 | 11.65 | 18.30 |
Fintech_Index | 7.31 | 0.00 | 5.98 | 13.29 |
Blockchain_Index | 11.98 | 5.17 | 0.00 | 17.14 |
TO | 19.29 | 11.82 | 17.63 | 48.73 |
Inc.Own | 19.29 | 11.82 | 17.63 | cTCI/TCI |
NET | 0.99 | -1.47 | 0.48 | 24.37/16.24 |
NPT | 2.00 | 0.00 | 1.00 |
# Convierte la columna 'Date' a formato Date
date <- as.Date(data_selected$Date, format="%d/%m/%Y")
# Crea la serie de tiempo (zoo) solo con las columnas seleccionadas
Y <- zoo(data_selected[, c("Fintech_Index", "AI_INDEX", "Blockchain_Index")], order.by = date)
# Asegúrate de que los datos sean numéricos y elimina valores NA
Y <- na.omit(zoo(data.frame(
Fintech_Index_diff = as.numeric(data_selected$Fintech_Index),
AI_INDEX_diff = as.numeric(data_selected$AI_INDEX),
Blockchain_Index_diff = as.numeric(data_selected$Blockchain_Index)
), order.by = date))
# Reemplaza los valores NA restantes por 0
Y[is.na(Y)] <- 0
# Establece 'dimnames' de Y con números como índices (como en Y2) sin cambiar el índice de fecha
dimnames(Y) <- list(as.character(1:nrow(Y)), colnames(Y))
NAMES = colnames(Y)
k = ncol(Y)
t = nrow(Y)
# Verifica la estructura de Y
str(Y)
## 'zoo' series from 2019-01-08 to 2024-08-19
## Data: num [1:2051, 1:3] -0.000675 -0.000675 -0.000676 -0.000676 -0.000677 ...
## - attr(*, "dimnames")=List of 2
## ..$ : chr [1:2051] "1" "2" "3" "4" ...
## ..$ : chr [1:3] "Fintech_Index_diff" "AI_INDEX_diff" "Blockchain_Index_diff"
## Index: Date[1:2051], format: "2019-01-08" "2019-01-09" "2019-01-10" "2019-01-11" "2019-01-12" ...
Fintech_Index_diff | AI_INDEX_diff | Blockchain_Index_diff | |
---|---|---|---|
Mean | 0.000*** | 0.001*** | 0.000*** |
(0.000) | (0.000) | (0.000) | |
Variance | 0 | 0 | 0 |
Skewness | -0.062 | -0.368*** | -0.440*** |
(0.249) | (0.000) | (0.000) | |
Ex.Kurtosis | 0.432*** | 0.032 | 2.179*** |
(0.001) | (0.712) | (0.000) | |
JB | 17.257*** | 46.284*** | 471.935*** |
(0.000) | (0.000) | (0.000) | |
ERS | -12.400 | -8.227 | -13.211 |
(0.000) | (0.000) | (0.000) | |
Q(20) | 3870.587*** | 3338.261*** | 3939.808*** |
(0.000) | (0.000) | (0.000) | |
Q2(20) | 3413.861*** | 5253.401*** | 4241.131*** |
(0.000) | (0.000) | (0.000) | |
kendall | Fintech_Index_diff | AI_INDEX_diff | Blockchain_Index_diff |
Fintech_Index_diff | 1.000*** | -0.062*** | 0.016 |
AI_INDEX_diff | -0.062*** | 1.000*** | 0.144*** |
Blockchain_Index_diff | 0.016 | 0.144*** | 1.000*** |
par(mfcol = c(ceiling(k/2),2), oma = c(0, 1, 0, 0) + 0.5, mar = c(1, 1, 1, 1) + 0.5, mgp = c(1, 0.4, 0))
for (i in 1:k) {
plot(date,Y[,i],type='l',las=1,xaxs='i',yaxs='i',ylim=c(-0.1,0.1),xlab='',ylab='',main=paste(NAMES[i]),tck=-.02,col='steelblue4')
grid(NA,NULL)
lines(date,Y[,i],col='steelblue4')
abline(h=0,lty=3)
box()
}
spec <- list()
# Itera sobre cada columna de Y
for (i in 1:k) {
# Intenta seleccionar el mejor modelo GARCH para cada serie
u <- tryCatch({
GARCHselection(Y[, i],
distributions = c("norm", "std", "sstd", "ged", "sged"),
models = c("sGARCH", "gjrGARCH", "eGARCH", "iGARCH", "AVGARCH", "TGARCH"))
}, error = function(e) {
warning(paste("Error al seleccionar modelo GARCH para la variable", i, ":", e$message))
NULL
})
# Si 'u' no es NULL y tiene un mejor modelo, agrega a 'spec'
if (!is.null(u) && !is.null(u$best_ugarch)) {
spec[[i]] <- u$best_ugarch
} else {
# Si no se seleccionó un modelo, asigna un modelo GARCH(1,1) por defecto
warning(paste("No se seleccionó ningún modelo GARCH para", NAMES[i], ". Usando modelo GARCH(1,1) con distribución normal"))
spec[[i]] <- ugarchspec(variance.model = list(model = "sGARCH", garchOrder = c(1, 1)),
mean.model = list(armaOrder = c(0, 0)),
distribution.model = "norm")
}
}
# Verifica que 'spec' tenga modelos para cada serie en Y
if (length(spec) != k) {
stop("spec no contiene modelos válidos para todas las series de Y")
}
# Ejecuta el modelo DCC-GARCH si spec es válida
fit <- BivariateDCCGARCH(Y, spec)
## [1] "DCC-GARCH estimation based on Fintech_Index_diff and AI_INDEX_diff"
## [1] "DCC-GARCH estimation based on Fintech_Index_diff and Blockchain_Index_diff"
## [1] "DCC-GARCH estimation based on AI_INDEX_diff and Blockchain_Index_diff"
H = fit$H_t
R = fit$R_t
uGARCH_table = NULL
for (i in 1:k) {
fit = ugarchfit(spec[[i]], Y[,i])
gt = GARCHtests(fit, lag=20)
uGARCH_table = rbind(uGARCH_table, gt$TABLE)
}
kable(uGARCH_table)
SignBias | WARCH(20) | VaR | CVaR | VaR Dur. | |
---|---|---|---|---|---|
statistics | 0.6230910 | 1.9437522 | 113.69976 | -93.6559 | 0.1666667 |
pvalues | 0.5332942 | 0.9997909 | 0.00000 | 0.1730 | 0.1755157 |
statistics | 0.8620658 | 1.1073277 | 76.58490 | -148.2286 | 0.2843137 |
pvalues | 0.3887523 | 0.9999953 | 0.00000 | 0.0000 | 0.3464012 |
statistics | 1.4127318 | 13.8866627 | 74.01928 | -152.5048 | 0.2941176 |
pvalues | 0.1578867 | 0.1760766 | 0.00000 | 0.2180 | 0.1284943 |
par(mfcol = c(ceiling(k/2),2), oma = c(0, 1, 0, 0) + 0.5, mar = c(1, 1, 1, 1) + 0.5, mgp = c(1, 0.4, 0))
for (j in 1:k) {
plot(date,R[j,j,],type='l',las=1,xaxs='i',yaxs='i',xlab='',ylab='',main=paste(NAMES[j]),tck=-.02,col=j,ylim=c(-1.1,1.1))
grid(NA,NULL)
for (i in 1:k) {
lines(date,R[j,i,],col=i)
abline(h=0,lty=3)
box()
}
legend("bottom",NAMES[-j],fill=c(1:k)[-j],bty="n",cex=0.75,ncol=k)
}
## | | | 0% | | | 1% | |= | 1% | |= | 2% | |== | 2% | |== | 3% | |== | 4% | |=== | 4% | |=== | 5% | |==== | 5% | |==== | 6% | |===== | 6% | |===== | 7% | |===== | 8% | |====== | 8% | |====== | 9% | |======= | 9% | |======= | 10% | |======= | 11% | |======== | 11% | |======== | 12% | |========= | 12% | |========= | 13% | |========= | 14% | |========== | 14% | |========== | 15% | |=========== | 15% | |=========== | 16% | |============ | 16% | |============ | 17% | |============ | 18% | |============= | 18% | |============= | 19% | |============== | 19% | |============== | 20% | |============== | 21% | |=============== | 21% | |=============== | 22% | |================ | 22% | |================ | 23% | |================ | 24% | |================= | 24% | |================= | 25% | |================== | 25% | |================== | 26% | |=================== | 26% | |=================== | 27% | |=================== | 28% | |==================== | 28% | |==================== | 29% | |===================== | 29% | |===================== | 30% | |===================== | 31% | |====================== | 31% | |====================== | 32% | |======================= | 32% | |======================= | 33% | |======================= | 34% | |======================== | 34% | |======================== | 35% | |========================= | 35% | |========================= | 36% | |========================== | 36% | |========================== | 37% | |========================== | 38% | |=========================== | 38% | |=========================== | 39% | |============================ | 39% | |============================ | 40% | |============================ | 41% | |============================= | 41% | |============================= | 42% | |============================== | 42% | |============================== | 43% | |============================== | 44% | |=============================== | 44% | |=============================== | 45% | |================================ | 45% | |================================ | 46% | |================================= | 46% | |================================= | 47% | |================================= | 48% | |================================== | 48% | |================================== | 49% | |=================================== | 49% | |=================================== | 50% | |=================================== | 51% | |==================================== | 51% | |==================================== | 52% | |===================================== | 52% | |===================================== | 53% | |===================================== | 54% | |====================================== | 54% | |====================================== | 55% | |======================================= | 55% | |======================================= | 56% | |======================================== | 56% | |======================================== | 57% | |======================================== | 58% | |========================================= | 58% | |========================================= | 59% | |========================================== | 59% | |========================================== | 60% | |========================================== | 61% | |=========================================== | 61% | |=========================================== | 62% | |============================================ | 62% | |============================================ | 63% | |============================================ | 64% | |============================================= | 64% | |============================================= | 65% | |============================================== | 65% | |============================================== | 66% | |=============================================== | 66% | |=============================================== | 67% | |=============================================== | 68% | |================================================ | 68% | |================================================ | 69% | |================================================= | 69% | |================================================= | 70% | |================================================= | 71% | |================================================== | 71% | |================================================== | 72% | |=================================================== | 72% | |=================================================== | 73% | |=================================================== | 74% | |==================================================== | 74% | |==================================================== | 75% | |===================================================== | 75% | |===================================================== | 76% | |====================================================== | 76% | |====================================================== | 77% | |====================================================== | 78% | |======================================================= | 78% | |======================================================= | 79% | |======================================================== | 79% | |======================================================== | 80% | |======================================================== | 81% | |========================================================= | 81% | |========================================================= | 82% | |========================================================== | 82% | |========================================================== | 83% | |========================================================== | 84% | |=========================================================== | 84% | |=========================================================== | 85% | |============================================================ | 85% | |============================================================ | 86% | |============================================================= | 86% | |============================================================= | 87% | |============================================================= | 88% | |============================================================== | 88% | |============================================================== | 89% | |=============================================================== | 89% | |=============================================================== | 90% | |=============================================================== | 91% | |================================================================ | 91% | |================================================================ | 92% | |================================================================= | 92% | |================================================================= | 93% | |================================================================= | 94% | |================================================================== | 94% | |================================================================== | 95% | |=================================================================== | 95% | |=================================================================== | 96% | |==================================================================== | 96% | |==================================================================== | 97% | |==================================================================== | 98% | |===================================================================== | 98% | |===================================================================== | 99% | |======================================================================| 99% | |======================================================================| 100%
Fintech_Index_diff | AI_INDEX_diff | Blockchain_Index_diff | FROM | |
---|---|---|---|---|
Fintech_Index_diff | 100.00 | 56.85 | 22.43 | 79.28 |
AI_INDEX_diff | 52.51 | 100.00 | 31.81 | 84.33 |
Blockchain_Index_diff | 23.42 | 37.14 | 100.00 | 60.56 |
TO | 75.93 | 93.99 | 54.24 | 224.17 |
Inc.Own | 175.93 | 193.99 | 154.24 | cTCI/TCI |
NET | -3.35 | 9.67 | -6.32 | 112.08/74.72 |
NPT | 1.00 | 2.00 | 0.00 |
r2c = apply(dca$CT,c(1,3),sum)-1
par(mfcol = c(ceiling(k/2),2), oma = c(0, 1, 0, 0) + 0.5, mar = c(1, 1, 1, 1) + 0.5, mgp = c(1, 0.4, 0))
for (j in 1:k) {
r2dd = matrix(0,ncol=k,nrow=t)
for (i in 1:t) {
ded = rep(0,k)
ded[j] = 1
r2dd[i,] = cumsum(dca$CT[j,,i]-ded)
}
plot(date,100*r2c[j,]*NA,type="l",las=1,xlab="",ylab="",ylim=c(0,100),xaxs="i",tck=-0.01,yaxs="i",main=NAMES[j])
grid(NA,NULL)
for (i in k:1) {
polygon(c(date, rev(date)), c(c(rep(0, t)), rev(100*r2dd[,i])), col=i, border=i)
}
box()
legend("topleft",NAMES[-j],fill=c(1:k)[-j],bty="n",cex=0.75,ncol=1)
}
par(mfcol = c(1,1), oma = c(0, 1, 0, 0) + 0.5, mar = c(1, 1, 1, 1) + 0.5, mgp = c(1, 0.4, 0))
plot(date,dca$TCI*NA,type='l',las=1,xaxs='i',yaxs='i',xlab='',ylab='',main='',tck=-.01, ylim=c(0,110))#c(max(c(0,min(TCI)-10)),max(TCI)+10))
grid(NA,NULL)
polygon(c(date, rev(date)), c(c(rep(0, t)), rev(dca$TCI)), col=1, border=1)
box()
par(mfcol = c(ceiling(k/2),2), oma = c(0, 0, 0, 0) + 0.5, mar = c(1, 1, 1, 1) + 0.5, mgp = c(1, 0.4, 0))
for (i in 1:k) {
plot(date,dca$NET[,i]*NA,type='l',las=1,xaxs='i',yaxs='i',ylim=c(-20,20),xlab='',ylab='',main=paste("NET",NAMES[i]),tck=-.025)
grid(NA,NULL)
polygon(c(date, rev(date)), c(c(rep(0, t)), rev(dca$NET[,i])), col=1, border=1)
abline(h=0,lty=3)
box()
}
method = "cumsum"
statistics = "Fisher"
metric = "StdDev"
hr = HedgeRatio(Y/100, H, statistics=statistics, method=method, metric=metric, digit=3)
kable(hr$TABLE)
Mean | Std.Dev. | 5% | 95% | HE | p-value | Return | Std.Dev | SR | |
---|---|---|---|---|---|---|---|---|---|
Fintech_Index_diff/AI_INDEX_diff | -0.014 | 1.232 | -1.342 | 1.608 | -2.092 | 0.084 | 0.002 | 0.001 | 2.921 |
Fintech_Index_diff/Blockchain_Index_diff | 0.043 | 0.600 | -0.716 | 0.846 | 0.040 | 0.000 | 0.001 | 0.000 | 4.142 |
AI_INDEX_diff/Fintech_Index_diff | 4.092 | 73.815 | -17.305 | 19.783 | -100.684 | 0.350 | -0.003 | 0.010 | -0.285 |
AI_INDEX_diff/Blockchain_Index_diff | 0.236 | 1.892 | -2.362 | 2.928 | 0.074 | 0.000 | 0.002 | 0.001 | 1.776 |
Blockchain_Index_diff/Fintech_Index_diff | -3.450 | 72.603 | -17.189 | 8.992 | -126.712 | 0.350 | 0.008 | 0.009 | 0.862 |
Blockchain_Index_diff/AI_INDEX_diff | 0.420 | 4.446 | -2.162 | 3.944 | -47.018 | 0.084 | 0.005 | 0.006 | 0.826 |
mhp = MultivariateHedgingPortfolio(Y/100, H, statistics=statistics, method=method, digit=3)
kable(mhp$TABLE)
Mean | Std.Dev. | 5% | 95% | HE | p-value | Return | Risk | SR | |
---|---|---|---|---|---|---|---|---|---|
Fintech_Index_diff/AI_INDEX_diff | -0.020 | 1.586 | -1.585 | 1.692 | -3.779 | 0.000 | 0.001 | 0.001 | 2.267 |
Fintech_Index_diff/Blockchain_Index_diff | 0.031 | 0.518 | -0.242 | 0.301 | -3.779 | 0.000 | 0.001 | 0.001 | 2.267 |
AI_INDEX_diff/Fintech_Index_diff | 2.927 | 54.692 | -15.067 | 14.699 | -54.608 | 0.000 | -0.002 | 0.007 | -0.290 |
AI_INDEX_diff/Blockchain_Index_diff | 0.193 | 1.211 | -0.928 | 1.570 | -54.608 | 0.000 | -0.002 | 0.007 | -0.290 |
Blockchain_Index_diff/Fintech_Index_diff | 0.292 | 38.048 | -9.031 | 10.540 | -78.478 | 0.000 | 0.006 | 0.007 | 0.742 |
Blockchain_Index_diff/AI_INDEX_diff | 1.106 | 6.472 | -2.225 | 6.007 | -78.478 | 0.000 | 0.006 | 0.007 | 0.742 |
par(mfcol = c(ceiling(k/2),2), oma = c(0, 1, 0, 0) + 0.5, mar = c(1, 1, 1, 1) + 0.5, mgp = c(1, 0.4, 0))
for (j in 1:k) {
plot(date,mhp$Beta[j,j,]*NA,type='l',las=1,xaxs='i',yaxs='i',xlab='',ylab='',
main=paste(NAMES[j]),tck=-.02,col=j,ylim=c(-0.5,70))
grid(NA,NULL)
coefs = lm(Y[,j]~Y[,-j])$coefficients[-1]
for (i in 1:k) {
if (i!=j) {
lines(date,mhp$Beta[j,i,],col=i)
abline(h=0,lty=3)
box()
}
}
col = 1:k
col = col[-j]
abline(h=coefs,lty=3,col=col)
legend("bottom",NAMES[-j],fill=c(1:k)[-j],bty="n",cex=0.6,ncol=k)
}
bpw = BivariatePortfolio(Y/100, H, statistics=statistics, method=method, metric=metric, digit=3)
## The optimal bivariate portfolios are computed according to:
## Kroner, K. F., & Ng, V. K. (1998). Modeling asymmetric comovements of asset returns. The Review of Financial Studies, 11(4), 817-844.
##
## Hedging effectiveness is calculated according to:
## Ederington, L. H. (1979). The hedging performance of the new futures markets. The Journal of Finance, 34(1), 157-170.
##
## Statistics of the hedging effectiveness measure are implemented according to:
## Antonakakis, N., Cunado, J., Filis, G., Gabauer, D., & de Gracia, F. P. (2020). Oil and asset classes implied volatilities: Investment strategies and hedging effectiveness. Energy Economics, 91, 104762.
kable(bpw$TABLE)
Mean | Std.Dev. | 5% | 95% | HE | p-value | SR | |
---|---|---|---|---|---|---|---|
Fintech_Index_diff/AI_INDEX_diff | 0.763 | 0.307 | 0.000 | 1.000 | 0.414 | 0.000 | 5.179 |
Fintech_Index_diff/Blockchain_Index_diff | 0.789 | 0.279 | 0.101 | 1.000 | 0.408 | 0.000 | 4.959 |
AI_INDEX_diff/Fintech_Index_diff | 0.237 | 0.307 | 0.000 | 1.000 | 0.942 | 0.000 | 5.179 |
AI_INDEX_diff/Blockchain_Index_diff | 0.451 | 0.395 | 0.000 | 1.000 | 0.648 | 0.000 | 1.711 |
Blockchain_Index_diff/Fintech_Index_diff | 0.211 | 0.279 | 0.000 | 0.899 | 0.924 | 0.000 | 4.959 |
Blockchain_Index_diff/AI_INDEX_diff | 0.549 | 0.395 | 0.000 | 1.000 | 0.545 | 0.000 | 1.711 |
PCIc = dca$PCI
PCIg = dca$CT
for (l in 1:dim(dca$CT)[3]) {
for (i in 1:k) {
for (j in 1:k) {
PCIg[i,j,l] = (2*R[i,j,l]^2)/(1+R[i,j,l]^2)
}
}
}
mvp = MinimumConnectednessPortfolio(Y/100, H, statistics=statistics, method=method, metric=metric, digit=3)
## The minimum connectedness portfolio is implemented according to:
## Broadstock, D. C., Chatziantoniou, I., & Gabauer, D. (2022). Minimum connectedness portfolios and the market for green bonds: Advocating socially responsible investment (SRI) activity. In Applications in Energy Finance (pp. 217-253). Palgrave Macmillan, Cham.
##
## Hedging effectiveness is calculated according to:
## Ederington, L. H. (1979). The hedging performance of the new futures markets. The Journal of Finance, 34(1), 157-170.
##
## Statistics of the hedging effectiveness measure are implemented according to:
## Antonakakis, N., Cunado, J., Filis, G., Gabauer, D., & de Gracia, F. P. (2020). Oil and asset classes implied volatilities: Investment strategies and hedging effectiveness. Energy Economics, 91, 104762.
mcp = MinimumConnectednessPortfolio(Y/100, R, statistics=statistics, method=method, metric=metric, digit=3)
## The minimum connectedness portfolio is implemented according to:
## Broadstock, D. C., Chatziantoniou, I., & Gabauer, D. (2022). Minimum connectedness portfolios and the market for green bonds: Advocating socially responsible investment (SRI) activity. In Applications in Energy Finance (pp. 217-253). Palgrave Macmillan, Cham.
##
## Hedging effectiveness is calculated according to:
## Ederington, L. H. (1979). The hedging performance of the new futures markets. The Journal of Finance, 34(1), 157-170.
##
## Statistics of the hedging effectiveness measure are implemented according to:
## Antonakakis, N., Cunado, J., Filis, G., Gabauer, D., & de Gracia, F. P. (2020). Oil and asset classes implied volatilities: Investment strategies and hedging effectiveness. Energy Economics, 91, 104762.
mpc = MinimumConnectednessPortfolio(Y/100, PCIc, statistics=statistics, method=method, metric=metric, digit=3)
## The minimum connectedness portfolio is implemented according to:
## Broadstock, D. C., Chatziantoniou, I., & Gabauer, D. (2022). Minimum connectedness portfolios and the market for green bonds: Advocating socially responsible investment (SRI) activity. In Applications in Energy Finance (pp. 217-253). Palgrave Macmillan, Cham.
##
## Hedging effectiveness is calculated according to:
## Ederington, L. H. (1979). The hedging performance of the new futures markets. The Journal of Finance, 34(1), 157-170.
##
## Statistics of the hedging effectiveness measure are implemented according to:
## Antonakakis, N., Cunado, J., Filis, G., Gabauer, D., & de Gracia, F. P. (2020). Oil and asset classes implied volatilities: Investment strategies and hedging effectiveness. Energy Economics, 91, 104762.
mpg = MinimumConnectednessPortfolio(Y/100, PCIg, statistics=statistics, method=method, metric=metric, digit=3)
## The minimum connectedness portfolio is implemented according to:
## Broadstock, D. C., Chatziantoniou, I., & Gabauer, D. (2022). Minimum connectedness portfolios and the market for green bonds: Advocating socially responsible investment (SRI) activity. In Applications in Energy Finance (pp. 217-253). Palgrave Macmillan, Cham.
##
## Hedging effectiveness is calculated according to:
## Ederington, L. H. (1979). The hedging performance of the new futures markets. The Journal of Finance, 34(1), 157-170.
##
## Statistics of the hedging effectiveness measure are implemented according to:
## Antonakakis, N., Cunado, J., Filis, G., Gabauer, D., & de Gracia, F. P. (2020). Oil and asset classes implied volatilities: Investment strategies and hedging effectiveness. Energy Economics, 91, 104762.
mrp = MinimumConnectednessPortfolio(Y/100, dca$CT, statistics=statistics, method=method, metric=metric, digit=3)
## The minimum connectedness portfolio is implemented according to:
## Broadstock, D. C., Chatziantoniou, I., & Gabauer, D. (2022). Minimum connectedness portfolios and the market for green bonds: Advocating socially responsible investment (SRI) activity. In Applications in Energy Finance (pp. 217-253). Palgrave Macmillan, Cham.
##
## Hedging effectiveness is calculated according to:
## Ederington, L. H. (1979). The hedging performance of the new futures markets. The Journal of Finance, 34(1), 157-170.
##
## Statistics of the hedging effectiveness measure are implemented according to:
## Antonakakis, N., Cunado, J., Filis, G., Gabauer, D., & de Gracia, F. P. (2020). Oil and asset classes implied volatilities: Investment strategies and hedging effectiveness. Energy Economics, 91, 104762.
MVA = rbind(mvp$TABLE, mcp$TABLE, mpc$TABLE, mpg$TABLE, mrp$TABLE)
kable(MVA)
Mean | Std.Dev. | 5% | 95% | HE | p-value | SR | |
---|---|---|---|---|---|---|---|
Fintech_Index_diff | 0.674 | 0.307 | 0.000 | 0.999 | 0.375 | 0.000 | 5.182 |
AI_INDEX_diff | 0.226 | 0.288 | 0.000 | 0.974 | 0.938 | 0.000 | 5.182 |
Blockchain_Index_diff | 0.099 | 0.166 | 0.000 | 0.476 | 0.920 | 0.000 | 5.182 |
Fintech_Index_diff | 0.390 | 0.171 | 0.000 | 0.555 | -1.456 | 0.000 | 2.604 |
AI_INDEX_diff | 0.365 | 0.220 | 0.000 | 0.569 | 0.757 | 0.000 | 2.604 |
Blockchain_Index_diff | 0.245 | 0.217 | 0.000 | 0.498 | 0.685 | 0.000 | 2.604 |
Fintech_Index_diff | 0.378 | 0.105 | 0.162 | 0.485 | -1.286 | 0.000 | 2.160 |
AI_INDEX_diff | 0.181 | 0.120 | 0.024 | 0.458 | 0.774 | 0.000 | 2.160 |
Blockchain_Index_diff | 0.441 | 0.068 | 0.328 | 0.497 | 0.707 | 0.000 | 2.160 |
Fintech_Index_diff | 0.452 | 0.179 | 0.000 | 0.580 | -1.480 | 0.000 | 2.264 |
AI_INDEX_diff | 0.114 | 0.199 | 0.000 | 0.505 | 0.755 | 0.000 | 2.264 |
Blockchain_Index_diff | 0.434 | 0.121 | 0.000 | 0.500 | 0.682 | 0.000 | 2.264 |
Fintech_Index_diff | 0.327 | 0.037 | 0.261 | 0.392 | -1.483 | 0.000 | 2.485 |
AI_INDEX_diff | 0.283 | 0.029 | 0.247 | 0.344 | 0.754 | 0.000 | 2.485 |
Blockchain_Index_diff | 0.390 | 0.042 | 0.310 | 0.464 | 0.682 | 0.000 | 2.485 |
library(PerformanceAnalytics)
library(xts)
port.mat = data.frame(
MVP=mvp$portfolio_return,
MCP=mcp$portfolio_return,
MPC=mpc$portfolio_return,
MPG=mpg$portfolio_return,
MRP=mrp$portfolio_return
)
port.xts = xts(port.mat, order.by=date)
ir = matrix(NA, nrow=1, ncol=ncol(port.xts))
colnames(ir) = colnames(port.xts)
rownames(ir) = "GRE"
for (i in 1:ncol(port.xts)) {
ir[,i] = InformationRatio(Ra=port.xts[,i], Rb=Y[,1]/100)
}
k = ncol(port.xts)
table = matrix(NA, ncol=k, nrow=5)
for (i in 1:k) {
table[,i] = c(
# Retorno anualizado
Return.annualized(port.xts[,i]),
# Desviación estándar anualizada
StdDev.annualized(port.xts[,i]),
# Ratio de Sharpe anualizado
SharpeRatio.annualized(port.xts[,i]),
# Valor en Riesgo (VaR) con distribución normal y percentil del 95%
VaR(as.numeric(port.xts[,i]), p=0.95, method="gaussian"),
# Expectativa de Pérdida (ES) con distribución normal y percentil del 95%
ES(as.numeric(port.xts[,i]), p=0.95, method="gaussian")
)
}
colnames(table) = colnames(port.xts)
rownames(table) = c("Return","StdDev","Sharpe Ratio (StdDev)","Sharpe Ratio (VaR)","Sharpe Ratio (CVaR)")
kable(rbind(table, ir))
MVP | MCP | MPC | MPG | MRP | |
---|---|---|---|---|---|
Return | 0.0012265 | 0.0012220 | 0.0009781 | 0.0010677 | 0.0011724 |
StdDev | 0.0002367 | 0.0004693 | 0.0004527 | 0.0004716 | 0.0004719 |
Sharpe Ratio (StdDev) | 5.1822047 | 2.6040207 | 2.1603544 | 2.2641097 | 2.4845743 |
Sharpe Ratio (VaR) | -0.0000241 | -0.0000460 | -0.0000436 | -0.0000475 | -0.0000466 |
Sharpe Ratio (CVaR) | -0.0000031 | -0.0000017 | -0.0000009 | -0.0000009 | -0.0000016 |
GRE | 2.0556984 | 1.1172527 | 0.7210579 | 0.9113081 | 1.0528818 |
dca3 = ConnectednessApproach(data_zoo2,
nlag=1,
nfore=12,
window.size=200,
model="TVP-VAR",
connectedness="Time",
VAR_config=list(TVPVAR=list(kappa1=0.99, kappa2=0.96, prior="BayesPrior")))
## Estimating model
## Computing connectedness measures
## The TVP-VAR connectedness approach is implemented according to:
## Antonakakis, N., Chatziantoniou, I., & Gabauer, D. (2020). Refined measures of dynamic connectedness based on time-varying parameter vector autoregressions. Journal of Risk and Financial Management, 13(4), 84.
DCA = list()
WINDOW.SIZE = c(50, 100, 200)
for (i in 1:length(WINDOW.SIZE)) {
DCA[[i]] = suppressMessages(ConnectednessApproach(data_zoo2,
nlag=1,
nfore=12,
window.size=WINDOW.SIZE[i]))
}
#Figure 7: Dynamic Total Connectedness
PlotTCI(dca3, ca=DCA, ylim=c(20,80))
# Figure 8: Net Total and Net Pairwise Directional Connectedness Measures
PlotNET(dca3, ca=DCA, ylim=c(-20,20))
# Función para crear la tabla con los cálculos dinámicos de conectividad
ConnectednessTableModified <- function(FEVD, digit = 2) {
if (length(dim(FEVD)) <= 1) {
stop("FEVD needs to be at least a 2-dimensional matrix")
}
# Nombres de los activos
NAMES = colnames(FEVD)
k = dim(FEVD)[1]
if (is.null(NAMES)) {
NAMES = 1:k
}
# Calcular las métricas principales
CT = apply(FEVD, 1:2, mean) * 100 # De otros hacia un activo específico
OWN = diag(diag(CT))
TO = colSums(CT - OWN) # Contribución hacia otros activos
FROM = rowSums(CT - OWN) # Contribución desde otros activos
NET = TO - FROM # Conectividad neta
TCI = mean(TO) # Índice total de conectividad
cTCI = TCI * k / (k - 1) # Índice corregido
NPDC = CT - t(CT) # Conectividad direccional neta par-a-par
NPT = rowSums(NPDC < 0) # Contador de transmisores netos
INFLUENCE = 100 * abs(NPDC / t(t(CT) + CT)) # Influencia porcentual
# Crear tabla final
table = format(round(cbind(CT, FROM), digit), nsmall = digit)
to = c(format(round(c(TO, sum(TO)), digit), nsmall = digit))
inc = c(format(round(colSums(CT), digit), nsmall = digit), "cTCI/TCI")
tci = paste0(format(round(cTCI, digit), nsmall = digit), "/", format(round(TCI, digit), nsmall = digit))
net = c(format(round(NET, digit), nsmall = digit))
net = c(net, tci)
npdc = c(format(round(NPDC, digit), nsmall = digit))
npt = c(format(round(NPT, digit), nsmall = digit), "")
TABLE = rbind(table, to, inc, net, npt)
colnames(TABLE) = c(NAMES, "FROM")
rownames(TABLE) = c(NAMES, "TO", "Inc.Own", "NET", "NPT")
PCI = matrix(NA, k, k)
for (i in 1:k) {
for (j in 1:k) {
PCI[i, j] = 200 * (CT[i, j] + CT[j, i]) / (CT[i, i] + CT[i, j] + CT[j, i] + CT[j, j])
}
}
return(list(
FEVD = CT,
TCI = TCI,
cTCI = cTCI,
PCI = PCI,
TO = TO,
FROM = FROM,
NET = NET,
NPDC = NPDC,
TABLE = TABLE,
NPT = NPT,
INFLUENCE = INFLUENCE
))
}
# Aplicar la función a las ventanas y mostrar las tablas
tables <- list() # Lista vacía para almacenar las tablas
for (i in seq_along(DCA)) {
result <- ConnectednessTableModified(DCA[[i]]$CT, digit = 2) # Ajusta según tu estructura
tables[[i]] <- result$TABLE # Guardar la tabla en la lista
cat(paste0("\nWindow Size: ", WINDOW.SIZE[i], "\n"))
print(result$TABLE) # Mostrar solo la tabla para cada ventana
}
##
## Window Size: 50
## AI_INDEX Fintech_Index Blockchain_Index FROM
## AI_INDEX "72.20" "12.04" "15.77" "27.80"
## Fintech_Index "11.31" "75.95" "12.73" "24.05"
## Blockchain_Index "16.73" "12.67" "70.60" "29.40"
## TO "28.04" "24.71" "28.50" "81.25"
## Inc.Own "100.24" "100.66" " 99.10" "cTCI/TCI"
## NET " 0.24" " 0.66" "-0.90" "40.63/27.08"
## NPT "1.00" "1.00" "1.00" ""
##
## Window Size: 100
## AI_INDEX Fintech_Index Blockchain_Index FROM
## AI_INDEX "80.58" " 6.93" "12.49" "19.42"
## Fintech_Index " 6.38" "87.13" " 6.49" "12.87"
## Blockchain_Index "13.60" " 7.09" "79.30" "20.70"
## TO "19.98" "14.02" "18.97" "52.98"
## Inc.Own "100.57" "101.16" " 98.28" "cTCI/TCI"
## NET " 0.57" " 1.16" "-1.72" "26.49/17.66"
## NPT "1.00" "2.00" "0.00" ""
##
## Window Size: 200
## AI_INDEX Fintech_Index Blockchain_Index FROM
## AI_INDEX "84.66" " 5.00" "10.34" "15.34"
## Fintech_Index " 4.14" "93.01" " 2.85" " 6.99"
## Blockchain_Index "11.66" " 3.62" "84.72" "15.28"
## TO "15.80" " 8.61" "13.19" "37.60"
## Inc.Own "100.46" "101.62" " 97.92" "cTCI/TCI"
## NET " 0.46" " 1.62" "-2.08" "18.80/12.53"
## NPT "1.00" "2.00" "0.00" ""