# Testing severity of VaR by changing portfolio component weights

Let's assume that I have a portfolio with two components:$$\omega_i = 0.3$$ $$\omega_j = 0.7$$

I also have two P&L vectors, $$v_i$$ and $$v_j$$ each containing 1000 P&Ls. I would like to play around with the weights and test out the VaR 95% at portfolio/total level. What would be the easiest and most intuitive approach (either in R or Python) if I wanted to extend this exercise to $$n$$ components instead of having only two?

Note that to find the portfolio P&L I am simply multiplying each P&L by each component's weight and then summing them together: $$PnL_t = \omega_i \times v_{i,t} + \omega_j \times v_{j,t}$$

• Is this a question how to program, given a rule for the weights or a question how to generate/choose meaningful weights?
– g g
Jul 13, 2021 at 7:04

In R, the simplest way to brute force through a predefined number of portfolio combinations would be to simply iterate over them:

set.seed(42)
returns <- matrix(rnorm(200),40,5)
weights <- list(c(1,0,0,0,0),
c(0,1,0,0,0),
c(0,0,1,0,0),
c(0,0,0,1,0),
c(0,0,0,0,1))

conf <- 0.99

sapply(weights,function(w){
quantile(
rowSums( sweep( returns, 2, w, "*")),
1-conf)
})


which would result in

       1%        1%        1%        1%        1%
-2.572220 -2.359415 -1.582364 -1.840156 -1.686373


Of course, you will need to play around with the various weights, or maybe you store them in a matrix instead of a list and iterate; same result.

Did this help?