Determine the number of cointegrating vectors (r)
From the summary, identify the rank r where the test statistic
is less than the critical value. Suppose r = 4
r <- 4
print("Number of cointegrating vectors (r):")
[1] "Number of cointegrating vectors (r):"
print(r)
[1] 4
Step 4: Estimate VECM and transform to VAR representation
vecm_var <- vec2var(johansen_test, r=r)
print("Dimensions of the VECM VAR representation:")
[1] "Dimensions of the VECM VAR representation:"
print(dim(vecm_var))
NULL
Step 5: Extract beta and alpha matrices
Extract the first r columns (cointegrating vectors) and exclude the constant term
beta <- johansen_test@V[1:ncol(Y), 1:r]
alpha <- johansen_test@W[, 1:r]
print("Extracted beta matrix:")
[1] "Extracted beta matrix:"
print(beta)
log_Spot.l1 log_Fut.l1
log_Spot.l1 1.0000000000 1.0000000
log_Fut.l1 -1.0017520247 -1.3244152
log_USO.l1 0.0023907033 -0.5014035
log_USL.l1 -0.0043867524 0.5028687
log_OIL.l1 0.0005538807 0.2823013
log_USO.l1 log_USL.l1
log_Spot.l1 1.00000 1.0000000
log_Fut.l1 16.80517 0.7523886
log_USO.l1 -74.82706 8.6459957
log_USL.l1 -35.29678 -6.3180603
log_OIL.l1 76.35024 -4.8355656
print("Dimensions of beta matrix:")
[1] "Dimensions of beta matrix:"
print(dim(beta))
[1] 5 4
print("Extracted alpha matrix:")
[1] "Extracted alpha matrix:"
print(alpha)
log_Spot.l1 log_Fut.l1
log_Spot.d -0.24199295 0.02295218
log_Fut.d 0.18580099 0.04212123
log_USO.d -0.15331674 0.06760043
log_USL.d -0.07075644 0.02880841
log_OIL.d -0.18973739 0.07470091
log_USO.l1 log_USL.l1
log_Spot.d 0.0003860446 -0.0045865854
log_Fut.d 0.0004176988 -0.0042320772
log_USO.d 0.0005287744 -0.0024737038
log_USL.d 0.0006447661 -0.0007056863
log_OIL.d 0.0002518853 -0.0017978899
print("Dimensions of alpha matrix:")
[1] "Dimensions of alpha matrix:"
print(dim(alpha))
[1] 5 4
Step 6: Compute orthogonal complements beta_perp and alpha_perp using SVD
Since we have (n - r) = 1 common stochastic trend, beta_perp and alpha_perp are vectors
t_beta_svd <- svd(t(beta))
beta_perp <- t_beta_svdv[,ncol(tbetasvdv)] # Last column corresponds to smallest singular value
print("Computed beta_perp:")
[1] "Computed beta_perp:"
print(beta_perp)
[1] -0.6856252 -0.6210203 0.2081991
[4] -0.1225053 0.2930828
print("Dimensions of beta_perp:")
[1] "Dimensions of beta_perp:"
print(dim(beta_perp))
NULL
t_alpha_svd <- svd(t(alpha))
alpha_perp <- t_alpha_svdv[,ncol(talphasvdv)] # Similarly for alpha
print("Computed alpha_perp:")
[1] "Computed alpha_perp:"
print(alpha_perp)
[1] 0.01412683 0.02298025 -0.10644853
[4] -0.89827793 0.42548538
print("Dimensions of alpha_perp:")
[1] "Dimensions of alpha_perp:"
print(dim(alpha_perp))
NULL
Normalize beta_perp and alpha_perp
beta_perp <- beta_perp / beta_perp[1]
alpha_perp <- alpha_perp / alpha_perp[1]
print("Normalized beta_perp:")
[1] "Normalized beta_perp:"
print(beta_perp)
[1] 1.0000000 0.9057723 -0.3036631
[4] 0.1786767 -0.4274679
print("Normalized alpha_perp:")
[1] "Normalized alpha_perp:"
print(alpha_perp)
[1] 1.000000 1.626709 -7.535201
[4] -63.586644 30.118949
Step 7: Compute long-run impact matrix Psi(1)
Since beta_perp and alpha_perp are vectors, Psi(1) is computed as:
Psi_1 <- beta_perp %% t(alpha_perp) / as.numeric(t(alpha_perp) %% beta_perp)
print("Computed long-run impact matrix Psi(1):")
[1] "Computed long-run impact matrix Psi(1):"
print(Psi_1)
[,1] [,2] [,3]
[1,] -0.051348548 -0.08352914 0.38692162
[2,] -0.046510093 -0.07565839 0.35046289
[3,] 0.015592659 0.02536472 -0.11749381
[4,] -0.009174791 -0.01492471 0.06913389
[5,] 0.021949856 0.03570603 -0.16539657
[,4] [,5]
[1,] 3.2650818 -1.5465643
[2,] 2.9574207 -1.4008351
[3,] -0.9914848 0.4696345
[4,] 0.5833942 -0.2763351
[5,] -1.3957177 0.6611066
print("Dimensions of Psi(1):")
[1] "Dimensions of Psi(1):"
print(dim(Psi_1))
[1] 5 5
Step 8: Compute residual covariance matrix Omega
residuals_var <- residuals(vecm_var)
Omega <- cov(residuals_var)
print("Computed residual covariance matrix Omega:")
[1] "Computed residual covariance matrix Omega:"
print(Omega)
resids of log_Spot
resids of log_Spot 0.0005644515
resids of log_Fut 0.0005321534
resids of log_USO 0.0004577378
resids of log_USL 0.0003636763
resids of log_OIL 0.0004834179
resids of log_Fut
resids of log_Spot 0.0005321534
resids of log_Fut 0.0005445740
resids of log_USO 0.0004583258
resids of log_USL 0.0003635811
resids of log_OIL 0.0004826371
resids of log_USO
resids of log_Spot 0.0004577378
resids of log_Fut 0.0004583258
resids of log_USO 0.0004424266
resids of log_USL 0.0003554061
resids of log_OIL 0.0004709637
resids of log_USL
resids of log_Spot 0.0003636763
resids of log_Fut 0.0003635811
resids of log_USO 0.0003554061
resids of log_USL 0.0003310452
resids of log_OIL 0.0003825727
resids of log_OIL
resids of log_Spot 0.0004834179
resids of log_Fut 0.0004826371
resids of log_USO 0.0004709637
resids of log_USL 0.0003825727
resids of log_OIL 0.0005346706
print("Dimensions of Omega:")
[1] "Dimensions of Omega:"
print(dim(Omega))
[1] 5 5
Step 9: Compute residual correlation matrix and its eigenvalues and eigenvectors
corr_matrix <- cov2cor(Omega)
eigen_result <- eigen(corr_matrix)
Lambda <- diag(eigen_resultvalues)G<−eigenresultvectors
print("Computed residual correlation matrix:")
[1] "Computed residual correlation matrix:"
print(corr_matrix)
resids of log_Spot
resids of log_Spot 1.0000000
resids of log_Fut 0.9598318
resids of log_USO 0.9159733
resids of log_USL 0.8413140
resids of log_OIL 0.8799668
resids of log_Fut
resids of log_Spot 0.9598318
resids of log_Fut 1.0000000
resids of log_USO 0.9337385
resids of log_USL 0.8563065
resids of log_OIL 0.8944358
resids of log_USO
resids of log_Spot 0.9159733
resids of log_Fut 0.9337385
resids of log_USO 1.0000000
resids of log_USL 0.9286681
resids of log_OIL 0.9683306
resids of log_USL
resids of log_Spot 0.8413140
resids of log_Fut 0.8563065
resids of log_USO 0.9286681
resids of log_USL 1.0000000
resids of log_OIL 0.9093419
resids of log_OIL
resids of log_Spot 0.8799668
resids of log_Fut 0.8944358
resids of log_USO 0.9683306
resids of log_USL 0.9093419
resids of log_OIL 1.0000000
print("Dimensions of correlation matrix:")
[1] "Dimensions of correlation matrix:"
print(dim(corr_matrix))
[1] 5 5
print("Eigenvalues of correlation matrix:")
[1] "Eigenvalues of correlation matrix:"
print(eigen_resultvalues))
[1] 5
print("Eigenvectors of correlation matrix:")
[1] "Eigenvectors of correlation matrix:"
print(G)
[,1] [,2] [,3]
[1,] -0.4434738 0.5646226 -0.18415788
[2,] -0.4480840 0.4715324 -0.09924278
[3,] -0.4580114 -0.1500695 0.26353846
[4,] -0.4373571 -0.5864481 -0.66839993
[5,] -0.4488832 -0.3040004 0.66334437
[,4] [,5]
[1,] 0.66946900 -0.04928965
[2,] -0.70169019 0.27323925
[3,] -0.16465350 -0.81923222
[4,] 0.03065071 0.13076471
[5,] 0.17717761 0.48442751
print("Dimensions of eigenvectors:")
[1] "Dimensions of eigenvectors:"
print(dim(G))
[1] 5 5
Step 10: Compute Cholesky decomposition of Omega
V <- t(chol(Omega))
print("Computed Cholesky decomposition of Omega (matrix V):")
[1] "Computed Cholesky decomposition of Omega (matrix V):"
print(V)
resids of log_Spot
resids of log_Spot 0.02375819
resids of log_Fut 0.02239874
resids of log_USO 0.01926653
resids of log_USL 0.01530741
resids of log_OIL 0.02034742
resids of log_Fut
resids of log_Spot 0.000000000
resids of log_Fut 0.006547554
resids of log_USO 0.004090063
resids of log_USL 0.003163691
resids of log_OIL 0.004105427
resids of log_USO
resids of log_Spot 0.000000000
resids of log_Fut 0.000000000
resids of log_USO 0.007382338
resids of log_USL 0.006440466
resids of log_OIL 0.008418477
resids of log_USL
resids of log_Spot 0.0000000000
resids of log_Fut 0.0000000000
resids of log_USO 0.0000000000
resids of log_USL 0.0067260564
resids of log_OIL 0.0005797034
resids of log_OIL
resids of log_Spot 0.000000000
resids of log_Fut 0.000000000
resids of log_USO 0.000000000
resids of log_USL 0.000000000
resids of log_OIL 0.005708903
print("Dimensions of matrix V:")
[1] "Dimensions of matrix V:"
print(dim(V))
[1] 5 5
Step 11: Compute F^M matrix
Lambda_inv_sqrt <- diag(1 / sqrt(eigen_result$values))
temp_matrix <- G %% Lambda_inv_sqrt %% t(G)
V_inv <- solve(V)
F_M_inv <- temp_matrix %*% V_inv
F_M <- solve(F_M_inv)
print("Computed F^M inverse matrix:")
[1] "Computed F^M inverse matrix:"
print(F_M_inv)
resids of log_Spot resids of log_Fut
[1,] 394.28409 -219.65122
[2,] -552.85929 628.98670
[3,] -16.70373 -616.82561
[4,] 50.63079 77.57887
[5,] 67.67344 184.29224
resids of log_USO resids of log_USL
[1,] -4.123141 -20.28489
[2,] -86.952894 -21.32204
[3,] 1177.184073 -123.78615
[4,] -354.546552 371.47613
[5,] -845.963463 -134.04522
resids of log_OIL
[1,] -44.44236
[2,] -32.12105
[3,] -349.31488
[4,] -96.23931
[5,] 607.50859
print("Dimensions of F^M inverse matrix:")
[1] "Dimensions of F^M inverse matrix:"
print(dim(F_M_inv))
[1] 5 5
print("Computed F^M matrix:")
[1] "Computed F^M matrix:"
print(F_M)
[,1] [,2]
resids of log_Spot 0.01591893 0.01091479
resids of log_Fut 0.01801606 0.01452761
resids of log_USO 0.01750445 0.01438370
resids of log_USL 0.01613659 0.01372343
resids of log_OIL 0.02069707 0.01743461
[,3] [,4]
resids of log_Spot 0.008741038 0.007265960
resids of log_Fut 0.010800059 0.008920024
resids of log_USO 0.013041080 0.010149610
resids of log_USL 0.013367719 0.013021034
resids of log_OIL 0.016859437 0.013491171
[,5]
resids of log_Spot 0.007918760
resids of log_Fut 0.009709158
resids of log_USO 0.011147485
resids of log_USL 0.011655206
resids of log_OIL 0.015913318
print("Dimensions of F^M matrix:")
[1] "Dimensions of F^M matrix:"
print(dim(F_M))
[1] 5 5
Step 12: Compute psi_1^G
psi_1_r <- Psi_1[1, ] # First row of Psi(1)
psi_1_G <- psi_1_r %*% F_M # This should now work without dimension errors
print("Computed psi_1^G:")
[1] "Computed psi_1^G:"
print(psi_1_G)
[,1] [,2] [,3]
[1,] 0.02512852 0.02163579 0.02126741
[,4] [,5]
[1,] 0.0244587 0.01653982
print("Dimensions of psi_1^G:")
[1] "Dimensions of psi_1^G:"
print(dim(psi_1_G))
[1] 1 5
Step 13: Compute numerator and denominator for GIS calculation
numerator <- psi_1_G^2
denominator <- as.numeric((psi_1_r) %% (Omega) %% t(psi_1_r))
Error in (psi_1_r) %% (Omega) %% t(psi_1_r) : non-conformable arguments