# Advanced tour of the Bayesian Optimization package

    [1]:

    from bayes_opt import BayesianOptimization

## 1. Suggest-Evaluate-Register Paradigm

Internally the `maximize` method is simply a wrapper around the methods `suggest`, `probe`, and `register`. If you need more control over your optimization loops the Suggest-Evaluate-Register paradigm should give you that extra flexibility.

For an example of running the `BayesianOptimization` in a distributed fashion (where the function being optimized is evaluated concurrently in different cores/machines/servers), checkout the `async_optimization.py` script in the examples folder.

    [2]:

    # Let's start by defining our function, bounds, and instantiating an optimization object.
    def black_box_function(x, y):
        return -x ** 2 - (y - 1) ** 2 + 1

One extra ingredient we will need is an `AcquisitionFunction`, such as `acquisition.UpperConfidenceBound`. In case it is not clear why, take a look at the literature to understand better how this method works.

    [3]:

    from bayes_opt import acquisition

    acq = acquisition.UpperConfidenceBound(kappa=2.5)

Notice that the evaluation of the blackbox function will NOT be carried out by the optimizer object. We are simulating a situation where this function could be being executed in a different machine, maybe it is written in another language, or it could even be the result of a chemistry experiment. Whatever the case may be, you can take charge of it and as long as you don’t invoke the `probe` or `maximize` methods directly, the optimizer object will ignore the blackbox function.

    [4]:

    optimizer = BayesianOptimization(
        f=None,
        acquisition_function=acq,
        pbounds={'x': (-2, 2), 'y': (-3, 3)},
        verbose=2,
        random_state=1,
    )

The `suggest` method of our optimizer can be called at any time. What you get back is a suggestion for the next parameter combination the optimizer wants to probe.

Notice that while the optimizer hasn’t observed any points, the suggestions will be random. However, they will stop being random and improve in quality the more points are observed.

    [5]:

    next_point_to_probe = optimizer.suggest()
    print("Next point to probe is:", next_point_to_probe)

    Next point to probe is: {'x': -0.331911981189704, 'y': 1.3219469606529486}

You are now free to evaluate your function at the suggested point however/whenever you like.

    [6]:

    target = black_box_function(**next_point_to_probe)
    print("Found the target value to be:", target)

    Found the target value to be: 0.7861845912690544

Last thing left to do is to tell the optimizer what target value was observed.

    [7]:

    optimizer.register(
        params=next_point_to_probe,
        target=target,
    )

### 1.1 The maximize loop

And that’s it. By repeating the steps above you recreate the internals of the `maximize` method. This should give you all the flexibility you need to log progress, hault execution, perform concurrent evaluations, etc.

    [8]:

    for _ in range(5):
        next_point = optimizer.suggest()
        target = black_box_function(**next_point)
        optimizer.register(params=next_point, target=target)

        print(target, next_point)
    print(optimizer.max)

    -18.503835804889988 {'x': 1.953072105336, 'y': -2.9609778030491904}
    -1.0819533157901717 {'x': 0.22703572807626315, 'y': 2.4249238905875123}
    -6.50219704520679 {'x': -1.9991881984624875, 'y': 2.872282989383577}
    -5.747604713731052 {'x': -1.994467585936897, 'y': -0.664242699361514}
    -2.9682431497650823 {'x': 1.9737252084307952, 'y': 1.269540259274744}
    {'target': 0.7861845912690544, 'params': {'x': -0.331911981189704, 'y': 1.3219469606529486}}

## 2. Dealing with discrete parameters

There is no principled way of dealing with discrete parameters using this package.

Ok, now that we got that out of the way, how do you do it? You’re bound to be in a situation where some of your function’s parameters may only take on discrete values. Unfortunately, the nature of bayesian optimization with gaussian processes doesn’t allow for an easy/intuitive way of dealing with discrete parameters - but that doesn’t mean it is impossible. The example below showcases a simple, yet reasonably adequate, way to dealing with discrete parameters.

    [9]:

    def func_with_discrete_params(x, y, d):
        # Simulate necessity of having d being discrete.
        assert type(d) == int

        return ((x + y + d) // (1 + d)) / (1 + (x + y) ** 2)

    [10]:

    def function_to_be_optimized(x, y, w):
        d = int(w)
        return func_with_discrete_params(x, y, d)

    [11]:

    optimizer = BayesianOptimization(
        f=function_to_be_optimized,
        pbounds={'x': (-10, 10), 'y': (-10, 10), 'w': (0, 5)},
        verbose=2,
        random_state=1,
    )

    [12]:

    optimizer.set_gp_params(alpha=1e-3)
    optimizer.maximize()

    |   iter    |  target   |     w     |     x     |     y     |
    -------------------------------------------------------------
    | 1         | -0.06199  | 2.085     | 4.406     | -9.998    |
    | 2         | -0.0344   | 1.512     | -7.065    | -8.153    |
    | 3         | -0.2177   | 0.9313    | -3.089    | -2.065    |
    | 4         | 0.1865    | 2.694     | -1.616    | 3.704     |
    | 5         | -0.2187   | 1.022     | 7.562     | -9.452    |
    | 6         | 0.2488    | 2.684     | -2.188    | 3.925     |
    | 7         | 0.2948    | 2.683     | -2.534    | 4.08      |
    | 8         | 0.3202    | 2.514     | -3.83     | 5.287     |
    | 9         | 0.0       | 4.057     | -4.458    | 3.928     |
    | 10        | 0.4802    | 2.296     | -3.518    | 4.558     |
    | 11        | 0.0       | 1.084     | -3.737    | 4.472     |
    | 12        | 0.0       | 2.649     | -3.861    | 4.353     |
    | 13        | 0.0       | 2.442     | -3.658    | 4.599     |
    | 14        | -0.05801  | 1.935     | -0.4758   | -8.755    |
    | 15        | 0.0       | 2.337     | 7.973     | -8.96     |
    | 16        | 0.07699   | 0.6926    | 5.59      | 6.854     |
    | 17        | -0.02025  | 3.534     | -8.943    | 1.987     |
    | 18        | 0.0       | 2.59      | -7.339    | 5.941     |
    | 19        | 0.0929    | 2.237     | -4.535    | 9.065     |
    | 20        | 0.1538    | 0.477     | 2.931     | 2.683     |
    | 21        | 0.0       | 0.9999    | 4.397     | -3.971    |
    | 22        | -0.01894  | 3.764     | -7.043    | -3.184    |
    | 23        | 0.03683   | 1.851     | 5.783     | 7.966     |
    | 24        | -0.04359  | 1.615     | -5.133    | -6.556    |
    | 25        | 0.02617   | 3.863     | 0.1052    | 8.579     |
    | 26        | -0.1071   | 0.8131    | -0.7949   | -9.292    |
    | 27        | 0.0       | 4.969     | 8.778     | -8.467    |
    | 28        | -0.1372   | 0.9475    | -1.019    | -7.018    |
    | 29        | 0.08078   | 1.917     | -0.2606   | 6.272     |
    | 30        | 0.02003   | 4.278     | 3.8       | 8.398     |
    =============================================================

## 3. Tuning the underlying Gaussian Process

The bayesian optimization algorithm works by performing a gaussian process regression of the observed combination of parameters and their associated target values. The predicted parameter \(\rightarrow\) target hyper-surface (and its uncertainty) is then used to guide the next best point to probe.

### 3.1 Passing parameter to the GP

Depending on the problem it could be beneficial to change the default parameters of the underlying GP. You can use the `optimizer.set_gp_params` method to do this:

    [13]:

    optimizer = BayesianOptimization(
        f=black_box_function,
        pbounds={'x': (-2, 2), 'y': (-3, 3)},
        verbose=2,
        random_state=1,
    )
    optimizer.set_gp_params(alpha=1e-3, n_restarts_optimizer=5)
    optimizer.maximize(
        init_points=1,
        n_iter=5
    )

    |   iter    |  target   |     x     |     y     |
    -------------------------------------------------
    | 1         | 0.7862    | -0.3319   | 1.322     |
    | 2         | -18.19    | 1.957     | -2.919    |
    | 3         | -12.05    | -1.969    | -2.029    |
    | 4         | -7.463    | 0.6032    | -1.846    |
    | 5         | -1.093    | 1.444     | 1.096     |
    | 6         | 0.8586    | -0.2165   | 1.307     |
    =================================================

### 3.2 Tuning the `alpha` parameter

When dealing with functions with discrete parameters,or particularly erratic target space it might be beneficial to increase the value of the `alpha` parameter. This parameters controls how much noise the GP can handle, so increase it whenever you think that extra flexibility is needed.

### 3.3 Changing kernels

By default this package uses the Matern 2.5 kernel. Depending on your use case you may find that tuning the GP kernel could be beneficial. You’re on your own here since these are very specific solutions to very specific problems. You should start with the scikit learn docs.

## Observers Continued

Observers are objects that subscribe and listen to particular events fired by the `BayesianOptimization` object.

When an event gets fired a callback function is called with the event and the `BayesianOptimization` instance passed as parameters. The callback can be specified at the time of subscription. If none is given it will look for an `update` method from the observer.

    [14]:

    from bayes_opt.event import DEFAULT_EVENTS, Events

    [15]:

    optimizer = BayesianOptimization(
        f=black_box_function,
        pbounds={'x': (-2, 2), 'y': (-3, 3)},
        verbose=2,
        random_state=1,
    )

    [16]:

    class BasicObserver:
        def update(self, event, instance):
            """Does whatever you want with the event and `BayesianOptimization` instance."""
            print("Event `{}` was observed".format(event))

    [17]:

    my_observer = BasicObserver()

    optimizer.subscribe(
        event=Events.OPTIMIZATION_STEP,
        subscriber=my_observer,
        callback=None, # Will use the `update` method as callback
    )

Alternatively you have the option to pass a completely different callback.

    [18]:

    def my_callback(event, instance):
        print("Go nuts here!")

    optimizer.subscribe(
        event=Events.OPTIMIZATION_START,
        subscriber="Any hashable object",
        callback=my_callback,
    )

    [19]:

    optimizer.maximize(init_points=1, n_iter=2)

    Go nuts here!
    Event `optimization:step` was observed
    Event `optimization:step` was observed
    Event `optimization:step` was observed

For a list of all default events you can checkout `DEFAULT_EVENTS`

    [20]:

    DEFAULT_EVENTS

    [20]:

    ['optimization:start', 'optimization:step', 'optimization:end']