def test_domain_in_list_bspline(self): """Test the evaluation of FDataBasis""" for bspline in (BSpline(domain_range=[(0, 1)], nbasis=5, order=3), BSpline(domain_range=((0, 1),), nbasis=5, order=3), BSpline(domain_range=np.array((0, 1)), nbasis=5, order=3), BSpline(domain_range=np.array([(0, 1)]), nbasis=5, order=3) ): coefficients = [[0.00078238, 0.48857741, 0.63971985, 0.23, 0.33], [0.01778079, 0.73440271, 0.20148638, 0.54, 0.12]] f = FDataBasis(bspline, coefficients) t = np.linspace(0, 1, 4) res = np.array([[0.001, 0.564, 0.435, 0.33], [0.018, 0.468, 0.371, 0.12]]) np.testing.assert_array_almost_equal(f(t).round(3), res) np.testing.assert_array_almost_equal(f.evaluate(t).round(3), res) # Check error with np.testing.assert_raises(ValueError): BSpline(domain_range=[(0, 1), (0, 1)])
def test_evaluation_composed_keepdims_monomial(self): """Test behaviour of keepdims with composed evaluation""" monomial = Monomial(domain_range=(0, 1), nbasis=3) coefficients = [[1, 2, 3], [0.5, 1.4, 1.3]] f = FDataBasis(monomial, coefficients) f_keepdims = FDataBasis(monomial, coefficients, keepdims=True) t = [[0, 0.5, 0.6], [0.2, 0.7, 0.1]] res = np.array([[1., 2.75, 3.28], [0.832, 2.117, 0.653]]) res_keepdims = res.reshape((2, 3, 1)) # Case default behaviour keepdims=False np.testing.assert_array_almost_equal( f(t, aligned_evaluation=False).round(3), res) np.testing.assert_array_almost_equal(f(t, aligned_evaluation=False, keepdims=False).round(3), res) np.testing.assert_array_almost_equal(f(t, aligned_evaluation=False, keepdims=True).round(3), res_keepdims) # Case default behaviour keepdims=True np.testing.assert_array_almost_equal( f_keepdims(t, aligned_evaluation=False).round(3), res_keepdims) np.testing.assert_array_almost_equal( f_keepdims(t, aligned_evaluation=False, keepdims=False).round(3), res) np.testing.assert_array_almost_equal( f_keepdims(t, aligned_evaluation=False, keepdims=True).round(3), res_keepdims)
def test_evaluation_keepdims_monomial(self): """Test behaviour of keepdims """ monomial = Monomial(domain_range=(0, 1), nbasis=3) coefficients = [[1, 2, 3], [0.5, 1.4, 1.3]] f = FDataBasis(monomial, coefficients) f_keepdims = FDataBasis(monomial, coefficients, keepdims=True) np.testing.assert_equal(f.keepdims, False) np.testing.assert_equal(f_keepdims.keepdims, True) t = np.linspace(0, 1, 4) res = np.array([[1., 2., 3.667, 6.], [0.5, 1.111, 2.011, 3.2]]) res_keepdims = res.reshape((2, 4, 1)) # Case default behaviour keepdims=False np.testing.assert_array_almost_equal(f(t).round(3), res) np.testing.assert_array_almost_equal( f(t, keepdims=False).round(3), res) np.testing.assert_array_almost_equal(f(t, keepdims=True).round(3), res_keepdims) # Case default behaviour keepdims=True np.testing.assert_array_almost_equal( f_keepdims(t).round(3), res_keepdims) np.testing.assert_array_almost_equal( f_keepdims(t, keepdims=False).round(3), res) np.testing.assert_array_almost_equal( f_keepdims(t, keepdims=True).round(3), res_keepdims)
def test_evaluation_keepdims_bspline(self): """Test behaviour of keepdims """ bspline = BSpline(domain_range=(0, 1), n_basis=5, order=3) coefficients = [[0.00078238, 0.48857741, 0.63971985, 0.23, 0.33], [0.01778079, 0.73440271, 0.20148638, 0.54, 0.12]] f = FDataBasis(bspline, coefficients) f_keepdims = FDataBasis(bspline, coefficients, keepdims=True) np.testing.assert_equal(f.keepdims, False) np.testing.assert_equal(f_keepdims.keepdims, True) t = np.linspace(0, 1, 4) res = np.array([[0.001, 0.564, 0.435, 0.33], [0.018, 0.468, 0.371, 0.12]]) res_keepdims = res.reshape((2, 4, 1)) # Case default behaviour keepdims=False np.testing.assert_array_almost_equal(f(t).round(3), res) np.testing.assert_array_almost_equal( f(t, keepdims=False).round(3), res) np.testing.assert_array_almost_equal( f(t, keepdims=True).round(3), res_keepdims) # Case default behaviour keepdims=True np.testing.assert_array_almost_equal( f_keepdims(t).round(3), res_keepdims) np.testing.assert_array_almost_equal( f_keepdims(t, keepdims=False).round(3), res) np.testing.assert_array_almost_equal( f_keepdims(t, keepdims=True).round(3), res_keepdims)
def test_evaluation_composed_keepdims_bspline(self): """Test behaviour of keepdims with composed evaluation""" bspline = BSpline(domain_range=(0, 1), n_basis=5, order=3) coefficients = [[0.00078238, 0.48857741, 0.63971985, 0.23, 0.33], [0.01778079, 0.73440271, 0.20148638, 0.54, 0.12]] f = FDataBasis(bspline, coefficients) f_keepdims = FDataBasis(bspline, coefficients, keepdims=True) t = [[0, 0.5, 0.6], [0.2, 0.7, 0.1]] res = np.array([[0.001, 0.57, 0.506], [0.524, 0.399, 0.359]]) res_keepdims = res.reshape((2, 3, 1)) # Case default behaviour keepdims=False np.testing.assert_array_almost_equal( f(t, aligned_evaluation=False).round(3), res) np.testing.assert_array_almost_equal( f(t, aligned_evaluation=False, keepdims=False).round(3), res) np.testing.assert_array_almost_equal( f(t, aligned_evaluation=False, keepdims=True).round(3), res_keepdims) # Case default behaviour keepdims=True np.testing.assert_array_almost_equal( f_keepdims(t, aligned_evaluation=False).round(3), res_keepdims) np.testing.assert_array_almost_equal( f_keepdims(t, aligned_evaluation=False, keepdims=False).round(3), res) np.testing.assert_array_almost_equal( f_keepdims(t, aligned_evaluation=False, keepdims=True).round(3), res_keepdims)
def test_regression_mixed(self): multivariate = np.array([[0, 0], [2, 7], [1, 7], [3, 9], [4, 16], [2, 14], [3, 5]]) X = [ multivariate, FDataBasis(Monomial(n_basis=3), [[1, 0, 0], [0, 1, 0], [0, 0, 1], [1, 0, 1], [1, 0, 0], [0, 1, 0], [0, 0, 1]]) ] # y = 2 + sum([3, 1] * array) + int(3 * function) intercept = 2 coefs_multivariate = np.array([3, 1]) coefs_functions = FDataBasis(Monomial(n_basis=3), [[3, 0, 0]]) y_integral = np.array([3, 3 / 2, 1, 4, 3, 3 / 2, 1]) y_sum = multivariate @ coefs_multivariate y = 2 + y_sum + y_integral scalar = LinearRegression() scalar.fit(X, y) np.testing.assert_allclose(scalar.intercept_, intercept, atol=0.01) np.testing.assert_allclose(scalar.coef_[0], coefs_multivariate, atol=0.01) np.testing.assert_allclose(scalar.coef_[1].coefficients, coefs_functions.coefficients, atol=0.01) y_pred = scalar.predict(X) np.testing.assert_allclose(y_pred, y, atol=0.01)
def test_regression_single_explanatory(self): x_basis = Monomial(n_basis=7) x_fd = FDataBasis(x_basis, np.identity(7)) beta_basis = Fourier(n_basis=5) beta_fd = FDataBasis(beta_basis, [1, 1, 1, 1, 1]) y = [ 0.9999999999999993, 0.162381381441085, 0.08527083481359901, 0.08519946930844623, 0.09532291032042489, 0.10550022969639987, 0.11382675064746171 ] scalar = LinearRegression(coef_basis=[beta_basis]) scalar.fit(x_fd, y) np.testing.assert_allclose(scalar.coef_[0].coefficients, beta_fd.coefficients) np.testing.assert_allclose(scalar.intercept_, 0.0, atol=1e-6) y_pred = scalar.predict(x_fd) np.testing.assert_allclose(y_pred, y) scalar = LinearRegression(coef_basis=[beta_basis], fit_intercept=False) scalar.fit(x_fd, y) np.testing.assert_allclose(scalar.coef_[0].coefficients, beta_fd.coefficients) np.testing.assert_equal(scalar.intercept_, 0.0) y_pred = scalar.predict(x_fd) np.testing.assert_allclose(y_pred, y)
def test_init_integer(self): """Tests initializations which only specify the order.""" # Checks for a zero order Lfd object lfd_0 = LinearDifferentialOperator(order=0) weightfd = [FDataBasis(Constant(domain_range=(0, 1)), 1)] self._assert_equal_weights( lfd_0.weights, weightfd, "Wrong list of weight functions of the linear operator") # Checks for a non zero order Lfd object lfd_3 = LinearDifferentialOperator(3) consfd = FDataBasis( Constant(domain_range=(0, 1)), [[0], [0], [0], [1]], ) bwtlist3 = list(consfd) self._assert_equal_weights( lfd_3.weights, bwtlist3, "Wrong list of weight functions of the linear operator") # Negative order must fail with np.testing.assert_raises(ValueError): LinearDifferentialOperator(-1)
def test_fdatabasis_derivative_bspline(self): bspline = FDataBasis(BSpline(n_basis=8), [1, 5, 8, 9, 7, 8, 4, 5]) bspline2 = FDataBasis( BSpline(n_basis=5), [[4, 9, 7, 4, 3], [1, 7, 9, 8, 5], [4, 6, 6, 6, 8]]) bs0 = bspline.derivative(order=0) bs1 = bspline.derivative() bs2 = bspline.derivative(order=2) np.testing.assert_equal(bs1.basis, BSpline(n_basis=7, order=3)) np.testing.assert_almost_equal( bs1.coefficients, np.atleast_2d([60, 22.5, 5, -10, 5, -30, 15])) np.testing.assert_equal(bs0, bspline) np.testing.assert_equal(bs2.basis, BSpline(n_basis=6, order=2)) np.testing.assert_almost_equal( bs2.coefficients, np.atleast_2d([-375, -87.5, -75, 75, -175, 450])) bs0 = bspline2.derivative(order=0) bs1 = bspline2.derivative() bs2 = bspline2.derivative(order=2) np.testing.assert_equal(bs1.basis, BSpline(n_basis=4, order=3)) np.testing.assert_almost_equal( bs1.coefficients, [[30, -6, -9, -6], [36, 6, -3, -18], [12, 0, 0, 12]]) np.testing.assert_equal(bs0, bspline2) np.testing.assert_equal(bs2.basis, BSpline(n_basis=3, order=2)) np.testing.assert_almost_equal( bs2.coefficients, [[-144, -6, 12], [-120, -18, -60], [-48, 0, 48]])
def test_comutativity_inprod(self): monomial = Monomial(n_basis=4) bspline = BSpline(n_basis=5, order=3) bsplinefd = FDataBasis(bspline, np.arange(0, 15).reshape(3, 5)) np.testing.assert_array_almost_equal( bsplinefd.inner_product(monomial).round(3), np.transpose(monomial.inner_product(bsplinefd).round(3)))
def test_error_beta_not_basis(self): """ Test that all beta are Basis objects. """ x_fd = FDataBasis(Monomial(n_basis=7), np.identity(7)) y = [1 for _ in range(7)] beta = FDataBasis(Monomial(n_basis=7), np.identity(7)) scalar = LinearScalarRegression([beta]) np.testing.assert_raises(ValueError, scalar.fit, [x_fd], y)
def test_error_y_is_FData(self): """Tests that none of the explained variables is an FData object """ x_fd = FDataBasis(Monomial(n_basis=7), np.identity(7)) y = list(FDataBasis(Monomial(n_basis=7), np.identity(7))) scalar = LinearScalarRegression([Fourier(n_basis=5)]) np.testing.assert_raises(ValueError, scalar.fit, [x_fd], y)
def test_fdatabasis_times_fdatabasis_list(self): monomial = FDataBasis(Monomial(nbasis=3), [[1, 2, 3], [4, 5, 6], [7, 8, 9]]) result = monomial.times([3, 2, 1]) expec_basis = Monomial(nbasis=3) expec_coefs = np.array([[3, 6, 9], [8, 10, 12], [7, 8, 9]]) self.assertEqual(expec_basis, result.basis) np.testing.assert_array_almost_equal(expec_coefs, result.coefficients)
def test_fdatabasis_times_fdatabasis_int(self): monomial = FDataBasis(Monomial(n_basis=3), [[1, 2, 3], [4, 5, 6], [7, 8, 9]]) result = monomial.times(3) expec_basis = Monomial(n_basis=3) expec_coefs = np.array([[3, 6, 9], [12, 15, 18], [21, 24, 27]]) self.assertEqual(expec_basis, result.basis) np.testing.assert_array_almost_equal(expec_coefs, result.coefficients)
def test_fdatabasis_times_fdatabasis_fdatabasis(self): monomial = FDataBasis(Monomial(nbasis=3), [1, 2, 3]) bspline = FDataBasis(BSpline(nbasis=6, order=4), [1, 2, 4, 1, 0, 1]) times_fdar = monomial.times(bspline) prod_basis = BSpline(nbasis=9, order=6, knots=[0, 0.25, 0.5, 0.75, 1]) prod_coefs = np.array([[0.9788352, 1.6289955, 2.7004969, 6.2678739, 8.7636441, 4.0069960, 0.7126961, 2.8826708, 6.0052311]]) self.assertEqual(prod_basis, times_fdar.basis) np.testing.assert_array_almost_equal(prod_coefs, times_fdar.coefficients)
def test_fdatabasis__div__(self): monomial1 = FDataBasis(Monomial(n_basis=3), [1, 2, 3]) monomial2 = FDataBasis(Monomial(n_basis=3), [[1, 2, 3], [3, 4, 5]]) self.assertTrue((monomial1 / 2).equals( FDataBasis(Monomial(n_basis=3), [[1 / 2, 1, 3 / 2]]))) self.assertTrue((monomial2 / 2).equals( FDataBasis(Monomial(n_basis=3), [[1 / 2, 1, 3 / 2], [3 / 2, 2, 5 / 2]]))) self.assertTrue((monomial2 / [1, 2]).equals( FDataBasis(Monomial(n_basis=3), [[1, 2, 3], [3 / 2, 2, 5 / 2]])))
def test_init_list_int(self): coefficients = [1, 3, 4, 5, 6, 7] constant = Constant((0, 1)) fd = FDataBasis(constant, np.array(coefficients).reshape(-1, 1)) lfd = LinearDifferentialOperator(weights=coefficients) np.testing.assert_equal(lfd.order, 5, "Wrong deriv order of the linear operator") np.testing.assert_equal( lfd.weights, fd.to_list(), "Wrong list of weight functions of the linear operator")
def test_fdatabasis__mul__2(self): monomial1 = FDataBasis(Monomial(n_basis=3), [1, 2, 3]) monomial2 = FDataBasis(Monomial(n_basis=3), [[1, 2, 3], [3, 4, 5]]) np.testing.assert_equal(monomial1 / 2, FDataBasis(Monomial(n_basis=3), [[1 / 2, 1, 3 / 2]])) np.testing.assert_equal(monomial2 / 2, FDataBasis(Monomial(n_basis=3), [[1 / 2, 1, 3 / 2], [3 / 2, 2, 5 / 2]])) np.testing.assert_equal(monomial2 / [1, 2], FDataBasis(Monomial(n_basis=3), [[1, 2, 3], [3 / 2, 2, 5 / 2]]))
def test_evaluation_simple_monomial(self): """Test the evaluation of FDataBasis""" monomial = Monomial(domain_range=(0, 1), n_basis=3) coefficients = [[1, 2, 3], [0.5, 1.4, 1.3]] f = FDataBasis(monomial, coefficients) t = np.linspace(0, 1, 4) res = np.array([[1., 2., 3.667, 6.], [0.5, 1.111, 2.011, 3.2]]) np.testing.assert_array_almost_equal(f(t).round(3), res) np.testing.assert_array_almost_equal(f.evaluate(t).round(3), res)
def test_evaluation_derivative_monomial(self): """Test the evaluation of the derivative of a FDataBasis""" monomial = Monomial(domain_range=(0, 1), n_basis=3) coefficients = [[1, 2, 3], [0.5, 1.4, 1.3]] f = FDataBasis(monomial, coefficients) t = np.linspace(0, 1, 4) f_deriv = f.derivative() np.testing.assert_array_almost_equal( f_deriv(t).round(3), np.array([[2., 4., 6., 8.], [1.4, 2.267, 3.133, 4.]])[..., np.newaxis])
def test_evaluation_composed_fourier(self): """Test the evaluation of FDataBasis the a matrix of times instead of a list of times """ fourier = Fourier(domain_range=(0, 1), nbasis=3) coefficients = np.array([[0.00078238, 0.48857741, 0.63971985], [0.01778079, 0.73440271, 0.20148638]]) f = FDataBasis(fourier, coefficients) t = np.linspace(0, 1, 4) res_test = f(t) # Test same result than evaluation standart np.testing.assert_array_almost_equal(f([1]), f([[1], [1]], aligned_evaluation=False)) np.testing.assert_array_almost_equal(f(t), f(np.vstack((t, t)), aligned_evaluation=False)) # Different evaluation times t_multiple = [[0, 0.5], [0.2, 0.7]] np.testing.assert_array_almost_equal(f(t_multiple[0])[0], f(t_multiple, aligned_evaluation=False)[0]) np.testing.assert_array_almost_equal(f(t_multiple[1])[1], f(t_multiple, aligned_evaluation=False)[1])
def test_evaluation_composed_monomial(self): """Test the evaluation of FDataBasis the a matrix of times instead of a list of times """ monomial = Monomial(domain_range=(0, 1), nbasis=3) coefficients = [[1, 2, 3], [0.5, 1.4, 1.3]] f = FDataBasis(monomial, coefficients) t = np.linspace(0, 1, 4) res_test = f(t) # Test same result than evaluation standart np.testing.assert_array_almost_equal(f([1]), f([[1], [1]], aligned_evaluation=False)) np.testing.assert_array_almost_equal(f(t), f(np.vstack((t, t)), aligned_evaluation=False)) # Different evaluation times t_multiple = [[0, 0.5], [0.2, 0.7]] np.testing.assert_array_almost_equal(f(t_multiple[0])[0], f(t_multiple, aligned_evaluation=False)[0]) np.testing.assert_array_almost_equal(f(t_multiple[1])[1], f(t_multiple, aligned_evaluation=False)[1])
def test_fdatabasis_fdatabasis_inprod(self): monomial = Monomial(n_basis=4) monomialfd = FDataBasis(monomial, [[5, 4, 1, 0], [4, 2, 1, 0], [4, 1, 6, 4], [4, 5, 0, 1], [5, 6, 2, 0]]) bspline = BSpline(n_basis=5, order=3) bsplinefd = FDataBasis(bspline, np.arange(0, 15).reshape(3, 5)) np.testing.assert_allclose( inner_product_matrix(monomialfd, bsplinefd), np.array([[16.14797697, 52.81464364, 89.4813103], [11.55565285, 38.22211951, 64.88878618], [18.14698361, 55.64698361, 93.14698361], [15.2495976, 48.9995976, 82.7495976], [19.70392982, 63.03676315, 106.37009648]]), rtol=1e-4)
def test_evaluation_composed_bspline(self): """Test the evaluation of FDataBasis the a matrix of times instead of a list of times """ bspline = BSpline(domain_range=(0, 1), n_basis=5, order=3) coefficients = [[0.00078238, 0.48857741, 0.63971985, 0.23, 0.33], [0.01778079, 0.73440271, 0.20148638, 0.54, 0.12]] f = FDataBasis(bspline, coefficients) t = np.linspace(0, 1, 4) # Test same result than evaluation standart np.testing.assert_array_almost_equal(f([1]), f([[1], [1]], aligned=False)) np.testing.assert_array_almost_equal( f(t), f(np.vstack((t, t)), aligned=False)) # Different evaluation times t_multiple = [[0, 0.5], [0.2, 0.7]] np.testing.assert_array_almost_equal( f(t_multiple[0])[0], f(t_multiple, aligned=False)[0]) np.testing.assert_array_almost_equal( f(t_multiple[1])[1], f(t_multiple, aligned=False)[1])
def test_evaluation_derivative_bspline(self): """Test the evaluation of the derivative of a FDataBasis""" bspline = BSpline(domain_range=(0, 1), n_basis=5, order=3) coefficients = [[0.00078238, 0.48857741, 0.63971985, 0.23, 0.33], [0.01778079, 0.73440271, 0.20148638, 0.54, 0.12]] f = FDataBasis(bspline, coefficients) t = np.linspace(0, 1, 4) f_deriv = f.derivative() np.testing.assert_array_almost_equal( f_deriv(t).round(3), np.array([[2.927, 0.453, -1.229, 0.6], [4.3, -1.599, 1.016, -2.52]])[..., np.newaxis])
def fit(self, X, y=None, sample_weight=None): y, X, weights = self._argcheck(y, X, sample_weight) nbeta = len(self.beta_basis) n_samples = X[0].n_samples y = np.asarray(y).reshape((n_samples, 1)) for j in range(nbeta): xcoef = X[j].coefficients inner_basis_x_beta_j = X[j].basis.inner_product(self.beta_basis[j]) inner_x_beta = ( xcoef @ inner_basis_x_beta_j if j == 0 else np.concatenate( (inner_x_beta, xcoef @ inner_basis_x_beta_j), axis=1)) if any(w != 1 for w in weights): inner_x_beta = inner_x_beta * np.sqrt(weights) y = y * np.sqrt(weights) gram_inner_x_beta = inner_x_beta.T @ inner_x_beta inner_x_beta_y = inner_x_beta.T @ y gram_inner_x_beta_inv = np.linalg.inv(gram_inner_x_beta) betacoefs = gram_inner_x_beta_inv @ inner_x_beta_y idx = 0 for j in range(0, nbeta): self.beta_basis[j] = FDataBasis( self.beta_basis[j], betacoefs[idx:idx + self.beta_basis[j].n_basis].T) idx = idx + self.beta_basis[j].n_basis self.beta_ = self.beta_basis return self
def test_evaluation_simple_bspline(self): """Test the evaluation of FDataBasis""" bspline = BSpline(domain_range=(0, 1), nbasis=5, order=3) coefficients = [[0.00078238, 0.48857741, 0.63971985, 0.23, 0.33], [0.01778079, 0.73440271, 0.20148638, 0.54, 0.12]] f = FDataBasis(bspline, coefficients) t = np.linspace(0, 1, 4) res = np.array([[0.001, 0.564, 0.435, 0.33], [0.018, 0.468, 0.371, 0.12]]) np.testing.assert_array_almost_equal(f(t).round(3), res) np.testing.assert_array_almost_equal(f.evaluate(t).round(3), res)
def test_comutativity_inprod(self): monomial = Monomial(n_basis=4) bspline = BSpline(n_basis=5, order=3) bsplinefd = FDataBasis(bspline, np.arange(0, 15).reshape(3, 5)) np.testing.assert_allclose( inner_product_matrix(bsplinefd, monomial), np.transpose(inner_product_matrix(monomial, bsplinefd)))
def test_init_default(self): """Tests default initialization (do not penalize).""" lfd = LinearDifferentialOperator() weightfd = [FDataBasis(Constant((0, 1)), 0)] np.testing.assert_equal( lfd.weights, weightfd, "Wrong list of weight functions of the linear operator")
def test_from_data_qr(self): t = np.linspace(0, 1, 5) x = np.sin(2 * np.pi * t) + np.cos(2 * np.pi * t) basis = BSpline((0, 1), n_basis=5) np.testing.assert_array_almost_equal( FDataBasis.from_data(x, t, basis, method='qr').coefficients.round(2), np.array([[1., 2.78, -3., -0.78, 1.]]))