# Documentation

### This is machine translation

Translated by
Mouseover text to see original. Click the button below to return to the English verison of the page.

# setSolver

Choose main solver and specify associated solver options for portfolio optimization

Use the `setSolver` function with a `Portfolio`, `PortfolioCVaR`, or `PortfolioMAD` object to choose main solver and specify associated solver options for portfolio optimization for portfolio objects.

For details on the respective workflows when using these different objects, see Portfolio Object Workflow, PortfolioCVaR Object Workflow, and PortfolioMAD Object Workflow.

## Syntax

``obj = setSolver(obj,solverType)``
``obj = setSolver(obj,solverType,varargin)``

## Description

example

````obj = setSolver(obj,solverType)` chooses main solver and specify associated solver options for portfolio optimization for portfolio objects.```

example

````obj = setSolver(obj,solverType,varargin)` chooses main solver and specify associated solver options for portfolio optimization for portfolio objects with additional options specified for `varargin` by using one or more `Name,Value` pair arguments, or an `optimoptions` object.After you specify a solver, the `varargin` argument accepts either name-value pair arguments to set options or, for the case of solvers from Optimization Toolbox™ software, a structure created by `optimoptions`.```

## Examples

collapse all

If you use the `quadprog` function as the `solverType`, the default is the `interior-point-convex` version of `quadprog`.

```load CAPMuniverse p = Portfolio('AssetList',Assets(1:12)); p = setDefaultConstraints(p); p = setSolver(p, 'quadprog'); display(p.solverType);```
```quadprog ```

You can switch back to `lcprog` with:

```p = setSolver(p, 'lcprog'); display(p.solverType);```
```lcprog ```

Use `'fmincon'` as the `solverType`.

```p = PortfolioCVaR; p = setSolver(p, 'fmincon'); display(p.solverType);```
```fmincon ```

Use `'fmincon'` as the `solverType` and use name-value pair arguments to set the algorithm to `'trust-region-reflective'` and to turn off the display.

```p = PortfolioCVaR; p = setSolver(p, 'fmincon', 'Algorithm', 'trust-region-reflective', 'Display', 'off'); display(p.solverOptions.Algorithm);```
```trust-region-reflective ```
`display(p.solverOptions.Display);`
```off ```

Use `'fmincon'` as the `solverType` and use an `optimoptions` object to set the algorithm to `'trust-region-reflective'` and to turn off the display.

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

Use `'cuttingplane'` as the `solverType` with default options.

```p = PortfolioCVaR; p = setSolver(p,'cuttingplane'); display(p.solverType);```
```cuttingplane ```

Use the Name-Value pair `'MaxIter'` to set the maximum number of iterations to `1500`.

```p = PortfolioCVaR; p = setSolver(p,'cuttingplane','MaxIter',1500); display(p.solverType);```
```cuttingplane ```
`display(p.solverOptions);`
``` MaxIter: 1500 AbsTol: 1.0000e-06 RelTol: 1.0000e-05 MasterSolverOptions: [1x1 optim.options.Linprog] ```

For the master solver, continue using the `dual-simplex` algorithm with no display, but tighten its termination tolerance to `1e-8`.

```p = PortfolioCVaR; options = optimoptions('linprog','Algorithm','Dual-Simplex','Display','off','OptimalityTolerance',1e-8); p = setSolver(p,'cuttingplane','MasterSolverOptions',options); display(p.solverType)```
```cuttingplane ```
`display(p.solverOptions)`
``` MaxIter: 1000 AbsTol: 1.0000e-06 RelTol: 1.0000e-05 MasterSolverOptions: [1x1 optim.options.Linprog] ```
`display(p.solverOptions.MasterSolverOptions.Algorithm)`
```dual-simplex ```
`display(p.solverOptions.MasterSolverOptions.Display)`
```off ```
`display(p.solverOptions.MasterSolverOptions.TolFun)`
``` 1.0000e-08 ```

For the master solver, use the `interior-point` algorithm with no display, and with a termination tolerance of `1e-7`.

```p = PortfolioCVaR; options = optimoptions('linprog','Algorithm','interior-point','Display','off','OptimalityTolerance',1e-7); p = setSolver(p,'cuttingplane','MasterSolverOptions',options); display(p.solverType)```
```cuttingplane ```
`display(p.solverOptions)`
``` MaxIter: 1000 AbsTol: 1.0000e-06 RelTol: 1.0000e-05 MasterSolverOptions: [1x1 optim.options.Linprog] ```
`display(p.solverOptions.MasterSolverOptions.Algorithm)`
```interior-point ```
`display(p.solverOptions.MasterSolverOptions.Display)`
```off ```
`display(p.solverOptions.MasterSolverOptions.TolFun)`
``` 1.0000e-07 ```

Use `'fmincon'` as the `solverType`.

```p = PortfolioMAD; p = setSolver(p, 'fmincon'); display(p.solverType);```
```fmincon ```

Use `'fmincon'` as the `solverType` and use name-value pair arguments to set the algorithm to|'sqp'| and to turn on the display.

```p = PortfolioMAD; p = setSolver(p, 'fmincon', 'Algorithm', 'sqp', 'Display', 'final'); display(p.solverOptions.Algorithm);```
```sqp ```
`display(p.solverOptions.Display);`
```final ```

Use `'fmincon'` as the `solverType` and use an `optimoptions` object to set the algorithm to `'trust-region-reflective'` and to turn off the display.

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

Use `'fmincon'` as the `solverType` and use an `optimoptions` object to set the algorithm to `'active-set'` and to set the gradients flag `'on'` for `'GradObj'` and turn off the display.

```p = PortfolioMAD; options = optimoptions('fmincon','algorithm','active-set','display','off','gradobj','on'); p = setSolver(p, 'fmincon', options); display(p.solverOptions.Algorithm);```
```active-set ```
`display(p.solverOptions.Display);`
```off ```

## Input Arguments

collapse all

Object for portfolio, specified using `Portfolio`, `PortfolioCVaR`, or `PortfolioMAD` object. For more information on creating a portfolio object, see

### Name-Value Pair Arguments

Specify optional comma-separated pairs of `Name,Value` arguments. `Name` is the argument name and `Value` is the corresponding value. `Name` must appear inside single quotes (`' '`). You can specify several name and value pair arguments in any order as `Name1,Value1,...,NameN,ValueN`.

Example: `p = setSolver(p,'cuttingplane','MasterSolverOptions',options)`

collapse all

Solver to use for portfolio optimization, specified using a character vector, or `optimoptions` object.

The `SolverType` input argument depends on which type of object (`obj`) is being used for a portfolio optimization.

For a `Portfolio` object:

• The default `solverType` for the Portfolio object is `'lcprog'` with the control variables `'maxiter'`, `'tiebreak'`, `'tolpiv'`

• The Portfolio object can also use `'quadprog'`, which has several different options that can be set with `optimoptions`. Like Optimization Toolbox software which uses the `interior-point-convex` algorithm as the default algorithm for `quadprog`, the portfolio optimization tools also uses the `interior-point-convex` algorithm. For more information about `quadprog` and quadratic programming algorithms and options, see Quadratic Programming Algorithms (Optimization Toolbox).

For a `PortfolioCVaR` object:

• The supported `solverType` are:

• `'fmincon'`

• `'cuttingplane'`

• The default is `'fmincon'` using the `'sqp'` algorithm.

For a `PortfolioMAD` object:

• The supported `solverType` is:

• `'fmincon'`

• The default is `'fmincon'` using the `'sqb'` algorithm and `'GradObj'` set to `'on'`

Options to control the solver specified in `solverType` as name-value pair arguments or an `optimoptions` object. Note, `optimoptions` is the default and recommended method to set solver options, however `optimset` is also supported.

The `varargin` input argument depends on which type of object (`obj`) is being used for a portfolio optimization

For a `Portfolio` object:

• The default `solverType` for the Portfolio object is `'lcprog'` with the control variables `'maxiter'`, `'tiebreak'`, `'tolpiv'`

• The Portfolio object can also use a `solverType` of `'quadprog'`, which has several different options that can be set with `optimoptions`. Like Optimization Toolbox software which uses the `interior-point-convex` algorithm as the default algorithm for `quadprog`, the portfolio optimization tools also uses the `interior-point-convex` algorithm. For more information about `quadprog` and quadratic programming algorithms and options, see Quadratic Programming Algorithms (Optimization Toolbox).

For a `PortfolioCVaR` object:

• For the default `solverType`, `'fmincon'`, `PortfolioCVaR` by default sets the algorithm to `'sqp'`, uses objective function gradients, and turns off the display. All `fmincon` options are supported, either as name-value pair arguments or using an `optimoptions` object. For more information about `fmincon` and quadratic programming algorithms and options, see Quadratic Programming Algorithms (Optimization Toolbox).

• For the `cuttingplane` solver, the following solver options are available as name-value pair arguments:

For a `PortfolioMAD` object:

Data Types: `char`

Maximum number of iterations, specified as a positive integer when using a `PortfolioCVaR` object.

Data Types: `char` | `double`

Absolute stopping tolerance, specified as a positive scalar when using a `PortfolioCVaR` object.

Data Types: `char` | `double`

Relative stopping tolerance, specified as a positive scalar when using a `PortfolioCVaR` object.

Data Types: `char` | `double`

Options for the master solver `linprog`, specified as an `optimoptions` object when using a `PortfolioCVaR` object. For more information about `linprog` and linear programming algorithms and options, see Linear Programming Algorithms (Optimization Toolbox).

Data Types: `double`

## Output Arguments

collapse all

Updated portfolio object, returned as a `Portfolio`, `PortfolioCVaR`, or `PortfolioMAD` object. For more information on creating a portfolio object, see

## Tips

You can also use dot notation to choose the solver and specify associated solver options.

`obj = obj.setSolver(solverType, varargin);`

## Algorithms

One version of the CVaR portfolio optimization problem minimizes risk, measured as the CVaR of the portfolio, subject to a target return and other linear constraints on the portfolio. For the definition of the CVaR function, see Risk Proxy.

`$minimiz{e}_{x}CVa{R}_{\alpha }\left(x\right)$`

`$Ax\le b$`
`${A}_{eq}x={b}_{eq}$`
`$lb\le x\le ub$`

Vector $\overline{y}$ is the mean return vector (the column-wise mean of the scenario matrix Y), so that ${\overline{y}}^{T}x$ is the expected return of portfolio x. The first constraint says that the expected return must be at least as good as a target return.

An alternative version of the CVaR portfolio optimization problem maximizes the expected return of the portfolio, subject to a target risk and other linear constraints on the portfolio.

`$maximiz{e}_{x}{\overline{y}}^{T}x$`

`$Ax\le b$`
`${A}_{eq}x={b}_{eq}$`
`$lb\le x\le ub$`

The first constraint in this case says that the portfolio CVaR cannot exceed a given CVaR limit.

By default, the CVaR portfolio object uses `fmincon` to solve the CVaR portfolio optimization problems. For information about `fmincon` and quadratic programming algorithms and options, see Quadratic Programming Algorithms (Optimization Toolbox).

Alternatively, the CVaR portfolio optimization problems can be solved with `'cuttingplane'`, an implementation of Kelley’s cutting-plane method. For more information, see Kelley [45] at Portfolio Optimization.

## References

[1] Kelley, J. E. "The Cutting-Plane Method for Solving Convex Programs." Journal of the Society for Industrial and Applied Mathematics. Vol. 8, No. 4, December 1960, pp. 703–712.

[2] Rockafellar, R. T. and S. Uryasev "Optimization of Conditional Value-at-Risk." Journal of Risk. Vol. 2, No. 3, Spring 2000, pp. 21–41.

[3] Rockafellar, R. T. and S. Uryasev "Conditional Value-at-Risk for General Loss Distributions." Journal of Banking and Finance. Vol. 26, 2002, pp. 1443–1471.