mirror of
https://github.com/SheffieldML/GPy.git
synced 2026-07-05 16:02:13 +02:00
Changed quantile computation via sampling and added fallback for predictive mean and variance if conditional mean and variance are not implemented yet
This commit is contained in:
parent
af20bed747
commit
926b53bfce
2 changed files with 34 additions and 26 deletions
|
|
@ -298,13 +298,8 @@ class Likelihood(Parameterized):
|
||||||
return self.conditional_mean(f)*p
|
return self.conditional_mean(f)*p
|
||||||
scaled_mean = [quad(int_mean, fmin, fmax,args=(mj,s2j))[0] for mj,s2j in zip(mu,variance)]
|
scaled_mean = [quad(int_mean, fmin, fmax,args=(mj,s2j))[0] for mj,s2j in zip(mu,variance)]
|
||||||
mean = np.array(scaled_mean)[:,None] / np.sqrt(2*np.pi*(variance))
|
mean = np.array(scaled_mean)[:,None] / np.sqrt(2*np.pi*(variance))
|
||||||
|
|
||||||
return mean
|
return mean
|
||||||
|
|
||||||
def _conditional_mean(self, f):
|
|
||||||
"""Quadrature calculation of the conditional mean: E(Y_star|f)"""
|
|
||||||
raise NotImplementedError("implement this function to make predictions")
|
|
||||||
|
|
||||||
def predictive_variance(self, mu,variance, predictive_mean=None, Y_metadata=None):
|
def predictive_variance(self, mu,variance, predictive_mean=None, Y_metadata=None):
|
||||||
"""
|
"""
|
||||||
Approximation to the predictive variance: V(Y_star)
|
Approximation to the predictive variance: V(Y_star)
|
||||||
|
|
@ -608,23 +603,30 @@ class Likelihood(Parameterized):
|
||||||
:param full_cov: whether to use the full covariance or just the diagonal
|
:param full_cov: whether to use the full covariance or just the diagonal
|
||||||
:type full_cov: Boolean
|
:type full_cov: Boolean
|
||||||
"""
|
"""
|
||||||
|
try:
|
||||||
pred_mean = self.predictive_mean(mu, var, Y_metadata)
|
pred_mean = self.predictive_mean(mu, var, Y_metadata=Y_metadata)
|
||||||
pred_var = self.predictive_variance(mu, var, pred_mean, Y_metadata)
|
pred_var = self.predictive_variance(mu, var, pred_mean, Y_metadata=Y_metadata)
|
||||||
|
except NotImplementedError:
|
||||||
|
print "Finding predictive mean and variance via sampling rather than quadrature"
|
||||||
|
Nf_samp = 300
|
||||||
|
Ny_samp = 1
|
||||||
|
s = np.random.randn(mu.shape[0], Nf_samp)*np.sqrt(var) + mu
|
||||||
|
ss_y = self.samples(s, Y_metadata, samples=Ny_samp)
|
||||||
|
pred_mean = np.mean(ss_y, axis=1)[:, None]
|
||||||
|
pred_var = np.var(ss_y, axis=1)[:, None]
|
||||||
|
|
||||||
return pred_mean, pred_var
|
return pred_mean, pred_var
|
||||||
|
|
||||||
def predictive_quantiles(self, mu, var, quantiles, Y_metadata=None):
|
def predictive_quantiles(self, mu, var, quantiles, Y_metadata=None):
|
||||||
#compute the quantiles by sampling!!!
|
#compute the quantiles by sampling!!!
|
||||||
N_samp = 500
|
Nf_samp = 300
|
||||||
s = np.random.randn(mu.shape[0], N_samp)*np.sqrt(var) + mu
|
Ny_samp = 1
|
||||||
#ss_f = s.flatten()
|
s = np.random.randn(mu.shape[0], Nf_samp)*np.sqrt(var) + mu
|
||||||
#ss_y = self.samples(ss_f, Y_metadata)
|
ss_y = self.samples(s, Y_metadata, samples=Ny_samp)
|
||||||
#ss_y = self.samples(s, Y_metadata, samples=100)
|
#ss_y = ss_y.reshape(mu.shape[0], mu.shape[1], Nf_samp*Ny_samp)
|
||||||
ss_y = self.samples(s, Y_metadata)
|
|
||||||
#ss_y = ss_y.reshape(mu.shape[0], N_samp)
|
|
||||||
|
|
||||||
return [np.percentile(ss_y ,q, axis=1)[:,None] for q in quantiles]
|
pred_quantiles = [np.percentile(ss_y, q, axis=1)[:,None] for q in quantiles]
|
||||||
|
return pred_quantiles
|
||||||
|
|
||||||
def samples(self, gp, Y_metadata=None, samples=1):
|
def samples(self, gp, Y_metadata=None, samples=1):
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
|
|
@ -107,11 +107,13 @@ def plot_fit(model, plot_limits=None, which_data_rows='all',
|
||||||
upper = m + 2*np.sqrt(v)
|
upper = m + 2*np.sqrt(v)
|
||||||
else:
|
else:
|
||||||
if isinstance(model,GPCoregionalizedRegression) or isinstance(model,SparseGPCoregionalizedRegression):
|
if isinstance(model,GPCoregionalizedRegression) or isinstance(model,SparseGPCoregionalizedRegression):
|
||||||
meta = {'output_index': Xgrid[:,-1:].astype(np.int)}
|
extra_data = Xgrid[:,-1:].astype(np.int)
|
||||||
else:
|
if Y_metadata is None:
|
||||||
meta = None
|
Y_metadata = {'output_index': extra_data}
|
||||||
m, v = model.predict(Xgrid, full_cov=False, Y_metadata=meta, **predict_kw)
|
else:
|
||||||
lower, upper = model.predict_quantiles(Xgrid, Y_metadata=meta)
|
Y_metadata['output_index'] = extra_data
|
||||||
|
m, v = model.predict(Xgrid, full_cov=False, Y_metadata=Y_metadata, **predict_kw)
|
||||||
|
lower, upper = model.predict_quantiles(Xgrid, Y_metadata=Y_metadata)
|
||||||
|
|
||||||
|
|
||||||
for d in which_data_ycols:
|
for d in which_data_ycols:
|
||||||
|
|
@ -120,7 +122,9 @@ def plot_fit(model, plot_limits=None, which_data_rows='all',
|
||||||
|
|
||||||
#optionally plot some samples
|
#optionally plot some samples
|
||||||
if samples: #NOTE not tested with fixed_inputs
|
if samples: #NOTE not tested with fixed_inputs
|
||||||
Ysim = model.posterior_samples(Xgrid, samples)
|
Ysim = model.posterior_samples(Xgrid, samples, Y_metadata=Y_metadata)
|
||||||
|
print Ysim.shape
|
||||||
|
print Xnew.shape
|
||||||
for yi in Ysim.T:
|
for yi in Ysim.T:
|
||||||
plots['posterior_samples'] = ax.plot(Xnew, yi[:,None], Tango.colorsHex['darkBlue'], linewidth=0.25)
|
plots['posterior_samples'] = ax.plot(Xnew, yi[:,None], Tango.colorsHex['darkBlue'], linewidth=0.25)
|
||||||
#ax.plot(Xnew, yi[:,None], marker='x', linestyle='--',color=Tango.colorsHex['darkBlue']) #TODO apply this line for discrete outputs.
|
#ax.plot(Xnew, yi[:,None], marker='x', linestyle='--',color=Tango.colorsHex['darkBlue']) #TODO apply this line for discrete outputs.
|
||||||
|
|
@ -185,10 +189,12 @@ def plot_fit(model, plot_limits=None, which_data_rows='all',
|
||||||
m, _ = model._raw_predict(Xgrid, **predict_kw)
|
m, _ = model._raw_predict(Xgrid, **predict_kw)
|
||||||
else:
|
else:
|
||||||
if isinstance(model,GPCoregionalizedRegression) or isinstance(model,SparseGPCoregionalizedRegression):
|
if isinstance(model,GPCoregionalizedRegression) or isinstance(model,SparseGPCoregionalizedRegression):
|
||||||
meta = {'output_index': Xgrid[:,-1:].astype(np.int)}
|
extra_data = Xgrid[:,-1:].astype(np.int)
|
||||||
else:
|
if Y_metadata is None:
|
||||||
meta = None
|
Y_metadata = {'output_index': extra_data}
|
||||||
m, v = model.predict(Xgrid, full_cov=False, Y_metadata=meta, **predict_kw)
|
else:
|
||||||
|
Y_metadata['output_index'] = extra_data
|
||||||
|
m, v = model.predict(Xgrid, full_cov=False, Y_metadata=Y_metadata, **predict_kw)
|
||||||
for d in which_data_ycols:
|
for d in which_data_ycols:
|
||||||
m_d = m[:,d].reshape(resolution, resolution).T
|
m_d = m[:,d].reshape(resolution, resolution).T
|
||||||
plots['contour'] = ax.contour(x, y, m_d, levels, vmin=m.min(), vmax=m.max(), cmap=pb.cm.jet)
|
plots['contour'] = ax.contour(x, y, m_d, levels, vmin=m.min(), vmax=m.max(), cmap=pb.cm.jet)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue