CS代考 19/01/2023, 16:30

19/01/2023, 16:30
28 November, 2022
Backtesting HS VaR
VaR and ES with Historical Simulation

Copyright By PowCoder代写 加微信 powcoder

Ts <- length(index(log_returns_demean)) # Number of trading days in sample - Ts VaR_HS <- xts(matrix(nrow = Ts, ncol = 1), order.by = index(log_returns_demean)) ES_HS <- xts(matrix(nrow = Ts, ncol = 1), order.by = index(log_returns_demean)) p = 0.01 WE <- 3 / p VaR_HS <- rollapply(data = log_returns_demean, width = WE, FUN = function(x) - sort(coredata(x)) ES_HS <- rollapply(data = log_returns_demean, width = WE, FUN = function(x) - mean(sort(coredata (x))[1:3])) VaR_HS <- lag(VaR_HS, k = 1, na.pad = TRUE) ES_HS <- lag(ES_HS, k = 1, na.pad = TRUE) plot(x = index(log_returns_demean), y = log_returns_demean, ylab = "VaR/Returns", lwd = 1, xlab = "Date", type = "l", col = "red") lines(x = index(log_returns_demean), y = -VaR_HS , col = "blue", lwd = 3) legend("bottomleft", legend = c('Returns', 'VaR'), lty = 1, col = c("red", "blue")) https://moodle.lse.ac.uk/pluginfile.php/2193781/mod_resource/content/0/CW9.html 19/01/2023, 16:30 CW9 Calculate the number of violations WT <- Ts - WE # Size of testing window v <- sum(log_returns_demean < - VaR_HS, na.rm = TRUE) # na.rm = remove missing values (NA) v0 <- WT - v EV <- p*WT # expected number of violations print(paste('Number of HS violations is',v)) https://moodle.lse.ac.uk/pluginfile.php/2193781/mod_resource/content/0/CW9.html 19/01/2023, 16:30 CW9 ## [1] "Number of HS violations is 30" print(paste('Number of no violations is',v0)) ## [1] "Number of no violations is 1827" print(paste('Expected number of violations is',EV)) ## [1] "Expected number of violations is 18.57" VR = v / EV # observed number of violations/expected number of violations print(paste('Violation ratio',VR)) ## [1] "Violation ratio 1.61550888529887" if (v > EV) {
print(‘You have underforecasted VaR’)
print(‘You have overforecasted VaR’)
## [1] “You have underforecasted VaR”
Coverage tests
Bernoulli Test – Unconditional coverage test
https://moodle.lse.ac.uk/pluginfile.php/2193781/mod_resource/content/0/CW9.html

19/01/2023, 16:30 CW9
ra <- log_returns_demean[(WE + 1):Ts] VaRa <- VaR_HS[(WE + 1):Ts] eta <- ra < - VaRa v1 <- sum(eta) v0 <- length(eta) - v1 picap <- v1 / (v1 + v0) a <- (1 - p)^v0 * p^v1 # likelihood of restricted model b <- (1 - picap)^v0 * picap^v1 # likelihood of unrestricted model LR <- 2 * (log(b / a)) if (LR > qchisq(p = 1 – p, df = 1)) {
print(‘null hypothesis H0 is rejected’) } else {
print(‘We cannot reject the null’)
## [1] “We cannot reject the null”
Independence Test
https://moodle.lse.ac.uk/pluginfile.php/2193781/mod_resource/content/0/CW9.html

19/01/2023, 16:30 CW9
logical <- matrix(nrow = WT, ncol = 4) for (i in 2:WT) { logical[i,1] <- coredata(eta)[i-1] == 0 & coredata(eta)[i] == 0 logical[i,2] <- coredata(eta)[i-1] == 0 & coredata(eta)[i] == 1 logical[i,3] <- coredata(eta)[i-1] == 1 & coredata(eta)[i] == 0 logical[i,4] <- coredata(eta)[i-1] == 1 & coredata(eta)[i] == 1 eta_00 = sum(logical[, 1], na.rm = TRUE) eta_01 = sum(logical[, 2], na.rm = TRUE) eta_10 = sum(logical[, 3], na.rm = TRUE) eta_11 = sum(logical[, 4], na.rm = TRUE) P_00 = eta_00 / (eta_00 + eta_01) P_01 = eta_01 / (eta_00 + eta_01) P_10 = eta_10 / (eta_10 + eta_11) P_11 = eta_11 / (eta_10 + eta_11) hat_p = (eta_01 + eta_11) / (eta_00 + eta_01 + eta_10 + eta_11) b1 = P_00^(eta_00) * P_01^(eta_01) * P_10^(eta_10) * P_11^(eta_11) a1 = (1 - hat_p)^(eta_00 + eta_10) * hat_p^(eta_01 + eta_11) LR1 = 2 * log(b1 / a1) if (LR1 > qchisq(p = 1 – p, df = 1)) { print(‘Null hypothesis H0 is rejected’)
print(‘We cannot reject the null’)
https://moodle.lse.ac.uk/pluginfile.php/2193781/mod_resource/content/0/CW9.html

19/01/2023, 16:30 CW9
## [1] “Null hypothesis H0 is rejected”
Backtest Expected Shortfall
ESa = ES_HS[(WE + 1):Ts]
NS <- ra[ra < - VaRa] / - ESa[ra < - VaRa] avNS = mean(NS) ## [1] 1.2171 https://moodle.lse.ac.uk/pluginfile.php/2193781/mod_resource/content/0/CW9.html 程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com