# Portfolio Optimization With SciPy

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 [rules and guidelines_](https://towardsdatascience.com/questions-96667b06af5), we do not endorse each author’s contribution. You should not rely on an author’s works without seeking professional advice. See our [Reader Terms_](https://towardsdatascience.com/readers-terms-b5d780a700a4) for details._

_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.

## Running A Portfolio Optimization

The two key inputs to a portfolio optimization are:

1. Expected returns for each asset being considered.
2. The covariance matrix of asset returns. Embedded in this are information on cross-asset correlations and each asset’s volatility (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 — July 2020

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

## Pipelines in Machine Learning | Data Science | Machine Learning | Python

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

## Data Science Projects | Data Science | Machine Learning | Python

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.

## Data Science Projects | Data Science | Machine Learning | Python

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.

## Data Science Projects | Data Science | Machine Learning | Python

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.