|
| 1 | +/* |
| 2 | + * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. |
| 3 | + * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by aaplicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | +*/ |
| 15 | + |
| 16 | +using NUnit.Framework; |
| 17 | +using QuantConnect.Algorithm.Framework.Portfolio; |
| 18 | +using System; |
| 19 | +using System.Collections.Generic; |
| 20 | +using System.Linq; |
| 21 | + |
| 22 | +namespace QuantConnect.Tests.Algorithm.Framework.Portfolio |
| 23 | +{ |
| 24 | + [TestFixture] |
| 25 | + public class UnconstrainedMeanVariancePortfolioOptimizerTests |
| 26 | + { |
| 27 | + private Dictionary<int, double[,]> _historicalReturns = new(); |
| 28 | + private Dictionary<int, double[]> _expectedReturns = new(); |
| 29 | + private Dictionary<int, double[,]> _covariances = new(); |
| 30 | + private Dictionary<int, double[]> _expectedResults = new(); |
| 31 | + |
| 32 | + [OneTimeSetUp] |
| 33 | + public void Setup() |
| 34 | + { |
| 35 | + double[,] historicalReturns1 = new double[,] { { 0.76, -0.06, 1.22, 0.17 }, { 0.02, 0.28, 1.25, -0.00 }, { -0.50, -0.13, -0.50, -0.03 }, { 0.81, 0.31, 2.39, 0.26 }, { -0.02, 0.02, 0.06, 0.01 } }; |
| 36 | + double[,] historicalReturns2 = new double[,] { { -0.15, 0.67, 0.45 }, { -0.44, -0.10, 0.07 }, { 0.04, -0.41, 0.01 }, { 0.01, 0.03, 0.02 } }; |
| 37 | + double[,] historicalReturns3 = new double[,] { { -0.02, 0.65, 1.25 }, { -0.29, -0.39, -0.50 }, { 0.29, 0.58, 2.39 }, { 0.00, -0.01, 0.06 } }; |
| 38 | + double[,] historicalReturns4 = new double[,] { { 0.76, 0.25, 0.21 }, { 0.02, -0.15, 0.45 }, { -0.50, -0.44, 0.07 }, { 0.81, 0.04, 0.01 }, { -0.02, 0.01, 0.02 } }; |
| 39 | + |
| 40 | + double[] expectedReturns1 = new double[] { 0.21, 0.08, 0.88, 0.08 }; |
| 41 | + double[] expectedReturns2 = new double[] { -0.13, 0.05, 0.14 }; |
| 42 | + double[] expectedReturns3 = null; |
| 43 | + double[] expectedReturns4 = null; |
| 44 | + |
| 45 | + double[,] covariance1 = new double[,] { { 0.31, 0.05, 0.55, 0.07 }, { 0.05, 0.04, 0.18, 0.01 }, { 0.55, 0.18, 1.28, 0.12 }, { 0.07, 0.01, 0.12, 0.02 } }; |
| 46 | + double[,] covariance2 = new double[,] { { 0.05, -0.02, -0.01 }, { -0.02, 0.21, 0.09 }, { -0.01, 0.09, 0.04 } }; |
| 47 | + double[,] covariance3 = new double[,] { { 0.06, 0.09, 0.28 }, { 0.09, 0.25, 0.58 }, { 0.28, 0.58, 1.66 } }; |
| 48 | + double[,] covariance4 = null; |
| 49 | + |
| 50 | + double[] expectedResult1 = new double[] { -13.288136, -23.322034, 8.79661, 9.389831 }; |
| 51 | + double[] expectedResult2 = new double[] { -0.142857, -35.285714, 82.857143 }; |
| 52 | + double[] expectedResult3 = new double[] { -13.232262, -3.709534, 4.009978 }; |
| 53 | + double[] expectedResult4 = new double[] { 4.621852, -9.651736, 5.098332 }; |
| 54 | + |
| 55 | + _historicalReturns.Add(1, historicalReturns1); |
| 56 | + _historicalReturns.Add(2, historicalReturns2); |
| 57 | + _historicalReturns.Add(3, historicalReturns3); |
| 58 | + _historicalReturns.Add(4, historicalReturns4); |
| 59 | + |
| 60 | + _expectedReturns.Add(1, expectedReturns1); |
| 61 | + _expectedReturns.Add(2, expectedReturns2); |
| 62 | + _expectedReturns.Add(3, expectedReturns3); |
| 63 | + _expectedReturns.Add(4, expectedReturns4); |
| 64 | + |
| 65 | + _covariances.Add(1, covariance1); |
| 66 | + _covariances.Add(2, covariance2); |
| 67 | + _covariances.Add(3, covariance3); |
| 68 | + _covariances.Add(4, covariance4); |
| 69 | + |
| 70 | + _expectedResults.Add(1, expectedResult1); |
| 71 | + _expectedResults.Add(2, expectedResult2); |
| 72 | + _expectedResults.Add(3, expectedResult3); |
| 73 | + _expectedResults.Add(4, expectedResult4); |
| 74 | + } |
| 75 | + |
| 76 | + [TestCase(1)] |
| 77 | + [TestCase(2)] |
| 78 | + [TestCase(3)] |
| 79 | + [TestCase(4)] |
| 80 | + public void TestOptimizeWeightings(int testCaseNumber) |
| 81 | + { |
| 82 | + var testOptimizer = new UnconstrainedMeanVariancePortfolioOptimizer(); |
| 83 | + |
| 84 | + var result = testOptimizer.Optimize( |
| 85 | + _historicalReturns[testCaseNumber], |
| 86 | + _expectedReturns[testCaseNumber], |
| 87 | + _covariances[testCaseNumber]); |
| 88 | + |
| 89 | + Assert.AreEqual(_expectedResults[testCaseNumber], result.Select(x => Math.Round(x, 6))); |
| 90 | + } |
| 91 | + |
| 92 | + public void EmptyPortfolioReturnsEmptyArrayOfDouble() |
| 93 | + { |
| 94 | + var testOptimizer = new UnconstrainedMeanVariancePortfolioOptimizer(); |
| 95 | + var historicalReturns = new double[,] { { } }; |
| 96 | + |
| 97 | + var result = testOptimizer.Optimize(historicalReturns); |
| 98 | + |
| 99 | + Assert.AreEqual(Array.Empty<double>(), result); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments