From cb1ab89d8a5eebca414284ef35d635ccafffe516 Mon Sep 17 00:00:00 2001 From: Neil Lawrence Date: Sun, 31 Dec 2017 22:54:43 +0100 Subject: [PATCH 1/3] Update gp.py Sample return seemed to have been based on number of training data, not number of posterior samples requested. --- GPy/core/gp.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GPy/core/gp.py b/GPy/core/gp.py index 7bad7648..4481d3bc 100644 --- a/GPy/core/gp.py +++ b/GPy/core/gp.py @@ -564,7 +564,7 @@ class GP(Model): if self.output_dim == 1: return sim_one_dim(m, v) else: - fsim = np.empty((self.output_dim, self.num_data, size)) + fsim = np.empty((self.output_dim, X.shape[1], size)) for d in range(self.output_dim): if full_cov and v.ndim == 3: fsim[d] = sim_one_dim(m[:, d], v[:, :, d]) From ff4f861fcb021054fb5acf3d99039b80e8cc1777 Mon Sep 17 00:00:00 2001 From: Neil Lawrence Date: Sun, 31 Dec 2017 23:22:23 +0100 Subject: [PATCH 2/3] Testing for dims should be checking whether 2nd dim is greater than 1 --- GPy/core/gp.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GPy/core/gp.py b/GPy/core/gp.py index 4481d3bc..5d99f0cf 100644 --- a/GPy/core/gp.py +++ b/GPy/core/gp.py @@ -568,7 +568,7 @@ class GP(Model): for d in range(self.output_dim): if full_cov and v.ndim == 3: fsim[d] = sim_one_dim(m[:, d], v[:, :, d]) - elif (not full_cov) and v.ndim == 2: + elif (not full_cov) and v.shape[1]>1: fsim[d] = sim_one_dim(m[:, d], v[:, d]) else: fsim[d] = sim_one_dim(m[:, d], v) From 0d26609b15d589ca4da878fb38c561e568f7ba73 Mon Sep 17 00:00:00 2001 From: Neil Lawrence Date: Mon, 1 Jan 2018 01:18:08 +0100 Subject: [PATCH 3/3] Rewrite poster_samples_f to return NxDxsize --- GPy/core/gp.py | 37 ++++++++++++++----------------------- 1 file changed, 14 insertions(+), 23 deletions(-) diff --git a/GPy/core/gp.py b/GPy/core/gp.py index 5d99f0cf..d546bf0e 100644 --- a/GPy/core/gp.py +++ b/GPy/core/gp.py @@ -538,7 +538,7 @@ class GP(Model): mag[n] = np.sqrt(np.linalg.det(G[n, :, :])) return mag - def posterior_samples_f(self,X, size=10, full_cov=True, **predict_kwargs): + def posterior_samples_f(self,X, size=10, **predict_kwargs): """ Samples the posterior GP at the points X. @@ -546,35 +546,28 @@ class GP(Model): :type X: np.ndarray (Nnew x self.input_dim) :param size: the number of a posteriori samples. :type size: int. - :param full_cov: whether to return the full covariance matrix, or just the diagonal. - :type full_cov: bool. - :returns: fsim: set of simulations - :rtype: np.ndarray (D x N x samples) (if D==1 we flatten out the first dimension) + :returns: set of simulations + :rtype: np.ndarray (Nnew x D x samples) """ - m, v = self._raw_predict(X, full_cov=full_cov, **predict_kwargs) + m, v = self._raw_predict(X, full_cov=True, **predict_kwargs) if self.normalizer is not None: m, v = self.normalizer.inverse_mean(m), self.normalizer.inverse_variance(v) def sim_one_dim(m, v): - if not full_cov: - return np.random.multivariate_normal(m.flatten(), np.diag(v.flatten()), size).T - else: - return np.random.multivariate_normal(m.flatten(), v, size).T + return np.random.multivariate_normal(m, v, size).T if self.output_dim == 1: - return sim_one_dim(m, v) + return sim_one_dim(m.flatten(), v)[:, np.newaxis, :] else: - fsim = np.empty((self.output_dim, X.shape[1], size)) + fsim = np.empty((X.shape[0], self.output_dim, size)) for d in range(self.output_dim): - if full_cov and v.ndim == 3: - fsim[d] = sim_one_dim(m[:, d], v[:, :, d]) - elif (not full_cov) and v.shape[1]>1: - fsim[d] = sim_one_dim(m[:, d], v[:, d]) + if v.ndim == 3: + fsim[:, d, :] = sim_one_dim(m[:, d], v[:, :, d]) else: - fsim[d] = sim_one_dim(m[:, d], v) + fsim[:, d, :] = sim_one_dim(m[:, d], v) return fsim - def posterior_samples(self, X, size=10, full_cov=False, Y_metadata=None, likelihood=None, **predict_kwargs): + def posterior_samples(self, X, size=10, Y_metadata=None, likelihood=None, **predict_kwargs): """ Samples the posterior GP at the points X. @@ -582,19 +575,17 @@ class GP(Model): :type X: np.ndarray (Nnew x self.input_dim.) :param size: the number of a posteriori samples. :type size: int. - :param full_cov: whether to return the full covariance matrix, or just the diagonal. - :type full_cov: bool. :param noise_model: for mixed noise likelihood, the noise model to use in the samples. :type noise_model: integer. :returns: Ysim: set of simulations, :rtype: np.ndarray (D x N x samples) (if D==1 we flatten out the first dimension) """ - fsim = self.posterior_samples_f(X, size, full_cov=full_cov, **predict_kwargs) + fsim = self.posterior_samples_f(X, size, **predict_kwargs) if likelihood is None: likelihood = self.likelihood if fsim.ndim == 3: - for d in range(fsim.shape[0]): - fsim[d] = likelihood.samples(fsim[d], Y_metadata=Y_metadata) + for d in range(fsim.shape[1]): + fsim[:, d] = likelihood.samples(fsim[:, d], Y_metadata=Y_metadata) else: fsim = likelihood.samples(fsim, Y_metadata=Y_metadata) return fsim