#VAR ~ Expected Shortfall

library(quantmod) library(PerformanceAnalytics) library(tidyverse) library(xts)

tickers <- c(“NU”, “C”, “ITUB”)

getSymbols(tickers, from=“2022-01-01”, to=Sys.Date(), auto.assign=TRUE)

portfolio_prices <- merge((NU\(NU.Close),(C\)C.Close),(ITUB$ITUB.Close)) plot(portfolio_prices, main=“Portfolio Prices”, grid.col=“NA”)

colnames(portfolio_prices) <- tickers

portfolio_returns <- na.omit(Return.calculate(portfolio_prices))

head(portfolio_returns)

Define portfolio weights

weights <- c(1/3, 1/3, 1/3)

#(NU × 1/3) + (C × 1/3) + (ITUB × 1/3) portfolio_returns\(Portfolio <- rowSums(portfolio_returns * weights) portVAR <- portfolio_returns\)Portfolio

#VAR

conf_level <- 0.95

#Historical ~ Gaussian ~ MonteCarlo

#HISTORICAL

VaR_hist <- quantile(portVAR, probs = 1 - conf_level) hist(portVAR, breaks = 50, col = “darkgrey”, main = “Historical VAR Histogram”, xlab = “Returns”) abline(v = VaR_hist, col = “red”, lwd = 2, lty = 2) cat(“Historical VAR at 95% confidence is:”,VaR_hist*100,“%”)

#GAUSSIAN mean_port <- mean(portVAR) ; sd_port <- sd(portVAR) VaR_Gaussian <- mean_port + qnorm(1 - conf_level) * sd_port hist(portVAR, breaks = 50, col = “darkgrey”, main = “Historical VAR Histogram”, xlab = “Returns”) abline(v = VaR_hist, col = “red”, lwd = 2, lty = 2) abline(v = VaR_Gaussian, col= “blue”, lwd = 2, lty = 2) cat(“Gaussian VAR at 95% confidence is:”,VaR_Gaussian*100,“%”)

#Montecarlo

length(portVAR) n_sim <- 835 simulated_returns <- rnorm(n_sim, mean_port, sd_port)

hist(simulated_returns, col = rgb(1, 0, 0, 0.5), main = “Histogram Comparison”, xlab = “Returns”, breaks=30) hist(portVAR, col = rgb(0, 0, 1, 0.5), add = TRUE, breaks=30)

n_sim <- 10000 simulated_returns <- rnorm(n_sim, mean_port, sd_port)

VaR_mc <- quantile(simulated_returns, probs = 1 - conf_level) hist(simulated_returns, breaks = 50, col = “darkgrey”, main = “Monte Carlo VAR Histogram”, xlab = “Returns”) abline(v = VaR_mc, col = “red”, lwd = 2, lty = 2) cat(“Montecarlo VAR at 95% confidence is:”,VaR_mc*100,“%”)

hist(simulated_returns, breaks = 50, col = “darkgrey”, main = “Monte Carlo VAR Histogram”, xlab = “Returns”, xlim = c(-0.04, -0.02)) abline(v = VaR_mc, col = “red”, lwd = 2, lty = 2) abline(v = VaR_hist, col = “black”, lwd = 2, lty = 2) abline(v = VaR_Gaussian, col= “blue”, lwd = 2, lty = 2) legend(“topright”, legend = c(“Monte Carlo VaR”, “Historical VaR”, “Gaussian VaR”), col = c(“red”, “black”, “blue”), lwd = 2, lty = 2)

cat(“Monte Carlo VaR:”, VaR_mc*100,“%”,” | Historical VaR:“, VaR_hist100,”%”,” | Gaussian VaR:”, VaR_Gaussian100,”%“,”“)

#Expected ShortFall (ES)

#HISTORICAL

ES_hist <- mean(portVAR[portVAR <= VaR_hist])

VaR_example <- quantile(portVAR, probs = conf_level) hist(portVAR, breaks = 50, col = “darkgrey”, main = “Historical VAR Histogram”, xlab = “Returns”) abline(v = VaR_hist, col = “darkblue”, lwd = 2, lty = 2) abline(v = VaR_example, col = “darkblue”, lwd = 2, lty = 2)

cat(“Historical Expected ShortFall at 95% is:”, ES_hist*100,“%”)

#MonteCarlo

ES_mc <- mean(simulated_returns[simulated_returns <= VaR_mc]) cat(“Montecarlo Expected ShortFall at 95% is:”, ES_mc*100,“%”)

results <- data.frame( Method = c(“Historical”, “Parametric”, “Monte Carlo”), VaR = c(VaR_hist, VaR_Gaussian, VaR_mc), ES = c(ES_hist, NA, ES_mc) )

investment <- 10000 results\(Loss_VaR <- results\)VaR * investment results\(Loss_ES <- results\)ES * investment

results