Today, we will dive into the technicalities of actually optimizing a portfolio with NumPy. All You Need Is Some Matrix Algebra To Make Optimal Portfolios

All You Need Is Some Matrix Algebra To Make Optimal Portfolios

*If you would like to see my code,* *you can find it on my GitHub here**.*

Today, we will dive into the technicalities of actually optimizing a portfolio. The cool think about portfolio optimization is that it can be done purely with matrix algebra, no optimization software needed.

Before we start, let’s refresh ourselves on our objective. Quoting my previous post:

What we want to make sure of (as much as we can) is that

for a specified level of risk, we are investing in a portfolio that maximizes our chances of earning a positive return. The portfolio that does that, a.k.a. the optimal portfolio, is theone with the highest expected return(or in statistical terms, the one with the highest Z-score).

```
For a given level of risk, solve for the weights, W, that:Maximize W.T @ ESubject to:
W.T @ Cov @ W = (target risk)^2
and sum(W) = 1Where W is a vector representing the weights of the asset in our portfolio.
E is a vector representing the expected returns of the asset.
Cov is the covariance matrix of the asset's returns.
@ denotes matrix multiplication.
.T denotes the transpose operation.
```

- W @ E is the expected return of the portfolio.
- The portfolio’s variance is calculated as W.T @ Cov @ W). Variance is the square of the portfolio’s standard deviation (a.k.a. its risk). In our objective function, we want our portfolio’s variance to be equal to the target variance (the square of our target risk level).

Minimization problems are often easier to solve than maximization problems so let’s flip our problem around:

```
For a given level of risk, solve for the weights, W, that:Minimize W.T @ Cov @ WSubject to:
W.T @ E = target return = mu
and sum(W) = 1Where W is a vector representing the weights of the asset in our portfolio.
E is a vector representing the expected returns of the asset.
Cov is the covariance matrix of the asset's returns.
@ denotes matrix multiplication.
.T denotes the transpose operation.
```

Now instead of maxing return for a given level of variance (a.k.a. the square of risk), **we are minimizing variance for a given level of return**.

To solve this analytically, we can take advantage of the Lagrange Multiplier and rewrite our problem as follows:

```
L(W,h1,h2)
= [email protected]@W + h1*([email protected] - mu) + h1*([email protected] - 1)ones denotes a vector of ones with the same length as W
```

Notice that we have now a single equation that includes the thing we want to minimize (**[email protected]@W**) as well as our two constraints — that the expected return of the portfolio must equal the target return (**[email protected] — mu**) and that the portfolio weights must sum to 1 (**[email protected] — 1**).

We can solve this by solving:

`gradient{L(W,h1,h2)} = 0`

This is basically gradient descent, where we want to find the point where the partial derivatives (a.k.a. slope) with respect to each of the variables is zero. When we reach that point, we know we are the minimum.

Practice your skills in Data Science with Python, by learning and then trying all these hands-on, interactive projects, that I have posted for you.