Backtesting and Comparing the Performance of Typical Portfolios
2016 Fall MATH 5800-030 — Group01 Course Project
Wenjie Wang, Hao Li, and Catherine Payzant
05 December 2016
Abstract
In this project, we reviewed the optimization algorithms for three popular portfolios: global minimum variance, mean-variance, and tangency covered in this course. We evaluated and compared the performance of these portfolios using the backtesting results from Quantopian. This project is motivated by a small Python algorithm for the minimum variance with constraints portfolio shared on the Quantopian community. We carry out a detailed discussion of the optimization algorithms and the backtesting results.
Introduction
In finance, an asset often means an investment instrument that can be traded. Suppose one purchased an asset for x0 dollars and sold it for x1 dollars later, the relative difference in percentage, (x1 − x0)/x0 is called the rate of return on the asset. Now consider an N assets' portfolio problem where N is a finite positive integer. Let ri, (i = 1, 2, …, N), denote the rate of return on asset i and assume it is distributed as Gaussian distribution with mean μi and standard deviation σi. Different assets are usually correlated with each other, which implies another assumption that Cov(ri, rj)=σij2 for all i ≠ j. Let r = (r1, r2, …, rN)T denote the expected rate of returns and Σ = (σij2) denote the variance-covariance matrix representing the risk of all the assets considered in the portfolio. Suppose we assign weights w = (w1, w2, …, wN)T to those assets, then the expected return is μ = rTw and the corresponding risk is \(\sigma=\sqrt{\mathbf{w}^T\mathbf{\Sigma w}}\). The natural constraint on those weights is that their summation has to be one. The weight taking negative value means the short selling.
The objective of this project is to review some typical algorithms determining the weights of any given set of assets so that the expected return with risk level constrained is maximized. How good their performance will be in the real stock market is of great interest. Back-testing those algorithms in Quantopian platform provides an convenient way for evaluation and comparison, where the formed portfolio can be automatically updated periodically.
The rest of this report is organized as follows. In Section 2, we give a full description of the global minimum variance portfolio in Section 2.1, mean-variance portfolio in Section 2.2, tangency portfolio in Section 2.3, and efficient frontier in Section 2.4. In Section 3, we present the backtesting results of the implemented algorithms with some discussion and plots. We conclude the report with some summary and more discussion in Section 4.
Portfolio Forms
Global Minimum Variance Portfolio
The global minimum variance (GMV) portfolio aims to minimize the overall risk globally, which can be rewritten into a quadratic programming problem:
$$\min_{\mathbf{w}} \mathbf{w}^T\mathbf{\Sigma}\mathbf{w},~\text{s.t.}~\mathbf{w}^T\mathbf{1} = 1,$$
where the only constraint is that the summation of the weights of assets in the portfolio is one.
The GMV portfolio is designed to minimize overall risk. However, in reality, investors usually tend to take more risk for possibly more returns.
Mean-Variance Portfolio
The basic GMV portfolio can be easily extended by imposing extra constraints on the objective function. In this subsection, we briefly review the Markowitz mean-variance portfolio.
Recall that ri is the expected rate of return on asset i. Compared with GMV, the extra constraint that mean-variance portfolio has is:
$$\mathbf{w}^T \mathbf{r} - K_0 \ge 0,$$
where r = (r1, r2, …, sN) and K0 is a specified constant.
Tangency Portfolio
The tangency portfolio aims to maximize the ratio of excess return (rTw − rf) to portfolio volatility and represents a trade-off between return and risk. The maximization problem is written as:
$$ \arg \max_{\mathbf{w}} \frac{\mathbf{r}^T\mathbf{w} - r_f} {\sqrt{\mathbf{w}^T\mathbf{\Sigma}\mathbf{w}}},~ \text{s.t.}~ \mathbf{w}^T\mathbf{1} = 1, $$
where rf is risk-free rate.
We may show that the maximization problem can be rewritten into a quadratic programming problem. Let w0 denotes the weight vector of the tangency portfolio. Note that rTw0 − rf = rTw0 − rf1Tw0 since 1Tw0 = w0T1 = 1. Then the maximum of the target function can be rewritten as
$$ \frac{(\mathbf{r} - r_f \mathbf{1})^T \mathbf{w}_0} {\sqrt{\mathbf{w}_0^T\mathbf{\Sigma}\mathbf{w}_0}} = \frac{(\mathbf{r} - r_f \mathbf{1})^T \mathbf{w}_k} {\sqrt{\mathbf{w}_k\mathbf{\Sigma}\mathbf{w}_k}}, $$
where wk = kw0, for some k > 0. The maximum of the target function does not depend on the choice of k, which means that we may find any wk that maximizes the target function and further re-scale them to satisfy the constraint: wT1 = 1. Therefore, the maximization problem of finding wk is equivalent to the following quadratic problem:
$$\min_{\mathbf{w}} \mathbf{w}^T\mathbf{\Sigma}\mathbf{w},~\text{s.t.}~ (\mathbf{r} - r_f \mathbf{1})^T \mathbf{w} = c_0,$$
where c0 is an arbitrary positive constant. Therefore, the original maximization problem is equivalent to the quadratic programming problem of finding one weight vector wk, and scaling wk to w0.
Efficient Frontier
Following the framework originally developed by Markowitz (1952), portfolios maximizing expected return under certain given level of risk or equivalently minimizing risk subject to a target expected return is defined as efficient portfolios. The efficient frontier consists of all efficient portfolios.
Thus, the naive constructing method of efficient frontier is by obtaining the mean variance portfolios subject to different target returns. Alternatively, it can be shown that any minimum variance portfolio can be represented as a convex combination of any two minimum variance portfolios with different target expected returns. If the expected return of the resulting portfolio is greater than the expected return of the global minimum variance portfolio, then the portfolio is an efficient frontier portfolio. (Otherwise, it is not.) Therefore, we can also get the efficient frontier based on the global minimum variance portfolio and tangency portfolio, both of which are known to be efficient.
We constructed one sample efficient frontier shown in Figure 1 from the simulated data for 20 assets. Each asset had 60 simulated rate of returns from normal distribution with mean 0.005 and standard deviation 0.015. For simplicity, no specific correlation was imposed on these assets. In addition, the expected returns and risk of those three portfolios were plotted in Figure 1. Clearly, global minimum variance, mean-variance, and tangency portfolio all lie on the efficient frontier.
Figure 1: Sample efficient frontier.
Backtesting
We backtested the portfolios discussed in Section 2 and compared their performance in Quantopian. The stocks of four famous companies, AAPL (Apple Inc.), AMZN (Amazon Inc.), GOOG (Alphabet Inc.), and FB (Facebook Inc.) were selected as sample stocks for backtesting. All the portfolios was implemented in Python with the help of Quantopian help document and community. The main body of function initialize(context) is given in the following code chunk.
def initialize(context):
context.stocks = [sid(24), # Apple Inc.
sid(16841), # Amazon.com Inc.
sid(46631), # Alphabet Inc. (Google's parent company)
sid(42950)] # facebook Inc
## allocate weights of assets one hour after the market opens
schedule_function(allocate, date_rules.every_day(),
time_rules.market_open(minutes = 60))
## trade one hour after the market opens every week
schedule_function(trade, date_rules.week_start(days_offset = 1),
time_rules.market_open(minutes = 60))
## record leverage every day
schedule_function(record_leverage, date_rules.every_day())
The selected stocks were save in context.stocks. The function allocate determined the weight of each stock at 10:30 AM on every market day. The function trade made weekly trades at 10:30 AM on the first day when the market opens in that week. For each asset, the trading weight was its average weight from last five daily allocations.
For a fair comparison, the trading time and rules were kept the same for all of the portfolios. The initial capital was set to be 10,000 USD. The time period for the back-test was from 2014/06/01 to 2016/11/01. Price history of the last week in minutes was considered in function data.history. The stock market opens at 9:30 AM and closes at 4:00 PM. Thus one weekday produces 6.5 × 60 = 390 history prices for every minute. Then the sample rate of returns were calculated by the relative difference between current prices and last prices in percentage. The expected returns were estimated by the mean of sample returns. The corresponding code is given as follows:
prices = data.history(context.stocks, 'price', 5 * 390, '1m')
ret = prices.pct_change()[1 : ].as_matrix(context.stocks)
ret_mean = prices.pct_change().mean()
The only difference was the determination of weights of assets in the portfolio as discussed in last section. The main part of function trade is given as follows:
def trade(context, data):
## only trade when there is no open order
if get_open_orders():
return
## order to target weights
for i, stock in enumerate(context.stocks):
order_target_percent(stock, allocation[i])
## record weights for each stock for output
record(AAPL = allocation[0])
record(AMZN = allocation[1])
record(GOOG = allocation[2])
record(FB = allocation[3])
We suppress all the remaining code to make this report concise and easy to follow. The complete Python scripts for these portfolios are available at the GitHub repository we set up for this course.
Figure 2, Figure 3, and Figure 4 represents the results of the global minimum variance, mean variance and the tangency portfolio, respectively.
Figure 2: Backtesting results of the global minimum variance portfolio.
From Figure 2, the GMV portfolio yielded 94.9% total returns at the end, which was much better than the benchmark by gaining 79.7% more. The total return curve seem to be always above the benchmark. Also, the volatility and maximum draw-down rate were the smallest among the three portfolios we considered here.
Figure 3: Backtesting results of the mean-variance portfolio.
For the mean-variance portfolio, the total returns at the end of the back-testing was 70.3% of the initial investment. While the returns of the benchmark (SPY) was just 15.2%. Therefore, the mean-variance portfolio also did a better job than the benchmark algorithm by gaining another 55.1% of total returns at the end.
Figure 4: Backtesting results of tangency portfolio.
From Figure 4, the total returns produced by the tangency portfolio at the end of the back-testing was 106.6% of the initial investment. Hence, it gave the largest returns among this three portfolios. The total returns gained at the end was about seven times of the returns from the benchmark algorithm. Although it did not always produce a better total returns than the benchmark at every time point, its maximum draw-down was close to the GMV portfolio. However, it has the largest volatility.
Summary and Discussion
In summary, we reviewed global minimum variance portfolios, mean-variance portfolios, and tangency portfolios that we learned from this course, and further compared their performance by back-testing in Quantopian. The backtesting results suggested that the tangency portfolio gave the largest total returns at the end, while the global minimum variance portfolio produced a consistently better total returns than the benchmark portfolio over time. Also, its maximum drawdown rate was the smallest among those portfolios. All of these portfolios outperformed the benchmark algorithm from Quantopian, though the performance of one certain portfolio is subject to the selection of stocks, follow-up time period, etc.
The report is mainly powered by Rmarkdown. The source document as well as the Python scripts used for backtesting in Quantopian platform are available at the GitHub repository named math5800 we set up for this course.
Acknowledgment
The completion of the project would not be possible without the great effort that our lovely team members have taken. It gave us much pleasure working together as a group. We would also like to show our gratitude to Dr. Do who introduced us the interesting lectures on financial programming and modeling, and whose broad programming knowledge will have lasting effect.
Reference
Markowitz, Harry. 1952. “Portfolio Selection.” The Journal of Finance 7 (1). Wiley Online Library: 77–91.