This model is an example of the classic Markowitz portfolio selection optimization model. We want to find the fraction of the portfolio to invest among a set of stocks that balances risk and return. It is a Quadratic Programming (QP) model with vector and matrix data for returns and risk, respectively. This is best suited to a matrix formulation, so we use the Gurobi Python matrix interface. The basic model is fairly simple, so we also solve it parametrically to find the efficient frontier.

Access the Jupyter Notebook Modeling Example

Click on the button below to access the example in Google Colab, which is a free, online Jupyter Notebook environment that allows you to write and execute Python code through your browser. 

How to Run the Jupyter Notebook Modeling Example

  • To run the example the first time, choose “Runtime” and then click “Run all”.
  • All the cells in the Jupyter Notebook will be executed.
  • The example will install the gurobipy package, which includes a limited Gurobi license that allows you to solve small models.
  • You can also modify and re-run individual cells.
  • For subsequent runs, choose “Runtime” and click “on “Restart and run all”.
  • The Gurobi Optimizer will find the optimal solution of the modeling example.

Check out the Colab Getting Started Guide for full details on how to use Colab Notebooks as well as create your own.

What's
New at Gurobi