About

This worksheet looks at VaR calculations for the two cases of single and multiple equity asset. This is the last lab worksheet, other than the extra credit assignment.

Setup

Remember to always set your working directory to the source file location. Go to ‘Session’, scroll down to ‘Set Working Directory’, and click ‘To Source File Location’. Read carefully the below and follow the instructions to complete the tasks and answer any questions. Submit your work to RPubs as detailed in previous notes.

Note

Always read carefully the instructions on Sakai. For clarity, tasks/questions to be completed/answered are highlighted in red color (visible in preview) and numbered according to their particular placement in the task section. Quite often you will need to add your own code chunk.

Execute all code chunks, preview, publish, and submit link on Sakai follwoing the naming convention. Make sure to add comments to your code where appropriate. Use own language!


Task 1: VaR Calculations: Single Equity Asset

#Install package quantmod 
install.packages("quantmod")
## Installing package into '/home/rstudio-user/R/x86_64-pc-linux-gnu-library/3.5'
## (as 'lib' is unspecified)
library("quantmod")
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: TTR
## Version 0.4-0 included new data defaults. See ?getSymbols.

Consider the time series of a stock of your choice (other than AAPL and your colleague!) and for the time-period from Jan 1, 2015 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.

##### 1A) Calculate the historical daily log returns, mean and standard deviation

library("quantmod")
getSymbols("RPM", from = "2015-01-01", to = Sys.Date())
## 'getSymbols' currently uses auto.assign=TRUE by default, but will
## use auto.assign=FALSE in 0.5-0. You will still be able to use
## 'loadSymbols' to automatically load data. getOption("getSymbols.env")
## and getOption("getSymbols.auto.assign") will still be checked for
## alternate defaults.
## 
## This message is shown once per session and may be disabled by setting 
## options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.
## 
## WARNING: There have been significant changes to Yahoo Finance data.
## Please see the Warning section of '?getSymbols.yahoo' for details.
## 
## This message is shown once per session and may be disabled by setting
## options("getSymbols.yahoo.warning"=FALSE).
## [1] "RPM"
price = RPM$RPM.Adjusted
price = as.numeric(price)
ret = diff(log(price))
mu = mean(ret)
sigma = sd(ret)

Mean = 0.0002323457 Standard Deviation = 0.01325401

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.

##### 1B) Plot a histogram and mark the quantile value that corresponds to 95% confidence level. A mark can be added to a plot by using the function abline().

hist(ret, main = "Historical RPM 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.

##### 1C) Assume $1000 investment in the stock, calculate the 95% VaR for 3 days’ time horizon. Explain the meaning of VaR in practical terms.

T = 3
alpha = 0.05
V = 1000
VaR = V * (exp(quantile(ret, probs = 0.05))-1)
VaR
##        5% 
## -20.42294

VaR for 3 day time horizon and $1,000 investment = 20.42294 In practical terms, this means there is a 5% chance of losing at least $20.42294 over a 3 day time horizon

##### 1D) How does the VaR calculated in 1C changes if we assumed simple returns instead of the log returns.

library("quantmod")
getSymbols("RPM", from = "2015-01-01", to =Sys.Date())
## [1] "RPM"
price = RPM$RPM.Adjusted
price = as.numeric(price)
ret2 = periodReturn(RPM, period = "daily", type = "arithmetic")
mu2 = mean(ret2)
sigma2 = sd(ret2)

hist(ret2, main = "RPM Simple Returns", breaks = 50)
q = quantile(ret2, probs = 0.05)
abline(v=q, col = "red")

T = 3
alpha = 0.05
V = 1000
VarSim = V*quantile(ret2, probs=0.05)
VarSim
##        5% 
## -20.47462

The simple return VaR = $20.47462, which is slightly higher than the VaR for the historical log returns

Calculations so far have been on the histogram of log returns. We can instead, looking at the histogram, assume a normal distribution of the log returns. The normal return assumption will have a mean and standard deviation equal to those calculated from the histogram, and properly scaled by the horizon time. Note that the mean is linearly scaled with time and volatility with square-root of time.

##### 1E) Repeat the calculation in 1D assuming instead a normal distribution. How the two results compare and why so?

T = 3
alpha = 0.05
V = 1000
VarSim2 = V*qnorm(alpha, mean =T*mu2, sd=sqrt(T)*sigma2)
VarSim2
## [1] -37.02154

The simple return VaR for a normal distribution = $37.02154

##### 1F) Write down the mathematical representation corresponding to the VaR calculation in 1E. Explain what each variable in the equation represents, and provide the corresponding numerical value.

To calculate the VaR (value-at-risk) of returns, we use the mathematical formula: VaR = -F^(-1)(1-C) c is the confidence level. Therefore, c = 0.95 in this example Since the confidence is 95%, = -F^(-1)(1-0.95) This can also be written as X = (Z*s) - mu

The 95% confidence level for standard normal distribution corresponds to a z value equal to -1.645%. z is the standard normal distribution transformation sigma is the standard deviation Mu is the mean

A 3-day VaR is obtained from the daily VaR by scaling standard deviation with sqrt(3). The amount invested in the portfolio is $1000. Scaling the 3-day horizon: s = sigma * √3 μ = mu * 3 X = (Zs) - mu VaR = V X

Task 2: VaR Calculations: Multiple Equity Assets

Consider the times series of the three stocks DIS, TWTR, and NFLX for the time-period from Jan 1,2015 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. Fiinally 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.

##### 2A) Calculate the portfolio 99% VaR for 1 day, and 3 days. Specify whether using log-returns or simple returns.

library("quantmod")
symbols = c("Dis","TWTR","NFLX")
getSymbols(symbols, src = "yahoo", from = "2015-01-01", to = Sys.Date())
## [1] "DIS"  "TWTR" "NFLX"
disRd = as.numeric(periodReturn(DIS$DIS.Adjusted, period ="daily", type = "log"))
TwtrRd = as.numeric(periodReturn(TWTR$TWTR.Adjusted, period ="daily", type = "log"))
NFLXRd = as.numeric(periodReturn(NFLX$NFLX.Adjusted, period ="daily", type = "log"))
m = cbind(disRd, TwtrRd, NFLXRd)
cor(m)
##            disRd    TwtrRd    NFLXRd
## disRd  1.0000000 0.2154637 0.2851774
## TwtrRd 0.2154637 1.0000000 0.2680482
## NFLXRd 0.2851774 0.2680482 1.0000000
cov(m)
##               disRd       TwtrRd       NFLXRd
## disRd  1.457160e-04 8.731851e-05 9.320778e-05
## TwtrRd 8.731851e-05 1.127086e-03 2.436548e-04
## NFLXRd 9.320778e-05 2.436548e-04 7.331061e-04
w = rep(1/3, 3)
var_p = w %*% cov(m) %*% w
mu_p = colMeans(m) %*% w 
T = 1
alpha = 0.01
V = 100000
Var1 = V*(exp(qnorm(alpha, mean = T*mu_p, sd= sqrt(T*var_p)))-1)
Var1
## [1] -3991.58

VaR for 1 day = $3,991.58

T = 3
alpha = 0.01
V = 100000
Var3 = V*(exp(qnorm(alpha, mean = T*mu_p, sd= sqrt(T*var_p)))-1)
Var3
## [1] -6730.162

The 3 day VaR is $6,730.162

We then describe the mathetical formulation useds in the code to perform the calculations

##### 2B) Write down the mathematical representations used in the code to calculate the VaR. Label properly each variable, what it represents, and the numerical value corresponding to in the code. VaR = V * Z * (Sqrt(T)sigma) Var1 = 100000 -3.090 * (sqrt(1)sigma) Var3 = 100000 -3.090 * (sqrt(3)*sigma)

We now calculated individual VaR corresponding to each asst in the portfolio

##### 2C) Calculate the three-individual asset 99% VaR for 1 day.

library("quantmod")
symbols = c("Dis","TWTR","NFLX")
getSymbols(symbols, src = "yahoo", from = "2015-01-01", to = Sys.Date())
## [1] "DIS"  "TWTR" "NFLX"
disRd = as.numeric(periodReturn(DIS$DIS.Adjusted, period ="daily", type = "log"))
TwtrRd = as.numeric(periodReturn(TWTR$TWTR.Adjusted, period ="daily", type = "log"))
NFLXRd = as.numeric(periodReturn(NFLX$NFLX.Adjusted, period ="daily", type = "log"))


mudis = mean(disRd)
sigmadis = sd(disRd)
T = 1
alpha = 0.01
V = 100000/3
VaRdis = V*(exp(qnorm(alpha, mean=T*mudis, sd=sqrt(T)*sigmadis))-1)
VaRdis
## [1] -915.4302
mutwtr = mean(TwtrRd)
sigmatwtr = sd(TwtrRd)
T = 1
alpha = 0.01
V = 100000/3
VaRtwtr = V*(exp(qnorm(alpha, mean=T*mutwtr, sd=sqrt(T)*sigmatwtr))-1)
VaRtwtr
## [1] -2506.13
munflx = mean(NFLXRd)
sigmanflx = sd(NFLXRd)
T = 1
alpha = 0.01
V = 100000/3
VaRnflx = V*(exp(qnorm(alpha, mean=T*munflx, sd=sqrt(T)*sigmanflx))-1)
VaRnflx
## [1] -1975.064

Disney VaR = $915.4302 Twitter VaR = $2,506.13 Netflix VaR = $1,975.064

Finally we compare the overall VaR to the individual VaR calculations

##### 2D) Compare the overall portfolio VaR to the sum of the individual asset VaR for the 1 day case. Share observations.

Overall portfolio VaR = $3,991.58 Sum of individual VaR = $5,396.62

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

*http://computationalfinance.lsi.upc.edu