def setUp(self): np.random.seed(123456) # if not os.path.exists('tests/gp_2d.pkl'): #Because it takes too long: pickle a gp x = np.array([[x1,x2] for x1 in range(10) for x2 in range(10)],dtype=np.float64) #np.atleast_2d(np.linspace(0, 10, 30)).T w = np.array([0.04,0.04]) v = 2 vt = 0#.01 theta = np.zeros(2+len(w)) theta[0] = np.log(v) theta[1] = np.log(vt) theta[2:2+len(w)] = np.log(w) y = GaussianProcess.get_realisation(x, GaussianCovariance(), theta) t = y + 0.1 * np.random.randn(len(x)) #-> vt = 0.01 self.gp_est = GaussianProcess(x, t, cov=GaussianCovariance()) means, variances = self.gp_est.estimate_many(x) sigma = np.sqrt(variances) for i in range(len(x)): self.assertAlmostEqual(means[i], y[i], delta=4 * sigma[i])
def test_pickle(self): x = np.atleast_2d(np.linspace(0, 10, 30)).T w = np.array([0.04]) v = 2 vt = 0#.01 theta = np.zeros(2+len(w)) theta[0] = np.log(v) theta[1] = np.log(vt) theta[2:2+len(w)] = np.log(w) y = GaussianProcess.get_realisation(x, GaussianCovariance(), theta) t = y + 0.1 * np.random.randn(len(x)) #-> vt = 0.01 gp_est = GaussianProcess(x, t, GaussianCovariance()) output = open('gp.pkl', 'wb') pickle.dump(gp_est, output,protocol=0) output.close() pkl_file = open('gp.pkl', 'rb') if sys.version_info.major == 3: gp_unpickled = pickle.load(pkl_file, encoding='latin1') else: gp_unpickled = pickle.load(pkl_file) pkl_file.close() os.remove('gp.pkl') means, variances = gp_est.estimate_many(x) sigma = np.sqrt(variances) meansp, variancesp = gp_unpickled.estimate_many(x) sigmap = np.sqrt(variancesp) for i in range(len(x)): self.assertEqual(means[i], meansp[i]) self.assertEqual(sigma[i], sigmap[i])
def test_covariance(self): gc = GaussianCovariance() x = np.array([[x1,x2] for x1 in range(10) for x2 in range(10)]) #np.atleast_2d(np.linspace(0, 10, 30)).T theta = np.log(np.array([2,0.01,0.04,0.04])) cov = gc.cov_matrix(x,theta) cov_super = Covariance.cov_matrix(gc,x,theta) self.assertLessEqual(np.abs((cov-cov_super-0.01*np.eye(100))).sum(),1e-10) dcov = gc._d_cov_matrix_d_theta(x,theta,2) dcov_super = Covariance._d_cov_matrix_d_theta(gc,x,theta,2) self.assertLessEqual(np.abs((dcov-dcov_super)).sum(),1e-10) #np.count_nonzero(cov-cov_super) t = GaussianProcess.get_realisation(x, GaussianCovariance(), theta) #t = y + 0.1 * np.random.randn(len(x)) #-> vt = 0.01 dndt = gc._d_nll_d_theta(x,t,theta) print(dndt) dndt_est = [] for j in range(len(theta)): d = np.zeros(len(theta)) d[j] = 1e-5 #d = np.log(1+d/np.exp(theta)) # Addition of log: log(x+y) = log(x) + log(1+y/x) dndt_est.append( (gc._negativeloglikelihood(x,t,theta+d) - gc._negativeloglikelihood(x,t,theta-d))/2e-5 ) dndt_est = np.array(dndt_est) print(dndt_est) print(np.abs(dndt_est - dndt)) self.assertTrue((np.abs(dndt_est - dndt) < 5e-1).all())
def setUp(self): # if not os.path.exists('tests/metis_gp.pkl'): # if os.path.exists('skgpuppy/tests/metis_data.pkl'): # with open('skgpuppy/tests/metis_data.pkl', 'rb') as output: # if sys.version_info.major == 3: # (collection,x,t) = pickle.load(output, encoding='latin1') # else: # (collection,x,t) = pickle.load(output) # else: # raise RuntimeError("Test data not found!") from skgpuppy.tests.metis_data import x,t self.gp_est = GaussianProcess(x, t,GaussianCovariance()) # with open('tests/metis_gp.pkl', 'wb') as output: # pickle.dump(self.gp_est, output, protocol=-1) # else: # with open('tests/metis_gp.pkl', 'rb') as output: # self.gp_est = pickle.load(output) min = np.array([0.1, 0, 0])#, 200 max = np.array([30, 10, 0.05])#, 1000 self.mean = (min+max)/2 self.Sigma = np.diag([2**2,1**2,0.005**2])
def setUp(self): np.random.seed(1234) self.x = np.atleast_2d(np.linspace(0, 10, 30)).T w = np.array([0.04]) v = 2 vt = 0#.01 theta = np.zeros(2+len(w)) theta[0] = np.log(v) theta[1] = np.log(vt) theta[2:2+len(w)] = np.log(w) y = GaussianProcess.get_realisation(self.x, GaussianCovariance(),theta) self.t = y + 0.1 * np.random.randn(len(self.x)) #-> vt = 0.01 self.gp_est = GaussianProcess(self.x, self.t, cov=GaussianCovariance())
def test_gp(self): x = np.atleast_2d(np.linspace(0, 10, 100)).T w = np.array([0.04]) v = 2 vt = 0#.01 theta = np.zeros(2+len(w)) theta[0] = np.log(v) theta[1] = np.log(vt) theta[2:2+len(w)] = np.log(w) y = GaussianProcess.get_realisation(x, GaussianCovariance(),theta) t = y + 0.1 * np.random.randn(len(x)) #-> vt = 0.01 n = 10 xt = np.atleast_2d(np.linspace(0, 10, 200)).T gp_est = GaussianProcess(x, t,GaussianCovariance()) print("Theta:", np.exp(gp_est.theta_min[0:3])) means_gp, variances_gp = gp_est.estimate_many(xt) sigma_gp = np.sqrt(variances_gp) spgpcov = SPGPCovariance(n) gp_est = GaussianProcess(x, t,spgpcov) print("Theta:", np.exp(gp_est.theta_min[0:3])) means, variances = gp_est.estimate_many(xt) theta_start = spgpcov.get_theta(x,t) print("Thetastart:", np.exp(theta_start[0:3])) #means = spgpcov.estimate(x,t,gp_est.theta_min,xt) sigma = np.sqrt(variances) # import matplotlib.pyplot as plt # fig = plt.figure() # plt.plot(x,y,x,t,'o',xt,means,xt,means_gp) # plt.fill_between(xt.ravel(), means + 1.96 * sigma, means - 1.96 * sigma, facecolor='red',alpha=0.5) # plt.fill_between(xt.ravel(), means_gp + 1.96 * sigma_gp, means_gp - 1.96 * sigma_gp, facecolor='lightblue',alpha=0.5) # plt.title('own') # plt.legend(['Realisation','Noisy','Estimation','EstimationGP']) # plt.show() for i in range(len(x)): self.assertAlmostEqual(means_gp[i*2], y[i], delta=4 * sigma[i*2]) self.assertAlmostEqual(means[i*2], y[i], delta=4 * sigma[i*2])
def test_gaussian_cov_derivative(self): x = np.atleast_2d(np.linspace(0, 10, 50)).T w = np.array([0.02]) v = 2 vt = 0.01 n,d = np.shape(x) theta_min = np.ones(2+d) theta_min[0] = np.log(v) theta_min[1] = np.log(vt) theta_min[2:2+d] = np.log(w) n,d = np.shape(x) cov = GaussianCovariance() for xi in x: for xj in x: for j in range(2+d): self.assertAlmostEqual(Covariance._d_cov_d_theta(cov,xi,xj,theta_min,j),cov._d_cov_d_theta(xi,xj,theta_min,j),delta=1e-3)
def test_spgp_nll(self): x = np.array([[x1,x2] for x1 in range(10) for x2 in range(10)]) #np.atleast_2d(np.linspace(0, 10, 30)).T w = np.array([0.04,0.04]) v = 2 vt = 0#.01 theta = np.zeros(2+len(w)) theta[0] = np.log(v) theta[1] = np.log(vt) theta[2:2+len(w)] = np.log(w) y = GaussianProcess.get_realisation(x, GaussianCovariance(), theta) t = y + 0.1 * np.random.randn(len(x)) #-> vt = 0.01 spgpcov = SPGPCovariance(10) gc = GaussianCovariance() theta = spgpcov.get_theta(x,t) theta_gc = gc.get_theta(x,t) start = time.time() res_g = gc._negativeloglikelihood(x,t,theta_gc) print("Gaussian NLL: ",res_g) print("Time: ",time.time()-start) start = time.time() res = Covariance._negativeloglikelihood(spgpcov,x,t,theta) print("My NLL: ",res) print("Time: ",time.time()-start) res_s = spgpcov._negativeloglikelihood(x,t,theta) print("Snelson NLL: ", res_s) print("Time: ", time.time()-start) self.assertAlmostEqual(res,res_s,delta=2e-1)
def test_gp_2D(self): x = np.array([[x1,x2] for x1 in range(10) for x2 in range(10)]) #np.atleast_2d(np.linspace(0, 10, 30)).T w = np.array([0.04,0.04]) v = 2 vt = 0#.01 theta = np.zeros(2+len(w)) theta[0] = np.log(v) theta[1] = np.log(vt) theta[2:2+len(w)] = np.log(w) y = GaussianProcess.get_realisation(x, GaussianCovariance(), theta) t = y + 0.1 * np.random.randn(len(x)) #-> vt = 0.01 # gp_est = GaussianProcess(x, t) # x_new = np.array([[x1/2.0,x2/2.0] for x1 in xrange(20) for x2 in xrange(20)]) # #x_new = x # means, variances = gp_est.estimate_many(x_new) # sigma = np.sqrt(variances) # # import pylab as p # # #import matplotlib.axes3d as p3 # import mpl_toolkits.mplot3d.axes3d as p3 # # # gp_est = GaussianProcess(x, t,SPGPCovariance(10)) #gp_est = GaussianProcess(x, t,GaussianCovariance()) means, variances = gp_est.estimate_many(x) sigma = np.sqrt(variances) for i in range(len(x)): self.assertAlmostEqual(means[i], y[i], delta=5 * sigma[i])
def test_spgp_covariance(self): spgpc = SPGPCovariance(10) gc = GaussianCovariance() x = np.array([[x1,x2] for x1 in range(10) for x2 in range(10)]) #np.atleast_2d(np.linspace(0, 10, 30)).T w = np.array([0.04,0.04]) v = 2 vt = 0#.01 theta = np.zeros(2+len(w)) theta[0] = np.log(v) theta[1] = np.log(vt) theta[2:2+len(w)] = np.log(w) y = GaussianProcess.get_realisation(x, GaussianCovariance(),theta) t = y + 0.1 * np.random.randn(len(x)) #-> vt = 0.01 theta = spgpc.get_theta(x,t) cov = spgpc.cov_matrix(x,theta) cov_super = Covariance.cov_matrix_ij(spgpc,x,x,theta) self.assertLessEqual(np.abs((cov-cov_super).sum()),1e-5)#-np.exp(theta[1])*np.eye(100) invcov = np.linalg.inv(cov) invcov2 = spgpc.inv_cov_matrix(x,theta) self.assertLessEqual(np.abs((invcov-invcov2)).sum(),1e-5) # theta_gc = gc.get_theta(x,t) # gcov = gc.cov_matrix(x,theta_gc) # invgcov = np.linalg.inv(gcov) # print np.max(np.abs(gcov-cov)/gcov) # print np.max(np.abs(invgcov-invcov)/invgcov) start = time.time() delta = 1e-5 dndt_est = [] for j in range(len(theta)): d = np.zeros(len(theta)) d[j] = delta #dndt_est.append( (spgpc.negativeloglikelihood(x,t,theta+d) - spgpc.negativeloglikelihood(x,t,theta-d))/2/delta ) dndt_est.append( (Covariance._negativeloglikelihood(spgpc,x,t,theta+d) - Covariance._negativeloglikelihood(spgpc,x,t,theta-d))/2/delta ) print("TIME numerical: ",time.time() -start) # j = 0 # d = np.zeros(len(theta)) # d[j] = delta # d_cov_dt = (spgpc.cov_matrix(x,theta+d)-spgpc.cov_matrix(x,theta-d))/2/delta # print spgpc.d_cov_matrix_d_theta(x,theta,j) - d_cov_dt start = time.time() dndt = Covariance._d_nll_d_theta(spgpc,x,t,theta) print("TIME classic: ",time.time() -start) dot = Dot() dot.reset() start = time.time() dndt = spgpc._d_nll_d_theta(x,t,theta) print("TIME opt: ",time.time() -start) print(dot) self.assertLessEqual(np.abs((dndt_est-dndt)).sum(),1e-4)
# return 3*x+4+np.random.normal(0, 0.01) ax1 = plt.subplot2grid((3, 5), (0, 2), colspan=3, rowspan=2) # x_data = np.linspace(0, 4, 5).reshape(-1,1) x_data = np.random.uniform(0, 4, 7).reshape(-1, 1) y_data = np.array([func(i) for i in x_data]) # plt.plot(x_data, y_data, '+k') plt.ylabel('f(x)') x_real = np.linspace(0, 4, 100).reshape(-1, 1) y_real = np.array([func(i) for i in x_real]) # plt.plot(x_real, y_real, '-g') gp_est = GaussianProcess(x_data, y_data, GaussianCovariance()) x_n = np.array([1.26]) m, s = gp_est.estimate(x_n) plt.plot(x_n, m, '*r') print(m, s) x_new = np.linspace(0, 6, 100).reshape(-1, 1) means, variances = gp_est.estimate_many(x_new) # print(means) # GPy # kernel = GPy.kern.RBF(input_dim=1, variance=10.9, lengthscale=0.5) # gpy = GPy.models.GPRegression(x_data, y_data, kernel) # my = np.zeros(len(x_new))
x = np.array([[x1, x2] for x1 in xrange(10) for x2 in xrange(10) ]) # 2d sim input (no need to be a neat grid in practice) w = np.array([0.04, 0.04]) # GP bandwidth parameter v = 2 # GP variance parameter vt = 0.01 # GP variance of the error epsilon # Preparing the parameter vector theta = np.zeros(2 + len(w)) theta[0] = np.log( v ) # We actually use the log of the parameters as it is easier to optimize (no > 0 constraint etc.) theta[1] = np.log(vt) theta[2:2 + len(w)] = np.log(w) # Simulating simulation data by drawing data from a random Gaussian process t = GaussianProcess.get_realisation(x, GaussianCovariance(), theta) # The regression step is pretty easy: # Input data x (list of input vectors) # Corresponding simulation output t (just a list of floats of the same length as x) # Covariance function of your choice (only GaussianCovariance can be used for uncertainty propagation at the moment) gp_est = GaussianProcess(x, t, GaussianCovariance()) # Getting some values from the regression GP for plotting x_new = np.array([[x1 / 2.0, x2 / 2.0] for x1 in xrange(20) for x2 in xrange(20)]) means, variances = gp_est.estimate_many(x_new) # Plotting the output import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D