def conditional(self, name, Xnew, pred_noise=False, given=None, **kwargs): R""" Returns the approximate conditional distribution of the GP evaluated over new input locations `Xnew`. Parameters ---------- name: string Name of the random variable Xnew: array-like Function input values. If one-dimensional, must be a column vector with shape `(n, 1)`. pred_noise: bool Whether or not observation noise is included in the conditional. Default is `False`. given: dict Can optionally take as key value pairs: `X`, `Xu`, `y`, `noise`, and `gp`. See the section in the documentation on additive GP models in PyMC for more information. **kwargs Extra keyword arguments that are passed to `MvNormal` distribution constructor. """ givens = self._get_given_vals(given) mu, cov = self._build_conditional(Xnew, pred_noise, False, *givens) shape = infer_shape(Xnew, kwargs.pop("shape", None)) return pm.MvNormal(name, mu=mu, cov=cov, size=shape, **kwargs)
def conditional(self, name, Xnew, **kwargs): """ Returns the conditional distribution evaluated over new input locations `Xnew`. `Xnew` will be split by columns and fed to the relevant covariance functions based on their `input_dim`. For example, if `cov_func1`, `cov_func2`, and `cov_func3` have `input_dim` of 2, 1, and 4, respectively, then `Xnew` must have 7 columns and a covariance between the prediction points .. code:: python cov_func(Xnew) = cov_func1(Xnew[:, :2]) * cov_func1(Xnew[:, 2:3]) * cov_func1(Xnew[:, 3:]) The distribution returned by `conditional` does not have a Kronecker structure regardless of whether the input points lie on a full grid. Therefore, `Xnew` does not need to have grid structure. Parameters ---------- name: string Name of the random variable Xnew: array-like Function input values. If one-dimensional, must be a column vector with shape `(n, 1)`. **kwargs Extra keyword arguments that are passed to `MvNormal` distribution constructor. """ mu, cov = self._build_conditional(Xnew) shape = infer_shape(Xnew, kwargs.pop("shape", None)) return pm.MvNormal(name, mu=mu, cov=cov, size=shape, **kwargs)
def conditional(self, name, Xnew, **kwargs): R""" Returns the conditional distribution evaluated over new input locations `Xnew`. Given a set of function values `f` that the TP prior was over, the conditional distribution over a set of new points, `f_*` is Parameters ---------- name: string Name of the random variable Xnew: array-like Function input values. **kwargs Extra keyword arguments that are passed to `MvNormal` distribution constructor. """ X = self.X f = self.f nu2, mu, cov = self._build_conditional(Xnew, X, f) shape = infer_shape(Xnew, kwargs.pop("shape", None)) return pm.MvStudentT(name, nu=nu2, mu=mu, cov=cov, size=shape, **kwargs)
def conditional(self, name, Xnew, given=None, **kwargs): R""" Returns the conditional distribution evaluated over new input locations `Xnew`. Given a set of function values `f` that the GP prior was over, the conditional distribution over a set of new points, `f_*` is .. math:: f_* \mid f, X, X_* \sim \mathcal{GP}\left( K(X_*, X) K(X, X)^{-1} f \,, K(X_*, X_*) - K(X_*, X) K(X, X)^{-1} K(X, X_*) \right) Parameters ---------- name: string Name of the random variable Xnew: array-like Function input values. given: dict Can optionally take as key value pairs: `X`, `y`, `noise`, and `gp`. See the section in the documentation on additive GP models in PyMC for more information. **kwargs Extra keyword arguments that are passed to `MvNormal` distribution constructor. """ givens = self._get_given_vals(given) mu, cov = self._build_conditional(Xnew, *givens) shape = infer_shape(Xnew, kwargs.pop("shape", None)) return pm.MvNormal(name, mu=mu, cov=cov, size=shape, **kwargs)
def _build_prior(self, name, X, reparameterize=True, **kwargs): mu = self.mean_func(X) cov = stabilize(self.cov_func(X)) shape = infer_shape(X, kwargs.pop("shape", None)) if reparameterize: v = pm.Normal(name + "_rotated_", mu=0.0, sigma=1.0, size=shape, **kwargs) f = pm.Deterministic(name, mu + cholesky(cov).dot(v)) else: f = pm.MvNormal(name, mu=mu, cov=cov, size=shape, **kwargs) return f
def _build_prior(self, name, X, reparameterize=True, **kwargs): mu = self.mean_func(X) cov = stabilize(self.cov_func(X)) shape = infer_shape(X, kwargs.pop("shape", None)) if reparameterize: chi2 = pm.ChiSquared(name + "_chi2_", self.nu) v = pm.Normal(name + "_rotated_", mu=0.0, sigma=1.0, size=shape, **kwargs) f = pm.Deterministic(name, (at.sqrt(self.nu) / chi2) * (mu + cholesky(cov).dot(v))) else: f = pm.MvStudentT(name, nu=self.nu, mu=mu, cov=cov, size=shape, **kwargs) return f
def marginal_likelihood(self, name, X, y, noise, is_observed=True, **kwargs): R""" Returns the marginal likelihood distribution, given the input locations `X` and the data `y`. This is integral over the product of the GP prior and a normal likelihood. .. math:: y \mid X,\theta \sim \int p(y \mid f,\, X,\, \theta) \, p(f \mid X,\, \theta) \, df Parameters ---------- name: string Name of the random variable X: array-like Function input values. If one-dimensional, must be a column vector with shape `(n, 1)`. y: array-like Data that is the sum of the function with the GP prior and Gaussian noise. Must have shape `(n, )`. noise: scalar, Variable, or Covariance Standard deviation of the Gaussian noise. Can also be a Covariance for non-white noise. is_observed: bool Whether to set `y` as an `observed` variable in the `model`. Default is `True`. **kwargs Extra keyword arguments that are passed to `MvNormal` distribution constructor. """ if not isinstance(noise, Covariance): noise = pm.gp.cov.WhiteNoise(noise) mu, cov = self._build_marginal_likelihood(X, noise) self.X = X self.y = y self.noise = noise if is_observed: return pm.MvNormal(name, mu=mu, cov=cov, observed=y, **kwargs) else: shape = infer_shape(X, kwargs.pop("shape", None)) return pm.MvNormal(name, mu=mu, cov=cov, size=shape, **kwargs)