def f(x): return x * np.sin(x) if __name__ == "__main__": print("639 main----------------------------------------------------1") plt.close('all') #example: fit a GP (with noisy observations) X = np.array([1., 3., 5., 6., 7., 8.]).reshape(-1,1) y = f(X).ravel() dy = 0.5 + 1.0*np.random.random(y.shape) #in [0.5, 1.5] <- std deviation per point y = y + np.random.normal(0, dy) #0-mean noise with variable std in [0.5, 1.5] gp = GaussianProcess(corr='cubic', nugget = (dy / y)**2, theta0=1e-1, thetaL=1e-3, thetaU=1, random_start=100, verbose=True) gp.fit(X, y) #ML est gp.get_params() Xt = np.array(np.linspace(np.min(X)-10,np.max(X)+10,1000)).reshape(-1,1) y_pred, MSE = gp.predict(Xt, eval_MSE=True) sigma = np.sqrt(MSE) plt.figure() plt.plot(Xt, f(Xt), color='k', lw=2.0, label = 'x sin(x) ground truth') plt.plot(X, y, 'r+', markersize=20, lw=2.0, label = 'observations') plt.errorbar(X.ravel(), y, dy, fmt='r.', markersize=10, label='Observations') plt.plot(Xt, y_pred, color = 'g', linestyle = '--', lw=1.5, label = 'GP prediction') plt.fill(np.concatenate([Xt, Xt[::-1]]), np.concatenate([y_pred-1.96*sigma, (y_pred+1.96*sigma)[::-1]]), alpha = 0.5, label = '95% conf interval') plt.title('GP regression') plt.xlabel('X1') plt.ylabel('X2') plt.grid(True)
with open('dw23_vs_wness_grid_a0_c0.txt') as fh: #with open('example_a0q1.txt') as fh: M = np.vstack(map(float, r.split(' ')) for r in fh.read().splitlines()) r = np.linspace(-0.3, 0.3, M.shape[1]) c = np.linspace(0, 1, M.shape[0]) #r = np.linspace(0, 1, 20) #c = np.linspace(-0.3, 0.3 100) rr, cc = np.meshgrid(r, c) vals = ~np.isnan(M) uncert = np.sqrt(M[vals]) #gp = GaussianProcess(verbose=True, nugget=uncert) gp = GaussianProcess(verbose=True,theta0=0.01, thetaL=.0001, thetaU=1., nugget=2.0) gp.fit(X=np.column_stack([rr[vals],cc[vals]]), y=M[vals]) print 'parameters: ', gp.get_params() print 'score: ', gp.score(np.column_stack([rr[vals],cc[vals]]), M[vals]) rr_cc_as_cols = np.column_stack([rr.flatten(), cc.flatten()]) #interpolated = gp.predict(rr_cc_as_cols).reshape(M.shape) interpolated , predUncert = gp.predict(rr_cc_as_cols,eval_MSE=True) interpolated = interpolated.reshape(M.shape) predUncert = predUncert.reshape(M.shape) upperBand = interpolated + predUncert lowerBand = interpolated - predUncert fig=pl.figure() ax = fig.gca(projection='3d') ax.plot_wireframe(rr,cc,interpolated,colors='blue') ax.plot_wireframe(rr,cc,upperBand,colors='red') ax.plot_wireframe(rr,cc,lowerBand,colors='red')
#example: fit a GP (with noisy observations) X = np.array([1., 3., 5., 6., 7., 8.]).reshape(-1, 1) y = f(X).ravel() dy = 0.5 + 1.0 * np.random.random( y.shape) #in [0.5, 1.5] <- std deviation per point y = y + np.random.normal(0, dy) #0-mean noise with variable std in [0.5, 1.5] gp = GaussianProcess(corr='cubic', nugget=(dy / y)**2, theta0=1e-1, thetaL=1e-3, thetaU=1, random_start=100, verbose=True) gp.fit(X, y) #ML est gp.get_params() Xt = np.array(np.linspace(np.min(X) - 10, np.max(X) + 10, 1000)).reshape(-1, 1) y_pred, MSE = gp.predict(Xt, eval_MSE=True) sigma = np.sqrt(MSE) plt.figure() plt.plot(Xt, f(Xt), color='k', lw=2.0, label='x sin(x) ground truth') plt.plot(X, y, 'r+', markersize=20, lw=2.0, label='observations') plt.errorbar(X.ravel(), y, dy, fmt='r.', markersize=10, label='Observations')
def f(x): return x * np.sin(x) if __name__ == "__main__": plt.close('all') #example: fit a GP (with noisy observations) X = np.array([1., 3., 5., 6., 7., 8.]).reshape(-1,1) y = f(X).ravel() dy = 0.5 + 1.0*np.random.random(y.shape) #in [0.5, 1.5] <- std deviation per point y = y + np.random.normal(0, dy) #0-mean noise with variable std in [0.5, 1.5] gp = GaussianProcess(corr='cubic', nugget = (dy / y)**2, theta0=1e-1, thetaL=1e-3, thetaU=1, random_start=100, verbose=True) gp.fit(X, y) #ML est gp.get_params() Xt = np.array(np.linspace(np.min(X)-10,np.max(X)+10,1000)).reshape(-1,1) y_pred, MSE = gp.predict(Xt, eval_MSE=True) sigma = np.sqrt(MSE) plt.figure() plt.plot(Xt, f(Xt), color='k', lw=2.0, label = 'x sin(x) ground truth') plt.plot(X, y, 'r+', markersize=20, lw=2.0, label = 'observations') plt.errorbar(X.ravel(), y, dy, fmt='r.', markersize=10, label='Observations') plt.plot(Xt, y_pred, color = 'g', linestyle = '--', lw=1.5, label = 'GP prediction') plt.fill(np.concatenate([Xt, Xt[::-1]]), np.concatenate([y_pred-1.96*sigma, (y_pred+1.96*sigma)[::-1]]), alpha = 0.5, label = '95% conf interval') plt.title('GP regression') plt.xlabel('X1') plt.ylabel('X2') plt.grid(True)