#question 1
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(rugarch)
## Loading required package: parallel
## 
## Attaching package: 'rugarch'
## The following object is masked from 'package:stats':
## 
##     sigma
library(tseries)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(forecast)
library(PerformanceAnalytics)
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## 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 object is masked from 'package:graphics':
## 
##     legend
#import data
data = read.csv("DATA2.csv")
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
ts.plot(d2)

ts.plot(d2, main = "Log returns for stock index data", ylab = "log returns")

return_stats = table.Stats(d2)
## Warning in log(1 + x): NaNs produced
skewness = PerformanceAnalytics::skewness(d2)
kurtosis = PerformanceAnalytics::kurtosis(d2)
View(return_stats)

#fitting the Egarch model
FA0 = garchFit(formula = ~garch(1,2), data = d2, cond.dist = "snorm"); FA0
## 
## 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.03909683 secs
## 
## --- END OF TRACE ---
## Warning in sqrt(diag(fit$cvar)): NaNs produced
## 
## Time to Estimate Parameters:
##  Time difference of 0.2420239 secs
## 
## Title:
##  GARCH Modelling 
## 
## Call:
##  garchFit(formula = ~garch(1, 2), data = d2, cond.dist = "snorm") 
## 
## Mean and Variance Equation:
##  data ~ garch(1, 2)
## <environment: 0x00000293a93988c8>
##  [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 14:22:54 2025 by user: SILVENUS
summary(FA0)
## 
## Title:
##  GARCH Modelling 
## 
## Call:
##  garchFit(formula = ~garch(1, 2), data = d2, cond.dist = "snorm") 
## 
## Mean and Variance Equation:
##  data ~ garch(1, 2)
## <environment: 0x00000293a93988c8>
##  [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 14:22:54 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
FA1 = garchFit(formula = ~garch(1,2), data = d2, cond.dist = "sged"); FA1
## 
## 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:          sged
##  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     TRUE
##  Index List of Parameters to be Optimized:
##     mu  omega alpha1  beta1  beta2   skew  shape 
##      1      2      3      5      6      8      9 
##  Persistence:                  0.9 
## 
## 
## --- START OF TRACE ---
## Selected Algorithm: nlminb 
## 
## R coded nlminb Solver: 
## 
##   0:     213.68376: 0.0719082 0.100000 0.100000 0.400000 0.400000  1.00000  4.00000
##   1:     200.06244: 0.0718654 0.134540 0.140391 0.430148 0.430664 0.992563  3.99534
##   2:     192.33629: 0.0717702 0.144817 0.173268 0.390317 0.389600 0.978545  3.98567
##   3:     190.03555: 0.0714745 0.169166 0.218462 0.395394 0.391824 0.937305  3.96651
##   4:     184.76687: 0.0710772 0.152310 0.226224 0.369224 0.360484 0.889237  3.94655
##   5:     182.29342: 0.0707325 0.173144 0.274999 0.368365 0.356799 0.850328  3.92688
##   6:     178.70717: 0.0701994 0.139821 0.286318 0.346441 0.329068 0.808050  3.90638
##   7:     176.30057: 0.0695120 0.142211 0.323833 0.356896 0.333574 0.757956  3.88173
##   8:     173.47083: 0.0687405 0.103473 0.341936 0.340971 0.308941 0.719735  3.85968
##   9:     171.20786: 0.0676017 0.0958126 0.386422 0.357560 0.312140 0.682194  3.83197
##  10:     169.59078: 0.0662598 0.0566461 0.413236 0.350153 0.284416 0.655960  3.80671
##  11:     167.39389: 0.0640990 0.0990788 0.416403 0.363961 0.260410 0.641755  3.77402
##  12:     165.54554: 0.0622314 0.0940374 0.433919 0.355701 0.211635 0.625766  3.74175
##  13:     163.45953: 0.0604648 0.0968479 0.473400 0.387876 0.195692 0.611484  3.70878
##  14:     162.02637: 0.0591630 0.0820251 0.495863 0.381985 0.146771 0.588441  3.68157
##  15:     159.91425: 0.0575610 0.111081 0.505231 0.416475 0.114658 0.577436  3.65026
##  16:     158.56292: 0.0568854 0.102066 0.537251 0.396412 0.0710994 0.549347  3.62864
##  17:     156.52930: 0.0555880 0.120865 0.559769 0.439950 0.0449740 0.542997  3.59787
##  18:     155.53283: 0.0546011 0.103352 0.578667 0.443496 1.00000e-08 0.524725  3.57142
##  19:     153.86862: 0.0525623 0.115664 0.598057 0.468855 1.00000e-08 0.519438  3.51899
##  20:     153.51581: 0.0514952 0.0967008 0.630800 0.437155 1.00000e-08 0.488976  3.48484
##  21:     152.09572: 0.0496647 0.123835 0.638436 0.426913 1.00000e-08 0.505643  3.43075
##  22:     151.30806: 0.0483636 0.112071 0.675247 0.414353 1.00000e-08 0.474803  3.38808
##  23:     150.47667: 0.0465411 0.126729 0.685926 0.404100 1.00000e-08 0.492471  3.33009
##  24:     147.87837: 0.0369779 0.125736 0.715727 0.297225 1.00000e-08 0.572771  3.01120
##  25:     139.97933: -0.000405331 0.0530910 0.738071 0.232390 1.00000e-08 0.849237  1.64980
##  26:     138.75942: -0.0665448 0.397418 0.501703 0.276136 1.00000e-08 0.646511  1.00000
##  27:     135.80787: -0.0677727 0.330670 0.590754 0.262543 1.00000e-08 0.574103  1.17880
##  28:     134.13980: -0.0680699 0.273506 0.598810 0.213618 1.00000e-08 0.597494  1.17552
##  29:     132.76728: -0.0692124 0.133271 0.787867 0.233650 1.00000e-08 0.517698  1.26387
##  30:     132.64053: -0.0693464 0.222659 0.818711 0.257270 1.00000e-08 0.579494  1.27844
##  31:     131.30126: -0.0696963 0.177293 0.820826 0.221640 1.00000e-08 0.584440  1.28028
##  32:     131.03384: -0.0707889 0.142906 0.851483 0.207439 1.00000e-08 0.590756  1.30836
##  33:     130.88656: -0.0729791 0.145092 0.885807 0.220199 1.00000e-08 0.603306  1.33923
##  34:     130.74579: -0.0769934 0.138208 0.889760 0.206781 1.00000e-08 0.600432  1.34320
##  35:     130.64066: -0.0808478 0.156036 0.897656 0.196886 1.00000e-08 0.597173  1.33855
##  36:     129.93968: -0.127139 0.144771 0.967635 0.201344 1.00000e-08 0.592556  1.40322
##  37:     129.44821: -0.173683 0.162905 0.955718 0.150997 1.00000e-08 0.567915  1.37021
##  38:     128.69774: -0.219913 0.166358  1.00000 0.153993 1.00000e-08 0.574711  1.30825
##  39:     128.35108: -0.320717 0.223340  1.00000 0.171702 1.00000e-08 0.520779  1.04351
##  40:     128.09259: -0.285665 0.198140  1.00000 0.160919 1.00000e-08 0.544229  1.16004
##  41:     128.05463: -0.267954 0.199448  1.00000 0.167311 1.00000e-08 0.559199  1.12191
##  42:     127.99689: -0.267565 0.200995  1.00000 0.173057 0.00511300 0.550515  1.12106
##  43:     127.99385: -0.268043 0.201528  1.00000 0.169279 1.00000e-08 0.551970  1.12141
##  44:     127.98047: -0.268039 0.203556  1.00000 0.172089 0.00165295 0.549360  1.12188
##  45:     127.96723: -0.268376 0.206708  1.00000 0.177959 1.00000e-08 0.547865  1.10337
##  46:     127.96430: -0.270345 0.208439  1.00000 0.179477 1.00000e-08 0.546598  1.10194
##  47:     127.96425: -0.270345 0.208460  1.00000 0.179510 1.00000e-08 0.546553  1.10195
##  48:     127.96424: -0.270345 0.208464  1.00000 0.179516 1.00000e-08 0.546547  1.10195
##  49:     127.96424: -0.270345 0.208467  1.00000 0.179520 1.00000e-08 0.546544  1.10195
##  50:     127.96424: -0.270344 0.208467  1.00000 0.179521 1.00000e-08 0.546548  1.10195
##  51:     127.96405: -0.269992 0.208686  1.00000 0.180610 1.00000e-08 0.546720  1.10065
##  52:     127.96404: -0.270342 0.208920  1.00000 0.180689 1.00000e-08 0.546686  1.10041
##  53:     127.96404: -0.270226 0.208898  1.00000 0.180597 1.00000e-08 0.546703  1.10060
##  54:     127.96404: -0.270211 0.208916  1.00000 0.180590 1.00000e-08 0.546699  1.10056
##  55:     127.96404: -0.270214 0.208935  1.00000 0.180582 1.00000e-08 0.546695  1.10053
##  56:     127.96404: -0.270216 0.208941  1.00000 0.180580 1.00000e-08 0.546693  1.10052
##  57:     127.96404: -0.270216 0.208936  1.00000 0.180581 1.00000e-08 0.546695  1.10053
## 
## Final Estimate of the Negative LLH:
##  LLH:  -2.862507    norm LLH:  -0.02425853 
##          mu       omega      alpha1       beta1       beta2        skew 
## -0.08916807  0.02275146  0.99999999  0.18058116  0.00000001  0.54669455 
##       shape 
##  1.10053358 
## 
## R-optimhess Difference Approximated Hessian Matrix:
##                mu      omega     alpha1      beta1      beta2       skew
## mu     -18337.894 -25039.292 -1415.6779 -2258.6687 -2519.8732  4403.9370
## omega  -25039.292 -40094.459 -2253.3084 -3186.2919 -3620.3201  4332.7413
## alpha1  -1415.678  -2253.308  -354.7735  -405.4328  -448.6927   499.6435
## beta1   -2258.669  -3186.292  -405.4328  -680.1415  -857.0798   720.9675
## beta2   -2519.873  -3620.320  -448.6927  -857.0798 -1457.9312   847.4049
## skew     4403.937   4332.741   499.6435   720.9675   847.4049 -2549.9059
## shape   -1242.802  -2179.034  -255.2144  -302.9894  -393.5820   431.4117
##             shape
## mu     -1242.8022
## omega  -2179.0337
## alpha1  -255.2144
## beta1   -302.9894
## beta2   -393.5820
## skew     431.4117
## shape   -266.9376
## attr(,"time")
## Time difference of 0.09384584 secs
## 
## --- END OF TRACE ---
## 
## 
## Time to Estimate Parameters:
##  Time difference of 0.4197559 secs
## 
## Title:
##  GARCH Modelling 
## 
## Call:
##  garchFit(formula = ~garch(1, 2), data = d2, cond.dist = "sged") 
## 
## Mean and Variance Equation:
##  data ~ garch(1, 2)
## <environment: 0x00000293ac82ba48>
##  [data = d2]
## 
## Conditional Distribution:
##  sged 
## 
## Coefficient(s):
##          mu        omega       alpha1        beta1        beta2         skew  
## -0.08916807   0.02275146   0.99999999   0.18058116   0.00000001   0.54669455  
##       shape  
##  1.10053358  
## 
## Std. Errors:
##  based on Hessian 
## 
## Error Analysis:
##          Estimate  Std. Error  t value Pr(>|t|)    
## mu     -8.917e-02   4.017e-02   -2.220   0.0264 *  
## omega   2.275e-02   2.559e-02    0.889   0.3740    
## alpha1  1.000e+00   1.335e-01    7.491 6.82e-14 ***
## beta1   1.806e-01   1.238e-01    1.459   0.1447    
## beta2   1.000e-08   5.836e-02    0.000   1.0000    
## skew    5.467e-01   4.541e-02   12.040  < 2e-16 ***
## shape   1.101e+00   1.581e-01    6.962 3.37e-12 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Log Likelihood:
##  2.862507    normalized:  0.02425853 
## 
## Description:
##  Thu May 29 14:22:55 2025 by user: SILVENUS
summary(FA1)
## 
## Title:
##  GARCH Modelling 
## 
## Call:
##  garchFit(formula = ~garch(1, 2), data = d2, cond.dist = "sged") 
## 
## Mean and Variance Equation:
##  data ~ garch(1, 2)
## <environment: 0x00000293ac82ba48>
##  [data = d2]
## 
## Conditional Distribution:
##  sged 
## 
## Coefficient(s):
##          mu        omega       alpha1        beta1        beta2         skew  
## -0.08916807   0.02275146   0.99999999   0.18058116   0.00000001   0.54669455  
##       shape  
##  1.10053358  
## 
## Std. Errors:
##  based on Hessian 
## 
## Error Analysis:
##          Estimate  Std. Error  t value Pr(>|t|)    
## mu     -8.917e-02   4.017e-02   -2.220   0.0264 *  
## omega   2.275e-02   2.559e-02    0.889   0.3740    
## alpha1  1.000e+00   1.335e-01    7.491 6.82e-14 ***
## beta1   1.806e-01   1.238e-01    1.459   0.1447    
## beta2   1.000e-08   5.836e-02    0.000   1.0000    
## skew    5.467e-01   4.541e-02   12.040  < 2e-16 ***
## shape   1.101e+00   1.581e-01    6.962 3.37e-12 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Log Likelihood:
##  2.862507    normalized:  0.02425853 
## 
## Description:
##  Thu May 29 14:22:55 2025 by user: SILVENUS 
## 
## 
## Standardised Residuals Tests:
##                                  Statistic      p-Value
##  Jarque-Bera Test   R    Chi^2  65.8660178 4.996004e-15
##  Shapiro-Wilk Test  R    W       0.9049246 4.257435e-07
##  Ljung-Box Test     R    Q(10)  19.5518912 3.378755e-02
##  Ljung-Box Test     R    Q(15)  26.7102125 3.118807e-02
##  Ljung-Box Test     R    Q(20)  33.9609046 2.639037e-02
##  Ljung-Box Test     R^2  Q(10)   4.4190899 9.264674e-01
##  Ljung-Box Test     R^2  Q(15)   6.9958760 9.577641e-01
##  Ljung-Box Test     R^2  Q(20)  11.5740517 9.299572e-01
##  LM Arch Test       R    TR^2    7.2803815 8.385422e-01
## 
## Information Criterion Statistics:
##        AIC        BIC        SIC       HQIC 
## 0.07012700 0.23448965 0.06360023 0.13686308
FA2 = garchFit(formula = ~garch(1,2), data = d2, cond.dist = "std"); FA2
## 
## 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:          std
##  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    FALSE
##     shape   1.00000000  10.0000000 4.00000000     TRUE
##  Index List of Parameters to be Optimized:
##     mu  omega alpha1  beta1  beta2  shape 
##      1      2      3      5      6      9 
##  Persistence:                  0.9 
## 
## 
## --- START OF TRACE ---
## Selected Algorithm: nlminb 
## 
## R coded nlminb Solver: 
## 
##   0:     147.45016: 0.0719082 0.100000 0.100000 0.400000 0.400000  4.00000
##   1:     146.71427: 0.0718894 0.0893871 0.0948900 0.387497 0.387457  3.99967
##   2:     139.42908: 0.0458939 0.233096  1.00000 1.00000e-08 1.00000e-08  3.97586
##   3:     137.87767: 0.0541041 0.283005  1.00000 0.0306658 0.0680911  3.46048
##   4:     137.62666: 0.0455829 0.240842  1.00000 0.0636565 0.0672508  3.97991
##   5:     137.13516: 0.0307210 0.103671  1.00000 0.223152 0.000331189  4.41614
##   6:     137.01132: 0.0226453 0.182205  1.00000 0.160134 1.00000e-08  4.23722
##   7:     136.92987: 0.0228678 0.155016  1.00000 0.229540 1.00000e-08  4.15202
##   8:     136.89040: 0.0226057 0.156048 0.995538 0.201667 1.00000e-08  4.19149
##   9:     136.88596: 0.0226284 0.151838 0.995681 0.209764 1.00000e-08  4.18694
##  10:     136.88426: 0.0225073 0.151525 0.993537 0.213050 1.00000e-08  4.19620
##  11:     136.88352: 0.0223897 0.148215 0.991522 0.214552 1.00000e-08  4.19795
##  12:     136.87694: 0.0206467 0.150141 0.982458 0.209955 1.00000e-08  4.23833
##  13:     136.85339: 0.0128715 0.146758 0.927438 0.222137 1.00000e-08  4.48504
##  14:     136.84368: 0.0165704 0.141028 0.931850 0.217525 1.00000e-08  4.64811
##  15:     136.84215: 0.0188297 0.141285 0.916402 0.221270 1.00000e-08  4.67465
##  16:     136.84211: 0.0191467 0.141099 0.919151 0.221736 1.00000e-08  4.67473
##  17:     136.84209: 0.0190217 0.141023 0.918139 0.221726 1.00000e-08  4.67718
##  18:     136.84209: 0.0190140 0.141038 0.918108 0.221713 1.00000e-08  4.67739
##  19:     136.84209: 0.0190109 0.141037 0.918099 0.221716 1.00000e-08  4.67750
## 
## Final Estimate of the Negative LLH:
##  LLH:  6.015549    norm LLH:  0.05097923 
##          mu       omega      alpha1       beta1       beta2       shape 
## 0.006273365 0.015357775 0.918099153 0.221715770 0.000000010 4.677500167 
## 
## R-optimhess Difference Approximated Hessian Matrix:
##                  mu        omega      alpha1        beta1        beta2
## mu     -1531.764781   -235.29583   -1.772935   -22.652570  -121.418104
## omega   -235.295830 -32108.91276 -478.579950 -1572.650273 -1700.738776
## alpha1    -1.772935   -478.57995  -18.619276   -38.718023   -52.675496
## beta1    -22.652570  -1572.65027  -38.718023  -171.630280  -205.687277
## beta2   -121.418104  -1700.73878  -52.675496  -205.687277  -236.772636
## shape     -3.013536    -86.22731   -2.224710    -4.526777    -6.763704
##              shape
## mu      -3.0135360
## omega  -86.2273133
## alpha1  -2.2247104
## beta1   -4.5267765
## beta2   -6.7637038
## shape   -0.6219538
## attr(,"time")
## Time difference of 0.03333402 secs
## 
## --- END OF TRACE ---
## Warning in sqrt(diag(fit$cvar)): NaNs produced
## 
## Time to Estimate Parameters:
##  Time difference of 0.105582 secs
## 
## Title:
##  GARCH Modelling 
## 
## Call:
##  garchFit(formula = ~garch(1, 2), data = d2, cond.dist = "std") 
## 
## Mean and Variance Equation:
##  data ~ garch(1, 2)
## <environment: 0x00000293af0c1578>
##  [data = d2]
## 
## Conditional Distribution:
##  std 
## 
## Coefficient(s):
##         mu       omega      alpha1       beta1       beta2       shape  
## 0.00627336  0.01535777  0.91809915  0.22171577  0.00000001  4.67750017  
## 
## Std. Errors:
##  based on Hessian 
## 
## Error Analysis:
##         Estimate  Std. Error  t value Pr(>|t|)   
## mu     6.273e-03   2.318e-02    0.271  0.78668   
## omega  1.536e-02   7.430e-03    2.067  0.03874 * 
## alpha1 9.181e-01   3.638e-01    2.524  0.01161 * 
## beta1  2.217e-01         NaN      NaN      NaN   
## beta2  1.000e-08         NaN      NaN      NaN   
## shape  4.678e+00   1.743e+00    2.683  0.00729 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Log Likelihood:
##  -6.015549    normalized:  -0.05097923 
## 
## Description:
##  Thu May 29 14:22:55 2025 by user: SILVENUS
summary(FA2)
## 
## Title:
##  GARCH Modelling 
## 
## Call:
##  garchFit(formula = ~garch(1, 2), data = d2, cond.dist = "std") 
## 
## Mean and Variance Equation:
##  data ~ garch(1, 2)
## <environment: 0x00000293af0c1578>
##  [data = d2]
## 
## Conditional Distribution:
##  std 
## 
## Coefficient(s):
##         mu       omega      alpha1       beta1       beta2       shape  
## 0.00627336  0.01535777  0.91809915  0.22171577  0.00000001  4.67750017  
## 
## Std. Errors:
##  based on Hessian 
## 
## Error Analysis:
##         Estimate  Std. Error  t value Pr(>|t|)   
## mu     6.273e-03   2.318e-02    0.271  0.78668   
## omega  1.536e-02   7.430e-03    2.067  0.03874 * 
## alpha1 9.181e-01   3.638e-01    2.524  0.01161 * 
## beta1  2.217e-01         NaN      NaN      NaN   
## beta2  1.000e-08         NaN      NaN      NaN   
## shape  4.678e+00   1.743e+00    2.683  0.00729 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Log Likelihood:
##  -6.015549    normalized:  -0.05097923 
## 
## Description:
##  Thu May 29 14:22:55 2025 by user: SILVENUS 
## 
## 
## Standardised Residuals Tests:
##                                   Statistic      p-Value
##  Jarque-Bera Test   R    Chi^2  110.4985376 0.000000e+00
##  Shapiro-Wilk Test  R    W        0.9186264 2.382849e-06
##  Ljung-Box Test     R    Q(10)   21.3196537 1.897163e-02
##  Ljung-Box Test     R    Q(15)   29.2166590 1.508164e-02
##  Ljung-Box Test     R    Q(20)   35.2636248 1.874434e-02
##  Ljung-Box Test     R^2  Q(10)    1.7138937 9.980999e-01
##  Ljung-Box Test     R^2  Q(15)    2.7336826 9.997753e-01
##  Ljung-Box Test     R^2  Q(20)    4.1088667 9.999421e-01
##  LM Arch Test       R    TR^2     5.3489252 9.452982e-01
## 
## Information Criterion Statistics:
##       AIC       BIC       SIC      HQIC 
## 0.2036534 0.3445356 0.1988083 0.2608557
spec = ugarchspec(variance.model = list(model = "eGARCH", garchOrder = c(1,2)),
                  mean.model = list(armaOrder = c(0,0), include.mean = FALSE)); spec
## 
## *---------------------------------*
## *       GARCH Model Spec          *
## *---------------------------------*
## 
## Conditional Variance Dynamics    
## ------------------------------------
## GARCH Model      : eGARCH(1,2)
## Variance Targeting   : FALSE 
## 
## Conditional Mean Dynamics
## ------------------------------------
## Mean Model       : ARFIMA(0,0,0)
## Include Mean     : FALSE 
## GARCH-in-Mean        : FALSE 
## 
## Conditional Distribution
## ------------------------------------
## Distribution :  norm 
## Includes Skew    :  FALSE 
## Includes Shape   :  FALSE 
## Includes Lambda  :  FALSE
fit = ugarchfit(spec = spec, data = d2)
forecast = ugarchforecast(fit, n.ahead = 60, conf.int = 0.99)
plot(forecast, which = 1)

q#uestion 2
## function (save = "default", status = 0, runLast = TRUE) 
## .Internal(quit(save, status, runLast))
## <bytecode: 0x00000293aec839a0>
## <environment: namespace:base>
library(survival)
library(survminer)
## Loading required package: ggplot2
## Loading required package: ggpubr
## 
## Attaching package: 'ggpubr'
## The following object is masked from 'package:forecast':
## 
##     gghistogram
## 
## Attaching package: 'survminer'
## The following object is masked from 'package:survival':
## 
##     myeloma
d1=data.frame(time=c(3,4,6,11,17,21,24,25,26,30),
              event=c(1,1,0,1,0,1,0,0,1,0))
kmc=with(d1,Surv(time,event));kmc
##  [1]  3   4   6+ 11  17+ 21  24+ 25+ 26  30+
plot(kmc)

plot(kmc,fun="cumhaz")

kmc2=surv_fit(Surv(time,event)~1,data = d1);kmc2
## Call: survfit(formula = Surv(time, event) ~ 1, data = structure(list(
##     time = c(3, 4, 6, 11, 17, 21, 24, 25, 26, 30), event = c(1, 
##     1, 0, 1, 0, 1, 0, 0, 1, 0)), class = "data.frame", row.names = c(NA, 
## -10L)))
## 
##       n events median 0.95LCL 0.95UCL
## [1,] 10      5     26      11      NA
summary(kmc2)
## Call: survfit(formula = Surv(time, event) ~ 1, data = structure(list(
##     time = c(3, 4, 6, 11, 17, 21, 24, 25, 26, 30), event = c(1, 
##     1, 0, 1, 0, 1, 0, 0, 1, 0)), class = "data.frame", row.names = c(NA, 
## -10L)))
## 
##  time n.risk n.event survival std.err lower 95% CI upper 95% CI
##     3     10       1    0.900  0.0949       0.7320            1
##     4      9       1    0.800  0.1265       0.5868            1
##    11      7       1    0.686  0.1515       0.4447            1
##    21      5       1    0.549  0.1724       0.2963            1
##    26      2       1    0.274  0.2122       0.0602            1
#question 4
library(fitdistrplus)
## Loading required package: MASS
## 
## Attaching package: 'fitdistrplus'
## The following object is masked from 'package:rugarch':
## 
##     fitdist
library(goftest)
data = read.csv("DATA2.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 = "lightblue", 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")