I have recently implemented MPT in Python, however, when I allow negative weights (short selling), they do not add up to one. Isn't it suppose not to happen? On the other hand, when I don't allow, they add up to one as usual.

Here's what I did:

  1. Defined the return as usual: $\vec{W}^{T} \vec{R}$, where $\vec{W}$ have all the weights and $\vec{R}$ is the random vector of the returns;
  2. Calculated the expected returns and the covariance matrix all as percentages;
  3. Defined risk as the standard deviation, which satisfies: $\sigma^2(\vec{W}) = \vec{W}^{T} \Sigma \vec{W}$;
  4. Minimized $\sigma^2(\vec{W})$ under i) $\vec{W}^{T} \vec{I}=1$, where $\vec{I} = [1\,\ 1 \cdots 1]^{T}$ and ii) $\vec{W}^{T} \mathbb{E}[\vec{R}] = \mu_0$, where $\mu_0$ is given;
  5. Set the lagrangian as follows: $\mathcal{L}(\vec{W}, \lambda_1, \lambda_2) = \sigma^2(\vec{W}) + \lambda_1 (\vec{W}^{T} \vec{I} - 1) + \lambda_2 (\vec{W}^{T} \mathbb{E}[\vec{R}] - \mu_0)$
  6. Take all partial derivatives and end up in a system of equations like so:

\begin{equation} \begin{bmatrix} 2 \Sigma \,\,\, \vec{I} \,\,\, \mathbb{E}[\vec{R}] \\ \vec{I}^{T} \,\,\, 0 \,\,\, 0 \\ \mathbb{E}[\vec{R}]^{T} \,\,\, 0 \,\,\, 0 \end{bmatrix} \cdot \begin{bmatrix} \vec{W} \\ \lambda_1 \\ \lambda_2 \end{bmatrix} = \begin{bmatrix} \vec{0} \\ 1 \\ \mu_0 \end{bmatrix} \end{equation}

  1. Since this is a system of $A \vec{x} = \vec{b}(\mu_0)$ kind and $\vec{b}$ is parametrized by $\mu_0$, I used QR factorization to solve it for every $\mu_0$;

  2. Once I have the solution $\vec{x}$, I have the weights given by $\vec{W}$ as well. Therefore, it's been solved.

Is there anything missing? Does it happen commonly?



Thank you guys, I managed to solve the problem. The function I created to calculate all the weights was incorrect (I was using norm 1).

  • $\begingroup$ The second row of the equation in 6 says, in English "the sum of the elements in W is equal to 1". So how can it be that in the "solution" the elements of W do not add up to 1? It can only happen if the system is not solved properly. $\endgroup$
    – nbbo2
    Oct 20, 2021 at 14:26
  • 2
    $\begingroup$ Your problem is solved by $\vec{W} = \Sigma^{-1}\left(c_1 \vec{I} + c_2 E\left[\vec{R}\right]\right)$. Just use the two equality conditions to solve for $c_i$. This is easy to do analytically because you have no positivity constraint. $\endgroup$
    – shabbychef
    Oct 20, 2021 at 16:26
  • $\begingroup$ @noob2 Indeed. Then all steps are correct I suppose. Thanks. I thought my QR factorization was working just fine.... To calculate the inverse of the matrix R, I followed Lewis' answer: math.stackexchange.com/questions/1003801/… . Maybe the problem is there. $\endgroup$
    – Mr. N
    Oct 20, 2021 at 18:00
  • 3
    $\begingroup$ Try to work around inverting matrices although I doubt that this is your problem right now. It's easy to check whether your matrix inversion code is correct: multiply by the original and check that you get the identity back. $\endgroup$
    – Bob Jansen
    Oct 20, 2021 at 18:13
  • $\begingroup$ @BobJansen QR factorization looks correct. Thanks I'll check the inverses and work as the links says. $\endgroup$
    – Mr. N
    Oct 20, 2021 at 18:56

1 Answer 1


We will use bold notation for vectors and write $\pmb \mu = \mathbb E[\vec R]$.

From the first equation you have $$\pmb w = \frac 12 \pmb\Sigma^{-1}\left(\pmb 1^T\lambda_1 + \pmb\mu\lambda_2\right).$$ Hence, we will first find $\lambda_1$ and $\lambda_2$. Using the constraints, we may write \begin{aligned} 1 = \pmb1^T\pmb w &= \frac 12 \pmb1^T\pmb\Sigma^{-1}\left(\pmb 1^T\lambda_1 + \pmb\mu\lambda_2\right)\\ \mu_0 = \pmb \mu^T\pmb w &= \frac 12 \pmb\mu^T\pmb\Sigma^{-1}\left(\pmb 1^T\lambda_1 + \pmb\mu\lambda_2\right) \end{aligned} In other words, we need to solve the system

$$ \frac 12\begin{bmatrix} \pmb 1^T \pmb \Sigma^{-1}\pmb 1^T & \pmb 1^T \pmb \Sigma^{-1}\pmb \mu^T\\ \pmb 1^T \pmb \Sigma^{-1}\pmb \mu^T & \pmb \mu^T \pmb \Sigma^{-1}\pmb \mu^T \end{bmatrix}\begin{bmatrix} \lambda_1\\ \lambda_2 \end{bmatrix} = \begin{bmatrix} 1\\ \mu_0 \end{bmatrix}. $$

The solution is given by $$ \pmb\lambda = \frac{2}{(\pmb 1^T \pmb \Sigma^{-1}\pmb 1^T)(\pmb \mu^T \pmb \Sigma^{-1}\pmb \mu^T) - (\pmb 1^T \pmb \Sigma^{-1}\pmb \mu^T)^2}\begin{bmatrix} \pmb \mu^T \pmb \Sigma^{-1}\pmb \mu^T & -\pmb 1^T \pmb \Sigma^{-1}\pmb \mu^T\\ -\pmb 1^T \pmb \Sigma^{-1}\pmb \mu^T & \pmb 1^T \pmb \Sigma^{-1}\pmb 1^T \end{bmatrix}\begin{bmatrix} 1\\ \mu_0 \end{bmatrix}. $$


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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