Gpyopt: Can GpyOpt plot the opportunity cost?

Created on 23 Jul 2020  路  7Comments  路  Source: SheffieldML/GPyOpt

Hi,
In this example: https://www.blopig.com/blog/wp-content/uploads/2019/10/GPyOpt-Tutorial1.html
The codes are:

Import Modules

GPyOpt - Cases are important, for some reason

import GPyOpt
from GPyOpt.methods import BayesianOptimization

numpy

import numpy as np
from numpy.random import multivariate_normal #For later example

import pandas as pd

Plotting tools

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np
from numpy.random import multivariate_normal

def obj_func_2d(x,y):
return((x2 + y2)(np.sin(x)*2 - np.cos(y)))

fig = plt.figure(figsize=plt.figaspect(0.3))
fig.suptitle('Plots of our objective function')

X = np.arange(0, 10, 0.25)
Y = np.arange(0, 10, 0.25)
X, Y = np.meshgrid(X, Y)

ax = fig.add_subplot(1, 2, 1)
ax.contour(X,Y,Z)
Z = obj_func_2d(X,Y)

ax = fig.add_subplot(1, 2, 1)
ax.contour(X,Y,Z)

ax = fig.add_subplot(1, 2, 2, projection='3d')

surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm,
linewidth=0, antialiased=False)

ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))

fig.colorbar(surf, shrink=0.5, aspect=5)

plt.show()

def objfunc2d(x):
"""
x is a 2 dimensional vector.
"""
x1 = x[:, 0]
x2 = x[:, 1]
return((x12 + x22)(np.sin(x1)*2 - np.cos(x2)))

bounds2d = [{'name': 'var_1', 'type': 'continuous', 'domain': (0,10)},
{'name': 'var_2', 'type': 'continuous', 'domain': (0,10)}]
maxiter = 50

myBopt_2d = GPyOpt.methods.BayesianOptimization(objfunc2d, domain=bounds2d)
myBopt_2d.run_optimization(max_iter = maxiter)
print("="20)
print("Value of (x,y) that minimises the objective:"+str(myBopt_2d.x_opt))
print("Minimum value of the objective: "+str(myBopt_2d.fx_opt))
print("="
20)
myBopt_2d.plot_acquisition()
myBopt_2d.plot_convergence()

We can see it can plot convergence with respect to 'best_Y'
Can we plot 'opportunity cost' like below? And can you show us the code that can plot 'opportunity cost'
image

Thank you very much for your help!

All 7 comments

Hi. Can you please elaborate how one would define "opportunity cost" you are looking to plot? I don't believe there is anything in gpyopt that plots it out of the box, but maybe we could advice on how to go around plotting it yourself.

Thank you so much for your reply!
The 'opportunity cost' that I want to plot is the [(true best value-predicted best value)/true best value]. You can see it from the below picture.
image

Could you tell me how to plot it in GpyOpt?

What would "true best" be in case of BO? Is it the best value that optimization managed to find? Or real true minimum of the function which we somehow know from elsewhere?

Regardless, it should be pretty easy to plot. Predicted best value at iteration i is just the min(myBopt_2d.Y[:i]). So assuming we understand "true best" to be the final best value BO managed to find, the formula becomes (min(myBopt_2d.Y[:i]) - min(myBopt_2d.Y)) / min(myBopt_2d.Y). Feels like you might want to take absolute value of this fraction.

Hi, thank you so much for your help!
Your understanding is very correct. The 'true best' is the best value BO managed to find as you said.

Could you show me how to code in the above 3D function example(https://www.blopig.com/blog/wp-content/uploads/2019/10/GPyOpt-Tutorial1.html) to plot 'opportunity cost' since I'm a beginner for python(I'm not familiar with call variables outside GpyOpt package)?
image

Thank you!

i here is and index for iteration. maybe something along these lines, can't vouch it's 100% what you are looking for though

best = min(myBopt_2d.Y)
opportunity_cost = [(min(myBopt_2d.Y[:i]) - best)/best for i in range(1, len(myBopt_2d.Y))]

Hi,
Thank you for your reply!
It indeed what I'm looking for
Could you show me how to plot 'opportunity cost' changes as iteration increases?
image

Thank you very much!

I think you just need to substitute i in plt.plot call with something like range(len(opportunity_cost)) and it should work

Going forward, let's use GitHub issues for GpyOpt specific questions. Resources like StackOverflow or even Python tutorials are much better suited for basic questions like this.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cbelth picture cbelth  路  6Comments

lewisscola picture lewisscola  路  11Comments

iiternalfire picture iiternalfire  路  4Comments

1Reinier picture 1Reinier  路  4Comments

CarrKnight picture CarrKnight  路  7Comments