MCPcopy
hub / github.com/ddbourgin/numpy-ml / sample

Method sample

numpy_ml/nonparametric/gp.py:223–258  ·  view source on GitHub ↗

Sample functions from the GP prior or posterior predictive distribution. Parameters ---------- X : :py:class:`ndarray ` of shape `(N, M)` The collection of datapoints to generate predictions on. Only used if `dist` = 'p

(self, X, n_samples=1, dist="posterior_predictive")

Source from the content-addressed store, hash-verified

221 return marginal_ll
222
223 def sample(self, X, n_samples=1, dist="posterior_predictive"):
224 """
225 Sample functions from the GP prior or posterior predictive
226 distribution.
227
228 Parameters
229 ----------
230 X : :py:class:`ndarray <numpy.ndarray>` of shape `(N, M)`
231 The collection of datapoints to generate predictions on. Only used if
232 `dist` = 'posterior_predictive'.
233 n_samples: int
234 The number of samples to generate. Default is 1.
235 dist : {"posterior_predictive", "prior"}
236 The distribution to draw samples from. Default is
237 "posterior_predictive".
238
239 Returns
240 -------
241 samples : :py:class:`ndarray <numpy.ndarray>` of shape `(n_samples, O, N)`
242 The generated samples for the points in `X`.
243 """
244 mvnorm = np.random.multivariate_normal
245
246 if dist == "prior":
247 mu = np.zeros((X.shape[0], 1))
248 cov = self.kernel(X, X)
249 elif dist == "posterior_predictive":
250 mu, _, cov = self.predict(X, return_cov=True)
251 else:
252 raise ValueError("Unrecognized dist: '{}'".format(dist))
253
254 if mu.ndim == 1:
255 mu = mu[:, np.newaxis]
256
257 samples = np.array([mvnorm(_mu, cov, size=n_samples) for _mu in mu.T])
258 return samples.swapaxes(0, 1)

Callers 2

plot_gp_distFunction · 0.95
is_multidimensionalFunction · 0.45

Calls 1

predictMethod · 0.95

Tested by

no test coverage detected