Skip to content

Commit

Permalink
testing equivalence of mixed gp and single gp with custom kernel
Browse files Browse the repository at this point in the history
  • Loading branch information
e-dorigatti committed Dec 19, 2024
1 parent 6ad1dfd commit 4a2a547
Showing 1 changed file with 89 additions and 0 deletions.
89 changes: 89 additions & 0 deletions tests/bofire/surrogates/test_gps.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
AdditiveKernel,
HammingDistanceKernel,
MaternKernel,
MultiplicativeKernel,
RBFKernel,
ScaleKernel,
)
Expand Down Expand Up @@ -617,3 +618,91 @@ def test_MixedSingleTaskGPModel_mordred(kernel, scaler, output_scaler):
model2.loads(dump)
preds2 = model2.predict(experiments.iloc[:-1])
assert_frame_equal(preds, preds2)


def test_SingleTaskGP_with_categoricals_is_equivalent_to_MixedSingleTaskGP():
inputs = Inputs(
features=[
ContinuousInput(
key=f"x_{i+1}",
bounds=(-4, 4),
)
for i in range(2)
]
+ [
CategoricalInput(key="x_cat_1", categories=["mama", "papa"]),
CategoricalInput(key="x_cat_2", categories=["cat", "dog"]),
]
)
outputs = Outputs(features=[ContinuousOutput(key="y")])
experiments = inputs.sample(n=10, seed=194387)
experiments.eval("y=((x_1**2 + x_2 - 11)**2+(x_1 + x_2**2 -7)**2)", inplace=True)
experiments.loc[experiments.x_cat_1 == "mama", "y"] *= 5.0
experiments.loc[experiments.x_cat_1 == "papa", "y"] /= 2.0
experiments.loc[experiments.x_cat_2 == "cat", "y"] *= -2.0
experiments.loc[experiments.x_cat_2 == "dog", "y"] /= -5.0
experiments["valid_y"] = 1

gp_data = SingleTaskGPSurrogate(
inputs=inputs,
outputs=outputs,
kernel=AdditiveKernel( # use the same kernel as botorch.models.MixedSingleTaskGP
kernels=[
ScaleKernel(
base_kernel=HammingDistanceKernel(
ard=True,
features=["x_cat_1", "x_cat_2"],
)
),
ScaleKernel(
base_kernel=RBFKernel(
ard=True,
lengthscale_prior=HVARFNER_LENGTHSCALE_PRIOR(),
features=[f"x_{i+1}" for i in range(2)],
)
),
ScaleKernel(
base_kernel=MultiplicativeKernel(
kernels=[
HammingDistanceKernel(
ard=True,
features=["x_cat_1", "x_cat_2"],
),
RBFKernel(
ard=True,
lengthscale_prior=HVARFNER_LENGTHSCALE_PRIOR(),
features=[f"x_{i+1}" for i in range(2)],
),
]
)
),
]
),
)

gp_mapped = surrogates.map(gp_data)
gp_mapped.fit(experiments)
pred = gp_mapped.predict(experiments)
single_task_gp_mse = ((pred["y_pred"] - experiments["y"]) ** 2).mean()
assert single_task_gp_mse < 3.5

model = MixedSingleTaskGPSurrogate(
inputs=inputs,
outputs=outputs,
input_preprocessing_specs={
"x_cat_1": CategoricalEncodingEnum.ONE_HOT,
"x_cat_2": CategoricalEncodingEnum.ONE_HOT,
},
continuous_kernel=RBFKernel(
ard=True,
lengthscale_prior=HVARFNER_LENGTHSCALE_PRIOR(),
),
categorical_kernel=HammingDistanceKernel(ard=True),
)
model = surrogates.map(model)
model.fit(experiments)
pred = model.predict(experiments)
mixed_single_task_gp_mse = ((pred["y_pred"] - experiments["y"]) ** 2).mean()
assert mixed_single_task_gp_mse < 3.5

assert abs(single_task_gp_mse - mixed_single_task_gp_mse) < 0.005

0 comments on commit 4a2a547

Please sign in to comment.