Download

Continuous Portfolio Optimization on Dirac-3

Device: Dirac-3

Introduction

This approach seeks to identify a sub-portfolio of stocks that have superior risk-return profiles compared to the full portfolio. This identifies opportunities for an investor to simplify their investment strategy without sacrificing (and potentially enhancing) the risk-adjusted return. While the expected return on a portfolio is relatively straightforward to optimize by itself, optimizing against risk is more subtle when choosing a set of assets that individually have high returns. The reason that optimizing against risk is more challenging is that asset performances can be correlated. Intuitively, one can see that investing in two highly-correlated assets is more risky than if they are uncorrelated or even anti-correlated. If one of a highly correlated asset is performing poorly, the other in the pair is likely to do so as well. Thus, the variance of a portfolio with both assets can be significantly higher than it would be if they were uncorrelated. Minimizing the overall variance for the return of a portfolio therefore needs to take into account the covariance between the assets, making this a fundamentally quadratic problem, ideal for our Dirac-3 solver

Importance

Investments need not only give a good return on average, but also need to balance the potential risks. This balance will depend on the goal of the investor. For example, someone investing their retirement fund is likely to favor modest returns with low risks because the consequences of major losses are severe. On the other hand, someone who is day-trading in the hopes of having more money for entertainment might be willing to take more risks. There are of course many other scenarios with other risk levels. In general, the portfolio optimization problem is viewed as being a multi-objective problem. The goal is to balance the objective of maximizing return with the objective of minimizing risk. Even with multiple objectives there is still a sense of optimality, a portfolio is said to be "efficient" or "Pareto optimal" if the only ways to decrease risk would be to also decrease return. Regardless of one's appetite for risk, it never makes sense to invest in a non-Pareto-Optimal portfolio. Therefore, our goal is to find those that are Pareto optimal and match the appetite for risk which is parameterized by a term ξ\xi in our description. It is worth noting that this tutorial is based on a relatively simplified but still commonly used model of portfolio optimization. Various efforts exist to take into account more complex structure in the distribution of expected returns, in particular the failure to capture extreme events.

Applications

Portfolios diversification is necessary to achieve satisfactory outcomes for investors, making the kind of portfolio optimization discussed here (and potentially more complex variants) highly important. In spite of its simplicity, the model of diversification presented here, often referred to as modern portfolio theory, is still used. Improvements of the models presented here comprise a subject known as post-modern portfolio theory. One improvement is to consider a quantity known as downside risk instead of variance. Downside risk only takes into account the risk of portfolio elements underperforming a goal, rather than their total variation. Since the goal of diversification is to protect from risk, this approach can yield better performance.

Continuous optimization helps in selecting and determining how much to invest in each stock. Unlike binary optimization, where you decide whether to include a stock or not (1 or 0), continuous optimization allows for more precise investment decisions by assigning continuous values to represent the proportion of your investment in each stock. This method helps create a well-diversified portfolio with varying levels of investment in different stocks to better manage risk and achieve desired returns.

Continuous portfolio conceptual explanation

Constructing our Objective Function Conceptually

In our tutorial, we have historical datasets containing stock prices over a period of time. The goal is to optimally diversify a portfolio as discussed previously. This involves balancing the risk (quantified by overall variance in the portfolio return) with the return. Recall that to calculate the total variance, it is important to consider both the variance of each stock price as well as the covariance between stock prices. The risk appetite of the investor is parameterized by a weight ξ\xi.

We define the following variables as:

  1. ii: Index of stock

  2. KK: Total number of stocks being considered for investment.

  3. RR: Daily returns or the Returns per Day of the portfolio.

  4. E(R)E(R): Expected Return of the portfolio.

  5. Var(R)Var(R): Variance of portfolio's total return.

  6. ξ\xi: A parameter that balances the importance of maximizing returns versus minimizing risk, a larger value of ξ\xi corresponds to a more conservative strategy.

  7. wiw_i: Weight of stock i in the portfolio; i{1,2,...,K}i \in \{1, 2, ..., K \}, in other words what portion of the portfolio consists of this stock

Now, we can define our Objective Function, i.e, the function that encapsulates the goal of constructing a portfolio that balances maximizing expected returns and minimizing risk. as follows:

minw(E(R))2+ξVar(R) \min_w -\left( E(R)\right)^2 \cdot + \xi\cdot \text{Var}(R)

We want our weights to sum to 100%100\%, therefore:

i=1kwi=100 \sum_{i=1}^k w_i = 100

We also want the portfolio to be diverse, so we apply an upper limit on all the weights:

wiWmax w_i \leq W_{\text{max}}

In our example we set our upper limit on all the weights WmaxW_{\text{max}} equal to 8080 so that the portfolio is never too heavily weighted toward one stock.

Hamiltonian Construction

The portfolio daily return over a time period mm can be written as a linear combination of daily returns of its constituent stocks over the same time period. We have,

R(t)=i=1Kwiri(t) R(t) = \sum_{i = 1}^K w_i r_i (t)

where, ri(t)r_i (t) is the daily return of stock ii at time tt .

The expectation of portfolio daily return over time period mm can thus be expanded as: E(R)=i=1KwiE(ri) E(R) = \sum_{i = 1}^K w_i E(r_i)

and the variance of portfolio daily returns is expanded as,

Var(R)=i=1Kj=1KwiwjCovar(ri,rj) \text{Var}(R) = \sum_{i = 1}^K \sum_{j = 1}^K w_i w_j \text{Covar}(r_i,r_j)

where Covar is the covariance function defined as

Covar(ri,rj)=1NkN(r(tk)iri)(r(tk)jrj)\text{Covar}(r_i,r_j)=\frac{1}{N}\sum^N_k\left(r(t_k)_i-\overline{r}_i\right)\left(r(t_k)_j-\overline{r}_j \right)

where ri=1NiNr(tk)i\overline{r}_i=\frac{1}{N}\sum_i^Nr(t_k)_i is the mean value of rr over NN time periods.

To impose the inequality constraints in wiWmax w_i \leq W_{\text{max}}, we can introduce KK auxiliary, (or "slack") variables wK+1,wK+2,...,w2K w_{K+1}, w_{K+2}, ..., w_{2K} , and impose KK equality constraints as,

wi+wi+K=Wmax w_i + w_{i+K} = W_{\text{max}}

We also wish to constrain the total value of the portfolio

iwi=Wtotal\sum_i w_i= W_{\text{total}} .

Our overall optimization problem is therefore

minimize iK(E(ri))2+ξi,jKCovar(ri,rj)-\sum^K_i(E(r_i))^2+ \xi \sum^K_{i,j} \cdot \text{Covar}(r_i, r_j)

such that wi+wi+K=Wmax w_i + w_{i+K} = W_{\text{max}}

and iwi=Wtotal\sum_i w_i= W_{\text{total}}

and wi0w_i\ge 0

An astute reader might be curious as to why the objective function includes the square of the return rather than simply the return. Note that our Dirac-3 device is completely capable of handling either representation. The reason comes down to the fact that ξ\xi should be a unitless quantity. From the definition of covariance, we see that (assuming stock values are in dollars) Covar(ri,rj)\text{Covar}(r_i, r_j)has units of dollars squared $2 \$^2 on the other hand returns will have units of dollars $ \$ . In an objective function defined as covariance plus return, ξ\xi would have to have units of inverse currency $1 \$^{-1} . This has the disadvantage that what ξ\ximeans would depend on how the values are defined. For example, if returns were instead given in cents, ξ\xi would have to be 100100 times bigger to get the same result as the expression calculated in units of dollars.

Such a dependences on how the problems are defined are not only annoying in terms of bookkeeping (especially if the same formula were sometimes applied to markets with non-US currencies), they are also a general sign that a formulation is not sound. How risk and returns are balanced shouldn't depend on what currency is being used, so a formulation where it does indicates a problem. Put another way, it makes conceptual sense to say "minimizing risk is three times more important than maximizing return", while it is not clear what it would mean to say "minimizing risk is three times per dollar more important than maximizing returns". One subtlety of this formulation is that it assumes that stocks with an overall negative return have been excluded as squaring the return puts positive and negative returns on equal footings. Without excluding these stocks the objective function could favour a portfolio which will lose a large amount of value with near certainty!

To avoid an over-fit on the portfolio data, we can minimize the average of the cost function over MM overlapping time periods, that is m{1,2,...,M}m \in \{ 1, 2, ..., M \}. The problem becomes,

f(w)=1Mm=1Mf(w) f(w) = \frac{1}{M} \sum_{m=1}^M f(w)

Adding the constraints to the objective function, the optimization problem reduces to,

minwi=12Kj=12KJijwiwj+i=12khiwi \min_w \sum_{i=1}^{2K} \sum_{j=1}^{2K} J_{ij} w_iw_j + \sum_{i=1}^{2k}h_iw_i

where the two body coupling coefficients JijJ_{ij} are defined as

Jij=Jij(obj)+βJij(constr) J_{ij}= J_{ij}^\mathrm{(obj)}+ \beta J_{ij}^\mathrm{(constr)}

where and objective function component of the coupling is defined as

Jij(obj)={1Mm=1MAij(m)+α+bi2if ijK1Mm=1MAij(m)+αif i,jKij0otherwise J_{ij}^\mathrm{(obj)} = \begin{cases} \frac{1}{M} \sum_{m=1}^M A_{ij}^{(m)} + \alpha+b^2_i & \quad \text{if } i \neq j \leq K \\ \frac{1}{M} \sum_{m=1}^M A_{ij}^{(m)} + \alpha & \quad \text{if } i,j \leq K \quad i \neq j \\ 0 & \quad \text{otherwise} \end{cases}

and a constraint component defined as

Jij(constr)={1if i=j1if ij=K0otherwise J_{ij}^\mathrm{(constr)} = \begin{cases} 1 & \quad \text{if } i=j \\ 1 & \quad \text{if } \left| i-j \right|=K \\ 0 & \quad \text{otherwise} \end{cases} .

The linear coefficients hih_i are defined as,

hi=hi(obj)+βhi(constr) h_{i}= h_{i}^\mathrm{(obj)}+ \beta h_{i}^\mathrm{(constr)}

where

hi(obj)={200αif iK0otherwise h_{i}^{(obj)} = \begin{cases} 200\alpha & \quad \text{if } i \leq K \\ 0 & \text{otherwise} \end{cases}

and

hi(constr)=2Wmaxi h_{i}^{(constr)} = - 2 W_{\text{max}} \quad \forall i

The Hamiltonian equation becomes:

H=[h12(J+JT)] H = [ h \mid \frac{1}{2} (J + J^T)]

where:

-(0.5(J+JT))( 0.5 \cdot (J + J^T) ) represents the symmetrized form of JJ - The notation h12(J+JT)h \mid \frac{1}{2} (J + J^T) denotes the horizontal concatenation of hh and 12(J+JT)\frac{1}{2} (J + J^T), in other words the addition of hh as a column of the matrix.

A Worked Example

You can download the full code base here. To actually run this code you will need to sign up for Free Trial Cloud Access. How to run the code is explained in the readme with examples shown in a Jupyter notebook.

Imagine you have 55 stocks in your portfolio and you want to find the optimized portfolio weights, our code repository contains a worked example of how to find them.

Optimal Portfolio with Allocated weights:

When we run our code, we will see that the optimal portfolio has the following stock allocations:

Stock

Allocation

NVDA

0.196433

AMZN

0.194561

MSFT

0.211609

AAPL

0.195888

GOOG

0.201509

Continuous portfolio optimization actual allocation

Conclusion

In conclusion, we observe that Dirac effectively finds the solution and successfully solves the portfolio optimization problem. For a more challenging and comprehensive use case, refer to our example tutorial on optimizing a 100-asset portfolio over a 21-year period using the Nasdaq-100 as a benchmark with the Dirac-3 machine. Click here to learn more.