Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion bayes_opt/bayesian_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ class BayesianOptimization:
numpy.random.RandomState. Otherwise the random state provided is used.
When set to None, an unseeded random state is generated.

alpha: float, optional(default=1e-6)
Regularization parameter to prevent numerical issues or to adjust to an
error gaussian distribution with high variance. The higher, the higher
the regularization.

verbose: int, optional(default=2)
The level of verbosity.

Expand All @@ -89,6 +94,7 @@ def __init__(
acquisition_function: AcquisitionFunction | None = None,
constraint: NonlinearConstraint | None = None,
random_state: int | RandomState | None = None,
alpha: float = 1e-6,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🌐 Web query:

Does scikit-learn GaussianProcessRegressor validate that the alpha parameter is positive?

💡 Result:

No clear positive-only validation is enforced for GaussianProcessRegressor(alpha=...). In the public API docs, alpha is described as a nonnegative “value added to the diagonal” to help ensure a positive definite matrix, but the docs do not explicitly state that scikit-learn validates alpha > 0 at input time [1]. In the current scikit-learn implementation file for GaussianProcessRegressor (sklearn/gaussian_process/_gpr.py), the fitting code shown performs shape/compatibility checks (e.g., alpha must be a scalar or an array with the same number of entries as the training data), but the snippet does not include any explicit check rejecting alpha <= 0 [2]. Therefore, based on the evidence found in the official documentation and the examined implementation, scikit-learn does not explicitly validate that alpha is strictly positive; instead, incorrect (e.g., nonpositive) values may lead to numerical issues later (e.g., failure to produce a positive definite matrix), rather than being rejected by an immediate ValueError [1][2].

Citations:


🏁 Script executed:

#!/bin/bash
set -euo pipefail
FILE="bayes_opt/bayesian_optimization.py"
# Show a small window around the line mentioned in the review comment
nl -ba "$FILE" | sed -n '70,130p'
# Also search for any mention of alpha in the file
rg -n "alpha" "$FILE" || true

Repository: bayesian-optimization/BayesianOptimization

Length of output: 129


🏁 Script executed:

#!/bin/bash
set -euo pipefail
FILE="bayes_opt/bayesian_optimization.py"
nl -ba "$FILE" | sed -n '70,130p'
rg -n "alpha" "$FILE" || true

Repository: bayesian-optimization/BayesianOptimization

Length of output: 129


🏁 Script executed:

#!/bin/bash
set -euo pipefail
FILE="bayes_opt/bayesian_optimization.py"

awk 'NR>=70 && NR<=130 {printf "%d\t%s\n", NR, $0}' "$FILE"
echo "----"
rg -n "alpha" "$FILE" || true

Repository: bayesian-optimization/BayesianOptimization

Length of output: 2876


🏁 Script executed:

#!/bin/bash
set -euo pipefail
FILE="bayes_opt/bayesian_optimization.py"
awk 'NR>=120 && NR<=170 {printf "%d\t%s\n", NR, $0}' "$FILE"

Repository: bayesian-optimization/BayesianOptimization

Length of output: 2137


Validate alpha is strictly positive in BayesianOptimization.__init__

alpha is passed directly to sklearn.gaussian_process.GaussianProcessRegressor(alpha=...), and scikit-learn doesn’t enforce alpha > 0 at input time—nonpositive values may fail later during GP fitting. Add an explicit check to fail fast with a clear ValueError.

🛡️ Suggested validation
 def __init__(
     self,
     f: Callable[..., float] | None,
     pbounds: BoundsMapping,
     acquisition_function: AcquisitionFunction | None = None,
     constraint: NonlinearConstraint | None = None,
     random_state: int | RandomState | None = None,
     alpha: float = 1e-6,
     verbose: int = 2,
     bounds_transformer: DomainTransformer | None = None,
     allow_duplicate_points: bool = False,
 ):
+    if alpha <= 0:
+        msg = "alpha must be positive"
+        raise ValueError(msg)
     self._random_state = ensure_rng(random_state)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
alpha: float = 1e-6,
def __init__(
self,
f: Callable[..., float] | None,
pbounds: BoundsMapping,
acquisition_function: AcquisitionFunction | None = None,
constraint: NonlinearConstraint | None = None,
random_state: int | RandomState | None = None,
alpha: float = 1e-6,
verbose: int = 2,
bounds_transformer: DomainTransformer | None = None,
allow_duplicate_points: bool = False,
):
if alpha <= 0:
msg = "alpha must be positive"
raise ValueError(msg)
self._random_state = ensure_rng(random_state)
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@bayes_opt/bayesian_optimization.py` at line 97, In
BayesianOptimization.__init__, add an explicit validation that the alpha
parameter is strictly positive (alpha > 0) before it’s passed to
sklearn.gaussian_process.GaussianProcessRegressor; if the check fails raise a
ValueError with a clear message (e.g., "alpha must be > 0, got {alpha}") so the
constructor fails fast and avoids later GP fitting errors. Ensure this check
sits at the top of __init__ (next to the alpha parameter handling) and covers
both int/float inputs and rejects non-positive values.

verbose: int = 2,
bounds_transformer: DomainTransformer | None = None,
allow_duplicate_points: bool = False,
Expand Down Expand Up @@ -123,7 +129,7 @@ def __init__(
# Internal GP regressor
self._gp = GaussianProcessRegressor(
kernel=wrap_kernel(Matern(nu=2.5), transform=self._space.kernel_transform),
alpha=1e-6,
alpha=alpha,
normalize_y=True,
n_restarts_optimizer=5,
random_state=self._random_state,
Expand Down
Loading