Portfolio Optimization with minimum risk using QAOA

We will look into optimization of portfolio for some stocks which is one of the optimization problems in finance. While creating a stocks portfolio it can get really confusing on which stocks to include and which ones to exclude or how to weigh the different stocks. We can select the portfolio with maximum return for a given risk or a portfolio with minimum risk for a given return or we can simply select the portfolio with maximum Sharpe ratio.


A safe bet is to invest in stocks which are least volatile or in statistics language we can say have the least variance. Thus in order to keep a minimum risk portfolio we try to keep it’s variance to the least. Here we will discuss on how to optimize a stocks profile with minimum risk or minimum variance using gate model of Quantum Computation. Here we won’t be looking into returns and will just concentrate on a profile with minimum risk.


For n number of stocks, we can have 2^n number of portfolios. Selecting the best combination of stocks becomes a NP-Hard problem. The conventional brute force technique would take exponential time to get the optimal solution. Some of the classical algorithms for this optimization are : Brute Force, Genetic Algorithms, Random Sampling, Monte Carlo Simulation (Simulated Annealing).


Covariance Matrix :

The covariance matrix is used to calculate the standard deviation of a portfolio of stocks which in turn is used by portfolio managers to quantify the risk associated with a particular portfolio. Here covariance is a measure of joint variability of two random variables i.e if both the variables move in the same direction, the covariance comes out to be positive otherwise it comes out to be negative. For us to calculate the variance of the portfolio, we need to find the covariance matrix of the stocks. Let say we have the information of the closing price of k stocks for n number of days. We can tabulate a n x k matrix giving us information of the stock activity for the number of days.

  • Let’s fetch the value of some stocks using pandas_datareader library and put it into a pandas dataframe.
import pandas as pd
import numpy as np
import pandas_datareader.data as webdata = web.DataReader(['BAC', 'GS', 'JPM', 'MS'],data_source="yahoo",start='12/01/2017',end='12/31/2017')['Adj Close']
data.round(2) # Round the data upto two decimal places


We fetch the data of 4 stocks ‘BAC’, ‘GS’, ‘JPM’, ‘MS’ :

  • Next, we find the covariance matrix out of the above table. For understanding on how to calculate the covariance matrix please refer to : Link. We use the cov() function here to get the desired matrix.
cov_matrix=data.cov()
cov_matrix.round(2)

Here we see that the diagonal elements are the variance for the respective stocks and the non diagonal elements are covariance between the (i,j) stocks.

The variance of the portfolio is given by : (W(Transpose) * Covariance_Mat * W).



Here W is the weight of the stocks where the sum of all the weights sum upto 1. We are looking for a problem where we either include a stock or not or a binary solution for the weights, thus for our problem the W vector is a binary vector where 1 denotes a stock being selected and 0 denotes it being excluded. With W vector being a binary vector we can convert the above portfolio variance to the form of :


which can easily be written in QUBO format where the diagonal terms of the above covariance matrix represent the linear terms while the non diagonal terms represent the quadratic terms.

The QUBO for the above covariance matrix comes out to be :

0.24x₁ + 18.33x₂ + 0.98x₃ + 0.24x₄ + 2*1.65x₁x₂ + 2*0.45x₁x₃ + 2*0.12x₁x₄ + 2*3.15x₂x₃ + 2*1.49x₂x₄ + 2*0.24x₃x₄.


We can convert it into Ising hamiltonian format using z = 2x-1, where z is the ising spin (-1,+1) and x is the above binary variable.

Thus the objective Hamiltonian of the portfolio optimization problem comes out of the Ising hamiltonian format where the spins are replaced by Pauli Z operators. For a detailed view on how to solve a QAOA problem of the form of Ising Hamiltonian , refer to this link: QAOA for problems in linear algebra.

Finally we get a solution in the form of above binary vector with those stocks included which reduce the overall variance of the portfolio. The expected return out of this portfolio comes out to be : W * M, where W is the binary solution vector and M is the vector with the mean price of the stocks for the n days. This optimization technique only considers the risk. In the next article we will look into the sharpe ratio which is used to help investors understand the return of an investment compared to its risk (keeping both the return and the risk in mind) a better measure for portfolio optimization. From here we will look into the CQR(Chicago Quantum Ratio) which is an improvement over the sharpe ratio.

References :

Gaurav Singh
Comments
Gaurav Singh
Related posts

blueqat Inc.

Shibuya Scramble Square 39F 2-24-12, Shibuya, Shibuya-ku, Tokyo
Contact: info@blueqat.com