Portfolio Optimization With SciPy. Use Python To Calculate The Optimal Portfolio With Constraints

** Note from Towards Data Science’s editors:**_ While we allow independent authors to publish articles in accordance with our

_Not intended to be investment advice. *[*You can find my code on my GitHub here._](https://github.com/yiuhyuk/portfolio_optimization_scipy)

As fun as it is to optimize a portfolio with just matrix algebra and NumPy, sometimes we need to add constraints. For example, many investors don’t want to or are not allowed to short investments. We can’t guarantee that the optimal portfolio produced using matrix algebra won’t include short positions (negative weights). So instead, we turn to **optimization**.

In case you didn’t read my previous articles on optimization, **optimization refers to the process of solving for the combination of assets that maximizes return for a given amount of risk (or minimizes risk for a specified level of return)**. The desired output from an optimization is a set of portfolio weights (for each asset) that would produce the optimal portfolio.

The two key inputs to a portfolio optimization are:

**Expected returns**for each asset being considered.- The
**covariance matrix of asset returns**. Embedded in this are information on**cross-asset correlations**and**each asset’s volatilit**y (the diagonals).

Expected returns are hard to estimate — some people like to use historical averages (dangerous as the past is often not representative of the future), others have their own methodology for estimating return forecasts. I am planning a whole post on this so I won’t go into the details this time. Today we will focus on the optimization process itself. Thus, we will pretend like we got expected return estimates from a consultant. By the way, ** these were very roughly estimated by me, so they should definitely not be taken as investment advice**.

Most popular Data Science and Machine Learning courses — August 2020. This list was last updated in August 2020 — and will be updated regularly so as to keep it relevant

Machine Learning Pipelines performs a complete workflow with an ordered sequence of the process involved in a Machine Learning task. The Pipelines can also

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.