Lean  $LEAN_TAG$
MaximumSharpeRatioPortfolioOptimizer.cs
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 applicable 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 System.Collections.Generic;
17 using Accord.Math;
18 using Accord.Math.Optimization;
19 using Accord.Statistics;
20 
22 {
23  /// <summary>
24  /// Provides an implementation of a portfolio optimizer that maximizes the portfolio Sharpe Ratio.
25  /// The interval of weights in optimization method can be changed based on the long-short algorithm.
26  /// The default model uses flat risk free rate and weight for an individual security range from -1 to 1.
27  /// </summary>
29  {
30  private double _lower;
31  private double _upper;
32  private double _riskFreeRate;
33 
34  /// <summary>
35  /// Initialize a new instance of <see cref="MaximumSharpeRatioPortfolioOptimizer"/>
36  /// </summary>
37  /// <param name="lower">Lower constraint</param>
38  /// <param name="upper">Upper constraint</param>
39  /// <param name="riskFreeRate"></param>
40  public MaximumSharpeRatioPortfolioOptimizer(double lower = -1, double upper = 1, double riskFreeRate = 0.0)
41  {
42  _lower = lower;
43  _upper = upper;
44  _riskFreeRate = riskFreeRate;
45  }
46 
47  /// <summary>
48  /// Sum of all weight is one: 1^T w = 1 / Σw = 1
49  /// </summary>
50  /// <param name="size">number of variables</param>
51  /// <returns>linear constraint object</returns>
52  protected LinearConstraint GetBudgetConstraint(int size)
53  {
54  return new LinearConstraint(size)
55  {
56  CombinedAs = Vector.Create(size, 1.0),
57  ShouldBe = ConstraintType.EqualTo,
58  Value = 1.0
59  };
60  }
61 
62  /// <summary>
63  /// Boundary constraints on weights: lw ≤ w ≤ up
64  /// </summary>
65  /// <param name="size">number of variables</param>
66  /// <returns>enumeration of linear constraint objects</returns>
67  protected IEnumerable<LinearConstraint> GetBoundaryConditions(int size)
68  {
69  for (int i = 0; i < size; i++)
70  {
71  yield return new LinearConstraint(1)
72  {
73  VariablesAtIndices = new int[] { i },
74  ShouldBe = ConstraintType.GreaterThanOrEqualTo,
75  Value = _lower
76  };
77  yield return new LinearConstraint(1)
78  {
79  VariablesAtIndices = new int[] { i },
80  ShouldBe = ConstraintType.LesserThanOrEqualTo,
81  Value = _upper
82  };
83  }
84  }
85 
86  /// <summary>
87  /// Perform portfolio optimization for a provided matrix of historical returns and an array of expected returns
88  /// </summary>
89  /// <param name="historicalReturns">Matrix of annualized historical returns where each column represents a security and each row returns for the given date/time (size: K x N).</param>
90  /// <param name="expectedReturns">Array of double with the portfolio annualized expected returns (size: K x 1).</param>
91  /// <param name="covariance">Multi-dimensional array of double with the portfolio covariance of annualized returns (size: K x K).</param>
92  /// <returns>Array of double with the portfolio weights (size: K x 1)</returns>
93  public double[] Optimize(double[,] historicalReturns, double[] expectedReturns = null, double[,] covariance = null)
94  {
95  covariance = covariance ?? historicalReturns.Covariance();
96  var returns = (expectedReturns ?? historicalReturns.Mean(0)).Subtract(_riskFreeRate);
97 
98  var size = covariance.GetLength(0);
99  var x0 = Vector.Create(size, 1.0 / size);
100  var k = returns.Dot(x0);
101 
102  var constraints = new List<LinearConstraint>
103  {
104  // Sharpe Maximization under Quadratic Constraints
105  // https://quant.stackexchange.com/questions/18521/sharpe-maximization-under-quadratic-constraints
106  // (µ − r_f)^T w = k
107  new LinearConstraint(size)
108  {
109  CombinedAs = returns,
110  ShouldBe = ConstraintType.EqualTo,
111  Value = k
112  }
113  };
114 
115  // Σw = 1
116  constraints.Add(GetBudgetConstraint(size));
117 
118  // lw ≤ w ≤ up
119  constraints.AddRange(GetBoundaryConditions(size));
120 
121  // Setup solver
122  var optfunc = new QuadraticObjectiveFunction(covariance, Vector.Create(size, 0.0));
123  var solver = new GoldfarbIdnani(optfunc, constraints);
124 
125  // Solve problem
126  var success = solver.Minimize(Vector.Copy(x0));
127  var sharpeRatio = returns.Dot(solver.Solution) / solver.Value;
128  return success ? solver.Solution : x0;
129  }
130  }
131 }