Quantum Opt’z: Portfolio Optimization

How to find the optimal portfolio for the given stocks?

Anonymousket
7 min readDec 29, 2022

--

In this article, we discuss about portfolio optimization to select the optimal solution using classical approach and IBM Qiskit library with Variational approach.

Setup:

Financial analysis is a critical tool to optimize investments in securities. It directly affects the quality of investment decisions made by an entity. One such area of financial analysis is portfolio optimization.

Problem:

Portfolio Optimization is the art of selecting the optimal portfolio for the selected stocks by maximize the expected returns and minimize the financial risk.

The expected return is the amount of profit or loss an investor can anticipate receiving on an investment.

The financial risk is basically the volatility of the returns and is measured through statistical instruments like variance, standard deviation, and covariance.

we see the mentioned jargon's below with corresponding formulae.

Classical Solution:

Get the optimal portfolio using the mean and standard deviation.

import required modules

consider number of assets be 6, start and end dates as shown below

gather the required data using yahoo finance api as shown below

Adjusted close(Adj Close) is the closing price after adjustments for all applicable splits and dividend distributions.

Let’s see the top five rows in the data frame, it shows that each row is comprised of monthly data.

Covariance and Correlation matrix:

the below image describes the each variable in the formulae

it is very clear that the data is a comparison between any two stocks, further discussion give more idea.

the above code block applies Covariance on the log of percentage change.

similarly, the below code block holds the correlation matrix on the log of percentage change.

Expected Returns:

let’s find the expected returns on the yearly data.

the above data shows a loss in the average, it seems covid impacted these companies a lot.

Let’s use the data to gain the best performing stocks to invest.

define the empty variables

number assets, let’s create 10,000 different portfolios assume these could be the future data and it picks the best future output.

run the simulation

capture the returns and the volatility

weight’s from the portfolio, weight’s gives the probability of happening or percentage of budget amount to be invested.

convert to the pandas data frame

Top 5 rows of the captured data

the below code block gives the minimum value in the volatility.

Variance can be calculated using the below formule

square root of the variance gives the standard deviation.

Sharpe ratio, using return of the portfolio, risk free rate, standard deviation(from the above block)

rf, let’s consider the risk free rate be 0.01

Let’s consider 15,000 coins then the investment portfolio shall be

AAPL: 15000 * 0.589962 = 8,849.43 coins

AMZN: 15000 * 0.002162 = 32.43 coins

GOOGL: 15000 * 0.034075 = 511.125 coins

META: 15000 * 0.024955 = 374.325 coins

MSFT: 15000 * 0.028178 = 422.67 coins

NKE: 15000 * 0.320668 = 4,810.02 coins

Let’s do the sum of all the above data which shall be equal to 15,000 coins

‭4810.02 + 422.67 + 374.325 + 511.125 + 32.43 + 8849.43 =‬ 15,000 coins

QAOA Solution:

We use ibm qiskit library and number of assets as 6.

import the required libraries

choose number of assets, start date, end date and list of stocks symbol as shown below

download the all 6 stocks related data and choose Adjusted close values using yahoo finance api as shown below

let’s formulate the problem and apply the constraints ashown below

the below block represents the variables definition and reference for the source.

the equivalent penalty for the constraint shall be as shown below

With the following assumptions, we proceed to create the QUBO,

  1. All assets have same price (normalized to 1).
  2. the full budget B has to spent, i.e., one has to select exactly B assets.

the below code block returns the division of two assets because returns and covariance shall be between two assets, for division by zero case nan shall be the value, for 0/0 case we shall return 1.

calculate the mean returns,

the below code block uses numpy’s mean function.

now calculate the mean covariance,

the below code block uses numpy’s cov fucntion.

now, select the risk factor here used the same value as mentioned in the qiskit finance section.

we select q = 0.5

B denotes the budget, i.e. the number of assets to be selected out of n.

here we select total amount shall be distributed among 3 stocks, so budget = 3

similarly, select to which budget penalty term shall to be scaled

penalty = num_assets

Now, let’s prepare the cplex Model and variables as follows,

the goal of the problem is to maximize the below term

let’s split the complete term as two terms namely, linear and quadratic as shown below

then we create the objective function as shown below

the below code block performs the maximize on the given objective function.

now, let’s apply the budget constraint

below code block applies the penalty to the objective function.

the above penalty applies such that the selected bit string or stocks shall follow the number of assets request for total amount distribution.

in this problem we selected 3 as output stocks.

convert the docplex Model to Quadratic Program Model

the below code block print the selected stocks

the below code blocks run the QAOA algorithm on the problem Model

suppose if we select 15,000 coins then 15,000 coins shall be invested equally to the selected shares with same ratio such as 5,000 coins for Nike, 5,000 coins for Meta and 5,000 coins for Microsoft.

Conclusion:

We can further extend to

  • increase the historical data(by changing start data).
  • increase number of stocks and add more symbols.
  • increase output stocks number or budget value.
  • allocate the budget amount in the floating value instead of equal value.

References:

Qiskit textbook, Wikipedia, open sources.

--

--