Links to all tutorial articles (same as those on the Exam pages)VaR disaggregation in R  Marginal and Component VaR
This article is a practical and real example of how valueatrisk, marginal VaR, betas, undiversified VaR and component VaRs are calculated. Credit to Pawel Lachowicz for the idea for this article, he has done the same thing in Python here: http://www.quantatrisk.com/2015/01/18/appliedportfoliovalueatriskdecomposition1marginalandcomponentvar/
The level of detail here may not be required for the PRM exam, for that just the previous tutorial will do (the one with the formulae and explanations). This article will be useful for practising risk analysts  we will extract real endofday prices, and perform our analysis on those in R. To run the R code, you will need to install R on your machine. The good thing with R is that it does not require administrative privileges, so if you can download it from Cran, you can run it. If you are looking for an introduction to R, you can look at this 20 minute video I posted a few years ago: https://www.youtube.com/user/riskprep
This is how we will do our analysis:
All using the same formulae that have been used in the previous tutorial.
Code: # The first two lines instal xts and Quandl  you will need to run these only once. library(xts) # d1 to d7 are time series variables that store the data downloaded from Quandl. d1 = Quandl("WIKI/AAPL", type="xts",start_date=as.Date("20120101"),end_date=as.Date("20141231")) d2 = Quandl("WIKI/DISCA", type="xts",start_date=as.Date("20120101"),end_date=as.Date("20141231")) d3 = Quandl("WIKI/IBM", type="xts",start_date=as.Date("20120101"),end_date=as.Date("20141231")) d4 = Quandl("WIKI/JNJ", type="xts",start_date=as.Date("20120101"),end_date=as.Date("20141231")) d5 = Quandl("WIKI/KO", type="xts",start_date=as.Date("20120101"),end_date=as.Date("20141231")) d6 = Quandl("WIKI/NKE", type="xts",start_date=as.Date("20120101"),end_date=as.Date("20141231")) d7 = Quandl("WIKI/TXN", type="xts",start_date=as.Date("20120101"),end_date=as.Date("20141231")) # We merge the 7 datasets # We calculate daily log returns, and store it in a time series called returns. # Now we enter our portfolio values. Assume that our portfolio has a total value of $1m. This w = matrix(data = c(50000, 170000, 80000, 170000, 200000, 140000, 190000), nrow = 7, ncol = 1, # We put the percentage weights in a matrix called wts. # We calculate portfolio returns. Note that %*% is matrix multiplication in R. # We calculate the covariance matrix. A one step process. Note that these are daily covariances. V = cov(na.omit(returns)) * 250
# We calculate the covariances of asset i to portfolio p. # Calculate the portfolio volatility, in percent, and in dollars. Note that we # Calculate the VaR, in dollars and percent. Note that 'qnorm' is equivalent to Excel's NORMSINV. # Calculate the volatility of the ith asset. We can then calculate the VaR of the ith asset. # We calculate the undiversified VaR as the sum of the VaRs of the individual assets. # We calculate the beta. Note that this is the same answer # Calculate Marginal VaR # Calculate Component VaR # At this point you can print each of the variables by typing them on the R prompt.
> print ("Portfolio Constituents in Dollars")
