Skip to content

Prediction for the last point incorrect #550

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dcferre opened this issue Mar 8, 2025 · 3 comments
Closed

Prediction for the last point incorrect #550

dcferre opened this issue Mar 8, 2025 · 3 comments

Comments

@dcferre
Copy link

dcferre commented Mar 8, 2025

Hello,

I am finding a recurrent issue with the predictions of the optimizer for the last point that was added to the dataset.

I am using the following chunk of code to initialize a BayesianOptimization instance:

Bounded region of parameter space

pbounds = {'x': (-20, 20)}

Bayesian optimization with upper confidence bound acquisition function

ucb = acquisition.UpperConfidenceBound(kappa=2.5)
bo = BayesianOptimization(
f=None,
acquisition_function = ucb,
pbounds=pbounds,
verbose=2,
random_state=2803,
)

Then, I declare the following function that plays the role of an "experiment" and one to add new runs to the dataset:

Exemple of "true" function

def experiment(x):
y = (1/2)np.sin(x/4) - np.cos(x/6) + 2np.sin(x/2)
return y

Add new runs to the optimizer

def next_experiment(bo, ne=1, verbose=1):

for i in range(ne):
    next_run = bo.suggest()
    xi = next_run['x']
    yi = experiment(xi)
    bo.register(params=next_run, target=yi)
    if verbose > 0:
        print(f'x = {xi:6.2f} : y = {yi:6.2f}')
    
return bo

For example, I can add three runs and I get the following output:

bo = next_experiment(bo, ne=3)

x = -4.85 : y = -2.47
x = -17.10 : y = -0.12
x = -17.08 : y = -0.14

However, when I use the predict method of the _gp model, I am not getting back the correct value for the last entry:

xi = bo.space.params.flatten()
yi = bo._gp.predict(xi.reshape(-1,1))
for x, y in zip(xi,yi):
print(f'x = {x:6.2f} : yp = {y:6.2f}')

x = -4.85 : yp = -2.47
x = -17.10 : yp = -0.12
x = -17.08 : yp = -0.87

But, if I add two new runs and I repeat the calculation of the predicted values, then yes, I get the correct value for the third entry (x = -17.08, yp = -0.14), but again, I am getting a wrong value for the last entry (x = 20.00), -0.06 instead of -0.58.

bo = next_experiment(bo, ne=2)

x = -19.92 : y = 2.49
x = 20.00 : y = -0.58

xi = bo.space.params.flatten()
yi = bo._gp.predict(xi.reshape(-1,1))
for x, y in zip(xi,yi):
print(f'x = {x:6.2f} : yp = {y:6.2f}')

x = -4.85 : yp = -2.47
x = -17.10 : yp = -0.12
x = -17.08 : yp = -0.14
x = -19.92 : yp = 2.49
x = 20.00 : yp = -0.06

Am I missing something? Is this the correct behaviour?

Thanks!!

@till-m
Copy link
Member

till-m commented Mar 8, 2025

Hey @dcferre,

see #513.

I'm realizing that while I added a note to this effect to the .maximize function docs (link) I never did the same for .probe. Would that have helped clear the confusion in your case?

@dcferre
Copy link
Author

dcferre commented Mar 10, 2025

Hi @till-m,

Yes, it helps. Thanks a lot for your fast reply.

And sorry for the mess of the layout of my first message, rookie using github.

@till-m till-m closed this as completed Mar 12, 2025
@till-m
Copy link
Member

till-m commented Mar 12, 2025

No problem! Let me know if you run into any other problem :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants