MCPcopy Index your code
hub / github.com/bayesian-optimization/BayesianOptimization / predict

Method predict

bayes_opt/constraint.py:153–221  ·  view source on GitHub ↗

r"""Calculate the probability that the constraint is fulfilled at `X`. Note that this does not try to approximate the values of the constraint function (for this, see `ConstraintModel.approx()`.), but probability that the constraint function is fulfilled. That is, this

(self, X: NDArray[Float])

Source from the content-addressed store, hash-verified

151 gp.fit(X, Y[:, i])
152
153 def predict(self, X: NDArray[Float]) -> NDArray[Float]:
154 r"""Calculate the probability that the constraint is fulfilled at `X`.
155
156 Note that this does not try to approximate the values of the
157 constraint function (for this, see `ConstraintModel.approx()`.), but
158 probability that the constraint function is fulfilled. That is, this
159 function calculates
160
161 .. math::
162 p = \text{Pr}\left\{c^{\text{low}} \leq \tilde{c}(x) \leq
163 c^{\text{up}} \right\} = \int_{c^{\text{low}}}^{c^{\text{up}}}
164 \mathcal{N}(c, \mu(x), \sigma^2(x)) \, dc.
165
166 with :math:`\mu(x)`, :math:`\sigma^2(x)` the mean and variance at
167 :math:`x` as given by the GP and :math:`c^{\text{low}}`,
168 :math:`c^{\text{up}}` the lower and upper bounds of the constraint
169 respectively.
170
171 Note
172 ----
173
174 In case of multiple constraints, we assume conditional independence.
175 This means we calculate the probability of constraint fulfilment
176 individually, with the joint probability given as their product.
177
178 Parameters
179 ----------
180 X : np.ndarray of shape (n_samples, n_features)
181 Parameters for which to predict the probability of constraint
182 fulfilment.
183
184
185 Returns
186 -------
187 np.ndarray of shape (n_samples,)
188 Probability of constraint fulfilment.
189
190 """
191 X_shape = X.shape
192 X = X.reshape((-1, self._model[0].n_features_in_))
193
194 result: NDArray[Float]
195 y_mean: NDArray[Float]
196 y_std: NDArray[Float]
197 p_lower: NDArray[Float]
198 p_upper: NDArray[Float]
199 if len(self._model) == 1:
200 y_mean, y_std = self._model[0].predict(X, return_std=True)
201
202 p_lower = (
203 norm(loc=y_mean, scale=y_std).cdf(self._lb[0]) if self._lb[0] != -np.inf else np.array([0])
204 )
205 p_upper = (
206 norm(loc=y_mean, scale=y_std).cdf(self._ub[0]) if self._ub[0] != np.inf else np.array([1])
207 )
208 result = p_upper - p_lower
209 return result.reshape(X_shape[:-1])
210

Callers 3

approxMethod · 0.45
acqMethod · 0.45
_update_gainsMethod · 0.45

Calls

no outgoing calls

Tested by

no test coverage detected