Everybody would like to see the future. **If you’re a portfolio manager, you’d definitely love to see the future**.

Many posts here on QuantDare deal with the challenge of predicting the future (with Prophet, Random Forests, Lasso, etc). This time, we talk about something different: imagine we are able to predict the future exactly. Now what? How could we exploit this priceless information? As we will see, the Viterbi algorithm gives us an answer.

## A simple case

Let’s suppose you have 4 € to invest in either of two assets and you have seen in the crystal ball that the returns for the next three days are: (+20%,+20%,+20%) for the first asset and (+40%,+40%,-60%) for the second. **How would you manage your money** in the next days? You would probably (and correctly) invest your 4 € in asset 2 today, keep your position tomorrow, and then sell all your stocks of asset 2 and buy asset 1 the next day. This way, you would make a profit of +135%, ignoring transaction costs.

That was easy. Now imagine you are a portfolio manager and have 4 billion € to invest in the same assets. In this case, applying the former sequence of actions might not be possible. Sell/buy orders at such scale can have a huge, undesirable impact on the market, incurring enormous costs. Also, additional constraints often exist on the maximum turnover of a portfolio. For these reasons, **we might only be allowed to trade a fraction of our positions in a given day**.

To illustrate the consequences, imagine now that you are only allowed to sell/buy 25% of your portfolio each day, and you start with a 50% exposure to each of the assets. How would you manage your money now? If your answer is “I’d sell as much as possible of the worst asset to buy as much as possible of the best one in each period”, then… you’d be wrong!

Let’s examine why this is not optimal. Following this** myopic strategy**, we would move 25% of our portfolio to asset 2 today, then another 25% tomorrow, and finally 25% back to asset 1 on the third day. Graphically, we can represent the portfolio as a set of 4 “boxes” (each containing 25%) in orange (asset 1) or blue (asset 2), with the shown returns:

The accumulated return of the portfolio is +13.4%.

Now consider this alternative sequence of actions: we preserve the 50/50 composition today, then we add 25% of the portfolio to asset 1 tomorrow and another 25% the following day.

The cumulative return of the portfolio is now +95%.

What happened here? The most intuitive explanation is that in the first case, our exposure to asset 2 was too large previous to its crash, while in the second case we have no more asset 2 when the crash arrives. In other words: in the first case, we chose a wrong sequence of decisions despite being able to see the future, because we made each decision day by day (i.e. myopically).

This example illustrates how** we should be prepared to exploit our clairvoyant superpowers**; otherwise, we might feel like the Greatest American Hero, a hilarious character who was unable to properly use his Superman-like suit because he lost the user’s manual in the first episode.

So, **how can we identify the optimal trajectory in the space of all the possible active portfolio managements**?

The quick answer is “explore them all, and choose the most profitable”, but is this computationally tractable? Imagine that instead of a 3-day period, we aim at managing the portfolio during a 90-day interval. How many alternative trajectories are there? Each day we have at least two actions at our disposal (either selling one of the assets to buy the other or do nothing), so the number of trajectories is at least \(2^{90}\). That’s higher than the estimated number of atoms in the Universe!

## The Viterbi algorithm

Don’t panic! There’s actually no need to explore all the trajectories. Instead, we can make use of the **Viterbi algorithm**. It’s widely used on hidden Markov models and digital communications to determine the most likely sequence of hidden states or symbols, respectively. When applied to our hindsight portfolio management problem, it makes use of a very intuitive simplification to avoid the exponential explosion of alternative trajectories: for a given portfolio composition at the n-*th* day, there’s no need to store all the alternative trajectories that lead to that state, but only the most profitable one. Recursively applying this simplification leads to a procedure with polynomial complexity in both the number of states (portfolio compositions) and period length.

Let us apply the algorithm to our example at hand. In the first day, there are three alternative actions to take, each one leading to the returns shown by the arrows:

In the second day, we have the following tree of possible actions. Each arrow shows the accumulated return from the beginning:

The discarded trajectories are shown as dashed lines. We only have to keep track of the solid lines, which show the best trajectory entering each state. Finally, in the third day we have the following trajectories:

As we can see, the best trajectory is the one which yields a 95% return, as stated before.

## An example with real data

Finally, we apply the Viterbi algorithm to an example with real data. It’s Christmas 2015, and an angel comes to shows us the future as in Frank Capra’s movie *It’s a Wonderful Life*. Specifically, the angel shows us the evolution of five assets along the forthcoming year: Euro Stoxx 50, S&P 500, Nikkei 225, an Emergent Markets index and cash. Their normalized prices are shown in the Figure:

Let us suppose we’re running a portfolio with these five assets, and we are only allowed to trade 5% of the portfolio each day (think of twenty boxes of five colors). In the following figures, we show the portfolio compositions that result from the application of both the myopic and the Viterbi approaches. We have assumed a transaction cost of 10 basis points, which is taken into account by the algorithm to evaluate the net return of each trajectory.

The next Figure shows the returns obtained by each strategy, including the best asset and an equally weighted portfolio, for comparison.

As we can see, **the Viterbi approach strongly outperforms the myopic one**. This is even the case when the myopic is consistently better than the best asset in the portfolio.

## Conclusion

What’s the best you can do if you can see the future? We have shown that the answer if not obvious if we are subject to operational constraints. The Viterbi algorithm gives us an answer without the need of exploring the whole set of alternative trajectories. This might not be very useful since most of us cannot see the future. **But it’s so much fun!**