Sys.setlocale("LC_ALL", "en_US.UTF-8")
## [1] "LC_COLLATE=en_US.UTF-8;LC_CTYPE=en_US.UTF-8;LC_MONETARY=en_US.UTF-8;LC_NUMERIC=C;LC_TIME=en_US.UTF-8"

1. Chuẩn bị thư viện

library(rugarch)
library(tseries)
library(FinTS)
library(PerformanceAnalytics)
library(readr)
library(dplyr)
library(lubridate)

2. Đọc dữ liệu

data <- read.csv("C:/Users/ADMIN/Downloads/VN-Index.csv")

3. Làm sạch dữ liệu

data$Price <- gsub(",", "", data$Price)
data$Price <- as.numeric(data$Price)

data$Date <- dmy(data$Date)

data <- data %>%
  arrange(Date)

4. Tính Log-return

data$Return <- c(NA, diff(log(data$Price)))

data <- na.omit(data)

return <- data$Return
plot(return, type="l", main="VNINDEX Log Return")

5. Kiểm định tính dừng (ADF test)

adf.test(return)
## 
##  Augmented Dickey-Fuller Test
## 
## data:  return
## Dickey-Fuller = -10, Lag order = 9, p-value = 0.01
## alternative hypothesis: stationary

Do p-value = 0.01 < 0.05 -> Chuỗi dừng

6. Kiểm định ARCH Effect

ArchTest(return, lags = 12)
## 
##  ARCH LM-test; Null hypothesis: no ARCH effects
## 
## data:  return
## Chi-squared = 139, df = 12, p-value <0.0000000000000002

p-value < 0.05 -> Tồn tại ARCH Effect -> phù hợp chạy EGARCH

7. Xây dựng mô hình EGARCH(1,1)

spec <- ugarchspec(
  variance.model = list(
    model = "eGARCH",
    garchOrder = c(1,1)
  ),
  mean.model = list(
    armaOrder = c(0,0),
    include.mean = TRUE
  ),
  distribution.model = "norm"
)

8. Ước lượng mô hình

fit <- ugarchfit(
  spec = spec,
  data = return
)

show(fit)
## 
## *---------------------------------*
## *          GARCH Model Fit        *
## *---------------------------------*
## 
## Conditional Variance Dynamics    
## -----------------------------------
## GARCH Model  : eGARCH(1,1)
## Mean Model   : ARFIMA(0,0,0)
## Distribution : norm 
## 
## Optimal Parameters
## ------------------------------------
##         Estimate  Std. Error  t value Pr(>|t|)
## mu      0.000592    0.000543   1.0900 0.275705
## omega  -0.796607    0.173464  -4.5923 0.000004
## alpha1 -0.133000    0.027966  -4.7557 0.000002
## beta1   0.909698    0.019374  46.9546 0.000000
## gamma1  0.275006    0.039244   7.0075 0.000000
## 
## Robust Standard Errors:
##         Estimate  Std. Error  t value Pr(>|t|)
## mu      0.000592    0.001072  0.55211 0.580871
## omega  -0.796607    0.397082 -2.00615 0.044840
## alpha1 -0.133000    0.060010 -2.21629 0.026672
## beta1   0.909698    0.045019 20.20686 0.000000
## gamma1  0.275006    0.050066  5.49286 0.000000
## 
## LogLikelihood : 3080 
## 
## Information Criteria
## ------------------------------------
##                     
## Akaike       -6.1740
## Bayes        -6.1494
## Shibata      -6.1741
## Hannan-Quinn -6.1647
## 
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                      4.270 0.03879
## Lag[2*(p+q)+(p+q)-1][2]     4.286 0.06317
## Lag[4*(p+q)+(p+q)-1][5]     4.795 0.17020
## d.o.f=0
## H0 : No serial correlation
## 
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
##                         statistic p-value
## Lag[1]                    0.03811  0.8452
## Lag[2*(p+q)+(p+q)-1][5]   0.38221  0.9743
## Lag[4*(p+q)+(p+q)-1][9]   0.95332  0.9881
## d.o.f=2
## 
## Weighted ARCH LM Tests
## ------------------------------------
##             Statistic Shape Scale P-Value
## ARCH Lag[3]    0.4676 0.500 2.000  0.4941
## ARCH Lag[5]    0.5130 1.440 1.667  0.8798
## ARCH Lag[7]    1.0607 2.315 1.543  0.9035
## 
## Nyblom stability test
## ------------------------------------
## Joint Statistic:  1.97
## Individual Statistics:              
## mu     0.20525
## omega  0.08272
## alpha1 0.09161
## beta1  0.08378
## gamma1 0.07129
## 
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic:          1.28 1.47 1.88
## Individual Statistic:     0.35 0.47 0.75
## 
## Sign Bias Test
## ------------------------------------
##                    t-value    prob sig
## Sign Bias           1.9031 0.05732   *
## Negative Sign Bias  0.9645 0.33501    
## Positive Sign Bias  0.2921 0.77029    
## Joint Effect        4.2606 0.23467    
## 
## 
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
##   group statistic       p-value(g-1)
## 1    20     76.73 0.0000000067597970
## 2    30     97.80 0.0000000021975483
## 3    40    101.75 0.0000001666939549
## 4    50    157.31 0.0000000000002756
## 
## 
## Elapsed time : 0.118

**gamma1 < 0 và có ý nghĩa thống kê

→ tồn tại Leverage Effect (hiệu ứng đòn bẩy).

9. Trích xuất volatility

volatility <- sigma(fit)

plot(volatility, type="l",
     main="Conditional Volatility - EGARCH")

10. Tính Value at Risk (VaR)

Historical VaR

VaR_95 <- quantile(return, 0.05)

VaR_95
##      5% 
## -0.0199

VaR bằng package

VaR_hist <- VaR(return,
                p = 0.95,
                method = "historical")

VaR_hist
##        [,1]
## VaR -0.0199

11. VaR từ mô hình EGARCH

forecast <- ugarchforecast(fit, n.ahead = 1)

sigma_forecast <- sigma(forecast)

mu_forecast <- fitted(forecast)

VaR_EGARCH <- mu_forecast + sigma_forecast * qnorm(0.05)

VaR_EGARCH
##     1972-09-23
## T+1    -0.0194

12. Backtesting VaR

VaR_series <- rep(VaR_95, length(return))

VaRTest(
  alpha = 0.05,
  actual = return,
  VaR = VaR_series
)
## $expected.exceed
## [1] 49
## 
## $actual.exceed
## [1] 50
## 
## $uc.H0
## [1] "Correct Exceedances"
## 
## $uc.LRstat
## [1] 0.000844
## 
## $uc.critical
## [1] 3.84
## 
## $uc.LRp
## [1] 0.977
## 
## $uc.Decision
## [1] "Fail to Reject H0"
## 
## $cc.H0
## [1] "Correct Exceedances & Independent"
## 
## $cc.LRstat
## [1] 6.27
## 
## $cc.critical
## [1] 5.99
## 
## $cc.LRp
## [1] 0.0435
## 
## $cc.Decision
## [1] "Reject H0"

fail to reject H0 → VaR ước lượng tốt.

13. Vẽ biểu đồ Return và VaR

plot(return, type="l", main="Return and VaR")
lines(VaR_series, col="red")
legend("bottomleft",
       legend=c("Return","VaR"),
       col=c("black","red"),
       lty=1)