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}$$

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

1 Answer 1


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

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

conf <- 0.99

        rowSums( sweep( returns, 2, w, "*")),

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?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.