In a finance textbook of mine, there is an equation for calculating the variance $\sigma^2$ of a portfolio of two risky assets (i.e. random variables) $X$ and $Y$ by considering the correlation $\rho$ of the assets. I do not know if this equation is generalized, and no derivation is provided.

The textbook equation: $\sigma^2 = x^2_1\sigma^2_1 + x^2_2 \sigma^2_2 + 2x_1x_2\rho \sigma_1 \sigma_2$

$x_1$ and $x_2$ in the equation refer to the assets' weight in the portfolio.

So, how can I calculate the variance for $N$ number of weighted random variables also consider their correlation?

Thank you for your help!

  • $\begingroup$ Variance of what? Of their (weighted) sum? $\endgroup$
    – Firebug
    Sep 17 at 10:09
  • $\begingroup$ Apologise @Firebug, I forgot to mention that In the textbook equation above, $x_1$ and $x_2$ refer to the weight of the assets in the portfolio. I updated the question with this information. $\endgroup$ Sep 17 at 10:19

2 Answers 2


From the fact that your question stems from a finance textbook, I assume that you are dealing with Markowitz's portfolio theory. In most books, the author just focuses on the 2-asset case, but this can easily be generalized to $n$ risky assets by using matrix algebra.

Formally, let $\boldsymbol{\omega}=(\omega_1,\dots,\omega_n)^\top$ be a vector of portfolio weights and let $\boldsymbol{R}=(R_1,\dots,R_n)^\top$ be the vector of returns. I.e., $R_i$ is the return of asset $i$, which is a random variable, an $\omega_i$ is the corresponding weight, which is a scalar. The covariance matrix of $\boldsymbol{R}$ is given by: \begin{align} \boldsymbol{\Sigma}&= \begin{pmatrix} Var(R_1) & Cov(R_1,R_2) & \dots &Cov(R_1,R_n) \\ Cov(R_2,R_1) & Var(R_2) & \dots &Cov(R_2,R_n) \\ \vdots & \vdots & \ddots & \vdots \\ Cov(R_n,R_1) & Cov(R_n,R_2) & \dots & Var(R_n) \end{pmatrix} =\begin{pmatrix} \sigma_1^2 & \sigma_{12} & \dots & \sigma_{1n} \\ \sigma_{21} & \sigma_2^2 & \dots &\sigma_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ \sigma_{n1} & \sigma_{n2} & \dots & \sigma_n^2 \end{pmatrix} \end{align} Then, $$ R_{\boldsymbol{\omega}}=\boldsymbol{\omega}^\top\boldsymbol{R}=\sum_{i=1}^n\omega_iR_i $$ is the return of your portfolio. Basically, you want to calculate the variance of $R_{\boldsymbol{\omega}}$ and the easiest way to do this is as follows: The expected return of the portfolio is given by: \begin{align} E(R_{\boldsymbol{\omega}})=E(\boldsymbol{\omega}^\top \boldsymbol{R})=\boldsymbol{\omega}^\top E(\boldsymbol{R})=\boldsymbol{\omega}^\top \boldsymbol{\mu} \end{align} The variance $\sigma_{\boldsymbol{\omega}}^2$ of the portfolio return is given by: \begin{align*} \sigma_{\boldsymbol{\omega}}^2&=E([R_{\boldsymbol{\omega}}-E(R_{\boldsymbol{\omega}})][R_{\boldsymbol{\omega}}-E(R_{\boldsymbol{\omega}})]^\top) \\ &=E([\boldsymbol{\omega}^\top\boldsymbol{R}-\boldsymbol{\omega}^\top \boldsymbol{\mu}][\boldsymbol{\omega}^\top\boldsymbol{R}-\boldsymbol{\omega}^\top \boldsymbol{\mu}]^\top) \\ &=E(\boldsymbol{\omega}^\top[\boldsymbol{R}-\boldsymbol{\mu}][\boldsymbol{\omega}^\top[\boldsymbol{R}-\boldsymbol{\mu}]]^\top) \\ &=E(\boldsymbol{\omega}^\top[\boldsymbol{R}-\boldsymbol{\mu}][\boldsymbol{R}-\boldsymbol{\mu}]^\top\boldsymbol{\omega}) \\ &=\boldsymbol{\omega}^\top E([\boldsymbol{R}-\boldsymbol{\mu}][\boldsymbol{R}-\boldsymbol{\mu}]^\top)\boldsymbol{\omega} \\ &= \boldsymbol{\omega}^\top \boldsymbol{\Sigma}\boldsymbol{\omega}\\ &=\sum_{i=1}^n\sum_{i=1}^n\omega_i\omega_jCov(R_i,R_j) \end{align*} Thus, to implement this in code, just specify a vector of weights, take your covariance matrix and calculate $\boldsymbol{\omega}^\top \boldsymbol{\Sigma}\boldsymbol{\omega}$.

I hope that this answers your question, if not, do not hesitate to ask further questions.

  • $\begingroup$ Thank you, Lars, this was a great finance/math answer that helped me to follow along. I still need to figure out some things about Matrix Algebra, such as what the $T$ transpose does. I will try to implement this in my code and hope you do not mind if I ask some clarifying questions if needed. And, indeed, this is related to Markowitz MPT! $\endgroup$ Sep 17 at 17:01

It's nothing but the application of variance of linear combination of random variables:

$$\operatorname{Var}\left(\sum_{i=1}^n a_iX_i\right) =\sum_{i=1}^n a_i^2\operatorname{Var}(X_i)+ 2\mathop{\sum_{i=1}^n\sum_{j=1}^n} \limits_{i<j} a_ia_j\operatorname{Cov}(X_i,X_j).$$ The proof can be seen here.
The present case is for $n =2.$ Also $\operatorname{Cov}(X_1, X_2) = \rho\sigma_1\sigma_2.$

  • $\begingroup$ Thank you @User1865345. This is most likely the answer I was looking for. Now I want to translate this into code. In my code I have a matrix of vectors representing the assets returns, let $X$ be the matrix of asset returns: $\[ X_{3\times5} = \left[ {\begin{array}{ccccc} x_{11} & x_{12} & x_{13}\\ x_{21} & x_{22} & x_{23}\\ x_{31} & x_{32} & x_{33}\\ \end{array} } \right] \]$ $\endgroup$ Sep 17 at 13:32
  • $\begingroup$ How do I perform the right hand side of the calculation, i.e. where you sum the covariances of $X_i$ and $X_j$? Does it mean I should I take each combination of those vectors and calculate the covariance? For example, if I have three assets in my portfolio as in the above matrix, would the calculation then be $a_1a_2Cov(X_1, X_2) + a_2a_3Cov(X_2, X_3) + a_1a_3Cov(X_1, X_3)$? Thank you for your help! $\endgroup$ Sep 17 at 13:38
  • $\begingroup$ As of the combinations, they should be $(i, j) : i< j, ~i, j\in\{1, 2,\ldots, n\}.$ So, the combination seems to be okay. $\endgroup$ Sep 17 at 13:52

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