This worksheet looks at VaR calculations for the two cases of single and multiple equity asset.
Consider the time series of a stock of your choice(we looked at Fedex stock) and for the time-period from Jan 1, 2018 to present.
We will first generate the historical time series of daily log returns, and calculate the mean and standard deviation of the time series.
Calculate the historical daily log returns, mean and standard deviation
#Install package quantmod
#install.packages("quantmod")
library("quantmod")
## Loading required package: xts
## Warning: package 'xts' was built under R version 4.0.5
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Loading required package: TTR
## Warning: package 'TTR' was built under R version 4.0.5
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
getSymbols("FDX", from = "2015-01-01", to = Sys.Date())
## [1] "FDX"
price = FDX$FDX.Adjusted
price = as.numeric(price)
ret = diff(log(price))
mu = mean(ret)
sigma = sd(ret)
sprintf("Mean is: %f", mu )
## [1] "Mean is: -0.000026"
sprintf("standard deviation is: %f",sigma)
## [1] "standard deviation is: 0.020741"
Given a time series we can generate a histogram and mark the quantile value that corresponds to the 95% confidence level. The quantile value in this case is the critical value for which 95% of the data is on the right (higher) of the critical value as represented by the histogram. The remaining 5% will be on the left. To find the quantile value you will need to use the function quantile() with the proper arguments. For example quantile(xts,probs=0.01) applies to a time series xts and return the critical value correspondng to 99% confidence level.
hist(ret, main = "Historical FDX Log Returns", breaks = 50)
q = quantile(ret, probs = 0.05)
abline(v = q, col = "blue")
Given the above, we can calculate a VaR value for a given investment and a time horizon. Assume $2000 investment in the stock, calculate the 95% VaR for 5 days’ time horizon.
As calculated we got VaR(with log return) for 5 day time horizon and $2,000 investment = 59.227379 In practical terms, this means there is a 5% chance of losing at least $59.227379 over a 5 day time horizon
VaR(with Simple return) for 5 day time horizon and $2,000 investment = 59.360998 In practical terms, this means there is a 5% chance of losing at least $59.360998 over a 5 day time horizon
Since log returns are continuously compounded returns, it is normal to see that the log returns var are lower than simple returns.
T = 5
alpha = 0.05
V = 2000
VaR = V * (exp(quantile(ret, probs = 0.05))-1)
sprintf("Var(value at risk ) is: %f", VaR )
## [1] "Var(value at risk ) is: -59.215548"
How does the VaR calculation change if we assume simple returns instead of the log returns
ret2 = periodReturn(FDX, period = "daily", type = "arithmetic")
mu2 = mean(ret2)
sigma2 = sd(ret2)
hist(ret2, main = "FDX Simple Returns", breaks = 50)
q = quantile(ret2, probs = 0.05)
abline(v=q, col = "red")
T = 5
alpha = 0.05
V = 2000
VarSim = V*quantile(ret2, probs=0.05)
VarSim
## 5%
## -59.35625
sprintf("Var(value at risk ) with simple returns is: %f", VarSim )
## [1] "Var(value at risk ) with simple returns is: -59.356252"
Consider the times series of the three stocks DIS, TWTR, and NFLX for the time-period from Jan 1,2018 to present. Assume an investment of $100,000 equally distributed among all three stocks.
To calculate the portfolio VaR we will follow the methodolgy described by the variance-covariance. First the covariance matrix needs to be computed. We then calculate the variance or volatility of the portolio as expressed in the varaince-covariance method taking into the weights associated with each asset in the portfolio. Finally we compute the mean or expected return of the portfolio taking also into account the weights. Given the expected return and volatility we should be able to compute the VaR of the portfolio. The assumption is we have a normal distribution of log returns.
First we calculate the overall portfolio VaR.
Calculate the portfolio 99% VaR for 3 day, and 5 days for 100k invested amount.
symbols = c("AMZN","NVDA","NFLX")
getSymbols(symbols, src = "yahoo", from = "2018-01-01", to = Sys.Date())
## [1] "AMZN" "NVDA" "NFLX"
AmznRd = as.numeric(periodReturn(AMZN$AMZN.Adjusted, period ="daily", type = "log"))
NvdaRd = as.numeric(periodReturn(NVDA$NVDA.Adjusted, period ="daily", type = "log"))
NFLXRd = as.numeric(periodReturn(NFLX$NFLX.Adjusted, period ="daily", type = "log"))
m = cbind(AmznRd, NvdaRd, NFLXRd)
cor(m)
## AmznRd NvdaRd NFLXRd
## AmznRd 1.0000000 0.6447532 0.6095033
## NvdaRd 0.6447532 1.0000000 0.5068820
## NFLXRd 0.6095033 0.5068820 1.0000000
w = rep(1/3, 3)
var_p = t(w) %*% cov(m) %*% w
mu_p = colMeans(m) %*% w
T = 3
alpha = 0.01
V = 100000
Var3 = V*(exp(qnorm(alpha, mean = T*mu_p, sd= sqrt(T*var_p)))-1)
sprintf("Portfolio Var for 3 days is %f",Var3)
## [1] "Portfolio Var for 3 days is -9119.652486"
T = 5
alpha = 0.01
V = 100000
Var5 = V*(exp(qnorm(alpha, mean = T*mu_p, sd= sqrt(T*var_p)))-1)
sprintf("Portfolio Var for 5 days is %f",Var5)
## [1] "Portfolio Var for 5 days is -11562.729695"
muAmzn = mean(AmznRd)
sigmaAmzn = sd(AmznRd)
T = 3
alpha = 0.01
V = 100000/3
VaRAmzn = V*(exp(qnorm(alpha, mean=T*muAmzn, sd=sqrt(T)*sigmaAmzn))-1)
sprintf("individual AMZN Var for 3 days is %f",VaRAmzn)
## [1] "individual AMZN Var for 3 days is -2777.772593"
muAmzn = mean(AmznRd)
sigmaAmzn = sd(AmznRd)
T = 3
alpha = 0.01
V = 100000/3
VaRAmzn = V*(exp(qnorm(alpha, mean=T*muAmzn, sd=sqrt(T)*sigmaAmzn))-1)
sprintf("individual AMZN Var for 3 days is %f",VaRAmzn)
## [1] "individual AMZN Var for 3 days is -2777.772593"
muNvda = mean(NvdaRd)
sigmaNvda = sd(NvdaRd)
T = 3
alpha = 0.01
V = 100000/3
VaRNvda = V*(exp(qnorm(alpha, mean=T*muNvda, sd=sqrt(T)*sigmaNvda))-1)
sprintf("individual NVDA Var for 3 days is %f",VaRNvda)
## [1] "individual NVDA Var for 3 days is -4025.168084"
muNflx = mean(NFLXRd)
sigmaNflx = sd(NFLXRd)
T = 3
alpha = 0.01
V = 100000/3
VaRNflx = V*(exp(qnorm(alpha, mean=T*muNflx, sd=sqrt(T)*sigmaNflx))-1)
sprintf("individual NFLX Var for 3 days is %f",VaRNflx)
## [1] "individual NFLX Var for 3 days is -3851.204269"
totalIndVar=VaRAmzn + VaRNvda + VaRNflx
sprintf("sum of individual AMZN,NVDA,NFLX var is %f",totalIndVar)
## [1] "sum of individual AMZN,NVDA,NFLX var is -10654.144947"
sprintf("Overall Portfolio Var for 3 days is %f",Var3)
## [1] "Overall Portfolio Var for 3 days is -9119.652486"
Based on the results we can see that the significant reduction in VaR for the overall portfolio relative to the sum of the individual asset VaR’s is due to the benefit of portfolio diversification