def test_exact_polynomial_approximation_1d(self): np.random.seed(42) X = np.random.normal(size=(10,2)) M = X.shape[0] X_1d = X[:,0].copy().reshape((M,1)) f_1d = 2 + 5*X_1d pr = rs.PolynomialApproximation(N=1) pr.train(X_1d, f_1d) print(('Rsqr: {:6.4f}'.format(pr.Rsqr))) np.random.seed(42) X = np.random.normal(size=(10,2)) M = X.shape[0] X_1d_test = X[:,0].copy().reshape((M,1)) f, df = pr.predict(X_1d_test, compgrad=True) np.testing.assert_almost_equal(f, 2+5*X_1d_test.reshape((10,1)), decimal=10) np.testing.assert_almost_equal(df, 5*np.ones((10,1)), decimal=10) f_1d = 2 - 3*X_1d + 5*X_1d*X_1d pr = rs.PolynomialApproximation(N=2) pr.train(X_1d, f_1d) print(('Rsqr: {:6.4f}'.format(pr.Rsqr))) np.random.seed(42) X = np.random.normal(size=(10,2)) M = X.shape[0] X_1d_test = X[:,0].copy().reshape((M,1)) f, df = pr.predict(X_1d_test, compgrad=True) f_test = 2 - 3*X_1d_test + 5*X_1d_test*X_1d_test df_test = -3 + 10*X_1d_test np.testing.assert_almost_equal(f, f_test.reshape((10,1)), decimal=10) np.testing.assert_almost_equal(df, df_test.reshape((10,1)), decimal=10)
def test_exact_polynomial_approximation_2d(self): np.random.seed(42) X = np.random.normal(size=(10,2)) X_train = X.copy() f_2d = 2 + 5*X_train[:,0] - 4*X_train[:,1] pr = rs.PolynomialApproximation(N=1) pr.train(X_train, f_2d.reshape((f_2d.size,1))) print(('Rsqr: {:6.4f}'.format(pr.Rsqr))) X = np.random.normal(size=(10,2)) X_test = X.copy() f, df = pr.predict(X_test, compgrad=True) f_test = 2 + 5*X_test[:,0] - 4*X_test[:,1] np.testing.assert_almost_equal(f, f_test.reshape((10,1)), decimal=10) np.testing.assert_almost_equal(df[:,0].reshape((10,1)), 5*np.ones((10,1)), decimal=10) np.testing.assert_almost_equal(df[:,1].reshape((10,1)), -4*np.ones((10,1)), decimal=10) f_2d = 2 - 3*X_train[:,1] + 5*X_train[:,0]*X_train[:,1] pr = rs.PolynomialApproximation(N=2) pr.train(X_train, f_2d.reshape((f_2d.size,1))) print(('Rsqr: {:6.4f}'.format(pr.Rsqr))) X = np.random.normal(size=(10,2)) X_test = X.copy() f, df = pr.predict(X_test, compgrad=True) f_test = 2 - 3*X_test[:,1] + 5*X_test[:,0]*X_test[:,1] df1_test = 5*X_test[:,1] df2_test = -3 + 5*X_test[:,0] np.testing.assert_almost_equal(f, f_test.reshape((10,1)), decimal=10) np.testing.assert_almost_equal(df[:,0].reshape((10,1)), df1_test.reshape((10,1)), decimal=10) np.testing.assert_almost_equal(df[:,1].reshape((10,1)), df2_test.reshape((10,1)), decimal=10)
def test_poly_order_2d(self): np.random.seed(42) X = np.random.uniform(-1.0, 1.0, size=(50, 2)) X_test = X.copy() xx = np.linspace(-1.0, 1.0, 21) X1, X2 = np.meshgrid(xx, xx) X_train = np.hstack((X1.reshape((441, 1)), X2.reshape((441, 1)))) f_train = np.sin(np.pi * X1.reshape( (441, 1))) * np.cos(np.pi * X2.reshape((441, 1))) print '\nPOLY 2D ORDER CONVERGENCE\n' for N in range(3, 10): pr = rs.PolynomialApproximation(N=N) pr.train(X_train, f_train) f, df = pr.predict(X_test, compgrad=True) f_true = np.sin(np.pi * X[:, 0].reshape( (50, 1))) * np.cos(np.pi * X[:, 1].reshape((50, 1))) df1_true = np.cos(np.pi * X[:, 1].reshape( (50, 1))) * np.pi * np.cos(np.pi * X[:, 0].reshape((50, 1))) df2_true = -np.sin(np.pi * X[:, 0].reshape( (50, 1))) * np.pi * np.sin(np.pi * X[:, 1].reshape((50, 1))) err_f = np.linalg.norm(f - f_true) / np.linalg.norm(f_true) err_df1 = np.linalg.norm(df[:, 0].reshape((50, 1)) - df1_true) / np.linalg.norm(df1_true) err_df2 = np.linalg.norm(df[:, 1].reshape((50, 1)) - df2_true) / np.linalg.norm(df2_true) print 'Order: %d, Error in f: %6.4e, Error in df1: %6.4e, Error in df2: %6.4e' % ( N, err_f, err_df1, err_df2)
def test_polynomial_grad_2d(self): np.random.seed(42) X = np.random.normal(size=(200, 2)) X_train = X.copy() ff0 = np.cos(X_train[:, 0]).reshape((200, 1)) ff1 = np.sin(X_train[:, 1]).reshape((200, 1)) f_2d = ff0 * ff1 pr = rs.PolynomialApproximation(N=5) pr.train(X_train, f_2d) X = np.random.normal(size=(10, 2)) X_test = X.copy() f0, df0 = pr.predict(X_test, compgrad=True) e = 1e-6 X_testp = X_test.copy() X_testp[:, 0] += e f1 = pr.predict(X_testp)[0] df1_fd = (f1 - f0) / e np.testing.assert_almost_equal(df0[:, 0].reshape((10, 1)), df1_fd, decimal=5) X_testp = X_test.copy() X_testp[:, 1] += e f1 = pr.predict(X_testp)[0] df2_fd = (f1 - f0) / e np.testing.assert_almost_equal(df0[:, 1].reshape((10, 1)), df2_fd, decimal=5)
def test_exact_polynomial_approximation_3d(self): np.random.seed(42) X = np.random.normal(size=(20, 3)) X_train = X.copy() f_3d = 2 + 5 * X_train[:, 0] - 4 * X_train[:, 1] + 2 * X_train[:, 2] pr = rs.PolynomialApproximation(N=3) pr.train(X_train, f_3d.reshape((f_3d.size, 1)))
def test_rs_data_train_pr_bnd(self): np.random.seed(42) X0 = np.random.uniform(-1.0, 1.0, size=(50, 3)) f0 = np.random.normal(size=(50, 1)) df0 = np.random.normal(size=(50, 3)) sub = ss.Subspaces() sub.compute(df=df0) avd = dom.BoundedActiveVariableDomain(sub) avm = dom.BoundedActiveVariableMap(avd) pr = rs.PolynomialApproximation() asrs = asm.ActiveSubspaceResponseSurface(avm, pr) asrs.train_with_data(X0, f0) XX = np.random.uniform(-1.0, 1.0, size=(10, 3)) ff, dff = asrs.predict(XX, compgrad=True)
def test_rs_fun_train_pr_ubnd_2d(self): np.random.seed(42) X0 = np.random.normal(size=(50, 3)) f0 = np.random.normal(size=(50, 1)) df0 = np.random.normal(size=(50, 3)) sub = ss.Subspaces() sub.compute(df=df0) sub.partition(2) avd = dom.UnboundedActiveVariableDomain(sub) avm = dom.UnboundedActiveVariableMap(avd) pr = rs.PolynomialApproximation() asrs = asm.ActiveSubspaceResponseSurface(avm, pr) asrs.train_with_interface(self.quad_fun, 10) XX = np.random.normal(size=(10, 3)) ff, dff = asrs.predict(XX, compgrad=True)
def test_poly_order_1d(self): np.random.seed(42) X = np.random.uniform(-1.0, 1.0, size=(50, 2)) X_1d_test = X[:, 0].copy().reshape((50, 1)) X_train = np.linspace(-1.0, 1.0, 201).reshape((201, 1)) f_train = np.sin(np.pi * X_train) print '\nPOLY 1D ORDER CONVERGENCE\n' for N in range(3, 10): pr = rs.PolynomialApproximation(N=N) pr.train(X_train, f_train) f, df = pr.predict(X_1d_test, compgrad=True) f_true = np.sin(np.pi * X_1d_test) err_f = np.linalg.norm(f - f_true) / np.linalg.norm(f_true) df_true = np.pi * np.cos(np.pi * X_1d_test) err_df = np.linalg.norm(df - df_true) / np.linalg.norm(df_true) print 'Order: %d, Error in f: %6.4e, Error in df: %6.4e' % ( N, err_f, err_df)
def test_polynomial_grad_1d(self): np.random.seed(42) X = np.random.normal(size=(10, 2)) M = X.shape[0] X_1d = X[:, 0].copy().reshape((M, 1)) f_1d = np.cos(X_1d) pr = rs.PolynomialApproximation(N=7) pr.train(X_1d, f_1d) X = np.random.normal(size=(10, 2)) M = X.shape[0] X_1d_test = X[:, 0].copy().reshape((M, 1)) f0, df0 = pr.predict(X_1d_test, compgrad=True) e = 1e-6 X_1d_testp = X_1d_test.copy() + e f1 = pr.predict(X_1d_testp)[0] df0_fd = (f1 - f0) / e np.testing.assert_almost_equal(df0, df0_fd, decimal=5)