kss = self.cov(Xs[idx], diag=True) # self variance Ks = self.cov(Xs[idx], X) # cross-covariances ms = self.mean(Xs[idx]) al, sW, L, C = self.post.alpha, self.post.sW, self.post.L, self.post.C fmu[idx] = ms + np.dot(Ks, al) if L == None: fs2[idx] = kss + np.sum(Ks * np.dot(Ks, L), axis=1) else: V = np.linalg.solve(L, sW * Ks.T) fs2[idx] = kss - np.sum(V * V, axis=0) if ys == 0: yi = 0 else: yi = ys[idx] lp[idx], ymu[idx], ys2[idx] = self.lik.pred(yi, fmu[idx], fs2[idx]) na += nb return fmu, fs2, ymu, ys2, lp if __name__ == "__main__": def f(x): return x * np.sin(x) X = np.atleast_2d([1., 3., 5., 6., 7., 8.]).T y = f(X).ravel() Xs = np.atleast_2d(np.linspace(0, 10, 2007)).T from gp import GaussianProcess as gp gp = gp(mean=1.0 * one()) post, nlZ, dnlZ = gp.inference(X, y, deriv=True) fmu, fs2, ymu, ys2, lp = gp.predict(X, y, Xs)
ymu,ys2 = np.zeros(ns),np.zeros(ns) lp = np.zeros(ns) while na<ns: idx = np.arange(na,min(na+nb,ns)) kss = self.cov(Xs[idx],diag=True) # self variance Ks = self.cov(Xs[idx],X) # cross-covariances ms = self.mean(Xs[idx]) al,sW,L,C = self.post.alpha,self.post.sW,self.post.L,self.post.C fmu[idx] = ms + np.dot(Ks,al) if L==None: fs2[idx] = kss + np.sum(Ks*np.dot(Ks,L),axis=1) else: V = np.linalg.solve(L,sW*Ks.T) fs2[idx] = kss - np.sum(V*V,axis=0) if ys==0: yi = 0 else: yi = ys[idx] lp[idx],ymu[idx],ys2[idx] = self.lik.pred(yi,fmu[idx],fs2[idx]) na += nb return fmu,fs2,ymu,ys2,lp if __name__ == "__main__": def f(x): return x * np.sin(x) X = np.atleast_2d([1., 3., 5., 6., 7., 8.]).T y = f(X).ravel() Xs = np.atleast_2d(np.linspace(0, 10, 2007)).T from gp import GaussianProcess as gp gp = gp(mean=1.0*one()) post,nlZ,dnlZ = gp.inference(X,y,deriv=True) fmu,fs2,ymu,ys2,lp = gp.predict(X,y,Xs)
def show_1d(X,y,Xs,ym,ys): plt.fill(np.concatenate([Xs, Xs[::-1]]), np.concatenate([ym - 1.96*ys,(ym + 1.96*ys)[::-1]]), alpha=0.25, fc='k', ec='k', label='95% confidence interval') plt.plot(X,y,'b+',ms=10) plt.plot(Xs,ym,'b',lw=2) plt.grid() plt.xlabel('input X') plt.ylabel('output y') def f(x): return x * np.sin(x) X = np.atleast_2d([1., 3., 5., 6., 7., 8.]).T y = f(X).ravel() Xs = np.atleast_2d(np.linspace(0, 10, 2007)).T from sklearn import gaussian_process gp = gaussian_process.GaussianProcess(corr='cubic', theta0=1e-2, thetaL=1e-4, thetaU=1e-1, random_start=100) gp.fit(X,y) ymn,ys2 = gp.predict(Xs, eval_MSE=True); ysd = np.sqrt(ys2) from gp import GaussianProcess as gp gp = gp(X=X,y=y) post = gp.inference(X,y) fmu,fs2,ymu,ys2,lp = gp.predict(X,y,Xs) f0 = plt.figure(0); plt.clf() show_1d(X,y,Xs,ymn,ysd) f1 = plt.figure(1); plt.clf() show_1d(X,y,Xs,ymu,ys2)
def f(x): return x * np.sin(x) X = np.atleast_2d([1., 3., 5., 6., 7., 8.]).T y = f(X).ravel() Xs = np.atleast_2d(np.linspace(0, 10, 2007)).T from sklearn import gaussian_process gp = gaussian_process.GaussianProcess(corr='cubic', theta0=1e-2, thetaL=1e-4, thetaU=1e-1, random_start=100) gp.fit(X, y) ymn, ys2 = gp.predict(Xs, eval_MSE=True) ysd = np.sqrt(ys2) from gp import GaussianProcess as gp gp = gp(X=X, y=y) post = gp.inference(X, y) fmu, fs2, ymu, ys2, lp = gp.predict(X, y, Xs) f0 = plt.figure(0) plt.clf() show_1d(X, y, Xs, ymn, ysd) f1 = plt.figure(1) plt.clf() show_1d(X, y, Xs, ymu, ys2)