def predict(self, Xstar, return_std=False, nsamples=10): """ Returns mean and covariances for each posterior sampled Student-t Process. Parameters ---------- Xstar: np.ndarray, shape=((nsamples, nfeatures)) Testing instances to predict. return_std: bool Whether to return the standard deviation of the posterior process. Otherwise, it returns the whole covariance matrix of the posterior process. nsamples: Number of posterior MCMC samples to consider. Returns ------- np.ndarray Mean of the posterior process for each MCMC sample and Xstar. np.ndarray Covariance posterior process for each MCMC sample and Xstar. """ chunk = list(self.trace) chunk = chunk[::-1][:nsamples] post_mean = [] post_var = [] for posterior_sample in chunk: params = self._extractParam(posterior_sample, self.covfunc.parameters) covfunc = self.covfunc.__class__(**params) gp = tStudentProcess(covfunc, nu=self.nu + self.n) gp.fit(self.X, self.y) m, s = gp.predict(Xstar, return_std=return_std) post_mean.append(m) post_var.append(s) return np.array(post_mean), np.array(post_var)
def test_tSP(): rng = np.random.RandomState(0) X = rng.uniform(0, 5, 20)[:, np.newaxis] y = 0.5 * np.sin(3 * X[:, 0]) + rng.normal(0, 0.5, X.shape[0]) sexp = squaredExponential() tsp = tStudentProcess(sexp) tsp.fit(X, y)
def test_tSP_opt_nograd(): rng = np.random.RandomState(0) X = rng.uniform(0, 5, 20)[:, np.newaxis] y = 0.5 * np.sin(3 * X[:, 0]) + rng.normal(0, 0.5, X.shape[0]) sexp = squaredExponential() tsp = tStudentProcess(sexp, optimize=True) tsp.fit(X, y) params = tsp.getcovparams() assert 0.3 < params['l'] < 0.5 assert 0.3 < params['sigmaf'] < 0.6 assert 0.2 < params['sigman'] < 0.4