Get startedGet started for free

Optimization backtest

1. Optimization backtest

This example picks up where the slides in the first video left off.

2. Optimization backtest: execution

You will use the returns and base portfolio specification from the slides in the first video of this chapter. Now that you have defined a base portfolio specification, the next step is to run the optimization with periodic rebalancing. For this example, you run the optimization with quarterly rebalancing using 5 years of data for the initial training period and rolling window. Recall that the returns data you are using are monthly returns, 12 months per year times 5 years is 60 periods. Next, you can extract the optimal weights at each rebalancing period with extractWeights and pass the weights and returns into Return.Portfolio to calculate the historical returns.

3. Optimization backtest: analysis

Let's take a closer look at the time series of optimal weights and chart them so we can visualize how the weights of the portfolio change through time. You can see in the chart that the allocation to US Bonds, shown in black, is very high throughout the entire period of the backtest. The minimum weight allocated to US Bonds is just under 60% for the first few years of the backtest, then increases to over 80% in the 1990s before falling back below 80% in the early 2000s, and then increasing to nearly 100% in the late 2000s at the end of the backtest period. For the first 10 years of the backtest, the allocation to US Bonds and Commodities makes up the majority of the portfolio, with a very small allocation to US and International Equities.

4. Optimization backtest: analysis

To compare the base portfolio returns with the benchmark returns, you can merge the returns and calculate annualized return metrics. Comparing the returns of the benchmark and the base portfolio, you can see that the base portfolio has similar annualized returns with a much lower annualized standard deviation. The performance statistics look good, but if you are concerned about the high allocation to a single asset, you have a valid concern.

5. Optimization backtest: refine constraints

You noticed in the visualization of the optimal weights that the portfolio can become concentrated with a high weight percentage allocated to a single asset. This exposes you to a concentration risk and you wish to constrain the weights to maintain an adequate level of diversification. One approach to address the concentration issue is to add a box constraint to specify a lower and upper bound on the weight of each asset. The base portfolio specification has a long only constraint which means that the weight of any asset must be greater than or equal to 0% and less than or equal to 100%. You want to update this constraint such that the weight of any asset must be greater than or equal to 5% and less than or equal to 40%.

6. Optimization backtest: refine constraints

To update the constraint, first make a copy of the base portfolio specification. This is not absolutely necessary, but it will avoid confusion later in the analysis if you wish to make further revisions to the base portfolio specification without the 5% to 40% weight constraints. To update a specific constraint, you call add-dot-constraint and specify the indexnum argument. In this case, the long only constraint was the second constraint added to the base portfolio specification so the index is 2. You wish to constrain the weights to a minimum of 5% and maximum of 40% so you set the constraint type to box, min equal to 0-point-05 and max equal to 0-point-4. Re-run the optimization with the box_port_spec as the portfolio and calculate the returns of the portfolio with box constraints.

7. Optimization backtest: analysis refined constraints

Let's look at the visualization of the optimal weights for the portfolio with box constraints. You can see from the chart that the portfolio is more balanced in terms of the weights. The US Bonds are held at their upper constraint of 40% throughout the entire backtest with larger allocations to equities relative to the base portfolio.

8. Optimization backtest: analysis refined constraints

Merge the returns of the box portfolio with the returns of the benchmark and base portfolio and calculate annualized performance statistics. You can see from the annualized performance metrics that the portfolio with box constraints outperforms the benchmark, but underperforms the base portfolio in terms of the Sharpe Ratio as a measure risk adjusted performance. Adding the box constraint addressed our weight concentration concern, but at the cost of lower annualized return and higher volatility. It looks like there is still some more work to be done on refining constraints and objectives. Perhaps you should look at diversification in terms of risk, rather than weights. In the upcoming exercises, you will do this by adding a risk budget objective.

9. Let's practice!

Let's move on to the exercises.