Linear Programming in Python using PuLP – Part 3: Optimizing Investment Portfolios with Multi-Objective Optimization


In the previous article, we introduced the concepts of linear programming and multi-objective optimization and showed how to solve a problem using the Pulp library in Python. In this follow-up article, we will apply these concepts to a finance domain problem. We will demonstrate how to use linear programming to find the optimal investment portfolio and balance multiple objectives, such as maximizing returns and minimizing risk.

Investment portfolios are an important tool for managing risk and maximizing returns. However, finding the optimal portfolio can be a complex task, especially when multiple objectives are involved. In this article, we will show how linear programming can be used to find the optimal portfolio that balances multiple objectives and provides the best possible returns while minimizing risk.

We will start by defining the problem and the variables, constraints, and objectives that need to be considered. Then, we will demonstrate how to use the Pulp library in Python to solve the problem and find the optimal investment portfolio. We will conclude by discussing the results and the implications of the solution for investors. Whether you’re an experienced investor or just starting out, this article will provide valuable insights into the world of linear programming and multi-objective optimization.

Defining the Problem

In this article, we will consider a problem of finding the optimal investment portfolio that balances multiple objectives, such as maximizing returns and minimizing risk. The problem can be formulated as follows:

  • Objective 1: Maximize the expected return on investment
  • Objective 2: Minimize the risk of the portfolio
  • Constraint 1: The portfolio must be fully invested (i.e., the sum of the investment amounts for all assets must equal 1)
  • Constraint 2: The investment in each asset must be non-negative

Given the above objectives and constraints, we will use linear programming and the Pulp library in Python to find the optimal investment portfolio that balances the trade-off between maximizing returns and minimizing risk.

Problem Summary

SectionDescription
ProblemFinding the optimal investment portfolio that balances multiple objectives, such as maximizing returns and minimizing risk
Objectives1. Maximize the expected return on investment, 2. Minimize the risk of the portfolio
Constraints1. The portfolio must be fully invested, 2. The investment in each asset must be non-negative
ApproachUse linear programming and the Pulp library in Python to find the optimal investment portfolio
Optimal Investment Portfolio Optimization Problem

Problem Formulation

In this problem, we are trying to find the optimal investment portfolio that balances the trade-off between maximizing returns and minimizing risk. The problem can be formulated mathematically as follows:

Let x_i be the fraction of the investment portfolio invested in asset i, where i = 1, 2, ..., n and n is the number of assets in the portfolio.

The expected return on investment for each asset i is given by r_i, and the risk of the portfolio is given by the standard deviation s.

The problem can be formulated as the following linear programming problem:

Where σ_i is the standard deviation of asset i.

Tabular Summary of the Problem

SymbolDescription
x_iFraction of the investment portfolio invested in asset i
r_iExpected return on investment for asset i
sStandard deviation of the portfolio
σ_iStandard deviation of asset i
nNumber of assets in the portfolio
Summary of Problem

How To: Solve the Problem

Here are the steps that we need to follow to solve the problem.

StepDescription
1Import the Pulp library
2Create two new LpProblem objects, one for maximizing the return and one for minimizing the risk
3Define variables using the LpVariable class, one for each asset in the portfolio
4Define the expected return and standard deviation for each asset
5Add constraints to both problems, ensuring that the portfolio is fully invested and the investment in each asset is non-negative
6Define the objectives for both problems, maximizing the expected return and minimizing the risk
7Solve both problems using the solve method of the LpProblem class
8Print the results, including the fraction of the portfolio invested in each asset and the expected return and standard deviation of the portfolio
Solving the Investment Portfolio Optimization Problem

Code: Solving the Problem

In this section, we will use the Pulp library in Python to solve the optimization problem we defined in the previous section. We will follow the steps outlined in the previous section to define variables, add constraints, define objectives, and solve the problem.

Here’s the code to solve the problem:

from pulp import *

# Define the number of assets in the portfolio
n = 4

# Define the expected return and standard deviation for each asset
returns = [0.1, 0.2, 0.15, 0.3]
deviations = [0.15, 0.2, 0.1, 0.3]

# Create two new LpProblem objects, one for maximizing the return and one for minimizing the risk
return_problem = LpProblem("Maximize Return", LpMaximize)
risk_problem = LpProblem("Minimize Risk", LpMinimize)

# Define variables using the LpVariable class, one for each asset in the portfolio
x = [LpVariable(f"x{i}", 0, 1) for i in range(n)]

# Add constraints to both problems, ensuring that the portfolio is fully invested and the investment in each asset is non-negative
return_problem += sum(x) == 1
risk_problem += sum(x) == 1

for i in range(n):
    return_problem += x[i] >= 0
    risk_problem += x[i] >= 0

# Define the objectives for both problems, maximizing the expected return and minimizing the risk
return_problem += sum([x[i] * returns[i] for i in range(n)])
risk_problem += sum([x[i] * deviations[i] for i in range(n)])

# Solve both problems using the solve method of the LpProblem class
return_problem.solve()
risk_problem.solve()

# Print the results, including the fraction of the portfolio invested in each asset and the expected return and standard deviation of the portfolio
print("--- Results ---")
print("Maximize Return:")
for i in range(n):
    print(f"Asset {i}: {x[i].value()}")
print(f"Expected Return: {value(return_problem.objective)}")

print("Minimize Risk:")
for i in range(n):
    print(f"Asset {i}: {x[i].value()}")
print(f"Standard Deviation: {value(risk_problem.objective)}")

In the above code, we first defined the number of assets in the portfolio and the expected return and standard deviation for each asset. Then, we created two LpProblem objects, one for maximizing the return and one for minimizing the risk. We defined variables using the LpVariable class and added constraints to both problems to ensure that the portfolio is fully invested and the investment in each asset is non-negative. Finally, we defined the objectives for both problems, maximizing the expected return and minimizing the risk, and solved both problems using the solve method of the LpProblem class. The results, including the fraction of the portfolio invested in each asset and the expected return and standard deviation of the portfolio, were printed at the end.

Understanding the Code

The code to solve the optimization problem uses the Pulp library in Python, which provides an easy-to-use interface for defining and solving linear programming problems.

In the code, we first imported the Pulp library using the following line:

from pulp import *

Next, we defined the number of assets in the portfolio and the expected return and standard deviation for each asset:

# Define the number of assets in the portfolio
n = 4

# Define the expected return and standard deviation for each asset
returns = [0.1, 0.2, 0.15, 0.3]
deviations = [0.15, 0.2, 0.1, 0.3]

Then, we created two LpProblem objects, one for maximizing the return and one for minimizing the risk:

# Create two new LpProblem objects, one for maximizing the return and one for minimizing the risk
return_problem = LpProblem("Maximize Return", LpMaximize)
risk_problem = LpProblem("Minimize Risk", LpMinimize)

The LpProblem class is the main class in the Pulp library and is used to define a linear programming problem. The first argument to the LpProblem class is a name for the problem, and the second argument is the optimization direction, either LpMaximize for maximizing the objective function or LpMinimize for minimizing the objective function.

Next, we defined variables using the LpVariable class, one for each asset in the portfolio:

# Define variables using the LpVariable class, one for each asset in the portfolio
x = [LpVariable(f"x{i}", 0, 1) for i in range(n)]

The LpVariable class is used to define variables in a linear programming problem. The first argument to the LpVariable class is a name for the variable, and the second and third arguments are the lower and upper bounds for the variable, respectively. In this case, we defined x_i as a variable between 0 and 1, representing the fraction of the portfolio invested in asset i.

Next, we added constraints to both problems, ensuring that the portfolio is fully invested and the investment in each asset is non-negative:

# Add constraints to both problems, ensuring that the portfolio is fully invested and the investment in each asset is non-negative
return_problem += sum(x) == 1
risk_problem += sum(x) == 1

for i in range(n):
    return_problem += x[i] >= 0
    risk_problem += x[i] >= 0

The += operator is used to add constraints to the LpProblem objects. The first constraint ensures that the sum of the investments in all assets is equal to 1, representing a fully invested portfolio. The second set of constraints ensures that the investment in each asset is non-negative.

Next, we defined the objectives for both problems, maximizing the expected return and minimizing the risk:

# Define the objectives for both problems, maximizing the expected return and minimizing the risk
return_problem += sum([x[i] * returns[i] for i in range (n)])
risk_problem += sum([x[i] * deviations[i] for i in range(n)])

The objectives for both problems were defined using the += operator and the sum function. The first objective maximizes the expected return, which is the sum of the products of the fraction of the portfolio invested in each asset and the expected return of that asset. The second objective minimizes the risk, which is the sum of the products of the fraction of the portfolio invested in each asset and the standard deviation of that asset.

Finally, we solved both problems using the solve method of the LpProblem class:

# Solve both problems using the solve method of the LpProblem class
return_problem.solve()
risk_problem.solve()

The solve method solves the linear programming problem and returns the optimal solution.

At the end, we printed the results, including the fraction of the portfolio invested in each asset and the expected return and standard deviation of the portfolio:

# Print the results, including the fraction of the portfolio invested in each asset and the expected return and standard deviation of the portfolio
print("--- Results ---")
print("Maximize Return:")
for i in range(n):
    print(f"Asset {i}: {x[i].value()}")
print(f"Expected Return: {value(return_problem.objective)}")

print("Minimize Risk:")
for i in range(n):
    print(f"Asset {i}: {x[i].value()}")
print(f"Standard Deviation: {value(risk_problem.objective)}")

In this section, we used the Pulp library in Python to solve a linear programming problem to find the optimal investment portfolio that balances the trade-off between maximizing returns and minimizing risk. The code defines variables, adds constraints, defines objectives, and solves the problem to find the optimal solution.

This series of articles has covered different optimization problems and the use of the Pulp library in Python to solve them. To conclude this series, we will solve an optimization problem related to logistics with more than 10 variables and 4 objectives and several constraints. This will provide an opportunity to demonstrate the versatility of the Pulp library and its ability to solve complex optimization problems. Stay tuned!

, ,


One response to “Linear Programming in Python using PuLP – Part 3: Optimizing Investment Portfolios with Multi-Objective Optimization”

  1. […] the previous article we saw an advanced problem for linear programming related to finance […]

Leave a Reply

Other posts

  • Object Extraction using Image Segmentation: A Comprehensive Tutorial with Detectron2 and Mask2Former

    Object Extraction using Image Segmentation: A Comprehensive Tutorial with Detectron2 and Mask2Former

    Discover how to perform object extraction using image segmentation with Detectron2 and Mask2Former in our step-by-step tutorial. Learn to set up the environment, configure the model, and visualize segmentation results, extracting objects from images with ease. Boost your computer vision skills and optimize your image processing projects with this comprehensive guide.

  • Building Your First Neural Network with TensorFlow – Deep Learning 2

    Building Your First Neural Network with TensorFlow – Deep Learning 2

    Neural networks are a fundamental concept in deep learning and are used for a wide range of applications such as image and speech recognition, natural language processing, and much more. In this article, we will walk you through the process of building your first neural network using TensorFlow, a popular open-source machine learning library. We'll…

  • Introduction to Deep Learning with TensorFlow – Deep Learning 1

    Introduction to Deep Learning with TensorFlow – Deep Learning 1

    In this article, we provide an introduction to deep learning with TensorFlow. We cover what deep learning is, what it can do, why TensorFlow is a great choice for deep learning, and an overview of TensorFlow itself. We also explore the different types of neural networks used in deep learning, and demonstrate how to build…

  • How To: Set Up PyTorch with GPU Support on Windows 11 – A Comprehensive Guide

    How To: Set Up PyTorch with GPU Support on Windows 11 – A Comprehensive Guide

    Introduction Hello tech enthusiasts! Pradeep here, your trusted source for all things related to machine learning, deep learning, and Python. As you know, I’ve previously covered setting up TensorFlow on Windows. Today, I’m excited to bring you a detailed guide on setting up another popular deep learning framework, PyTorch, with GPU support on Windows 11.…

  • Solving a Complex Logistics Optimization Problem using the Pulp Library in Python – Part 4

    Solving a Complex Logistics Optimization Problem using the Pulp Library in Python – Part 4

    In this article, we demonstrate how to solve a logistics optimization problem using the Pulp library in Python. By defining the variables, objective function, and constraints, and using the solve method to find the optimal solution, we are able to minimize the total cost of transportation while satisfying the constraints. This article concludes the multi-part…

  • Linear Programming in Python using PuLP – Part 3: Optimizing Investment Portfolios with Multi-Objective Optimization

    Linear Programming in Python using PuLP – Part 3: Optimizing Investment Portfolios with Multi-Objective Optimization

    In this article, we used the Pulp library in Python to solve a linear programming problem to find the optimal investment portfolio. We defined variables, added constraints, defined objectives, and solved the problem to find the optimal solution that balances the trade-off between maximizing returns and minimizing risk. The code was concise and easy to…

  • Linear Programming in Python using Pulp – Part 2

    Linear Programming in Python using Pulp – Part 2

    In this article, we delve deeper into linear programming and explore how to solve a multi-objective optimization problem using the Pulp library in Python. We present a problem in which a nutritionist must find the optimal meal plan for a patient suffering from anemia, balancing the intake of Vitamin B12 and fat. We demonstrate how…

  • Linear Programming in Python using PuLP – Part 1

    Linear Programming in Python using PuLP – Part 1

    Linear programming is an optimization technique used to find the best outcomes for a given problem. This technique relies on a set of constructs which are all expressed using a system of linear equations. It is important to understand that you should be able to express your objective as a linear equation dependent on an…

  • How To: Setup Tensorflow With GPU Support using Docker

    How To: Setup Tensorflow With GPU Support using Docker

    Previously I published a guide for setting up tensorflow in an anconda environment with GPU support. A lot of people liked it and I have been working with this environment myself for more than a year now. I am happy with the results however the process is a bit involved and requires quite a bit…

  • How To: Setup Tensorflow With GPU Support in Windows 11

    How To: Setup Tensorflow With GPU Support in Windows 11

    It's been just 2 days since Windows 11 came out and I am already setting up my system for the ultimate machine learning environment. Today we are going to setup a new anaconda environment with tensorflow 2.5 with GPU support using NVIDIA CUDA 11.4 and CUDNN 8.2.4 along with Python 3.8. This is going to…

  • Tools of The Trade – II

    Tools of The Trade – II

    In continuation of my previous post today I will talk about the website tanooja.com. I did this project on request of my wife because she wanted to pursue blogging and didn't want to go through the ordeal needed to write, publish and manage SEO using most of the prominent blogging platforms like WordPress, Joomla, Drupal…

  • Tools of The Trade – I

    Tools of The Trade – I

    In this post I will share a few tools and technologies that I am using to run a couple of blazing fast websites using latest modern tools and technologies. The caveat here is that I don't pay any infrastructure/hosting costs for any of these websites and they can scale infinitely in terms of supported users…

  • Building Lizzie – IV

    Building Lizzie – IV

    Another post about Lizzie. I started off with a Raspberry Pi 3 to build a personal assistant for my car and I have come a long way both in terms of the concept and the functionality. Most importantly I have formalized the application flow and also extended the scope from one device to almost all…

  • OBD-II with Raspberry Pi3

    OBD-II with Raspberry Pi3

    I am writing this article in response to a question posted on my YouTube channel. Here I would be talking about communicating to an OBD-II device (ELM327 chip with Bluetooth) hooked into your car’s OBD-II port. The OS I am using is Windows 10 IoT core. This information is important because it makes a difference…

  • Building Lizzie – III

    Building Lizzie – III

    As mentioned in previous article today I would be talking about OBD-II integration in Lizzie using a Bluetooth serial communication with an ELM327 adapter that fits on a OBD-II port in your car. OBD stands for On Board Diagnostics which is connected to the ECU (Engine Control Unit) and provides a ton of information (both…

  • Building Lizzie – II

    Building Lizzie – II

    In the previous post I described my experiments around building an intelligent artificial personal assistant – Lizzie. The pseudo intelligent agents available today around us (Siri, Cortana or Google Next) are all great feats of engineering given the fact that they reside on small devices like mobile phones and are able to do powerful things…

  • Building Lizzie – I

    Building Lizzie – I

    Recently I have been busy building a personal assistant that I would be fitting in my car. Currently I am in experimentation mode and I am experimenting with speech capabilities. I would start with a description of my journey so far. First let me show off a little bit with these videos that I created…

  • How To: Super Fast Tensorflow 2 Setup with GPU Support in VS Code

    I am publishing this article in response to a comment I received on How To: Setup Tensorflow With GPU Support using Docker article. Docker is all good and nice but all of us agree that we use IDEs (integrated dev environment – think VSCode) on our machines. So let’s say you want to get the…

%d bloggers like this: