Main Content

setSolver

Choose main solver and specify associated solver options for portfolio optimization

Description

example

obj = setSolver(obj,solverType) selects the main solver and enables you to specify associated solver options for portfolio optimization for Portfolio, PortfolioCVaR, or PortfolioMAD objects. For details on the respective workflows when using these different objects, see Portfolio Object Workflow, PortfolioCVaR Object Workflow, and PortfolioMAD Object Workflow.

example

obj = setSolver(obj,solverType,Name,Value) selects the main solver and enables you to specify associated solver options for portfolio optimization for portfolio objects with additional options specified by using one or more Name,Value arguments.

example

obj = setSolver(obj,solverType,optimoptions) selects the main solver and enables you to specify associated solver options for portfolio optimization for portfolio objects with an optimoptions object.

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 'interior-point' and to turn off the display.

p = PortfolioCVaR;
p = setSolver(p, 'fmincon', 'Algorithm', 'interior-point', 'Display', 'off');
display(p.solverOptions.Algorithm);
interior-point
display(p.solverOptions.Display);
off

Use 'fmincon' as the solverType and use an optimoptions object to set the algorithm to 'interior-point' and to turn off the display.

p = PortfolioCVaR;
options = optimoptions('fmincon', 'Algorithm', 'interior-point', 'Display', 'off');
p = setSolver(p, 'fmincon', options);
display(p.solverOptions.Algorithm);
interior-point
display(p.solverOptions.Display);
off

Use 'TrustRegionCP' as the solverType with default options.

p = PortfolioCVaR;
p = setSolver(p,'TrustRegionCP');
display(p.solverType);
trustregioncp
display(p.solverOptions);
  struct with fields:

                MaxIterations: 1000
         AbsoluteGapTolerance: 1.0000e-07
         RelativeGapTolerance: 1.0000e-05
       NonlinearScalingFactor: 1000
       ObjectiveScalingFactor: 1000
            MainSolverOptions: [1x1 optim.options.Linprog]
                      Display: 'off'
                CutGeneration: 'basic'
     MaxIterationsInactiveCut: 30
           ActiveCutTolerance: 1.0000e-07
                  ShrinkRatio: 0.7500
    TrustRegionStartIteration: 2
                 InitialDelta: 0.5000
                   DeltaLimit: 1000000

Use the name-value pair 'ShrinkRatio' to shrink the size of the trust region by 0.75.

p = PortfolioCVaR;
p = setSolver(p,'TrustRegionCP','ShrinkRatio',0.75);
display(p.solverType);
trustregioncp
display(p.solverOptions);
  struct with fields:

                MaxIterations: 1000
         AbsoluteGapTolerance: 1.0000e-07
         RelativeGapTolerance: 1.0000e-05
       NonlinearScalingFactor: 1000
       ObjectiveScalingFactor: 1000
            MainSolverOptions: [1x1 optim.options.Linprog]
                      Display: 'off'
                CutGeneration: 'basic'
     MaxIterationsInactiveCut: 30
           ActiveCutTolerance: 1.0000e-07
                  ShrinkRatio: 0.7500
    TrustRegionStartIteration: 2
                 InitialDelta: 0.5000
                   DeltaLimit: 1000000

For the main solver, continue using the dual-simplex algorithm with no display, but tighten its termination tolerance to 1e8.

p = PortfolioCVaR;
options = optimoptions('linprog','Algorithm','Dual-Simplex','Display','off','OptimalityTolerance',1e8);
p = setSolver(p,'TrustRegionCP','MainSolverOptions',options);
display(p.solverType)
trustregioncp
display(p.solverOptions)
  struct with fields:

                MaxIterations: 1000
         AbsoluteGapTolerance: 1.0000e-07
         RelativeGapTolerance: 1.0000e-05
       NonlinearScalingFactor: 1000
       ObjectiveScalingFactor: 1000
            MainSolverOptions: [1x1 optim.options.Linprog]
                      Display: 'off'
                CutGeneration: 'basic'
     MaxIterationsInactiveCut: 30
           ActiveCutTolerance: 1.0000e-07
                  ShrinkRatio: 0.7500
    TrustRegionStartIteration: 2
                 InitialDelta: 0.5000
                   DeltaLimit: 1000000
display(p.solverOptions.MainSolverOptions.Algorithm)
dual-simplex-highs
display(p.solverOptions.MainSolverOptions.Display)
off
display(p.solverOptions.MainSolverOptions.TolFun)
   100000000

For the main solver, use the interior-point algorithm with no display and with a termination tolerance of 1e7.

p = PortfolioCVaR;
options = optimoptions('linprog','Algorithm','interior-point','Display','off','OptimalityTolerance',1e7);
p = setSolver(p,'TrustRegionCP','MainSolverOptions',options);
display(p.solverType)
trustregioncp
display(p.solverOptions)
  struct with fields:

                MaxIterations: 1000
         AbsoluteGapTolerance: 1.0000e-07
         RelativeGapTolerance: 1.0000e-05
       NonlinearScalingFactor: 1000
       ObjectiveScalingFactor: 1000
            MainSolverOptions: [1x1 optim.options.Linprog]
                      Display: 'off'
                CutGeneration: 'basic'
     MaxIterationsInactiveCut: 30
           ActiveCutTolerance: 1.0000e-07
                  ShrinkRatio: 0.7500
    TrustRegionStartIteration: 2
                 InitialDelta: 0.5000
                   DeltaLimit: 1000000
display(p.solverOptions.MainSolverOptions.Algorithm)
interior-point
display(p.solverOptions.MainSolverOptions.Display)
off
display(p.solverOptions.MainSolverOptions.TolFun)
    10000000

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

Data Types: object

Solver to use for portfolio optimization, specified using a character vector or string for the supported solverType.

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

Using Portfolio Object

For a Portfolio object, the supported solverType are:

  • 'lcprog' (Default).

    • The 'lcprog' solver uses linear complementary programming with Lemke's algorithm with control variables name-value arguments for 'maxiter', 'tiebreak', 'tolpiv'. For more information about 'lcprog' name-value options, see Portfolio Object Name-Value Arguments.

  • 'fmincon'

  • 'quadprog'

Using PortfolioCVaR Object

For a PortfolioCVaR object, the supported solverType are:

Using PortfolioMAD Object

For a PortfolioMAD object, the supported solverType are:

Note

setSolver can also configure solver options for 'linprog'. linprog is a helper solver used in estimating efficient frontier problems for a Portfolio, PorfolioCVaR, or PortfolioMAD object. The default algorithm for 'linprog' is 'dual-simplex-highs'. For more information about 'linprog' name-value options, see Name-Value Arguments. For more details on using a helper solver, see Solver Guidelines for Portfolio Objects, Solver Guidelines for PortfolioCVaR Objects, or Solver Guidelines for PortfolioMAD Objects.

Data Types: char | string

(Optional) optimoptions object, specified as an optimoptions object that is created using optimoptions from Optimization Toolbox™. For example:

p = setSolver(p,'fmincon',optimoptions('fmincon','Display','iter'));

Note

optimoptions is the default and recommended method to set solver options, however optimset is also supported.

Data Types: object

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: p = setSolver(p,'cuttingplane','MainSolverOptions',options) sets cuttingplane options for a PortfolioCVaR object.

Depending on the obj type (Portfolio, PortfolioCVaR, or PortfolioMAD) and the specified solverType, the options for the associated name-value arguments are different.

Portfolio Object Name-Value Arguments
  • For a Portfolio object using a solverType of lcprog, choose a name-value value in this table.

    ValueDescription
    'maxiter'

    Maximum number of iterations,specified as the comma-separated pair consisting of 'MaxIter' and a positive integer. The default value is 1 + n3, where n is the dimension of the input.

    'tiebreak'

    Method to break ties for pivot selection, specified as the comma-separated pair consisting of 'tiebreak' and one of the following options:

    • first - Select pivot with lowest index.

    • last - Select pivot with highest index.

    • random - Select a pivot at random.

    The default value is first.

    'tolpiv'

    Pivot tolerance below which a number is considered to be zero, specified as the comma-separated pair consisting of 'tolpiv' and a numeric value. The default value is 1.0e-9.

  • For a Portfolio object using a solverType of fmincon, see options to choose name-value arguments.

  • For a Portfolio object using a solverType of linprog, see options to choose name-value arguments.

  • For a Portfolio object using a solverType of quadprog, see options to choose name-value arguments.

PortfolioCVaR Object Name-Value Arguments
  • For a PortfolioCVaR object using a solverType of fmincon, see options to choose name-value arguments.

  • For a PorfolioCVaR object using a solverType of 'TrustRegionCP' or 'ExtendedCP', see Name-Value Arguments for 'TrustRegionCP' and 'ExtendedCP' to choose name-value arguments.

  • For a PorfolioCVaR object using a solverType of 'cuttingplane', choose a name-value value in this table.

    ValueDescription
    'MaxIter'

    Maximum number of iterations, specified as the comma-separated pair consisting of 'MaxIter' and a positive integer. The default value is 1000.

    'AbsTol'

    Absolute stopping tolerance, specified as the comma-separated pair consisting of 'AbsTol' and a positive scalar. The default value is 1e6.

    'RelTol'

    Relative stopping tolerance, specified as the comma-separated pair consisting of 'RelTol' and a positive scalar. The default value is 1e5.

    'MainSolverOptions'

    Options for the main solver linprog, specified as the comma-separated pair consisting of 'MainSolverOptions' and an optimoptions object. The default is optimoptions('linprog','Algorithm','dual-simplex-highs','Display','off').

  • For a PortfolioCVaR object using a solverType of linprog, see options to choose name-value arguments.

PortfolioMAD Object Name-Value Arguments
Name-Value Arguments for 'TrustRegionCP' and 'ExtendedCP'

For a PortfolioCVaR or PortfolioMAD object using a solverType of 'TrustRegionCP' or 'ExtendedCP', choose a name-value pair in this table.

ValueDescription
'MaxIterations'

Maximum number of iterations, specified as the comma-separated pair consisting of 'MaxIterations' and a positive real number. The default value is 1e3.

'NonlinearScalingFactor'

Scales the nonlinear function and its gradient by a factor, specified as the comma-separated pair consisting of 'NonlinearScalingFactor' and a positive real number. The default value is 1e3.

'ObjectiveScalingFactor'

Scales the objective function by a factor, specified as the comma-separated pair consisting of 'ObjectiveScalingFactor' and a positive real number. The default value is 1e3.

'AbsoluteGapTolerance'Solver stops if the absolute difference between the approximated nonlinear function value and its true value is less than or equal to AbsoluteGapTolerance, specified as the comma-separated pair consisting of 'AbsoluteGapTolerance' and a positive real number. The default value is 1e7.
'RelativeGapTolerance'Solver stops if the relative difference between the approximated nonlinear function value and its true value is less than or equal to RelativeGapTolerance, specified as the comma-separated pair consisting of 'RelativeGapTolerance' and a positive real number. The default value is 1e5.
'Display'Level of display, specified as the comma-separated pair consisting of 'Display' and a supported value of:
  • 'iter' displays output at each iteration and gives the technical exit message.

  • 'final' displays just the final output and gives the final technical exit message.

  • 'off' is the default and displays no output.

'CutGeneration'Method to add the cut, specified as the comma-separated pair consisting of 'CutGeneration' and a supported value of:
  • 'basic' is the default and the new cut is added at the latest solution found.

  • 'midway' is where the new cut is added at the mid point between the latest and previous solution found.

'MaxIterationsInactiveCut'Removes constraints that are not active for the last MaxIterationsInactiveCut iterations, specified as the comma-separated pair consisting of 'MaxIterationsInactiveCut' and a positive integer. The default value is 30.
'ActiveCutTolerance'Determines if the cuts are active and is used together with MaxIterationsInactiveCut to decide which cuts to remove from the LP subproblem, specified as the comma-separated pair consisting of 'ActiveCutTolerance' and a real number. The default value is 1e7.
'MainSolverOptions'Options for the main solver linprog, specified as the comma-separated pair consisting of 'MainSolverOptions' and an optimoptions object. The default is optimoptions('linprog','Algorithm','dual-simplex-highs','Display','off').
'TrustRegionStartIteration'Use this parameter only for a solverType of 'TrustRegionCP'. Solver starts to apply the trust region heuristic at TrustRegionStartIteration. Nonnegative integer. Default is 2.
'ShrinkRatio'Use this parameter only for a solverType of 'TrustRegionCP'. If the approximated functions are not agreeing well in the previous iterations, the algorithm will shrink the size of trust region by the ShrinkRatio. Nonnegative real between 0 and 1. Default is 0.75.
'InitialDelta'Use this parameter only for a solverType of 'TrustRegionCP'. Value to initialize trust region. Nonnegative real. Default is 0.5.
'DeltaLimit'Use this parameter only for a solverType of 'TrustRegionCP'. The trust region of the approximated functions is bounded by DeltaLimit during the iterations. The DeltaLimit value is a nonnegative real and the default value is 1e6.

Note

Modifying 'DeltaLimit' might result in the solver not being able to find a solution.

If you modify 'DeltaLimit' without specifying a value for 'InitialDelta', the 'InitialDelta' is automatically set to InitialDelta' = 'DeltaLimit'/2.

'DeltaLowerBound'Use this parameter only for a solverType of 'TrustRegionCP'. Use 'DeltaLowerBound' to set the lower bound for the trust-region radius. The 'DeltaLowerBound' numeric value must be in [0,1] and you can include 0 and 1. The default value is 0.01.

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,Name,Value);

Algorithms

To solve the efficient frontier of a portfolio, one version of the portfolio optimization problem minimizes the portfolio risk Risk(x), subject to a target return, and other linear constraints specified for the Portfolio, PortfolioCVaR, or PortfolioMAD object. For the definition of portfolio risk and return, see Risk Proxy and Return Proxy.

MinimizexRisk(x)subject to  Return(x)TargetReturnAxbAeqx=beqlbxub

An alternative version of the portfolio optimization problem maximizes the expected return of the portfolio, subject to a target risk and other linear constraints specified for the Portfolio, PortfolioCVaR, or PortfolioMAD object.

MaximizexReturn(x)subject to  Risk(x)TargetRiskAxbAeqx=beqlbxub

The return proxy is always a linear function. Therefore, depending on the risk proxy and whether it is used as the objective or constraints, the problem needs to be solved by different solvers. For example, quadprog is appropriate for problems with a quadratic function as the objective and only linear constraints, and fmincon is appropriate for problems with nonlinear objective or constraints. In addition, there are solvers in Financial Toolbox™ suitable for certain special types of problems, such as the solverType lcprog, 'TrustRegionCP', or 'ExtendedCP'.

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.

Version History

Introduced in R2011a

expand all