# Question One: Applying fGarch Package to fit E-GARCH(1,2) Model
#Loading Libraries
library(rugarch)
## Warning: package 'rugarch' was built under R version 4.4.3
## Loading required package: parallel
##
## Attaching package: 'rugarch'
## The following object is masked from 'package:stats':
##
## sigma
library(moments)
library(forecast)
## Warning: package 'forecast' was built under R version 4.4.3
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
#Loading the file
setwd("C:/Users/derek/OneDrive/Desktop/R Class")
D1 = read.csv("monday192025.csv");D1
## Year Month STOCK GDP CPI PENSION INFLATION CLAIMS INTEREST
## 1 2025 April 3.74 4.11 6.12 13.50 5.58 4.59 4.12
## 2 2025 March 3.81 3.62 6.33 13.59 5.53 4.53 3.97
## 3 2025 February 3.98 3.45 6.47 13.49 5.70 4.53 4.07
## 4 2025 January 4.21 3.28 6.48 13.47 4.04 4.63 4.20
## 5 2024 December 4.50 2.99 6.51 13.70 4.35 4.95 4.44
## 6 2024 November 4.81 2.75 6.56 13.73 4.28 5.20 4.62
## 7 2024 October 5.14 2.72 6.57 13.71 3.95 5.61 4.45
## 8 2024 September 5.50 3.56 6.64 13.83 3.73 6.24 4.38
## 9 2024 August 5.77 4.36 6.66 13.85 4.18 6.89 4.42
## 10 2024 July 5.97 4.31 6.68 13.76 4.46 7.40 4.40
## 11 2024 June 4.64 6.22 6.68 13.66 4.83 7.79 4.37
## 12 2024 May 6.49 5.10 6.81 13.65 4.50 7.98 4.50
## 13 2024 April 6.73 5.00 7.06 13.61 4.73 8.15 4.21
## 14 2024 March 6.97 5.70 7.16 13.61 5.72 8.33 4.22
## 15 2024 February 7.26 6.31 7.19 13.81 7.06 8.40 4.30
## 16 2024 January 7.50 6.85 7.19 14.23 8.04 8.36 4.59
## 17 2023 December 7.67 6.63 7.20 14.29 7.47 8.21 4.73
## 18 2023 November 7.87 6.80 7.25 14.39 9.21 8.13 4.96
## 19 2023 October 8.10 6.92 7.23 14.24 11.70 7.84 4.78
## 20 2023 September 8.32 6.78 7.25 14.25 11.48 7.20 4.56
## 21 2023 August 8.52 6.73 7.27 14.45 10.28 6.76 4.56
## 22 2023 July 8.68 7.28 7.33 15.32 9.04 6.43 4.55
## 23 2023 June 8.77 7.88 7.36 15.49 6.99 6.26 4.57
## 24 2023 May 8.78 8.03 7.46 17.28 6.35 6.30 4.57
## 25 2023 April 8.71 7.90 7.62 18.47 6.68 6.43 5.70
## 26 2023 March 8.59 9.19 7.72 22.51 6.47 6.48 5.75
## 27 2023 February 8.30 9.23 7.75 23.03 6.34 6.50 5.52
## 28 2023 January 7.95 8.98 7.77 23.04 6.26 6.47 5.27
## 29 2022 December 7.66 9.06 7.78 23.43 6.40 6.44 5.53
## 30 2022 November 7.38 9.48 7.86 22.36 5.80 6.46 5.79
## 31 2022 October 7.48 9.59 7.87 23.27 5.00 6.59 5.04
## 32 2022 September 6.81 9.18 7.88 20.84 5.27 6.72 4.87
## 33 2022 August 6.61 8.53 7.95 19.98 6.45 6.88 4.71
## 34 2022 July 6.45 8.32 8.02 18.61 6.84 6.87 4.76
## 35 2022 June 6.29 7.91 8.06 17.76 7.78 6.77 4.91
## 36 2022 May 6.16 7.08 8.21 17.42 8.01 6.58 4.94
## 37 2022 April 6.05 6.47 8.41 16.69 7.32 6.42 4.83
## 38 2022 March 6.29 5.56 8.43 16.67 6.72 6.31 4.59
## 39 2022 February 6.23 5.08 8.60 16.76 5.97 6.29 4.53
## 40 2022 January 6.08 5.39 8.66 15.11 5.84 6.34 5.20
## 41 2021 December 5.62 5.73 8.69 14.05 6.62 6.54 4.74
## 42 2021 November 6.10 5.80 8.83 13.05 7.03 6.63 5.50
## 43 2021 October 6.07 6.45 8.84 12.20 6.87 6.65 5.04
## 44 2021 September 5.35 6.91 8.96 12.08 7.08 6.69 5.68
## 45 2021 August 5.71 6.57 9.13 11.30 6.31 6.63 6.57
## 46 2021 July 5.53 6.55 9.16 10.83 5.61 6.63 6.92
## 47 2021 June 5.35 6.32 9.19 9.81 5.53 6.74 9.25
## 48 2021 May 5.20 5.87 9.28 10.13 6.02 6.88 9.06
## 49 2021 April 4.66 5.76 9.57 10.32 6.09 6.97 9.15
## 50 2021 March 5.17 5.90 9.60 11.90 6.43 7.08 9.29
## 51 2021 February 5.16 5.78 9.76 11.80 6.60 7.19 10.22
## 52 2021 January 5.74 5.69 9.85 11.97 8.36 7.33 10.47
## 53 2020 December 5.41 5.62 9.92 12.46 7.67 7.19 11.03
## 54 2020 November 5.53 5.33 10.06 12.77 7.39 7.05 11.70
## 55 2020 October 5.67 4.84 10.16 13.33 7.30 6.85 10.85
## 56 2020 September 5.79 4.20 10.11 13.62 6.41 6.58 8.63
## 57 2020 August 5.87 4.36 10.23 13.89 6.27 6.39 7.64
## 58 2020 July 6.01 4.36 10.28 14.23 6.86 6.21 8.15
## 59 2020 June 6.16 4.59 10.34 14.19 7.21 6.01 9.32
## 60 2020 May 6.18 5.33 10.54 14.17 7.15 5.72 8.48
## 61 2020 April 6.03 6.01 10.92 14.05 7.36 5.39 7.29
## 62 2020 March 5.84 5.84 10.96 13.90 7.76 5.05 7.01
## 63 2020 February 5.72 7.17 11.17 14.28 8.29 4.75 6.84
## 64 2020 January 5.29 5.78 11.32 14.19 6.67 4.50 6.82
## 65 2019 December 5.20 5.82 11.28 14.29 6.03 4.44 7.23
## 66 2019 November 5.19 5.56 11.43 14.65 4.91 4.56 6.33
## 67 2019 October 5.19 4.95 11.46 14.54 4.05 4.96 5.98
## 68 2019 September 5.24 3.83 11.49 14.47 4.14 5.61 5.86
## 69 2019 August 5.40 5.00 11.77 14.33 4.11 6.33 5.00
## 70 2019 July 5.32 6.27 12.18 14.24 4.45 7.24 4.66
## 71 2019 June 5.16 5.70 12.47 14.95 3.67 8.20 4.43
## 72 2019 May 5.04 4.49 12.79 13.89 3.20 9.38 4.45
## 73 2019 April 4.91 6.58 12.73 14.11 3.25 10.67 4.06
## 74 2019 April 4.91 6.58 13.07 14.06 4.14 12.04 3.77
## 75 2019 March 4.67 4.35 13.31 14.53 5.32 13.29 4.22
## 76 2019 February 4.65 4.14 13.32 15.21 6.09 14.33 3.52
## 77 2019 January 4.68 4.70 13.67 15.88 7.74 15.27 3.85
## 78 2018 December 4.69 5.71 13.79 16.02 10.05 15.97 4.07
## 79 2018 November 4.59 5.58 13.96 15.94 12.22 16.40 3.88
## 80 2018 October 4.53 5.53 14.01 15.88 13.06 16.50 4.19
## 81 2018 September 4.53 5.70 14.38 15.89 15.61 16.45 3.88
## 82 2018 August 4.63 4.04 14.40 18.37 16.69 15.93 3.50
## 83 2018 July 4.95 4.35 14.48 17.85 18.31 15.10 3.53
## 84 2018 June 5.20 4.28 14.56 16.87 18.93 14.02 3.47
## 85 2018 May 5.61 3.95 14.83 16.67 19.72 12.82 3.31
## 86 2018 April 6.24 3.73 14.97 16.35 18.91 11.49 3.10
## 87 2018 March 6.89 4.18 16.27 15.96 17.32 10.18 3.01
## 88 2018 February 7.40 4.46 16.40 15.39 16.67 9.00 2.82
## 89 2018 January 7.79 4.83 16.94 14.67 15.53 7.88 2.59
## 90 2017 December 7.98 4.50 18.68 12.99 14.48 6.88 2.36
## 91 2017 November 8.15 4.73 18.46 11.25 12.95 5.96 2.21
## 92 2017 October 8.33 5.72 18.54 9.66 12.05 5.20 1.78
## 93 2017 September 8.40 7.06 18.85 8.93 9.19 4.49 1.65
## 94 2017 August 8.36 8.04 18.62 8.18 6.54 4.05 1.71
## 95 2017 July 8.21 7.47 18.92 7.55 5.42 3.93 1.38
## 96 2017 June 8.13 9.21 19.46 7.83 4.51 3.96 1.25
## 97 2017 May 7.84 11.70 19.59 7.65 3.84 4.02 1.16
## 98 2017 April 7.20 11.48 21.16 7.79 3.18 4.12 0.86
## 99 2017 March 6.76 10.28 21.83 8.44 3.21 4.40 0.75
## 100 2017 February 6.43 9.04 23.31 9.10 3.22 4.69 0.93
## 101 2017 January 6.26 6.99 24.16 9.48 3.57 5.03 1.26
## 102 2016 December 6.30 6.35 25.99 9.74 3.49 5.43 1.26
## 103 2016 November 6.43 6.68 26.47 10.38 3.88 5.85 1.18
## 104 2016 October 6.48 6.47 27.38 9.17 3.66 6.32 1.46
## 105 2016 September 6.50 6.34 29.02 8.01 3.97 7.03 1.24
## 106 2016 August 6.47 6.26 29.32 7.61 5.18 7.88 1.11
## 107 2016 July 6.44 6.40 29.63 7.21 5.95 8.64 1.00
## 108 2016 June 6.46 5.80 30.11 7.01 5.32 9.24 1.15
## 109 2016 May 6.59 5.00 31.64 6.67 5.00 10.24 0.96
## 110 2016 April 6.72 5.27 32.65 6.26 6.62 11.42 0.88
## 111 2016 March 6.88 6.45 33.54 6.22 6.74 12.41 0.73
## 112 2016 February 6.87 6.84 34.44 6.22 7.36 13.42 0.96
## 113 2016 January 6.77 7.78 34.09 6.20 8.44 14.35 0.76
## 114 2015 December 6.58 8.01 33.46 6.22 8.60 15.11 0.52
## 115 2015 November 6.42 7.32 33.70 6.54 9.61 15.93 0.43
## 116 2015 October 6.31 6.72 33.26 6.55 12.42 16.72 0.43
## 117 2015 September 6.29 5.97 32.75 6.92 14.60 17.07 0.32
## 118 2015 August 6.34 5.84 32.92 6.58 14.69 16.87 0.64
## 119 2015 July 6.54 6.62 32.12 6.44 13.22 16.56 0.88
#Loading the bonds
D2 = D1$STOCK;D2
## [1] 3.74 3.81 3.98 4.21 4.50 4.81 5.14 5.50 5.77 5.97 4.64 6.49 6.73 6.97 7.26
## [16] 7.50 7.67 7.87 8.10 8.32 8.52 8.68 8.77 8.78 8.71 8.59 8.30 7.95 7.66 7.38
## [31] 7.48 6.81 6.61 6.45 6.29 6.16 6.05 6.29 6.23 6.08 5.62 6.10 6.07 5.35 5.71
## [46] 5.53 5.35 5.20 4.66 5.17 5.16 5.74 5.41 5.53 5.67 5.79 5.87 6.01 6.16 6.18
## [61] 6.03 5.84 5.72 5.29 5.20 5.19 5.19 5.24 5.40 5.32 5.16 5.04 4.91 4.91 4.67
## [76] 4.65 4.68 4.69 4.59 4.53 4.53 4.63 4.95 5.20 5.61 6.24 6.89 7.40 7.79 7.98
## [91] 8.15 8.33 8.40 8.36 8.21 8.13 7.84 7.20 6.76 6.43 6.26 6.30 6.43 6.48 6.50
## [106] 6.47 6.44 6.46 6.59 6.72 6.88 6.87 6.77 6.58 6.42 6.31 6.29 6.34 6.54
#Model Fit
M1 = ugarchspec(
variance.model = list(model = "eGARCH", garchOrder =c(1,2)),
mean.model = list(armaOrder = c(1,2), include.mean = FALSE),
distribution.model = "norm"
)
print(M1)
##
## *---------------------------------*
## * GARCH Model Spec *
## *---------------------------------*
##
## Conditional Variance Dynamics
## ------------------------------------
## GARCH Model : eGARCH(1,2)
## Variance Targeting : FALSE
##
## Conditional Mean Dynamics
## ------------------------------------
## Mean Model : ARFIMA(1,0,2)
## Include Mean : FALSE
## GARCH-in-Mean : FALSE
##
## Conditional Distribution
## ------------------------------------
## Distribution : norm
## Includes Skew : FALSE
## Includes Shape : FALSE
## Includes Lambda : FALSE
#Model summary
summary(M1)
## Length Class Mode
## 1 uGARCHspec S4
#Log returns
log.D2 = diff(D2);log.D2
## [1] 0.07 0.17 0.23 0.29 0.31 0.33 0.36 0.27 0.20 -1.33 1.85 0.24
## [13] 0.24 0.29 0.24 0.17 0.20 0.23 0.22 0.20 0.16 0.09 0.01 -0.07
## [25] -0.12 -0.29 -0.35 -0.29 -0.28 0.10 -0.67 -0.20 -0.16 -0.16 -0.13 -0.11
## [37] 0.24 -0.06 -0.15 -0.46 0.48 -0.03 -0.72 0.36 -0.18 -0.18 -0.15 -0.54
## [49] 0.51 -0.01 0.58 -0.33 0.12 0.14 0.12 0.08 0.14 0.15 0.02 -0.15
## [61] -0.19 -0.12 -0.43 -0.09 -0.01 0.00 0.05 0.16 -0.08 -0.16 -0.12 -0.13
## [73] 0.00 -0.24 -0.02 0.03 0.01 -0.10 -0.06 0.00 0.10 0.32 0.25 0.41
## [85] 0.63 0.65 0.51 0.39 0.19 0.17 0.18 0.07 -0.04 -0.15 -0.08 -0.29
## [97] -0.64 -0.44 -0.33 -0.17 0.04 0.13 0.05 0.02 -0.03 -0.03 0.02 0.13
## [109] 0.13 0.16 -0.01 -0.10 -0.19 -0.16 -0.11 -0.02 0.05 0.20
#Plot logs
plot(log.D2)

ts.plot(log.D2)

#Removing negatives
s = log.D2^2;s
## [1] 0.0049 0.0289 0.0529 0.0841 0.0961 0.1089 0.1296 0.0729 0.0400 1.7689
## [11] 3.4225 0.0576 0.0576 0.0841 0.0576 0.0289 0.0400 0.0529 0.0484 0.0400
## [21] 0.0256 0.0081 0.0001 0.0049 0.0144 0.0841 0.1225 0.0841 0.0784 0.0100
## [31] 0.4489 0.0400 0.0256 0.0256 0.0169 0.0121 0.0576 0.0036 0.0225 0.2116
## [41] 0.2304 0.0009 0.5184 0.1296 0.0324 0.0324 0.0225 0.2916 0.2601 0.0001
## [51] 0.3364 0.1089 0.0144 0.0196 0.0144 0.0064 0.0196 0.0225 0.0004 0.0225
## [61] 0.0361 0.0144 0.1849 0.0081 0.0001 0.0000 0.0025 0.0256 0.0064 0.0256
## [71] 0.0144 0.0169 0.0000 0.0576 0.0004 0.0009 0.0001 0.0100 0.0036 0.0000
## [81] 0.0100 0.1024 0.0625 0.1681 0.3969 0.4225 0.2601 0.1521 0.0361 0.0289
## [91] 0.0324 0.0049 0.0016 0.0225 0.0064 0.0841 0.4096 0.1936 0.1089 0.0289
## [101] 0.0016 0.0169 0.0025 0.0004 0.0009 0.0009 0.0004 0.0169 0.0169 0.0256
## [111] 0.0001 0.0100 0.0361 0.0256 0.0121 0.0004 0.0025 0.0400
#Log Returns
log.D2 = diff(D2);log.D2
## [1] 0.07 0.17 0.23 0.29 0.31 0.33 0.36 0.27 0.20 -1.33 1.85 0.24
## [13] 0.24 0.29 0.24 0.17 0.20 0.23 0.22 0.20 0.16 0.09 0.01 -0.07
## [25] -0.12 -0.29 -0.35 -0.29 -0.28 0.10 -0.67 -0.20 -0.16 -0.16 -0.13 -0.11
## [37] 0.24 -0.06 -0.15 -0.46 0.48 -0.03 -0.72 0.36 -0.18 -0.18 -0.15 -0.54
## [49] 0.51 -0.01 0.58 -0.33 0.12 0.14 0.12 0.08 0.14 0.15 0.02 -0.15
## [61] -0.19 -0.12 -0.43 -0.09 -0.01 0.00 0.05 0.16 -0.08 -0.16 -0.12 -0.13
## [73] 0.00 -0.24 -0.02 0.03 0.01 -0.10 -0.06 0.00 0.10 0.32 0.25 0.41
## [85] 0.63 0.65 0.51 0.39 0.19 0.17 0.18 0.07 -0.04 -0.15 -0.08 -0.29
## [97] -0.64 -0.44 -0.33 -0.17 0.04 0.13 0.05 0.02 -0.03 -0.03 0.02 0.13
## [109] 0.13 0.16 -0.01 -0.10 -0.19 -0.16 -0.11 -0.02 0.05 0.20
#Plotting Log Returns
plot(log.D2)

ts.plot(log.D2)

#Model Fitting
M1fit = ugarchfit(data = log.D2, spec = M1);M1fit
##
## *---------------------------------*
## * GARCH Model Fit *
## *---------------------------------*
##
## Conditional Variance Dynamics
## -----------------------------------
## GARCH Model : eGARCH(1,2)
## Mean Model : ARFIMA(1,0,2)
## Distribution : norm
##
## Optimal Parameters
## ------------------------------------
## Estimate Std. Error t value Pr(>|t|)
## ar1 0.311434 0.042572 7.3154 0.000000
## ma1 0.010609 0.018537 0.5723 0.567121
## ma2 0.073935 0.012835 5.7607 0.000000
## omega -0.527684 0.313081 -1.6855 0.091901
## alpha1 0.299353 0.160045 1.8704 0.061424
## beta1 0.425412 0.122959 3.4598 0.000541
## beta2 0.338834 0.125646 2.6967 0.007002
## gamma1 1.196180 0.261571 4.5731 0.000005
##
## Robust Standard Errors:
## Estimate Std. Error t value Pr(>|t|)
## ar1 0.311434 0.044898 6.93644 0.000000
## ma1 0.010609 0.001744 6.08321 0.000000
## ma2 0.073935 0.001853 39.89242 0.000000
## omega -0.527684 0.561063 -0.94051 0.346957
## alpha1 0.299353 0.231491 1.29315 0.195959
## beta1 0.425412 0.116389 3.65509 0.000257
## beta2 0.338834 0.135398 2.50250 0.012332
## gamma1 1.196180 0.301857 3.96273 0.000074
##
## LogLikelihood : -0.1537826
##
## Information Criteria
## ------------------------------------
##
## Akaike 0.13820
## Bayes 0.32604
## Shibata 0.12976
## Hannan-Quinn 0.21447
##
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.2713 0.6025
## Lag[2*(p+q)+(p+q)-1][8] 4.5994 0.4182
## Lag[4*(p+q)+(p+q)-1][14] 8.5612 0.2816
## d.o.f=3
## H0 : No serial correlation
##
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.004073 0.9491
## Lag[2*(p+q)+(p+q)-1][8] 0.457946 0.9971
## Lag[4*(p+q)+(p+q)-1][14] 0.884533 0.9998
## d.o.f=3
##
## Weighted ARCH LM Tests
## ------------------------------------
## Statistic Shape Scale P-Value
## ARCH Lag[4] 0.1196 0.500 2.000 0.7295
## ARCH Lag[6] 0.2442 1.461 1.711 0.9587
## ARCH Lag[8] 0.4800 2.368 1.583 0.9834
##
## Nyblom stability test
## ------------------------------------
## Joint Statistic: 2.06
## Individual Statistics:
## ar1 0.3068
## ma1 0.4813
## ma2 0.1811
## omega 0.2880
## alpha1 0.1142
## beta1 0.3859
## beta2 0.3969
## gamma1 0.2344
##
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic: 1.89 2.11 2.59
## Individual Statistic: 0.35 0.47 0.75
##
## Sign Bias Test
## ------------------------------------
## t-value prob sig
## Sign Bias 0.1323865 0.8949
## Negative Sign Bias 0.0001613 0.9999
## Positive Sign Bias 0.5647460 0.5734
## Joint Effect 0.3264686 0.9550
##
##
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
## group statistic p-value(g-1)
## 1 20 32.17 0.02991
## 2 30 41.15 0.06678
## 3 40 55.56 0.04147
## 4 50 68.62 0.03351
##
##
## Elapsed time : 0.8137319
#Plots
par(mfrow = c(3, 4)) # 3x4 grid for 12 plots
plot_types <- c(1:12)
plot_titles <- c(
"Series with 2 Conditional SD",
"Series with 1% VaR Limits",
"Conditional SD vs Absolute Returns",
"ACF of Returns",
"ACF of Squared Returns",
"ACF of Absolute Returns",
"Cross Correlation",
"Density of Standardized Residuals",
"QQ-Plot of Residuals",
"ACF of Standardized Residuals",
"ACF of Squared Standardized Residuals",
"News-Impact Curve"
)
for (i in seq_along(plot_types)) {
plot(M1fit, which = plot_types[i])
title(main = plot_titles[i], line = 2.5)
grid()
}
##
## please wait...calculating quantiles...

#Summary
M1fit
##
## *---------------------------------*
## * GARCH Model Fit *
## *---------------------------------*
##
## Conditional Variance Dynamics
## -----------------------------------
## GARCH Model : eGARCH(1,2)
## Mean Model : ARFIMA(1,0,2)
## Distribution : norm
##
## Optimal Parameters
## ------------------------------------
## Estimate Std. Error t value Pr(>|t|)
## ar1 0.311434 0.042572 7.3154 0.000000
## ma1 0.010609 0.018537 0.5723 0.567121
## ma2 0.073935 0.012835 5.7607 0.000000
## omega -0.527684 0.313081 -1.6855 0.091901
## alpha1 0.299353 0.160045 1.8704 0.061424
## beta1 0.425412 0.122959 3.4598 0.000541
## beta2 0.338834 0.125646 2.6967 0.007002
## gamma1 1.196180 0.261571 4.5731 0.000005
##
## Robust Standard Errors:
## Estimate Std. Error t value Pr(>|t|)
## ar1 0.311434 0.044898 6.93644 0.000000
## ma1 0.010609 0.001744 6.08321 0.000000
## ma2 0.073935 0.001853 39.89242 0.000000
## omega -0.527684 0.561063 -0.94051 0.346957
## alpha1 0.299353 0.231491 1.29315 0.195959
## beta1 0.425412 0.116389 3.65509 0.000257
## beta2 0.338834 0.135398 2.50250 0.012332
## gamma1 1.196180 0.301857 3.96273 0.000074
##
## LogLikelihood : -0.1537826
##
## Information Criteria
## ------------------------------------
##
## Akaike 0.13820
## Bayes 0.32604
## Shibata 0.12976
## Hannan-Quinn 0.21447
##
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.2713 0.6025
## Lag[2*(p+q)+(p+q)-1][8] 4.5994 0.4182
## Lag[4*(p+q)+(p+q)-1][14] 8.5612 0.2816
## d.o.f=3
## H0 : No serial correlation
##
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.004073 0.9491
## Lag[2*(p+q)+(p+q)-1][8] 0.457946 0.9971
## Lag[4*(p+q)+(p+q)-1][14] 0.884533 0.9998
## d.o.f=3
##
## Weighted ARCH LM Tests
## ------------------------------------
## Statistic Shape Scale P-Value
## ARCH Lag[4] 0.1196 0.500 2.000 0.7295
## ARCH Lag[6] 0.2442 1.461 1.711 0.9587
## ARCH Lag[8] 0.4800 2.368 1.583 0.9834
##
## Nyblom stability test
## ------------------------------------
## Joint Statistic: 2.06
## Individual Statistics:
## ar1 0.3068
## ma1 0.4813
## ma2 0.1811
## omega 0.2880
## alpha1 0.1142
## beta1 0.3859
## beta2 0.3969
## gamma1 0.2344
##
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic: 1.89 2.11 2.59
## Individual Statistic: 0.35 0.47 0.75
##
## Sign Bias Test
## ------------------------------------
## t-value prob sig
## Sign Bias 0.1323865 0.8949
## Negative Sign Bias 0.0001613 0.9999
## Positive Sign Bias 0.5647460 0.5734
## Joint Effect 0.3264686 0.9550
##
##
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
## group statistic p-value(g-1)
## 1 20 32.17 0.02991
## 2 30 41.15 0.06678
## 3 40 55.56 0.04147
## 4 50 68.62 0.03351
##
##
## Elapsed time : 0.8137319
#1) Basic Statistics for Returns with Skewness/Kurtosis Interpretation
# Calculate basic statistics
returns_stats <- data.frame(
Mean = mean(log.D2, na.rm = TRUE),
SD = sd(log.D2, na.rm = TRUE),
Min = min(log.D2, na.rm = TRUE),
Max = max(log.D2, na.rm = TRUE),
Skewness = moments::skewness(log.D2, na.rm = TRUE),
Kurtosis = moments::kurtosis(log.D2, na.rm = TRUE),
JB_test = tseries::jarque.bera.test(na.omit(log.D2))$p.value
)
knitr::kable(returns_stats, caption = "Descriptive Statistics of Log Returns")
Descriptive Statistics of Log Returns
0.0237288 |
0.3299878 |
-1.33 |
1.85 |
0.7383968 |
11.80859 |
0 |
# Interpretation guidelines
cat("Skewness Interpretation:
Normal ~0 | Negative (<-0.5) Left-skewed | Positive (>0.5) Right-skewed
Kurtosis Interpretation:
Normal ~3 | <3 (Platykurtic - lighter tails) | >3 (Leptokurtic - heavier tails)
")
## Skewness Interpretation:
## Normal ~0 | Negative (<-0.5) Left-skewed | Positive (>0.5) Right-skewed
##
## Kurtosis Interpretation:
## Normal ~3 | <3 (Platykurtic - lighter tails) | >3 (Leptokurtic - heavier tails)
#2) EGARCH(1,2) Parameter Analysis (3×5 Matrix)
# Extracting and formating parameters
params <- data.frame(
Parameter = c("ω", "α1", "γ1", "β1", "β2"),
Estimate = M1fit@fit$matcoef[,1][1:5],
Std.Error = M1fit@fit$matcoef[,2][1:5],
t.value = M1fit@fit$matcoef[,3][1:5],
p.value = M1fit@fit$matcoef[,4][1:5]
)
knitr::kable(params, caption = "EGARCH(1,2) Parameter Estimates")
EGARCH(1,2) Parameter Estimates
ar1 |
ω |
0.3114335 |
0.0425721 |
7.3154436 |
0.0000000 |
ma1 |
α1 |
0.0106089 |
0.0185375 |
0.5722966 |
0.5671210 |
ma2 |
γ1 |
0.0739352 |
0.0128345 |
5.7606579 |
0.0000000 |
omega |
β1 |
-0.5276837 |
0.3130806 |
-1.6854562 |
0.0919006 |
alpha1 |
β2 |
0.2993528 |
0.1600448 |
1.8704310 |
0.0614240 |
# Interpretation
cat("Parameter Ranges:
α (Alpha): Measures volatility reaction to shocks (Typical range 0.05-0.3)
γ (Gamma): Asymmetry effect (Negative values indicate negative shocks increase volatility more)
β (Beta): Volatility persistence (Sum should be <1 for stationarity, typical range 0.8-0.98)
")
## Parameter Ranges:
## α (Alpha): Measures volatility reaction to shocks (Typical range 0.05-0.3)
## γ (Gamma): Asymmetry effect (Negative values indicate negative shocks increase volatility more)
## β (Beta): Volatility persistence (Sum should be <1 for stationarity, typical range 0.8-0.98)
#3) Model Selection Criteria Comparison (4×4 Matrix)
selection_criteria <- data.frame(
Criterion = c("AIC", "BIC", "SIC", "HQIC"),
EGARCH = c(infocriteria(M1fit)[1], infocriteria(M1fit)[2],
infocriteria(M1fit)[3], infocriteria(M1fit)[4]),
GARCH = c(infocriteria(ugarchfit(ugarchspec(variance.model=list(garchOrder=c(1,1))), log.D2))[1:4]),
TGARCH = c(infocriteria(ugarchfit(ugarchspec(variance.model=list(model="apARCH")), log.D2))[1:4])
)
knitr::kable(selection_criteria, caption = "Model Selection Criteria Comparison")
Model Selection Criteria Comparison
AIC |
0.1381997 |
0.4936318 |
0.2552942 |
BIC |
0.3260427 |
0.6345140 |
0.4431373 |
SIC |
0.1297617 |
0.4887867 |
0.2468562 |
HQIC |
0.2144695 |
0.5508341 |
0.3315640 |
# Interpretation rule: Lower values indicate better model fit
#4) 60-Month Forecast at 99% CI with Visualization
# Generate forecasts
forecast <- ugarchforecast(M1fit, n.ahead = 60, n.roll = 0)
cond_vol <- sigma(forecast)
mean_forecast <- fitted(forecast)
# Create confidence intervals
upper_99 <- mean_forecast + qnorm(0.995) * cond_vol
lower_99 <- mean_forecast - qnorm(0.995) * cond_vol
# Plotting
# Reset plot layout
par(mfrow = c(1, 1))
plot(1:60, mean_forecast, type = "l", col = "blue", ylim = range(c(lower_99, upper_99)),
main = "60-Month Stock Index Forecast (99% CI)", xlab = "Months", ylab = "Log Returns")
polygon(c(1:60, 60:1), c(upper_99, rev(lower_99)), col = "gray80", border = NA)
lines(mean_forecast, col = "red", lwd = 2)
legend("topright", legend = c("Mean Forecast", "99% CI"),
col = c("red", "gray80"), lwd = c(2,10), bty = "n")

#Question Two; Fitting Kaplan-Meir Model Using aml data in survival package
#Importing Libraries
library(survival)
library(survminer)
## Warning: package 'survminer' was built under R version 4.4.3
## Loading required package: ggplot2
## Loading required package: ggpubr
## Warning: package 'ggpubr' was built under R version 4.4.3
##
## Attaching package: 'ggpubr'
## The following object is masked from 'package:forecast':
##
## gghistogram
##
## Attaching package: 'survminer'
## The following object is masked from 'package:survival':
##
## myeloma
#Dataframe
D1=data.frame(time = c(1,2,2,3,3,5,6,8,9,10),
event = c(0,1,0,1,0,1,0,1,0,1))
km=with(D1,Surv(time,event));km
## [1] 1+ 2 2+ 3 3+ 5 6+ 8 9+ 10
#Fitting the Kaplan-Meir Model
km = surv_fit(Surv(time, event) ~ 1, data = D1); km
## Call: survfit(formula = Surv(time, event) ~ 1, data = structure(list(
## time = c(1, 2, 2, 3, 3, 5, 6, 8, 9, 10), event = c(0, 1,
## 0, 1, 0, 1, 0, 1, 0, 1)), class = "data.frame", row.names = c(NA,
## -10L)))
##
## n events median 0.95LCL 0.95UCL
## [1,] 10 5 8 5 NA
#Summary
km
## Call: survfit(formula = Surv(time, event) ~ 1, data = structure(list(
## time = c(1, 2, 2, 3, 3, 5, 6, 8, 9, 10), event = c(0, 1,
## 0, 1, 0, 1, 0, 1, 0, 1)), class = "data.frame", row.names = c(NA,
## -10L)))
##
## n events median 0.95LCL 0.95UCL
## [1,] 10 5 8 5 NA
#Plotting Kaplan-Meir curve
plot(km,
col = "coral",
lwd = 2,
xlab = "Time",
ylab = "Survival Probability",
main = "Kaplan-Meier Curve")

# Plotting cumulative hazard
plot(
km,
fun = "cumhaz",
col = "green",
lwd = 2,
xlab = "Time (Days)",
ylab = "Cumulative Hazard",
main = "Cumulative Hazard Function"
)

#Question Four; Fitting Weibull Statistical Model
#to estimate parameters of distance between
#real returns and squared returns for maximum goodness of fit
#load the necessary libraries
library(fitdistrplus)
## Warning: package 'fitdistrplus' was built under R version 4.4.3
## Loading required package: MASS
##
## Attaching package: 'fitdistrplus'
## The following object is masked from 'package:rugarch':
##
## fitdist
library(goftest)
#Loading Files
setwd("C:/Users/derek/OneDrive/Desktop/R Class")
data = read.csv("Monday192025.csv")
cpi = data$CPI
# convert simple returns to logs
D1 = diff(cpi); D1
## [1] 0.21 0.14 0.01 0.03 0.05 0.01 0.07 0.02 0.02 0.00 0.13 0.25
## [13] 0.10 0.03 0.00 0.01 0.05 -0.02 0.02 0.02 0.06 0.03 0.10 0.16
## [25] 0.10 0.03 0.02 0.01 0.08 0.01 0.01 0.07 0.07 0.04 0.15 0.20
## [37] 0.02 0.17 0.06 0.03 0.14 0.01 0.12 0.17 0.03 0.03 0.09 0.29
## [49] 0.03 0.16 0.09 0.07 0.14 0.10 -0.05 0.12 0.05 0.06 0.20 0.38
## [61] 0.04 0.21 0.15 -0.04 0.15 0.03 0.03 0.28 0.41 0.29 0.32 -0.06
## [73] 0.34 0.24 0.01 0.35 0.12 0.17 0.05 0.37 0.02 0.08 0.08 0.27
## [85] 0.14 1.30 0.13 0.54 1.74 -0.22 0.08 0.31 -0.23 0.30 0.54 0.13
## [97] 1.57 0.67 1.48 0.85 1.83 0.48 0.91 1.64 0.30 0.31 0.48 1.53
## [109] 1.01 0.89 0.90 -0.35 -0.63 0.24 -0.44 -0.51 0.17 -0.80
# fit the distributions
D1_positive = D1[D1 > 0]
#Fitting Anderson Darling Model
fitwMGEAD = fitdist(data = D1_positive,
distr = "weibull",
method = "mge", gof = "AD")
summary(fitwMGEAD)
## Fitting of the distribution ' weibull ' by maximum goodness-of-fit
## Parameters :
## estimate
## shape 0.7634249
## scale 0.2174130
## Loglikelihood: 36.49133 AIC: -68.98267 BIC: -63.67475
#Fitting Cramer von Misses
fitwMGECvM = fitdist(data = D1_positive,
distr = "weibull",
method = "mge", gof = "CvM")
summary(fitwMGECvM)
## Fitting of the distribution ' weibull ' by maximum goodness-of-fit
## Parameters :
## estimate
## shape 0.8263981
## scale 0.1991286
## Loglikelihood: 34.45488 AIC: -64.90977 BIC: -59.60185
#Fitting Kolmogorov Smirnov
fitwMGEKS = fitdist(data = D1_positive,
distr = "weibull",
method = "mge", gof = "KS")
summary(fitwMGEKS)
## Fitting of the distribution ' weibull ' by maximum goodness-of-fit
## Parameters :
## estimate
## shape 0.8401815
## scale 0.2120730
## Loglikelihood: 34.87007 AIC: -65.74015 BIC: -60.43222
# quantile match
q25 = quantile(D1_positive, probs = 0.25); q25
## 25%
## 0.04
q75 = quantile(D1_positive, probs = 0.75); q75
## 75%
## 0.3
# Histogram with Weibull fit
# Reset plot layout
par(mfrow = c(1, 1))
hist(D1_positive, probability = TRUE,
main = "Histogram with Weibull Fit (AD)",
xlab = "Positive CPI Differences", col = "lightblue")
curve(dweibull(x, shape = fitwMGEAD$estimate["shape"],
scale = fitwMGEAD$estimate["scale"]),
col = "red", lwd = 2, add = TRUE)
legend("topright", legend = "Weibull Fit", col = "red", lwd = 2)

# P-P Plot
ppcomp(list(fitwMGEAD), main = "P-P Plot for Weibull Fit (AD)",
xlab = "Theoretical Probabilities",
ylab = "Empirical Probabilities")

# THE END THANKS!