def __init__(self, *argL, **argD ): self.gp = GP(*argL, **argD)
def __init__(self, *argL, **argD): self.gp = GP(*argL, **argD)
class MyGP: def __init__(self, *argL, **argD ): self.gp = GP(*argL, **argD) def set_hypers(self, state): print 'setting chooser state' self.gp.__dict__.update(state) print self self.hypers_initialized = True def __str__(self): infoD = {} for field in ['mean', 'amp2', 'noise', 'ls']: infoD[ field ] = getattr(self.gp, field) return '\n'.join([ "%s : %s"%(key, str(val)) for key, val in infoD.items() ]) def fit(self, x, y): # y, self.mean, self.std = normalize(y) m,d = x.shape assert y.shape == (m,), "m = %d, y.shape = %s"%(m, str(y.shape)) self.x = x self.y = y self.gp.real_init(d,y) if not self.hypers_initialized: self.gp.optimize_hypers(x,y) # The primary covariances for prediction. K = self.gp.cov(x) assert K.shape == (m,m) # Compute the required Cholesky. obsv_cov = K + self.gp.noise*np.eye(x.shape[0]) obsv_chol = spla.cholesky( obsv_cov, lower=True ) # Solve the linear systems. self.alpha = spla.cho_solve((obsv_chol, True), y - self.gp.mean) self.obsv_chol = obsv_chol return self def predict(self,x): cand_cross = self.gp.cov(self.x, x) # O(mdn) assert cand_cross.shape == (self.x.shape[0], x.shape[0]) y_mean = np.dot(cand_cross.T, self.alpha) + self.gp.mean # O(mn) return y_mean def predict_proba(self,x): cand_cross = self.gp.cov(self.x, x) # O(mdn) beta = spla.solve_triangular(self.obsv_chol, cand_cross, lower=True) # O(mmn) assert cand_cross.shape == (self.x.shape[0], x.shape[0]) assert beta.shape == (self.x.shape[0],x.shape[0]) # Predict the marginal means and variances at candidates. y_mean = np.dot(cand_cross.T, self.alpha) + self.gp.mean # O(mn) y_var = self.gp.amp2*(1+1e-6) - np.sum(beta**2, axis=0) # O(mn) return y_mean, y_var def ei(self,x): best = np.min(self.x) y_mean, y_var = self.predict_proba(x) y_std = np.sqrt(y_var) u = (best - y_mean) / y_std ncdf = sps.norm.cdf(u) npdf = sps.norm.pdf(u) ei = y_std*( u*ncdf + npdf) return ei, y_mean, y_var
class MyGP: def __init__(self, *argL, **argD): self.gp = GP(*argL, **argD) def set_hypers(self, state): print 'setting chooser state' self.gp.__dict__.update(state) print self self.hypers_initialized = True def __str__(self): infoD = {} for field in ['mean', 'amp2', 'noise', 'ls']: infoD[field] = getattr(self.gp, field) return '\n'.join( ["%s : %s" % (key, str(val)) for key, val in infoD.items()]) def fit(self, x, y): # y, self.mean, self.std = normalize(y) m, d = x.shape assert y.shape == (m, ), "m = %d, y.shape = %s" % (m, str(y.shape)) self.x = x self.y = y self.gp.real_init(d, y) if not self.hypers_initialized: self.gp.optimize_hypers(x, y) # The primary covariances for prediction. K = self.gp.cov(x) assert K.shape == (m, m) # Compute the required Cholesky. obsv_cov = K + self.gp.noise * np.eye(x.shape[0]) obsv_chol = spla.cholesky(obsv_cov, lower=True) # Solve the linear systems. self.alpha = spla.cho_solve((obsv_chol, True), y - self.gp.mean) self.obsv_chol = obsv_chol return self def predict(self, x): cand_cross = self.gp.cov(self.x, x) # O(mdn) assert cand_cross.shape == (self.x.shape[0], x.shape[0]) y_mean = np.dot(cand_cross.T, self.alpha) + self.gp.mean # O(mn) return y_mean def predict_proba(self, x): cand_cross = self.gp.cov(self.x, x) # O(mdn) beta = spla.solve_triangular(self.obsv_chol, cand_cross, lower=True) # O(mmn) assert cand_cross.shape == (self.x.shape[0], x.shape[0]) assert beta.shape == (self.x.shape[0], x.shape[0]) # Predict the marginal means and variances at candidates. y_mean = np.dot(cand_cross.T, self.alpha) + self.gp.mean # O(mn) y_var = self.gp.amp2 * (1 + 1e-6) - np.sum(beta**2, axis=0) # O(mn) return y_mean, y_var def ei(self, x): best = np.min(self.x) y_mean, y_var = self.predict_proba(x) y_std = np.sqrt(y_var) u = (best - y_mean) / y_std ncdf = sps.norm.cdf(u) npdf = sps.norm.pdf(u) ei = y_std * (u * ncdf + npdf) return ei, y_mean, y_var