In this series 2 (multi-factor Alpha strategy backtesting), we backtested four quantitative factors of the U.S. stock market. In this case, we will use the built-in dolphin Database quadprog function to optimize the mean variance of each factor’s weight to determine the best factor weight.
After executing the scripts provided in this series 2, the data table factorPnl stores the daily revenue for each factor. The following script is based on the data table factorPnl.
RetMatrix =matrix(select C0, C1, C2, C3 from factorPnl) H = cross(covar, retMatrix, retMatrix) f=-0.25*each(avg, Aeq=matrix([1],[1],[1],[1]) beq=[1] result=quadprog(H, f, A, b, Aeq, beq) result[1]Copy the code
This code solves the following optimization problems:
Here our objective function is maximization (1/4* expected return – 1/2* variance), while ensuring that the sum of the weights of the four factors is 1, and the minimum weight of each factor is 10%. H is the variance-covariance matrix of the four-factor return rate. Note that H, A, and Aeq here must be matrices with equal number of columns; F, b and BEq have to be vectors. A is the negative identity matrix and is used with B to ensure that each factor has A weight greater than 10%. Refer to the DolphinDB user development document quadprog for details about the Quadprog function.
Result [1] gives the optimal factor weight based on the given optimization conditions. The results are as follows:
[0.3612, 0.1, 0.438804, 0.1]
Copy the code
In the script provided in this series 2,
signalNames = `signal_mom`signal_vol`signal_beta`signal_size
Copy the code
Thus, the optimal weighting is 36.1% momentum factor, 10% volatility factor, 43.9% beta factor and 10% market cap factor.
The above calculations used data from all historical periods. You can also use the last 10 years of data to calculate, just make the following changes to the first line of the above script:
RetMatrix =matrix(select C0, C1, C2 from factorPnl where date>=2007.01.01)Copy the code
The results are as follows:
[0.19277, 0.1, 0.1, 0.60723]
Copy the code
Thus, the optimal weighting is 19.3% momentum factor, 10% volatility factor, 10% beta factor and 60.7% market cap factor.
It can be seen that the historical period selected for mean variance optimization has an important influence on the results.