setSolver
Choose main solver and specify associated solver options for portfolio optimization
Syntax
Description
selects the main solver and enables you to specify associated solver options for portfolio
optimization for obj
= setSolver(obj
,solverType
)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.
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
obj
= setSolver(obj
,solverType
,Name,Value
)Name,Value
arguments.
selects the main solver and enables you to specify associated solver options for portfolio
optimization for portfolio objects with an obj
= setSolver(obj
,solverType
,optimoptions
)optimoptions
object.
Examples
Set Solver Type for a Portfolio Object
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
Set the Solver Type as 'fmincon' for a PortfolioCVaR Object
Use 'fmincon'
as the solverType
.
p = PortfolioCVaR;
p = setSolver(p, 'fmincon');
display(p.solverType);
fmincon
Set the Solver Type as 'fmincon' and Use Name-Value Pair Arguments to Set the Algorithm for a PortfolioCVaR Object
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
Set the Solver Type as 'fmincon' and Use an optimoptions Object to Set the Algorithm for a PortfolioCVaR Object
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
Set 'TrustRegionCP'
as the Solver Type with Default Options for a PortfolioCVaR Object
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
Set 'TrustRegionCP'
as the Solver Type with 'ShrinkRatio'
for a PortfolioCVaR Object
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
Set 'TrustRegionCP'
as the Solver Type and Change the Main Solver Option for a PortfolioCVaR Object
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
Set Solver Type as 'fmincon' for a PortfolioMAD Object
Use 'fmincon'
as the solverType
.
p = PortfolioMAD;
p = setSolver(p, 'fmincon');
display(p.solverType);
fmincon
Set the Solver Type as 'fmincon' and Use Name-Value Pair Arguments to Set the Algorithm for a PortfolioMAD Object
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
Set Solver Type as 'fmincon' and Use an optimoptions Structure to Set the Algorithm for a PortfolioMAD Object
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
Set the Solver Type as 'fmincon' and Use an optimoptions Structure to Set the Algorithm and Use of Gradients for a PortfolioMAD Object
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
obj
— Object for portfolio
object
Object for portfolio, specified using Portfolio
,
PortfolioCVaR
, or PortfolioMAD
object. For more
information on creating a portfolio object, see
Data Types: object
solverType
— Solver to use for portfolio optimization
character vector | string
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'
The default algorithm for
'fmincon'
is'sqp'
. For more information about'fmincon'
name-value options, see Portfolio Object Name-Value Arguments.
'quadprog'
The default algorithm for
'quadprog'
isinterior-point-convex
. For more information about'quadprog'
name-value options, see Portfolio Object Name-Value Arguments.
Using PortfolioCVaR Object
For a PortfolioCVaR
object, the supported
solverType
are:
'TrustRegionCP'
(Default)'TrustRegionCP'
is an implementation of Kelley's [1] cutting-plane method for convex optimization. For more information about'TrustRegionCP'
name-value options, see Name-Value Arguments for 'TrustRegionCP' and 'ExtendedCP'.
'ExtendedCP'
'ExtendedCP'
is an implementation of Kelley's [1] cutting-plane method for convex optimization. For more information about'ExtendedCP'
name-value options, see Name-Value Arguments for 'TrustRegionCP' and 'ExtendedCP'.
'fmincon'
The default algorithm for
'fmincon'
is'sqp'
. For more information about'fmincon'
name-value options, see PortfolioCVaR Object Name-Value Arguments.
'cuttingplane'
The
'cuttingplane'
solver is an implementation of Kelley's [1] cutting-plane method for convex optimization with name-value arguments for'MaxIter'
,'Abstol'
,'Reltol'
and'MainSolverOptions'
. For more information about'cuttingplane'
name-value options, see PortfolioCVaR Object Name-Value Arguments.
Using PortfolioMAD Object
For a PortfolioMAD
object, the supported
solverType
are:
'TrustRegionCP'
(Default)'TrustRegionCP'
is an implementation of Kelley's [1] cutting-plane method for convex optimization. For more information about'TrustRegionCP'
name-value options, see Name-Value Arguments for 'TrustRegionCP' and 'ExtendedCP'.
'ExtendedCP'
'ExtendedCP'
is an implementation of Kelley's [1] cutting-plane method for convex optimization. For more information about'ExtendedCP'
name-value options, see Name-Value Arguments for 'TrustRegionCP' and 'ExtendedCP'.
'fmincon'
The default algorithm for
'fmincon'
is the'sqp'
algorithm and'GradObj'
set to'on'
. For more information about'fmincon'
name-value options, see PortfolioMAD Object Name-Value Arguments.
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
optimoptions
— optimoptions
object
object
(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 ArgumentsFor a
Portfolio
object using asolverType
oflcprog
, choose a name-value value in this table.Value Description 'maxiter'
Maximum number of iterations,specified as the comma-separated pair consisting of
'MaxIter'
and a positive integer. The default value is1 + n
3, 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 is1.0e-9
.For a
Portfolio
object using asolverType
offmincon
, see options to choose name-value arguments.For a
Portfolio
object using asolverType
oflinprog
, see options to choose name-value arguments.For a
Portfolio
object using asolverType
ofquadprog
, see options to choose name-value arguments.
PortfolioCVaR
Object Name-Value ArgumentsFor a
PortfolioCVaR
object using asolverType
offmincon
, see options to choose name-value arguments.For a
PorfolioCVaR
object using asolverType
of'TrustRegionCP'
or'ExtendedCP'
, see Name-Value Arguments for 'TrustRegionCP' and 'ExtendedCP' to choose name-value arguments.For a
PorfolioCVaR
object using asolverType
of'cuttingplane'
, choose a name-value value in this table.Value Description 'MaxIter'
Maximum number of iterations, specified as the comma-separated pair consisting of
'MaxIter'
and a positive integer. The default value is1000
.'AbsTol'
Absolute stopping tolerance, specified as the comma-separated pair consisting of
'AbsTol'
and a positive scalar. The default value is1e6
.'RelTol'
Relative stopping tolerance, specified as the comma-separated pair consisting of
'RelTol'
and a positive scalar. The default value is1e5
.'MainSolverOptions'
Options for the main solver
linprog
, specified as the comma-separated pair consisting of'MainSolverOptions'
and anoptimoptions
object. The default isoptimoptions('linprog','Algorithm','dual-simplex-highs','Display','off')
.For a
PortfolioCVaR
object using asolverType
oflinprog
, see options to choose name-value arguments.
PortfolioMAD
Object Name-Value ArgumentsFor a
PortfolioMAD
object using asolverType
offmincon
, see options to choose name-value arguments.For a
PorfolioMAD
object using asolverType
of'TrustRegionCP'
or'ExtendedCP'
, see Name-Value Arguments for 'TrustRegionCP' and 'ExtendedCP' to choose name-value arguments.For a
PortfolioMAD
object using asolverType
oflinprog
, see options to choose name-value arguments.
'TrustRegionCP'
and
'ExtendedCP'
For a PortfolioCVaR
or PortfolioMAD
object using a
solverType
of 'TrustRegionCP'
or
'ExtendedCP'
, choose a name-value pair in this table.
Value | Description |
---|---|
'MaxIterations' | Maximum number of iterations, specified as the comma-separated pair
consisting of |
'NonlinearScalingFactor' | Scales the nonlinear function and its gradient by a factor, specified
as the comma-separated pair consisting of
|
'ObjectiveScalingFactor' | Scales the objective function by a factor, specified as the
comma-separated pair consisting of |
'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:
|
'CutGeneration' | Method to add the cut, specified as the comma-separated pair consisting of
'CutGeneration' and a supported value of:
|
'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 If you modify |
'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
obj
— Updated portfolio object
object for portfolio
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.
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.
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 R2011aR2024a: Default for name-value argument MainSolverOptions
is changed
The default for the name-value argument MainSolverOptions
is
changed to
optimoptions('linprog','Algorithm','dual-simplex-highs','Display','off')
.
R2023a: Added name-value argument 'DeltaLowerBound'
for solverType
of 'TrustRegionCP'
When using a solverType
of 'TrustRegionCP'
, you can
use the name-value argument 'DeltaLowerBound'
to set the lower bound for the
trust-region radius.
R2023a: Renamed 'MasterSolverOptions'
name-value argument to 'MainSolverOptions'
The 'MasterSolverOptions'
name-value
argument is renamed to 'MainSolverOptions'
. The use of
'MasterSolverOptions'
name-value argument
is discouraged.
See Also
getOneWayTurnover
| setTurnover
| setInitPort
| setCosts
| setSolverMINLP
Topics
- Working with One-Way Turnover Constraints Using Portfolio Object
- Working with One-Way Turnover Constraints Using PortfolioCVaR Object
- Working with One-Way Turnover Constraints Using PortfolioMAD Object
- Portfolio Optimization Examples Using Financial Toolbox
- Supported Constraints for Portfolio Optimization Using Portfolio Objects
- Supported Constraints for Portfolio Optimization Using PortfolioCVaR Object
- Supported Constraints for Portfolio Optimization Using PortfolioMAD Object
- Choosing and Controlling the Solver for Mean-Variance Portfolio Optimization
- Choosing and Controlling the Solver for PortfolioCVaR Optimizations
- Choosing and Controlling the Solver for PortfolioMAD Optimizations
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)