## Variables for a Bayesian Optimization

### Syntax for Creating Optimization Variables

For each variable in your objective function, create a variable description object using `optimizableVariable`. Each variable has a unique name and a range of values. The minimal syntax for variable creation is

`variable = optimizableVariable(Name,Range)`

This function creates a real variable that ranges from the lower bound `Range(1)` to the upper bound `Range(2)`.

You can specify three types of variables in the `Type` name-value pair:

• `'real'` — Continuous real values between finite bounds. Give `Range` as the two-element vector `[lower upper]`, which represent the lower and upper bounds.

• `'integer'` — Integer values between finite bounds, similar to `'real'`.

• `'categorical'` — Cell array of names of possible values, such as `{'red','green','blue'}`, that you specify in the `Range` argument.

For `'real'` or `'integer'` variables, you can specify that `bayesopt` searches in a log-scaled space by setting the `Transform` name-value pair to `'log'`. For this transformation, ensure that the lower bound in the `Range` is strictly positive.

Include variables for `bayesopt` as a vector in the second argument.

`results = bayesopt(fun,[xvar,ivar,rvar])`

To exclude a variable from an optimization, set `Optimize` to `false`, either in the name-value pair of `optimizableVariable`, or by dot notation:

`xvar.Optimize = false;`

### Tip

• There are two names associated with an `optimizableVariable`:

• The MATLAB® workspace variable name

• The name of the variable in the optimization

For example,

`xvar = optimizableVariable('spacevar',[1,100]);`

`xvar` is the MATLAB workspace variable, and `'spacevar'` is the variable in the optimization.

Use these names as follows:

• Use `xvar` as an element in the vector of variables you pass to `bayesopt`. For example,

`results = bayesopt(fun,[xvar,tvar])`
• Use `'spacevar'` as the name of the variable in the optimization. For example, in an objective function,

```function objective = mysvmfun(x,cdata,grp) SVMModel = fitcsvm(cdata,grp,'KernelFunction','rbf',... 'BoxConstraint',x.spacevar,... 'KernelScale',x.tvar); objective = kfoldLoss(crossval(SVMModel));```

### Variables for Optimization Examples

Real variable from 0 to 1:

`var1 = optimizableVariable('xvar',[0 1])`
```var1 = optimizableVariable with properties: Name: 'xvar' Range: [0 1] Type: 'real' Transform: 'none' Optimize: 1 ```

Integer variable from 1 to 1000 on a log scale:

`var2 = optimizableVariable('ivar',[1 1000],'Type','integer','Transform','log')`
```var2 = optimizableVariable with properties: Name: 'ivar' Range: [1 1000] Type: 'integer' Transform: 'log' Optimize: 1 ```

Categorical variable of rainbow colors:

`var3 = optimizableVariable('rvar',{'r' 'o' 'y' 'g' 'b' 'i' 'v'},'Type','categorical')`
```var3 = optimizableVariable with properties: Name: 'rvar' Range: {'r' 'o' 'y' 'g' 'b' 'i' 'v'} Type: 'categorical' Transform: 'none' Optimize: 1 ```