## Choosing and Controlling the Solver for Mean-Variance Portfolio Optimization

The default solver for mean-variance portfolio optimization is `lcprog`, which implements a linear complementarity programming (LCP) algorithm. Although `lcprog` works for most problems, you can adjust arguments to control the algorithm. Alternatively, the mean-variance portfolio optimization tools let you use any of the variations of `quadprog` from Optimization Toolbox™ software. Like Optimization Toolbox which uses the `interior-point-convex` algorithm as the default algorithm for `quadprog`, the portfolio optimization tools also use the `interior-point-convex` algorithm as the default. For details about `quadprog` and quadratic programming algorithms and options, see Quadratic Programming Algorithms.

### Using `'lcprog'` and `'quadprog'`

To modify either `lcprog` or to specify `quadprog` as your solver, use the `setSolver` function to set the hidden properties `solverType` and `solverOptions` that specify and control the solver. Since the solver properties are hidden, you cannot set these using the `Portfolio` object. The default solver is `lcprog` so you do not need to use `setSolver` to specify this solver. To use `quadprog`, you can set the default `interior-point-convex` algorithm of `quadprog` using:

```p = Portfolio; p = setSolver(p, 'quadprog'); display(p.solverType) display(p.solverOptions)```
```quadprog options: Options used by current Algorithm ('interior-point-convex'): (Other available algorithms: 'active-set', 'trust-region-reflective') Set properties: Algorithm: 'interior-point-convex' Display: 'off' OptimalityTolerance: 1.0000e-12 Default properties: ConstraintTolerance: 1.0000e-08 LinearSolver: 'auto' MaxIterations: 200 StepTolerance: 1.0000e-12```
and you can switch back to`lcprog` with:
```p = setSolver(p, 'lcprog'); display(p.solverType); display(p.solverOptions)```
```lcprog MaxIter: [] TieBreak: [] TolPiv: 5.0000e-08```
In both cases, `setSolver` sets up default options associated with either solver. If you want to specify additional options associated with a given solver, `setSolver` accepts these options with argument name-value pair arguments in the function call. For example, if you intend to use `quadprog` and want to use the `'trust-region-reflective'` algorithm, call `setSolver` with:
```p = Portfolio; p = setSolver(p, 'quadprog', 'Algorithm', 'trust-region-reflective'); display(p.solverOptions)```
``` quadprog options: Options used by current Algorithm ('trust-region-reflective'): (Other available algorithms: 'active-set', 'interior-point-convex') Set properties: Algorithm: 'trust-region-reflective' Default properties: Display: 'final' FunctionTolerance: 'default dependent on problem' HessianMultiplyFcn: [] MaxIterations: 'default dependent on problem' OptimalityTolerance: 'default dependent on problem' StepTolerance: 2.2204e-14 SubproblemAlgorithm: 'cg' TypicalX: 'ones(numberOfVariables,1)'```

In addition, if you want to specify any of the options for `quadprog` that are normally set through `optimoptions` from Optimization Toolbox, `setSolver` accepts an `optimoptions` object as the second argument. For example, you can start with the default options for `quadprog` set by `setSolver` and then change the algorithm to `'trust-region-reflective'` with no displayed output:

```p = Portfolio; options = optimoptions('quadprog', 'Algorithm', 'trust-region-reflective', 'Display', 'off'); p = setSolver(p, 'quadprog', options); display(p.solverOptions.Algorithm) display(p.solverOptions.Display)```
```trust-region-reflective off```

### Using the Mixed Integer Nonlinear Programming (MINLP) Solver

The mixed integer nonlinear programming (MINLP) solver, configured using `setSolverMINLP`, enables you to specify associated solver options for portfolio optimization for a `Portfolio` object. The MINLP solver is used when any one, or any combination of `'Conditional'` `BoundType`, `MinNumAssets`, or `MaxNumAssets` constraints are active. In this case, the portfolio problem is formulated by adding `NumAssets` binary variables, where `0` indicates not invested, and `1` is invested. For more information on using `'Conditional'` `BoundType`, see `setBounds`. For more information on specifying `MinNumAssets` and `MaxNumAssets`, see `setMinMaxNumAssets`.

When using the `estimate` functions with a `Portfolio` object where `'Conditional'` `BoundType`, `MinNumAssets`, or `MaxNumAssets` constraints are active, the mixed integer nonlinear programming (MINLP) solver is automatically used.

### Solver Guidelines for Portfolio Objects

The following table provides guidelines for using `setSolver` and `setSolverMINLP`.

Portfolio ProblemPortfolio FunctionType of Optimization ProblemMain Solver Helper Solver
Portfolio without tracking error constraints`estimateFrontierByRisk`Optimizing a portfolio for a certain risk level introduces a nonlinear constraint. Therefore, this problem has a linear objective with linear and nonlinear constraints.`'fmincon'` using `setSolver`

For `‘min’`: quadratic objective, `'quadprog'` or `'lcprog'` using `setSolver`

For `‘max’`: linear objective, `'linprog'` or `'lcprog'` using `setSolver`

Portfolio without tracking error constraints`estimateFrontierByReturn`Quadratic objective with linear constraints`'quadprog'` or `'lcprog'` using `setSolver`

For `‘min’`: quadratic objective, `'quadprog'` or `'lcprog'` using `setSolver`

For `‘max’`: linear objective, `'linprog'` or `'lcprog'` using `setSolver`

Portfolio without tracking error constraints`estimateFrontierLimits`

Quadratic or linear objective with linear constraints

For `‘min’`: quadratic objective, `'quadprog'` or `'lcprog'` using `setSolver`

For `‘max’`: linear objective, `'linprog'` or `'lcprog'` using `setSolver`

Not applicable
Portfolio without tracking error constraints`estimateMaxSharpeRatio`Quadratic objective with linear constraints`'quadprog'` using `setSolver`

Because `estimateMaxSharpeRatio` internally calls `estimateFrontierLimits`, all solvers needed by `estimateFrontierLimits` will be the helper solvers

Portfolio with tracking error constraints`estimateFrontierByRisk`Linear objective with linear and nonlinear constraints`'fmincon'` using `setSolver`Not applicable
Portfolio with tracking error constraints`estimateFrontierByReturn`Linear objective with linear and nonlinear constraints`'fmincon'` using `setSolver`Not applicable
Portfolio with tracking error constraints`estimateFrontierLimits`Quadratic (min risk problem) or linear (max return problem) objective with linear and nonlinear constraints`'fmincon'` using `setSolver`Not applicable
Portfolio with tracking error constraints`estimateMaxSharpeRatio`Quadratic objective with linear and nonlinear constraints`'fmincon'` using `setSolver`Not applicable
Portfolio with active `'Conditional'` `BoundType`, `MinNumAssets`, and `MaxNumAssets``estimateFrontierByRisk`The problem is formulated by introducing `NumAssets` binary variables to indicate whether the corresponding asset is invested or not. Therefore, it requires a mixed integer nonlinear programming solver. Three types of MINLP solvers are offered, see `setSolverMINLP`.Mixed integer nonlinear programming solver (MINLP) using `setSolverMINLP``'quadprog'` or `'fmincon'` are used when the `estimate` functions reduce the problem into NLP. These two solvers can be configured through `setSolver`.
Portfolio with active `'Conditional'` `BoundType`, `MinNumAssets`, and `MaxNumAssets``estimateFrontierByReturn`The problem is formulated by introducing `NumAssets` binary variables to indicate whether the corresponding asset is invested or not. Therefore, it requires a mixed integer nonlinear programming solver. Three types of MINLP solvers are offered, see `setSolverMINLP`.Mixed integer nonlinear programming solver (MINLP) using `setSolverMINLP``'quadprog'` or `'fmincon'` are used when the `estimate` functions reduce the problem into NLP. These two solvers can be configured through `setSolver`
Portfolio with active `'Conditional'` `BoundType`, `MinNumAssets`, and `MaxNumAssets``estimateFrontierLimits`The problem is formulated by introducing `NumAssets` binary variables to indicate whether the corresponding asset is invested or not. Therefore, it requires a mixed integer nonlinear programming solver. Three types of MINLP solvers are offered, see `setSolverMINLP`.Mixed integer nonlinear programming solver (MINLP) using `setSolverMINLP``'quadprog'` or `'fmincon'` are used when the `estimate` functions reduce the problem into NLP. These two solvers can be configured through `setSolver`
Portfolio with active `'Conditional'` `BoundType`, `MinNumAssets`, and `MaxNumAssets``estimateMaxSharpeRatio`The problem is formulated by introducing `NumAssets` binary variables to indicate whether the corresponding asset is invested or not. Therefore, it requires a mixed integer nonlinear programming solver. Three types of MINLP solvers are offered, see `setSolverMINLP`.Mixed integer nonlinear programming solver (MINLP) using `setSolverMINLP``'quadprog'` or `'fmincon'` are used, when the `estimate` functions reduce the problem into NLP. These two solvers can be configured through `setSolver`