当前位置:网站首页>Course paper: Copula modeling code of portfolio risk VaR
Course paper: Copula modeling code of portfolio risk VaR
2022-06-26 09:08:00 【ML_ python_ get√】
List of articles
- One 、 Load package
- Two 、 Set up the workspace
- 3、 ... and 、 Import data
- Four 、 Data cleaning
- 5、 ... and 、 Sequence diagram
- 6、 ... and 、 Log yield distribution
- 7、 ... and 、 Autocorrelation function 、 Partial autocorrelation function
- 8、 ... and 、ARMA Order determination
- Nine 、 fitting ARMA-Garch Model
- Ten 、jb test : Normal distribution
- 11、 ... and 、LJUNG BOX TEST : Autocorrelation
- Twelve 、arch test
- 13、 ... and 、 Set the rate of return as GNG Distribution
- fourteen 、 The normalized residual probability is changed into uniform data
- 15、 ... and 、 Correlation test
- sixteen 、 structure copula function
- seventeen 、 Generate simulation data
- eighteen 、 Calculation VaR
- nineteen 、 test
One 、 Load package
library("quantmod")
library("tidyquant")
library("rugarch")
library("copula")
library("VGAM")
library("ggplot2")
library("GoFKernel")
library("mistr")
library("tseries")
library("stats")
library("fDMA")
library("xts")
Two 、 Set up the workspace
setwd("D:/paper")
getwd()
3、 ... and 、 Import data
nobank <- read.csv("./nobank.csv", sep = ",", header = T)
bank <- read.csv("./bank.csv", sep = ",", header = T)
house <- read.csv("./house.csv", sep = ",", header = T)
data <- read.csv("./data.csv", sep = ",", header = T)
Four 、 Data cleaning
contract <- function(df) {
names(df) <- c("date", "return")
return <- as.numeric(sub("%", "", df$return))
df$return <- return
q1 <- quantile(df$return, 0.01)
q99 <- quantile(df$return, 0.99)
df[df$return < q1, ]$return <- q1
df[df$return > q99, ]$return <- q99
df
}
nobank <- contract(nobank)
bank <- contract(bank)
house <- contract(house)
data <- contract(data)
summary(nobank)
summary(bank)
summary(house)
summary(data)
cleandata <- function(df) {
names(df) <- c("date", "return")
date <- ymd(as.character(df$date))
df <- xts(df$return, order.by = date)
df
}
nobank <- cleandata(nobank)
bank <- cleandata(bank)
house <- cleandata(house)
data <- cleandata(data)
class(nobank)
class(bank)
class(house)
class(data)
5、 ... and 、 Sequence diagram
layout(matrix(c(1, 2, 3, 4), 2, 2, byrow = TRUE))
plot(nobank, col = "darkorange2", lwd = 1.5)
plot(bank, col = "deeppink3", lwd = 1.5)
plot(house, col = "red", lwd = 1.5)
plot(data, col = "blue", lwd = 1.5)
layout(matrix(c(1), 1, 1))
plot(cbind(nobank, bank, house, data), col = c("darkorange2", "deeppink3", "red", "blue"), lwd = 1.5)
6、 ... and 、 Log yield distribution
layout(matrix(c(1, 2, 3, 4), 2, 2, byrow = TRUE))
qqnorm(nobank, main = "nobank log return")
qqline(nobank, col = "darkorange2")
qqnorm(bank, main = "bank log return")
qqline(bank, col = "deeppink3")
qqnorm(house, main = "house log return")
qqline(house, col = "red")
qqnorm(data, main = "data log return")
qqline(data, col = "blue")
7、 ... and 、 Autocorrelation function 、 Partial autocorrelation function
layout(matrix(c(1, 2, 3, 4), 2, 2, byrow = TRUE))
acf(nobank, main = "nobank ACF")
acf(bank, main = "bank ACF")
acf(house, main = "house ACF")
acf(data, main = "data ACF")
layout(matrix(c(1, 2, 3, 4), 2, 2, byrow = TRUE))
pacf(nobank, main = "nobank PACF")
pacf(bank, main = "bank PACF")
pacf(house, main = "house PACF")
pacf(data, main = "data PACF")
8、 ... and 、ARMA Order determination
order_EACF <- function(p_max, q_max, data) {
final.aic <- Inf
final.order <- c(0, 0, 0)
for (p in 0:p_max) {
for (q in 0:q_max) {
if (p == 0 && q == 0) {
next
}
arimaFit <- tryCatch(arima(data, order = c(p, 0, q)),
error = function(err) FALSE,
warning = function(err) FALSE
)
if (!is.logical(arimaFit)) {
current.aic <- AIC(arimaFit)
if (current.aic < final.aic) {
final.aic <- current.aic
final.order <- c(p, 0, q)
final.arima <- arima(data, order = final.order)
}
}
else {
next
}
}
}
return(c(final.order, final.aic))
}
order_nobank <- order_EACF(4, 4, nobank)
order_bank <- order_EACF(4, 4, bank)
order_house <- order_EACF(4, 4, house)
order_data <- order_EACF(4, 4, data)
Nine 、 fitting ARMA-Garch Model
Non bank finance
garchspec_nobank <- ugarchspec(
mean.model = list(armaOrder = c(order_nobank[1], order_nobank[3])),
variance.model = list(model = "gjrGARCH", garchOrder = c(1, 1)),
distribution.model = "sstd"
)
garchfit_nobank <- ugarchfit(data = nobank, spec = garchspec_nobank)
coef(garchfit_nobank)
Standardized residuals
standardize_residual_nobank <- residuals(garchfit_nobank) / sigma(garchfit_nobank)
layout(matrix(c(1, 2, 3, 4), 2, 2, byrow = TRUE))
acf(standardize_residual_nobank, main = "nobank residual ACF")
pacf(standardize_residual_nobank, main = "nobank residual PACF")
plot(standardize_residual_nobank, main = "residual", col = "darkorange2", lwd = 1)
qqnorm(standardize_residual_nobank, main = "nobank qq-plot")
qqline(standardize_residual_nobank, col = "darkorange2")
Bank
garchspec_bank <- ugarchspec(
mean.model = list(armaOrder = c(order_bank[1], order_bank[3])),
variance.model = list(model = "gjrGARCH", garchOrder = c(1, 1)),
distribution.model = "sstd"
)
garchfit_bank <- ugarchfit(data = bank, spec = garchspec_bank)
coef(garchfit_bank)
Standardized residuals
standardize_residual_bank <- residuals(garchfit_bank) / sigma(garchfit_bank)
layout(matrix(c(1, 2, 3, 4), 2, 2, byrow = TRUE))
acf(standardize_residual_bank, main = "bank residual ACF")
pacf(standardize_residual_bank, main = "bank residual PACF")
plot(standardize_residual_bank, main = "residual", col = "darkorange2", lwd = 1)
qqnorm(standardize_residual_bank, main = "bank qq-plot")
qqline(standardize_residual_bank, col = "darkorange2")
real estate
garchspec_house <- ugarchspec(
mean.model = list(armaOrder = c(order_house[1], order_house[3])),
variance.model = list(model = "gjrGARCH", garchOrder = c(1, 1)),
distribution.model = "sstd"
)
garchfit_house <- ugarchfit(data = house, spec = garchspec_house)
coef(garchfit_house)
Standardized residuals
standardize_residual_house <- residuals(garchfit_house) / sigma(garchfit_house)
layout(matrix(c(1, 2, 3, 4), 2, 2, byrow = TRUE))
acf(standardize_residual_house, main = "house residual ACF")
pacf(standardize_residual_house, main = "house residual PACF")
plot(standardize_residual_house, main = "residual", col = "darkorange2", lwd = 1)
qqnorm(standardize_residual_house, main = "house qq-plot")
qqline(standardize_residual_house, col = "darkorange2")
big data
garchspec_data <- ugarchspec(
mean.model = list(armaOrder = c(order_data[1], order_data[3])),
variance.model = list(model = "gjrGARCH", garchOrder = c(1, 1)),
distribution.model = "sstd"
)
garchfit_data <- ugarchfit(data = data, spec = garchspec_data)
coef(garchfit_data)
Standardized residuals
standardize_residual_data <- residuals(garchfit_data) / sigma(garchfit_data)
layout(matrix(c(1, 2, 3, 4), 2, 2, byrow = TRUE))
acf(standardize_residual_data, main = "data residual ACF")
pacf(standardize_residual_data, main = "data residual PACF")
plot(standardize_residual_data, main = "residual", col = "darkorange2", lwd = 1)
qqnorm(standardize_residual_data, main = "data qq-plot")
qqline(standardize_residual_data, col = "darkorange2")
Histogram summary
layout(matrix(c(1, 2, 3, 4), 2, 2, byrow = TRUE))
hist(standardize_residual_nobank, main = "nobank standardized residual", breaks = 70, col = "darkorange2")
hist(standardize_residual_bank, main = "bank standardized residual", breaks = 70, col = "deeppink3")
hist(standardize_residual_house, main = "house standardized residual", breaks = 70, col = "red")
hist(standardize_residual_data, main = "data standardized residual", breaks = 70, col = "blue")
Ten 、jb test : Normal distribution
Jarque-Bera test for log-return
JBtest_nobank_return <- jarque.bera.test(nobank)
JBtest_bank_return <- jarque.bera.test(bank)
JBtest_house_return <- jarque.bera.test(house)
JBtest_data_return <- jarque.bera.test(data)
JBtest_return <- matrix(c(JBtest_nobank_return$statistic, JBtest_nobank_return$p.value, JBtest_bank_return$statistic, JBtest_bank_return$p.value, JBtest_house_return$statistic, JBtest_house_return$p.value, JBtest_data_return$statistic, JBtest_data_return$p.value), 2, 4)
dimnames(JBtest_return) <- list(c("Jarque-Bera TS", "Jarque-Bera p-value"), c("nobank", "bank", "house", "data"))
JBtest_return
11、 ... and 、LJUNG BOX TEST : Autocorrelation
LJung- Box test for both log-return and standardized residuals
LJtest_nobank_return <- Box.test(nobank)
LJtest_bank_return <- Box.test(bank)
LJtest_house_return <- Box.test(house)
LJtest_data_return <- Box.test(data)
LJtest_return <- matrix(c(LJtest_nobank_return$statistic, LJtest_nobank_return$p.value, LJtest_bank_return$statistic, LJtest_bank_return$p.value, LJtest_house_return$statistic, LJtest_house_return$p.value, LJtest_data_return$statistic, LJtest_data_return$p.value), 2, 4)
dimnames(LJtest_return) <- list(c("LJung-Box TS", "LJung-Box p-value"), c("nobank", "bank", "house", "data"))
LJtest_return
Residual mix test
LJtest_nobank_residual <- Box.test(standardize_residual_nobank)
LJtest_bank_residual <- Box.test(standardize_residual_bank)
LJtest_house_residual <- Box.test(standardize_residual_house)
LJtest_data_residual <- Box.test(standardize_residual_data)
LJtest_residual <- matrix(c(LJtest_nobank_residual$statistic, LJtest_nobank_residual$p.value, LJtest_bank_residual$statistic, LJtest_bank_residual$p.value, LJtest_house_residual$statistic, LJtest_house_residual$p.value, LJtest_data_residual$statistic, LJtest_data_residual$p.value), 2, 4)
dimnames(LJtest_residual) <- list(c("LJung-Box TS", "LJung-Box p-value"), c("nobank", "bank", "house", "data"))
LJtest_residual
Twelve 、arch test
LM test , Whether the lag term has autocorrelation
** for both log-return and standardized residuals**
ARCHtest_nobank_return <- archtest(as.vector(nobank))
ARCHtest_bank_return <- archtest(as.vector(bank))
ARCHtest_house_return <- archtest(as.vector(house))
ARCHtest_data_return <- archtest(as.vector(data))
ARCHtest_return <- matrix(c(ARCHtest_nobank_return$statistic, ARCHtest_nobank_return$p.value, ARCHtest_bank_return$statistic, ARCHtest_bank_return$p.value, ARCHtest_house_return$statistic, ARCHtest_house_return$p.value, ARCHtest_data_return$statistic, ARCHtest_data_return$p.value), 2, 4)
dimnames(ARCHtest_return) <- list(c("Engle-ARCH TS", "Engle-ARCH p-value"), c("nobank", "bank", "house", "data"))
ARCHtest_return
The rate of return exists ARCH effect
ARCHtest_nobank_residual <- archtest(as.vector(standardize_residual_nobank))
ARCHtest_bank_residual <- archtest(as.vector(standardize_residual_bank))
ARCHtest_house_residual <- archtest(as.vector(standardize_residual_house))
ARCHtest_data_residual <- archtest(as.vector(standardize_residual_data))
ARCHtest_residual <- matrix(c(ARCHtest_nobank_residual$statistic, ARCHtest_nobank_residual$p.value, ARCHtest_bank_residual$statistic, ARCHtest_bank_residual$p.value, ARCHtest_house_residual$statistic, ARCHtest_house_residual$p.value, ARCHtest_data_residual$statistic, ARCHtest_data_residual$p.value), 2, 4)
dimnames(ARCHtest_residual) <- list(c("Engle-ARCH TS", "Engle-ARCH p-value"), c("nobank", "bank", "house", "data"))
ARCHtest_residual
The residuals do not exist arch effect
summary_testing <- rbind(rep(1, 4), rep(0, 4), c(0, 0, 0, 0), rep(1, 4), rep(0, 4))
dimnames(summary_testing) <- list(c("Jarque-Bera test", "LJung-Box test (return)", "LJung-Box test (residual)", "Engle-ARCH test (return)", "Engle-ARCH test (residual)"), c("nobank", "bank", "house", "data"))
summary_testing
Non normal distribution 、 The rate of return is irrelevant but exists arch effect 、 The residuals are not autocorrelated and have no arch effect
13、 ... and 、 Set the rate of return as GNG Distribution
Real residual simulation sequence
x_nobank <- coredata(standardize_residual_nobank)
x_bank <- coredata(standardize_residual_bank)
x_house <- coredata(standardize_residual_house)
x_data <- coredata(standardize_residual_data)
fit_nobank <- GNG_fit(standardize_residual_nobank, start = c(break1 = -2, break2 = 1.5, mean = 0, sd = 1, shape1 = 0.1, shape2 = 0.1))
plot(fit_nobank)
fit_bank <- GNG_fit(standardize_residual_bank, start = c(break1 = -2, break2 = 1.5, mean = 0, sd = 1, shape1 = 0.1, shape2 = 0.1))
plot(fit_bank)
fit_house <- GNG_fit(standardize_residual_house, start = c(break1 = -2, break2 = 1.5, mean = 0, sd = 1, shape1 = 0.1, shape2 = 0.1))
plot(fit_house)
fit_data <- GNG_fit(standardize_residual_data, start = c(break1 = -2, break2 = 1.5, mean = 0, sd = 1, shape1 = 0.1, shape2 = 0.1))
plot(fit_data)
Real residual calculation pdf、cdf、pdf The inverse
nobank
PDF_nobank <- function(x) {
d(distribution(fit_nobank), x)
}
CDF_nobank <- function(x) {
p(distribution(fit_nobank), x)
}
inverse_CDF_nobank <- function(x) {
q(distribution(fit_nobank), x)
}
bank
PDF_bank <- function(x) {
d(distribution(fit_bank), x)
}
CDF_bank <- function(x) {
p(distribution(fit_bank), x)
}
inverse_CDF_bank <- function(x) {
q(distribution(fit_bank), x)
}
house
PDF_house <- function(x) {
d(distribution(fit_house), x)
}
CDF_house <- function(x) {
p(distribution(fit_house), x)
}
inverse_CDF_house <- function(x) {
q(distribution(fit_house), x)
}
data
PDF_data <- function(x) {
d(distribution(fit_data), x)
}
CDF_data <- function(x) {
p(distribution(fit_data), x)
}
inverse_CDF_data <- function(x) {
q(distribution(fit_data), x)
}
graph
layout(matrix(c(1, 2, 3, 4), 2, 2, byrow = TRUE))
curve(PDF_nobank, -3, 3, col = "darkorange2")
curve(PDF_bank, -3, 3, col = "deeppink3")
curve(PDF_house, -3, 3, col = "red")
curve(PDF_data, -3, 3, col = "blue")
layout(matrix(c(1, 2, 3, 4), 2, 2, byrow = TRUE))
curve(CDF_nobank, -3, 3, col = "darkorange2")
curve(CDF_bank, -3, 3, col = "deeppink3")
curve(CDF_house, -3, 3, col = "red")
curve(CDF_data, -3, 3, col = "blue")
layout(matrix(c(1, 2, 3, 4), 2, 2, byrow = TRUE))
curve(inverse_CDF_nobank, col = "darkorange2")
curve(inverse_CDF_bank, col = "deeppink3")
curve(inverse_CDF_house, col = "red")
curve(inverse_CDF_data, col = "blue")
fourteen 、 The normalized residual probability is changed into uniform data
U_nobank <- CDF_nobank(standardize_residual_nobank)
U_bank <- CDF_bank(standardize_residual_bank)
U_house <- CDF_house(standardize_residual_house)
U_data <- CDF_data(standardize_residual_data)
all_data <- cbind(U_nobank, U_bank, U_house, U_data)
layout(matrix(c(1, 2, 3, 4), 2, 2, byrow = TRUE))
plot(x_nobank, x_bank)
plot(x_nobank, x_house)
plot(x_nobank, x_data)
plot(x_bank, x_house)
plot(x_bank, x_data)
plot(x_house, x_data)
plot(U_nobank, U_bank)
plot(U_nobank, U_house)
plot(U_nobank, U_data)
plot(U_bank, U_house)
plot(U_bank, U_data)
plot(U_house, U_data)
15、 ... and 、 Correlation test
Spearman’s rho
rho <- cor(all_data, method = "spearman")
rho
Kendall tau
kendall <- cor(all_data, method = "kendall")
kendall
Use the maximum likelihood method to find copula Parameters
X <- as.matrix(all_data)
fit_Gaussian <- fitCopula(normalCopula(dim = 4), X, method = "ml")
summary(fit_Gaussian)
coef(fit_Gaussian)
fit_tStudent <- fitCopula(tCopula(dim = 4), X, method = "ml")
summary(fit_tStudent)
coef(fit_tStudent)
sixteen 、 structure copula function
Gaussian Copula
Gaussian_model <- normalCopula(coef(fit_Gaussian), dim = 4)
getSigma(Gaussian_model)
T copula
T_model <- tCopula(coef(fit_tStudent)[1], dim = 4, df = coef(fit_tStudent)[2])
getSigma(T_model)
seventeen 、 Generate simulation data
1000 Group residuals are converted to logarithmic returns
n <- 1000
m <- nrow(all_data)
Generate matrix
nobank_matrix_G <- matrix(rep(0, n * m), nrow = m)
bank_matrix_G <- matrix(rep(0, n * m), nrow = m)
house_matrix_G <- matrix(rep(0, n * m), nrow = m)
data_matrix_G <- matrix(rep(0, n * m), nrow = m)
nobank_matrix_T <- matrix(rep(0, n * m), nrow = m)
bank_matrix_T <- matrix(rep(0, n * m), nrow = m)
house_matrix_T <- matrix(rep(0, n * m), nrow = m)
data_matrix_T <- matrix(rep(0, n * m), nrow = m)
simulation gaussian residual
for (i in 1:n) {
set.seed(i)
U <- rCopula(m, copula = Gaussian_model)
nobank_matrix_G[, i] <- inverse_CDF_nobank(U[, 1])
bank_matrix_G[, i] <- inverse_CDF_bank(U[, 2])
house_matrix_G[, i] <- inverse_CDF_house(U[, 3])
data_matrix_G[, i] <- inverse_CDF_data(U[, 4])
}
SR_nobank_G <- xts(x = nobank_matrix_G, order.by = index(standardize_residual_nobank))
SR_bank_G <- xts(x = bank_matrix_G, order.by = index(standardize_residual_bank))
SR_house_G <- xts(x = house_matrix_G, order.by = index(standardize_residual_house))
SR_data_G <- xts(x = data_matrix_G, order.by = index(standardize_residual_data))
layout(matrix(c(1, 2, 1, 2), 2, 2))
plot(SR_nobank_G, ylim = c(-7, 7), main = "Simulated S.RESIDUAL-nobank (Gaussian)", lwd = 0.5)
plot(SR_bank_G, ylim = c(-7, 7), main = "Simulated S.RESIDUAL-bank (Gaussian)", lwd = 0.5)
plot(SR_house_G, ylim = c(-7, 7), main = "Simulated S.RESIDUAL-house (Gaussian)", lwd = 0.5)
plot(SR_data_G, ylim = c(-7, 7), main = "Simulated S.RESIDUAL-data (Gaussian)", lwd = 0.5)
simulation t Distribution residuals
for (i in 1:n) {
set.seed(i)
U <- rCopula(m, copula = T_model)
nobank_matrix_T[, i] <- inverse_CDF_nobank(U[, 1])
bank_matrix_T[, i] <- inverse_CDF_bank(U[, 2])
house_matrix_T[, i] <- inverse_CDF_house(U[, 3])
data_matrix_T[, i] <- inverse_CDF_data(U[, 4])
}
SR_nobank_T <- xts(x = nobank_matrix_T, order.by = index(standardize_residual_nobank))
SR_bank_T <- xts(x = bank_matrix_T, order.by = index(standardize_residual_bank))
SR_house_T <- xts(x = house_matrix_T, order.by = index(standardize_residual_house))
SR_data_T <- xts(x = data_matrix_T, order.by = index(standardize_residual_data))
layout(matrix(c(1, 2, 1, 2), 2, 2))
plot(SR_nobank_T, ylim = c(-7, 7), main = "Simulated S.RESIDUAL-nobank (T)", lwd = 0.5)
plot(SR_bank_T, ylim = c(-7, 7), main = "Simulated S.RESIDUAL-bank (T)", lwd = 0.5)
plot(SR_house_T, ylim = c(-7, 7), main = "Simulated S.RESIDUAL-house (T)", lwd = 0.5)
plot(SR_data_T, ylim = c(-7, 7), main = "Simulated S.RESIDUAL-data (T)", lwd = 0.5)
Will the residual error arma_garch Conversion yield
return <- function(model, SR, num_col) {
sigma_matrix <- coredata(sigma(model))
diagonal_sigma <- diag(as.numeric(sigma_matrix), nrow = length(sigma_matrix))
simulated_residuals <- diagonal_sigma %*% SR
# sigma* Standardized residuals = Original residual
simulated_log_returns <- simulated_residuals + matrix(rep(as.numeric(coredata(fitted(model))), num_col), ncol = num_col)
simulated_log_returns <- xts(simulated_log_returns, order.by = index(sigma(model)))
simulated_log_returns
}
return_nobank_G <- return(garchfit_nobank, SR_nobank_G, num_col = n)
return_bank_G <- return(garchfit_bank, SR_bank_G, num_col = n)
return_house_G <- return(garchfit_house, SR_house_G, num_col = n)
return_data_G <- return(garchfit_data, SR_data_G, num_col = n)
layout(matrix(c(1, 2, 1, 2), 2, 2))
plot(return_nobank_G, main = "Simulated RETURN-nobank (Gaussian)", lwd = 0.5)
plot(return_bank_G, main = "Simulated RETURN-bank (Gaussian)", lwd = 0.5)
plot(return_house_G, main = "Simulated RETURN-house (Gaussian)", lwd = 0.5)
plot(return_data_G, main = "Simulated RETURN-data (Gaussian)", lwd = 0.5)
return_nobank_T <- return(garchfit_nobank, SR_nobank_T, num_col = n)
return_bank_T <- return(garchfit_bank, SR_bank_T, num_col = n)
return_house_T <- return(garchfit_house, SR_house_T, num_col = n)
return_data_T <- return(garchfit_data, SR_data_T, num_col = n)
layout(matrix(c(1, 2, 1, 2), 2, 2))
plot(return_nobank_T, main = "Simulated RETURN-nobank (T)", lwd = 0.5)
plot(return_bank_T, main = "Simulated RETURN-bank (T)", lwd = 0.5)
plot(return_house_T, main = "Simulated RETURN-house (T)", lwd = 0.5)
plot(return_data_T, main = "Simulated RETURN-data (T)", lwd = 0.5)
eighteen 、 Calculation VaR
Real data
weight <- c(0.25, 0.25, 0.25, 0.25)
real_port_return <- (weight[1] * nobank) + (weight[2] * bank) + (weight[3] * house) + (weight[4] * data)
plot(real_port_return, ylim = c(-5, 5), main = "real portfolio return", lwd = 1, col = "darkblue")
Gauss return
port_return_G <- weight[1] * return_nobank_G + weight[2] * return_bank_G + weight[3] * return_house_G + weight[4] * return_data_G
VaR 99
VaR99_return_G <- xts(matrix(rep(1, m), ncol = 1), order.by = index((port_return_G)))
for (i in 1:m) {
VaR99_return_G[i, ] <- quantile(port_return_G[i, ], p = 0.01)
}
VaR 95
VaR95_return_G <- xts(matrix(rep(1, m), ncol = 1), order.by = index((port_return_G)))
for (i in 1:m) {
VaR95_return_G[i, ] <- quantile(port_return_G[i, ], p = 0.05)
}
VaR 90
VaR90_return_G <- xts(matrix(rep(1, m), ncol = 1), order.by = index((port_return_G)))
for (i in 1:m) {
VaR90_return_G[i, ] <- quantile(port_return_G[i, ], p = 0.1)
}
layout(matrix(c(1, 2, 1, 2), 2, 2))
plot(port_return_G, ylim = c(-10, 10), main = "Simulated port_return (Gaussian)", lwd = 0.5)
VaRplot(0.01, real_port_return, VaR99_return_G)
VaRplot(0.05, real_port_return, VaR95_return_G)
VaRplot(0.1, real_port_return, VaR90_return_G)
layout(matrix(c(1), 1, 1))
plot(cbind(real_port_return, VaR99_return_G, VaR95_return_G, VaR90_return_G), col = c("black", "deeppink3", "darkorange2", "blue"), main = "GAUSSIAN COPULA", lwd = 0.7)
t Return
port_return_T <- weight[1] * return_nobank_T + weight[2] * return_bank_T + weight[3] * return_house_T + weight[4] * return_data_T
VaR 99
VaR99_return_T <- xts(matrix(rep(1, m), ncol = 1), order.by = index((port_return_T)))
for (i in 1:m) {
VaR99_return_T[i, ] <- quantile(port_return_T[i, ], p = 0.01)
}
VaR 95
VaR95_return_T <- xts(matrix(rep(1, m), ncol = 1), order.by = index((port_return_T)))
for (i in 1:m) {
VaR95_return_T[i, ] <- quantile(port_return_T[i, ], p = 0.05)
}
VaR 90
VaR90_return_T <- xts(matrix(rep(1, m), ncol = 1), order.by = index((port_return_T)))
for (i in 1:m) {
VaR90_return_T[i, ] <- quantile(port_return_T[i, ], p = 0.1)
}
layout(matrix(c(1, 2, 1, 2), 2, 2))
plot(port_return_T, ylim = c(-10, 10), main = "Simulated port_return (T-Student)", lwd = 0.5)
VaRplot(0.01, real_port_return, VaR99_return_T)
VaRplot(0.05, real_port_return, VaR95_return_T)
VaRplot(0.1, real_port_return, VaR90_return_T)
layout(matrix(c(1), 1, 1))
plot(cbind(real_port_return, VaR99_return_T, VaR95_return_T, VaR90_return_T), col = c("black", "deeppink3", "darkorange2", "blue"), main = "T-STUDENT COPULA", lwd = 0.7)
plot(cbind(real_port_return, VaR99_return_G, VaR99_return_T), col = c("black", "blue", "red"), main = "VAR 99%", lwd = 0.7)
plot(cbind(real_port_return, VaR95_return_G, VaR95_return_T), col = c("black", "blue", "red"), main = "VAR 95%", lwd = 0.7)
plot(cbind(real_port_return, VaR90_return_G, VaR90_return_T), col = c("black", "blue", "red"), main = "VAR 90%", lwd = 0.7)
nineteen 、 test
5% significance level
Gaussian Copula
test1_99_G <- VaRTest(0.01, real_port_return, VaR99_return_G)
test1_95_G <- VaRTest(0.05, real_port_return, VaR95_return_G)
test1_90_G <- VaRTest(0.1, real_port_return, VaR90_return_G)
T Copula
test1_99_T <- VaRTest(0.01, real_port_return, VaR99_return_T)
test1_95_T <- VaRTest(0.05, real_port_return, VaR95_return_T)
test1_90_T <- VaRTest(0.1, real_port_return, VaR90_return_T)
Summary of the two tests
Kupiec_G <- c(test1_99_G$uc.LRp, test1_99_G$uc.Decision, test1_95_G$uc.LRp, test1_95_G$uc.Decision, test1_90_G$uc.LRp, test1_90_G$uc.Decision)
Christoffersen_G <- c(test1_99_G$cc.LRp, test1_99_G$cc.Decision, test1_95_G$cc.LRp, test1_95_G$cc.Decision, test1_90_G$cc.LRp, test1_90_G$cc.Decision)
cbind(Kupiec_G, Christoffersen)
Kupiec_T <- c(test1_99_T$uc.LRp, test1_99_T$uc.Decision, test1_95_T$uc.LRp, test1_95_T$uc.Decision, test1_90_T$uc.LRp, test1_90_T$uc.Decision)
Christoffersen_T <- c(test1_99_T$cc.LRp, test1_99_T$cc.Decision, test1_95_T$cc.LRp, test1_95_T$cc.Decision, test1_90_T$cc.LRp, test1_90_T$cc.Decision)
cbind(Kupiec_T, Christoffersen)
The model setting is correct .
边栏推荐
- 行為樹XML文件 熱加載
- 力扣399【除法求值】【并查集】
- Programming training 7- date conversion problem
- Phpcms V9 background article list adds one click push to Baidu function
- Selenium builds cookies pool to bypass authentication and anti crawl login
- Isinstance() function usage
- 上下架和橱窗推荐如何设置,优化过程需要注意的地方
- 百度小程序富文本解析工具bdParse
- 编程训练7-日期转换问题
- Unity WebGL发布无法运行问题
猜你喜欢

编辑类型信息

phpcms小程序插件api接口升级到4.3(新增批量获取接口、搜索接口等)

Regular Expression 正则表达式

The solution of positioning failure caused by framework jump

Practice is the fastest way to become a network engineer

Baidu applet rich text parsing tool bdparse

设置QCheckbox 样式的注意事项

修复小程序富文本组件不支持video视频封面、autoplay、controls等属性问题

Section IV HQL execution process

Uniapp uses uparse to parse the content of the background rich text editor and modify the uparse style
随机推荐
行為樹XML文件 熱加載
爬虫 对 Get/Post 请求时遇到编码问题的解决方案
[qnx hypervisor 2.2 user manual]12.1 terminology (I)
phpcms小程序插件api接口升级到4.3(新增批量获取接口、搜索接口等)
phpcms小程序插件4.0版正式上线
Particles and sound effect system in games104 music 12 game engine
Differences between commonjs and ES6 modularity
Computer mall based on SSM
Programming training 7- date conversion problem
Drawing with MATLAB (1)
cookie session 和 token
【MATLAB GUI】 键盘回调中按键识别符查找表
Solution to the encoding problem encountered by the crawler when requesting get/post
In depth study paper reading target detection (VII) Chinese version: yolov4 optimal speed and accuracy of object detection
Phpcms applet interface new universal interface get_ diy. php
力扣399【除法求值】【并查集】
Phpcms applet plug-in version 4.0 was officially launched
Machine learning (Part 2)
拦截器与过滤器的实现代码
远程工作的一些命令