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 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.
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 .
We define the following variables as:
: Index of stock
: Total number of stocks being considered for investment.
: Daily returns or the Returns per Day of the portfolio.
: Expected Return of the portfolio.
: Variance of portfolio's total return.
: A parameter that balances the importance of maximizing returns versus minimizing risk, a larger value of corresponds to a more conservative strategy.
: Weight of stock i in the portfolio; , 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:
We want our weights to sum to , therefore:
We also want the portfolio to be diverse, so we apply an upper limit on all the weights:
In our example we set our upper limit on all the weights equal to so that the portfolio is never too heavily weighted toward one stock.
Hamiltonian Construction
The portfolio daily return over a time period can be written as a linear combination of daily returns of its constituent stocks over the same time period. We have,
where, is the daily return of stock at time .
The expectation of portfolio daily return over time period can thus be expanded as:
and the variance of portfolio daily returns is expanded as,
where Covar is the covariance function defined as
where is the mean value of over time periods.
To impose the inequality constraints in , we can introduce auxiliary, (or "slack") variables , and impose equality constraints as,
We also wish to constrain the total value of the portfolio
.
Our overall optimization problem is therefore
minimize
such that
and
and
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 should be a unitless quantity. From the definition of covariance, we see that (assuming stock values are in dollars) has units of dollars squared on the other hand returns will have units of dollars . In an objective function defined as covariance plus return, would have to have units of inverse currency . This has the disadvantage that what means would depend on how the values are defined. For example, if returns were instead given in cents, would have to be 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 overlapping time periods, that is . The problem becomes,
Adding the constraints to the objective function, the optimization problem reduces to,
where the two body coupling coefficients are defined as
where and objective function component of the coupling is defined as
and a constraint component defined as
.
The linear coefficients are defined as,
where
and
The Hamiltonian equation becomes:
where:
- represents the symmetrized form of - The notation denotes the horizontal concatenation of and , in other words the addition of 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 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 |
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.