#QUESTION 1
library(rugarch)
## Loading required package: parallel
## 
## Attaching package: 'rugarch'
## The following object is masked from 'package:stats':
## 
##     sigma
library(timeSeries)
## Loading required package: timeDate
## 
## Attaching package: 'timeSeries'
## The following object is masked from 'package:rugarch':
## 
##     quantile
## The following objects are masked from 'package:graphics':
## 
##     lines, points
library(fGarch)
## NOTE: Packages 'fBasics', 'timeDate', and 'timeSeries' are no longer
## attached to the search() path when 'fGarch' is attached.
## 
## If needed attach them yourself in your R script by e.g.,
##         require("timeSeries")
library(PerformanceAnalytics)
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following object is masked from 'package:timeSeries':
## 
##     time<-
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## 
## Attaching package: 'PerformanceAnalytics'
## The following objects are masked from 'package:fGarch':
## 
##     ES, VaR
## The following objects are masked from 'package:timeDate':
## 
##     kurtosis, skewness
## The following object is masked from 'package:graphics':
## 
##     legend
data= read.csv("DATA25.csv");data
##     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
D1=data$STOCK;D1
##   [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
D2=diff(D1);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(D2)

ts.plot(D2)

ts.plot(D2)
ts.plot(D2, main = "stock Log Return", ylab = "Log Returns")

M1 = garchFit(formula = ~garch(1,2), data = D2, cond.dist = "snorm"); M1
## 
## Series Initialization:
##  ARMA Model:                arma
##  Formula Mean:              ~ arma(0, 0)
##  GARCH Model:               garch
##  Formula Variance:          ~ garch(1, 2)
##  ARMA Order:                0 0
##  Max ARMA Order:            0
##  GARCH Order:               1 2
##  Max GARCH Order:           2
##  Maximum Order:             2
##  Conditional Dist:          snorm
##  h.start:                   3
##  llh.start:                 1
##  Length of Series:          118
##  Recursion Init:            mci
##  Series Scale:              0.3299878
## 
## Parameter Initialization:
##  Initial Parameters:          $params
##  Limits of Transformations:   $U, $V
##  Which Parameters are Fixed?  $includes
##  Parameter Matrix:
##                      U           V     params includes
##     mu     -0.71908150   0.7190815 0.07190815     TRUE
##     omega   0.00000100 100.0000000 0.10000000     TRUE
##     alpha1  0.00000001   1.0000000 0.10000000     TRUE
##     gamma1 -0.99999999   1.0000000 0.10000000    FALSE
##     beta1   0.00000001   1.0000000 0.40000000     TRUE
##     beta2   0.00000001   1.0000000 0.40000000     TRUE
##     delta   0.00000000   2.0000000 2.00000000    FALSE
##     skew    0.10000000  10.0000000 1.00000000     TRUE
##     shape   1.00000000  10.0000000 4.00000000    FALSE
##  Index List of Parameters to be Optimized:
##     mu  omega alpha1  beta1  beta2   skew 
##      1      2      3      5      6      8 
##  Persistence:                  0.9 
## 
## 
## --- START OF TRACE ---
## Selected Algorithm: nlminb 
## 
## R coded nlminb Solver: 
## 
##   0:     158.22240: 0.0719082 0.100000 0.100000 0.400000 0.400000  1.00000
##   1:     157.00115: 0.0718866 0.0848552 0.0963416 0.382913 0.382824 0.997854
##   2:     156.11519: 0.0718169 0.0908528 0.123383 0.381533 0.381947 0.989882
##   3:     155.35927: 0.0717291 0.0795271 0.134394 0.365653 0.366012 0.981090
##   4:     154.52650: 0.0715898 0.0901513 0.157160 0.363245 0.363594 0.967384
##   5:     153.85902: 0.0714515 0.0827135 0.166454 0.347021 0.346636 0.955627
##   6:     153.09593: 0.0712784 0.0971498 0.186338 0.344822 0.343462 0.941139
##   7:     152.49917: 0.0711504 0.0882191 0.194524 0.328256 0.325769 0.931290
##   8:     151.80588: 0.0709914 0.104495 0.214564 0.328031 0.324132 0.918630
##   9:     151.14289: 0.0708532 0.0958646 0.222030 0.311531 0.305882 0.908875
##  10:     150.48882: 0.0706986 0.110922 0.243120 0.310850 0.303484 0.896545
##  11:     149.77795: 0.0705455 0.103160 0.251082 0.294777 0.284927 0.886372
##  12:     148.70988: 0.0701797 0.130087 0.293028 0.288816 0.273359 0.859006
##  13:     147.14418: 0.0697308 0.122400 0.300881 0.258228 0.230864 0.836039
##  14:     146.14907: 0.0693365 0.148798 0.344514 0.257389 0.221559 0.809382
##  15:     145.03265: 0.0688743 0.128836 0.355393 0.230236 0.179659 0.789948
##  16:     144.07209: 0.0682625 0.158863 0.393506 0.239960 0.169948 0.761431
##  17:     143.18237: 0.0676038 0.135146 0.411172 0.222054 0.128009 0.741666
##  18:     142.26338: 0.0666659 0.154691 0.450737 0.246569 0.115836 0.717959
##  19:     141.46902: 0.0657696 0.128013 0.475605 0.245733 0.0750194 0.701332
##  20:     140.58825: 0.0647495 0.140135 0.512082 0.280969 0.0573670 0.687308
##  21:     139.78260: 0.0638370 0.118387 0.536955 0.292982 0.0143258 0.673924
##  22:     139.29099: 0.0632218 0.144400 0.565803 0.326805 1.00000e-08 0.663623
##  23:     138.71445: 0.0618675 0.0988726 0.588715 0.340646 1.00000e-08 0.646750
##  24:     138.45571: 0.0586914 0.110209 0.624425 0.348787 1.00000e-08 0.649521
##  25:     138.09292: 0.0552808 0.105936 0.636322 0.316986 1.00000e-08 0.652348
##  26:     137.89714: 0.0528465 0.116320 0.667950 0.320506 1.00000e-08 0.618259
##  27:     137.65249: 0.0491862 0.0917482 0.681645 0.314804 1.00000e-08 0.621970
##  28:     137.40234: 0.0458058 0.105439 0.702605 0.305152 1.00000e-08 0.644302
##  29:     137.21752: 0.0418394 0.104567 0.700496 0.290545 1.00000e-08 0.631357
##  30:     137.02326: 0.0378728 0.106251 0.714713 0.302440 1.00000e-08 0.625087
##  31:     136.89262: 0.0339824 0.0922285 0.731010 0.301017 1.00000e-08 0.631189
##  32:     136.68177: 0.0298741 0.103516 0.733704 0.295725 1.00000e-08 0.632549
##  33:     136.53865: 0.0258629 0.101379 0.733617 0.282447 1.00000e-08 0.620881
##  34:     135.46147: -0.0148118 0.0869752 0.775421 0.287650 1.00000e-08 0.641636
##  35:     134.43178: -0.0554155 0.121756 0.741130 0.283905 1.00000e-08 0.608036
##  36:     134.26206: -0.0953977 0.145425 0.788345 0.200213 1.00000e-08 0.666517
##  37:     133.59275: -0.135793 0.130915 0.822745 0.218306 1.00000e-08 0.595349
##  38:     133.52026: -0.140075 0.132369 0.830699 0.235119 1.00000e-08 0.621230
##  39:     133.48864: -0.149418 0.137116 0.829051 0.219430 1.00000e-08 0.619217
##  40:     133.48742: -0.148312 0.136969 0.831257 0.220073 1.00000e-08 0.615241
##  41:     133.48730: -0.148787 0.137222 0.827499 0.221000 1.00000e-08 0.615725
##  42:     133.48726: -0.148882 0.137433 0.828886 0.220425 1.00000e-08 0.615890
##  43:     133.48725: -0.148890 0.137445 0.828808 0.220453 1.00000e-08 0.615758
##  44:     133.48725: -0.148898 0.137457 0.828774 0.220459 1.00000e-08 0.615751
##  45:     133.48725: -0.148899 0.137459 0.828774 0.220458 1.00000e-08 0.615751
## 
## Final Estimate of the Negative LLH:
##  LLH:  2.660707    norm LLH:  0.02254837 
##          mu       omega      alpha1       beta1       beta2        skew 
## -0.04913478  0.01496817  0.82877385  0.22045798  0.00000001  0.61575116 
## 
## R-optimhess Difference Approximated Hessian Matrix:
##                  mu      omega       alpha1       beta1        beta2
## mu     -1849.870609  -3513.099    -7.438658    58.47853   -18.663082
## omega  -3513.099186 -71069.441 -1088.691371 -3132.40646 -3512.182244
## alpha1    -7.438658  -1088.691   -53.973799   -97.56155  -103.545884
## beta1     58.478534  -3132.406   -97.561550  -380.13691  -313.240476
## beta2    -18.663082  -3512.182  -103.545884  -313.24048  -214.546412
## skew     -96.063547  -1257.281   -21.331750   -61.43075    -1.605818
##                skew
## mu       -96.063547
## omega  -1257.280880
## alpha1   -21.331750
## beta1    -61.430754
## beta2     -1.605818
## skew    -234.599533
## attr(,"time")
## Time difference of 0.04183602 secs
## 
## --- END OF TRACE ---
## Warning in sqrt(diag(fit$cvar)): NaNs produced
## 
## Time to Estimate Parameters:
##  Time difference of 0.214797 secs
## 
## Title:
##  GARCH Modelling 
## 
## Call:
##  garchFit(formula = ~garch(1, 2), data = D2, cond.dist = "snorm") 
## 
## Mean and Variance Equation:
##  data ~ garch(1, 2)
## <environment: 0x00000173dd5b6238>
##  [data = D2]
## 
## Conditional Distribution:
##  snorm 
## 
## Coefficient(s):
##          mu        omega       alpha1        beta1        beta2         skew  
## -0.04913478   0.01496817   0.82877385   0.22045798   0.00000001   0.61575116  
## 
## Std. Errors:
##  based on Hessian 
## 
## Error Analysis:
##          Estimate  Std. Error  t value Pr(>|t|)    
## mu     -4.913e-02   2.599e-02   -1.891  0.05866 .  
## omega   1.497e-02   4.978e-03    3.007  0.00264 ** 
## alpha1  8.288e-01   1.800e-01    4.605 4.12e-06 ***
## beta1   2.205e-01   5.654e-02    3.899 9.66e-05 ***
## beta2   1.000e-08         NaN      NaN      NaN    
## skew    6.158e-01   6.112e-02   10.075  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Log Likelihood:
##  -2.660707    normalized:  -0.02254837 
## 
## Description:
##  Thu May 29 12:26:35 2025 by user: SILVENUS
summary(M1)
## 
## Title:
##  GARCH Modelling 
## 
## Call:
##  garchFit(formula = ~garch(1, 2), data = D2, cond.dist = "snorm") 
## 
## Mean and Variance Equation:
##  data ~ garch(1, 2)
## <environment: 0x00000173dd5b6238>
##  [data = D2]
## 
## Conditional Distribution:
##  snorm 
## 
## Coefficient(s):
##          mu        omega       alpha1        beta1        beta2         skew  
## -0.04913478   0.01496817   0.82877385   0.22045798   0.00000001   0.61575116  
## 
## Std. Errors:
##  based on Hessian 
## 
## Error Analysis:
##          Estimate  Std. Error  t value Pr(>|t|)    
## mu     -4.913e-02   2.599e-02   -1.891  0.05866 .  
## omega   1.497e-02   4.978e-03    3.007  0.00264 ** 
## alpha1  8.288e-01   1.800e-01    4.605 4.12e-06 ***
## beta1   2.205e-01   5.654e-02    3.899 9.66e-05 ***
## beta2   1.000e-08         NaN      NaN      NaN    
## skew    6.158e-01   6.112e-02   10.075  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Log Likelihood:
##  -2.660707    normalized:  -0.02254837 
## 
## Description:
##  Thu May 29 12:26:35 2025 by user: SILVENUS 
## 
## 
## Standardised Residuals Tests:
##                                  Statistic      p-Value
##  Jarque-Bera Test   R    Chi^2  63.6187925 1.532108e-14
##  Shapiro-Wilk Test  R    W       0.9162099 1.739095e-06
##  Ljung-Box Test     R    Q(10)  21.6429491 1.703055e-02
##  Ljung-Box Test     R    Q(15)  29.4520794 1.405805e-02
##  Ljung-Box Test     R    Q(20)  36.4413440 1.364209e-02
##  Ljung-Box Test     R^2  Q(10)   3.4703174 9.680966e-01
##  Ljung-Box Test     R^2  Q(15)   5.2805158 9.894731e-01
##  Ljung-Box Test     R^2  Q(20)   8.6683565 9.864399e-01
##  LM Arch Test       R    TR^2    6.5679093 8.847989e-01
## 
## Information Criterion Statistics:
##       AIC       BIC       SIC      HQIC 
## 0.1467916 0.2876739 0.1419466 0.2039940
return_stats = table.Stats(D2)
## Warning in log(1 + x): NaNs produced
skewness = PerformanceAnalytics::skewness(D2)
kurtosis = PerformanceAnalytics::kurtosis(D2)
View(return_stats)
# Forecast using best model
spec = ugarchspec(variance.model = list(model = "eGARCH", garchOrder = c(1,2)),
                  mean.model = list(armaOrder = c(1,2), include.mean = FALSE),
                  distribution.model = "std"); spec
## 
## *---------------------------------*
## *       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 :  std 
## Includes Skew    :  FALSE 
## Includes Shape   :  TRUE 
## Includes Lambda  :  FALSE
fit = ugarchfit(spec = spec, data = D2)
forecast = ugarchforecast(fit, n.ahead = 60, conf.int = 0.99)
plot(forecast, which=1)

plot(forecast,which=3)

#QUESTION 2
#Kaplan Meier
library(survival)
data(package = "survival")
str(aml)
## 'data.frame':    23 obs. of  3 variables:
##  $ time  : num  9 13 13 18 23 28 31 34 45 48 ...
##  $ status: num  1 1 0 1 1 0 1 1 0 1 ...
##  $ x     : Factor w/ 2 levels "Maintained","Nonmaintained": 1 1 1 1 1 1 1 1 1 1 ...
head(aml)
##   time status          x
## 1    9      1 Maintained
## 2   13      1 Maintained
## 3   13      0 Maintained
## 4   18      1 Maintained
## 5   23      1 Maintained
## 6   28      0 Maintained
surv_object = Surv(time = aml$time, event = aml$status)
km_fit = survfit(surv_object ~ x, data = aml)
print(km_fit)
## Call: survfit(formula = surv_object ~ x, data = aml)
## 
##                  n events median 0.95LCL 0.95UCL
## x=Maintained    11      7     31      18      NA
## x=Nonmaintained 12     11     23       8      NA
summary(km_fit)
## Call: survfit(formula = surv_object ~ x, data = aml)
## 
##                 x=Maintained 
##  time n.risk n.event survival std.err lower 95% CI upper 95% CI
##     9     11       1    0.909  0.0867       0.7541        1.000
##    13     10       1    0.818  0.1163       0.6192        1.000
##    18      8       1    0.716  0.1397       0.4884        1.000
##    23      7       1    0.614  0.1526       0.3769        0.999
##    31      5       1    0.491  0.1642       0.2549        0.946
##    34      4       1    0.368  0.1627       0.1549        0.875
##    48      2       1    0.184  0.1535       0.0359        0.944
## 
##                 x=Nonmaintained 
##  time n.risk n.event survival std.err lower 95% CI upper 95% CI
##     5     12       2   0.8333  0.1076       0.6470        1.000
##     8     10       2   0.6667  0.1361       0.4468        0.995
##    12      8       1   0.5833  0.1423       0.3616        0.941
##    23      6       1   0.4861  0.1481       0.2675        0.883
##    27      5       1   0.3889  0.1470       0.1854        0.816
##    30      4       1   0.2917  0.1387       0.1148        0.741
##    33      3       1   0.1944  0.1219       0.0569        0.664
##    43      2       1   0.0972  0.0919       0.0153        0.620
##    45      1       1   0.0000     NaN           NA           NA
plot(km_fit,
     xlab = "Time (Weeks)",
     ylab = "Survival Probability",
     col = c("blue", "red"),
     lty = c(1, 2),
     main = "Kaplan-Meier Survival Curves for AML Patients by Treatment",
     mark.time = TRUE)
legend("topright",
       legend = levels(aml$x),
       col = c("blue", "red"),
       lty = c(1, 2))

# Optional: Confidence intervals
plot(km_fit,
     xlab = "Time (Weeks)",
     ylab = "Survival Probability",
     col = c("blue", "red"),
     lty = c(1, 2),
     main = "Kaplan-Meier Survival Curves with Confidence Intervals",
     mark.time = TRUE,
     conf.int = TRUE)
legend("topright",
       legend = levels(aml$x),
       col = c("blue", "red"),
       lty = c(1, 2))

surv_diff = survdiff(surv_object ~ x, data = aml)
print(surv_diff)
## Call:
## survdiff(formula = surv_object ~ x, data = aml)
## 
##                  N Observed Expected (O-E)^2/E (O-E)^2/V
## x=Maintained    11        7    10.69      1.27       3.4
## x=Nonmaintained 12       11     7.31      1.86       3.4
## 
##  Chisq= 3.4  on 1 degrees of freedom, p= 0.07
#QUESTION FOUR
library(fitdistrplus)
## Loading required package: MASS
## 
## Attaching package: 'fitdistrplus'
## The following object is masked from 'package:rugarch':
## 
##     fitdist
library(goftest)
data = read.csv("DATA25.csv")
claims = data$CLAIMS
D1 = diff(claims); D1
##   [1] -0.06  0.00  0.10  0.32  0.25  0.41  0.63  0.65  0.51  0.39  0.19  0.17
##  [13]  0.18  0.07 -0.04 -0.15 -0.08 -0.29 -0.64 -0.44 -0.33 -0.17  0.04  0.13
##  [25]  0.05  0.02 -0.03 -0.03  0.02  0.13  0.13  0.16 -0.01 -0.10 -0.19 -0.16
##  [37] -0.11 -0.02  0.05  0.20  0.09  0.02  0.04 -0.06  0.00  0.11  0.14  0.09
##  [49]  0.11  0.11  0.14 -0.14 -0.14 -0.20 -0.27 -0.19 -0.18 -0.20 -0.29 -0.33
##  [61] -0.34 -0.30 -0.25 -0.06  0.12  0.40  0.65  0.72  0.91  0.96  1.18  1.29
##  [73]  1.37  1.25  1.04  0.94  0.70  0.43  0.10 -0.05 -0.52 -0.83 -1.08 -1.20
##  [85] -1.33 -1.31 -1.18 -1.12 -1.00 -0.92 -0.76 -0.71 -0.44 -0.12  0.03  0.06
##  [97]  0.10  0.28  0.29  0.34  0.40  0.42  0.47  0.71  0.85  0.76  0.60  1.00
## [109]  1.18  0.99  1.01  0.93  0.76  0.82  0.79  0.35 -0.20 -0.31
D1_positive = D1[D1 > 0]
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.9491740
## scale 0.4775768
## Loglikelihood:  -14.89649   AIC:  33.79297   BIC:  38.23199
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.9092325
## scale 0.4803812
## Loglikelihood:  -15.56588   AIC:  35.13177   BIC:  39.57078
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.8976101
## scale 0.4866929
## Loglikelihood:  -15.85954   AIC:  35.71907   BIC:  40.15809
set.seed(123)
weibull_data = rweibull(100, shape = 2, scale = 3)
fit_weibull = fitdist(weibull_data, "weibull")
# Histogram with density
hist(weibull_data, probability = TRUE, main = "Histogram of Weibull Data with Fit",
     xlab = "Value", col = "blue", border = "white")

# Add Weibull fit curve
curve(dweibull(x, shape = fit_weibull$estimate["shape"], scale = fit_weibull$estimate["scale"]),
      col = "red", lwd = 2, add = TRUE)

# P–P plot
ppcomp(list(fit_weibull), main = "P–P Plot for Weibull Fit")