BayesianOptimization
Bayesian optimization results
Description
A BayesianOptimization
object contains the
results of a Bayesian optimization. It is the output of bayesopt
or a fit function that accepts the
OptimizeHyperparameters
namevalue pair such as fitcdiscr
. In addition, a BayesianOptimization
object contains data for each iteration of
bayesopt
that can be accessed by a plot function or an output
function.
Creation
Create a BayesianOptimization
object by using the
bayesopt
function or one of the following
fit functions with the OptimizeHyperparameters
namevalue
argument.
Classification fit functions:
fitcdiscr
,fitcecoc
,fitcensemble
,fitcgam
,fitckernel
,fitcknn
,fitclinear
,fitcnb
,fitcnet
,fitcsvm
,fitctree
Regression fit functions:
fitrensemble
,fitrgam
,fitrgp
,fitrkernel
,fitrlinear
,fitrnet
,fitrsvm
,fitrtree
Properties
Problem Definition Properties
ObjectiveFcn
— ObjectiveFcn
argument used by
bayesopt
function handle
This property is readonly.
ObjectiveFcn
argument used by
bayesopt
, specified as a function handle.
If you call
bayesopt
directly,ObjectiveFcn
is thebayesopt
objective function argument.If you call a fit function containing the
'OptimizeHyperparameters'
namevalue pair argument,ObjectiveFcn
is a function handle that returns the misclassification rate for classification or returns the logarithm of one plus the crossvalidation loss for regression, measured by fivefold crossvalidation.
Data Types: function_handle
VariableDescriptions
— VariableDescriptions
argument that bayesopt
used
vector of optimizableVariable
objects
This property is readonly.
VariableDescriptions
argument that
bayesopt
used, specified as a vector of optimizableVariable
objects.
If you called
bayesopt
directly,VariableDescriptions
is thebayesopt
variable description argument.If you called a fit function with the
OptimizeHyperparameters
namevalue pair,VariableDescriptions
is the vector of hyperparameters.
Options
— Options that bayesopt
used
structure
This property is readonly.
Options that bayesopt
used, specified as a
structure.
If you called
bayesopt
directly,Options
is the options used inbayesopt
, which are the namevalue pairs Seebayesopt
Input Arguments.If you called a fit function with the
OptimizeHyperparameters
namevalue pair,Options
are the defaultbayesopt
options, modified by theHyperparameterOptimizationOptions
namevalue pair.
Options
is a readonly structure containing the
following fields.
Option Name  Meaning 

AcquisitionFunctionName  Acquisition function name. See Acquisition Function Types. 
IsObjectiveDeterministic  true means the objective function
is deterministic, false
otherwise. 
ExplorationRatio  Used only when
AcquisitionFunctionName is
'expectedimprovementplus' or
'expectedimprovementpersecondplus' .
See Plus. 
MaxObjectiveEvaluations  Objective function evaluation limit. 
MaxTime  Time limit. 
XConstraintFcn  Deterministic constraints on variables. See Deterministic Constraints — XConstraintFcn. 
ConditionalVariableFcn  Conditional variable constraints. See Conditional Constraints — ConditionalVariableFcn. 
NumCoupledConstraints  Number of coupled constraints. See Coupled Constraints. 
CoupledConstraintTolerances  Coupled constraint tolerances. See Coupled Constraints. 
AreCoupledConstraintsDeterministic  Logical vector specifying whether each coupled constraint is deterministic. 
Verbose  Commandline display level. 
OutputFcn  Function called after each iteration. See Bayesian Optimization Output Functions. 
SaveVariableName  Variable name for the
@assignInBase output function.

SaveFileName  File name for the @saveToFile
output function. 
PlotFcn  Plot function called after each iteration. See Bayesian Optimization Plot Functions 
InitialX  Points where bayesopt evaluated
the objective function. 
InitialObjective  Objective function values at
InitialX . 
InitialConstraintViolations  Coupled constraint function values at
InitialX . 
InitialErrorValues  Error values at InitialX . 
InitialObjectiveEvaluationTimes  Objective function evaluation times at
InitialX . 
InitialIterationTimes  Time for each iteration, including objective function evaluation and other computations. 
Data Types: struct
Solution Properties
MinObjective
— Minimum observed value of objective function
real scalar
This property is readonly.
Minimum observed value of objective function, specified as a real scalar. When there are coupled constraints or evaluation errors, this value is the minimum over all observed points that are feasible according to the final constraint and Error models.
Data Types: double
XAtMinObjective
— Observed point with minimum objective function value
1
byD
table
This property is readonly.
Observed point with minimum objective function value, specified as a
1
byD
table, where
D
is the number of variables.
Data Types: table
MinEstimatedObjective
— Estimated objective function value
real scalar
This property is readonly.
Estimated objective function value at
XAtMinEstimatedObjective
, specified as a real
scalar.
MinEstimatedObjective
is the mean value of the
posterior distribution of the final objective model. The software
estimates the MinEstimatedObjective
value by
passing XAtMinEstimatedObjective
to the object
function predictObjective
.
Data Types: double
XAtMinEstimatedObjective
— Point with minimum upper confidence bound of objective function value
1
byD
table
This property is readonly.
Point with the minimum upper confidence bound of the objective
function value among the visited points, specified as a
1
byD
table, where
D
is the number of variables. The software uses
the final objective model to find the upper confidence bounds of the
visited points.
XAtMinEstimatedObjective
is the same as the best
point returned by the bestPoint
function with the
default criterion
('minvisitedupperconfidenceinterval'
).
Data Types: table
NumObjectiveEvaluations
— Number of objective function evaluations
positive integer
This property is readonly.
Number of objective function evaluations, specified as a positive integer. This includes the initial evaluations to form a posterior model as well as evaluation during the optimization iterations.
Data Types: double
TotalElapsedTime
— Total elapsed time of optimization in seconds
positive scalar
This property is readonly.
Total elapsed time of optimization in seconds, specified as a positive scalar.
Data Types: double
NextPoint
— Next point to evaluate if optimization continues
1
byD
table
This property is readonly.
Next point to evaluate if optimization continues, specified as a
1
byD
table, where
D
is the number of variables.
Data Types: table
Trace Properties
XTrace
— Points where the objective function was evaluated
T
byD
table
This property is readonly.
Points where the objective function was evaluated, specified as a
T
byD
table, where
T
is the number of evaluation points and
D
is the number of variables.
Data Types: table
ObjectiveTrace
— Objective function values
column vector of length T
This property is readonly.
Objective function values, specified as a column vector of length
T
, where T
is the number of
evaluation points. ObjectiveTrace
contains the
history of objective function evaluations.
Data Types: double
ObjectiveEvaluationTimeTrace
— Objective function evaluation times
column vector of length T
This property is readonly.
Objective function evaluation times, specified as a column vector of
length T
, where T
is the number of
evaluation points. ObjectiveEvaluationTimeTrace
includes the time in evaluating coupled constraints, because the
objective function computes these constraints.
Data Types: double
IterationTimeTrace
— Iteration times
column vector of length T
This property is readonly.
Iteration times, specified as a column vector of length
T
, where T
is the number of
evaluation points. IterationTimeTrace
includes both
objective function evaluation time and other overhead.
Data Types: double
ConstraintsTrace
— Coupled constraint values
T
byK
array
This property is readonly.
Coupled constraint values, specified as a
T
byK
array, where
T
is the number of evaluation points and
K
is the number of coupled constraints.
Data Types: double
ErrorTrace
— Error indications
column vector of length T
of 1
or 1
entries
This property is readonly.
Error indications, specified as a column vector of length
T
of 1
or
1
entries, where T
is the
number of evaluation points. Each 1
entry indicates
that the objective function errored or returned NaN
on the corresponding point in XTrace
. Each
1
entry indicates that the objective function
value was computed.
Data Types: double
FeasibilityTrace
— Feasibility indications
logical column vector of length T
This property is readonly.
Feasibility indications, specified as a logical column vector of
length T
, where T
is the number of
evaluation points. Each 1
entry indicates that the
final constraint model predicts feasibility at the corresponding point
in XTrace
.
Data Types: logical
FeasibilityProbabilityTrace
— Probability that evaluation point is feasible
column vector of length T
This property is readonly.
Probability that evaluation point is feasible, specified as a column
vector of length T
, where T
is the
number of evaluation points. The probabilities come from the final
constraint model, including the error constraint model, on the
corresponding points in XTrace
.
Data Types: double
IndexOfMinimumTrace
— Which evaluation gave minimum feasible objective
column vector of integer indices of length
T
This property is readonly.
Which evaluation gave minimum feasible objective, specified as a
column vector of integer indices of length T
, where
T
is the number of evaluation points. Feasibility
is determined with respect to the constraint models that existed at each
iteration, including the error constraint model.
Data Types: double
ObjectiveMinimumTrace
— Minimum observed objective
column vector of length T
This property is readonly.
Minimum observed objective, specified as a column vector of length
T
, where T
is the number of
evaluation points.
Data Types: double
EstimatedObjectiveMinimumTrace
— Estimated objective
column vector of length T
This property is readonly.
Estimated objective, specified as a column vector of length
T
, where T
is the number of
evaluation points. The estimated objective at each iteration is
determined with respect to the objective model at that iteration. At
each iteration, the software uses the object function predictObjective
to
estimate the objective function value at the point with the minimum
upper confidence bound of the objective function among the visited
points.
Data Types: double
UserDataTrace
— Auxiliary data from the objective function
cell array of length T
This property is readonly.
Auxiliary data from the objective function, specified as a cell array
of length T
, where T
is the number
of evaluation points. Each entry in the cell array is the
UserData
returned in the third output of the
objective function.
Data Types: cell
Object Functions
bestPoint  Best point in a Bayesian optimization according to a criterion 
plot  Plot Bayesian optimization results 
predictConstraints  Predict coupled constraint violations at a set of points 
predictError  Predict error value at a set of points 
predictObjective  Predict objective function at a set of points 
predictObjectiveEvaluationTime  Predict objective function run times at a set of points 
resume  Resume a Bayesian optimization 
Examples
Create a BayesianOptimization
Object Using bayesopt
This example shows how to create a BayesianOptimization
object by using bayesopt
to minimize crossvalidation loss.
Optimize hyperparameters of a KNN classifier for the ionosphere
data, that is, find KNN hyperparameters that minimize the crossvalidation loss. Have bayesopt
minimize over the following hyperparameters:
Nearestneighborhood sizes from 1 to 30
Distance functions
'chebychev'
,'euclidean'
, and'minkowski'
.
For reproducibility, set the random seed, set the partition, and set the AcquisitionFunctionName
option to 'expectedimprovementplus'
. To suppress iterative display, set 'Verbose'
to 0
. Pass the partition c
and fitting data X
and Y
to the objective function fun
by creating fun
as an anonymous function that incorporates this data. See Parameterizing Functions.
load ionosphere rng default num = optimizableVariable('n',[1,30],'Type','integer'); dst = optimizableVariable('dst',{'chebychev','euclidean','minkowski'},'Type','categorical'); c = cvpartition(351,'Kfold',5); fun = @(x)kfoldLoss(fitcknn(X,Y,'CVPartition',c,'NumNeighbors',x.n,... 'Distance',char(x.dst),'NSMethod','exhaustive')); results = bayesopt(fun,[num,dst],'Verbose',0,... 'AcquisitionFunctionName','expectedimprovementplus')
results = BayesianOptimization with properties: ObjectiveFcn: [function_handle] VariableDescriptions: [1x2 optimizableVariable] Options: [1x1 struct] MinObjective: 0.1197 XAtMinObjective: [1x2 table] MinEstimatedObjective: 0.1213 XAtMinEstimatedObjective: [1x2 table] NumObjectiveEvaluations: 30 TotalElapsedTime: 35.9996 NextPoint: [1x2 table] XTrace: [30x2 table] ObjectiveTrace: [30x1 double] ConstraintsTrace: [] UserDataTrace: {30x1 cell} ObjectiveEvaluationTimeTrace: [30x1 double] IterationTimeTrace: [30x1 double] ErrorTrace: [30x1 double] FeasibilityTrace: [30x1 logical] FeasibilityProbabilityTrace: [30x1 double] IndexOfMinimumTrace: [30x1 double] ObjectiveMinimumTrace: [30x1 double] EstimatedObjectiveMinimumTrace: [30x1 double]
Create a BayesianOptimization
Object Using a Fit Function
This example shows how to minimize the crossvalidation loss in the ionosphere
data using Bayesian optimization of an SVM classifier.
Load the data.
load ionosphere
Optimize the classification using the 'auto'
parameters.
rng default % For reproducibility Mdl = fitcsvm(X,Y,'OptimizeHyperparameters','auto')
=====================================================================================================  Iter  Eval  Objective  Objective  BestSoFar  BestSoFar  BoxConstraint KernelScale    result   runtime  (observed)  (estim.)    =====================================================================================================  1  Best  0.22222  10.656  0.22222  0.22222  64.836  0.0015729   2  Accept  0.35897  0.11083  0.22222  0.23074  0.036335  5.5755   3  Best  0.13105  4.2092  0.13105  0.14145  0.0022147  0.0023957   4  Accept  0.35897  0.16269  0.13105  0.13108  5.1259  98.62   5  Best  0.12821  0.29221  0.12821  0.12823  0.0010294  0.03159   6  Accept  0.12821  0.17544  0.12821  0.12682  0.0010607  0.012967   7  Accept  0.1339  1.0424  0.12821  0.12548  0.020096  0.014191   8  Best  0.12536  0.22216  0.12536  0.1265  0.0010012  0.014054   9  Accept  0.12536  0.38955  0.12536  0.12575  0.0010018  0.019314   10  Best  0.12251  0.40516  0.12251  0.12486  0.0010611  0.01853   11  Accept  0.14245  0.39029  0.12251  0.12368  0.0011014  0.00773   12  Accept  0.15385  10.118  0.12251  0.12375  979.53  0.11762   13  Accept  0.14245  0.54742  0.12251  0.12384  0.0010117  0.15318   14  Accept  0.12821  0.27639  0.12251  0.12388  0.022074  0.070395   15  Accept  0.1339  8.2951  0.12251  0.12388  0.00101  0.0010937   16  Accept  0.13105  0.93374  0.12251  0.12389  0.10385  0.042002   17  Accept  0.12251  0.14206  0.12251  0.12348  0.0010552  0.018969   18  Accept  0.12536  0.11093  0.12251  0.12391  0.0010088  0.019316   19  Accept  0.35897  0.30367  0.12251  0.12391  0.098754  23.156   20  Accept  0.35897  0.14523  0.12251  0.12392  7.9637  712.73  =====================================================================================================  Iter  Eval  Objective  Objective  BestSoFar  BestSoFar  BoxConstraint KernelScale    result   runtime  (observed)  (estim.)    =====================================================================================================  21  Accept  0.1453  0.14604  0.12251  0.12386  0.045895  1.2044   22  Accept  0.12821  0.18017  0.12251  0.12386  0.0776  0.57356   23  Accept  0.20513  0.12925  0.12251  0.12368  0.0033578  0.73435   24  Accept  0.12536  0.14011  0.12251  0.12364  1.5422  0.88903   25  Accept  0.12821  0.20119  0.12251  0.12366  1.1445  0.41251   26  Accept  0.12536  0.24591  0.12251  0.12363  0.27465  0.80507   27  Accept  0.13105  0.51799  0.12251  0.12366  30.203  0.83308   28  Accept  0.12536  0.23615  0.12251  0.12369  0.080054  0.20338   29  Accept  0.1339  0.539  0.12251  0.12394  0.0046287  0.024706   30  Accept  0.1396  0.2168  0.12251  0.12396  0.35132  0.12849 
__________________________________________________________ Optimization completed. MaxObjectiveEvaluations of 30 reached. Total function evaluations: 30 Total elapsed time: 68.495 seconds Total objective function evaluation time: 41.4809 Best observed feasible point: BoxConstraint KernelScale _____________ ___________ 0.0010611 0.01853 Observed objective function value = 0.12251 Estimated objective function value = 0.12396 Function evaluation time = 0.40516 Best estimated feasible point (according to models): BoxConstraint KernelScale _____________ ___________ 0.0010611 0.01853 Estimated objective function value = 0.12396 Estimated function evaluation time = 0.23913
Mdl = ClassificationSVM ResponseName: 'Y' CategoricalPredictors: [] ClassNames: {'b' 'g'} ScoreTransform: 'none' NumObservations: 351 HyperparameterOptimizationResults: [1x1 BayesianOptimization] Alpha: [92x1 double] Bias: 5.6332 KernelParameters: [1x1 struct] BoxConstraints: [351x1 double] ConvergenceInfo: [1x1 struct] IsSupportVector: [351x1 logical] Solver: 'SMO' Properties, Methods
The fit achieved about 12% loss for the default 5fold cross validation.
Examine the BayesianOptimization
object that is returned in the HyperparameterOptimizationResults
property of the returned model.
disp(Mdl.HyperparameterOptimizationResults)
BayesianOptimization with properties: ObjectiveFcn: @createObjFcn/inMemoryObjFcn VariableDescriptions: [5x1 optimizableVariable] Options: [1x1 struct] MinObjective: 0.1225 XAtMinObjective: [1x2 table] MinEstimatedObjective: 0.1240 XAtMinEstimatedObjective: [1x2 table] NumObjectiveEvaluations: 30 TotalElapsedTime: 68.4950 NextPoint: [1x2 table] XTrace: [30x2 table] ObjectiveTrace: [30x1 double] ConstraintsTrace: [] UserDataTrace: {30x1 cell} ObjectiveEvaluationTimeTrace: [30x1 double] IterationTimeTrace: [30x1 double] ErrorTrace: [30x1 double] FeasibilityTrace: [30x1 logical] FeasibilityProbabilityTrace: [30x1 double] IndexOfMinimumTrace: [30x1 double] ObjectiveMinimumTrace: [30x1 double] EstimatedObjectiveMinimumTrace: [30x1 double]
Version History
Introduced in R2016b
Open Example
You have a modified version of this example. Do you want to open this example with your edits?
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)