Ejemplo n.º 1
0
    def test_vector_valued(self):
        def f(x):
            return x**2

        def g(y):
            return 3 * y

        t = np.linspace(0, 1, 100)

        data_matrix = np.array([np.array([f(t), g(t)]).T])

        sample_points = [t]

        fd = skfda.FDataGrid(data_matrix, sample_points=sample_points)

        basis = VectorValued([Monomial(n_basis=5), Monomial(n_basis=5)])

        fd_basis = fd.to_basis(basis)

        res = 1 / 5 + 3

        np.testing.assert_allclose(skfda.misc.inner_product(fd, fd),
                                   res,
                                   rtol=1e-5)
        np.testing.assert_allclose(skfda.misc.inner_product(
            fd_basis, fd_basis),
                                   res,
                                   rtol=1e-5)
Ejemplo n.º 2
0
    def test_several_variables(self):
        def f(x, y, z):
            return x * y * z

        t = np.linspace(0, 1, 100)

        x2, y2, z2 = ndm(t, 2 * t, 3 * t)

        data_matrix = f(x2, y2, z2)

        sample_points = [t, 2 * t, 3 * t]

        fd = skfda.FDataGrid(data_matrix[np.newaxis, ...],
                             sample_points=sample_points)

        basis = Tensor([
            Monomial(n_basis=5, domain_range=(0, 1)),
            Monomial(n_basis=5, domain_range=(0, 2)),
            Monomial(n_basis=5, domain_range=(0, 3))
        ])

        fd_basis = fd.to_basis(basis)

        res = 8

        np.testing.assert_allclose(skfda.misc.inner_product(fd, fd),
                                   res,
                                   rtol=1e-5)
        np.testing.assert_allclose(skfda.misc.inner_product(
            fd_basis, fd_basis),
                                   res,
                                   rtol=1e-5)
Ejemplo n.º 3
0
    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)
Ejemplo n.º 4
0
    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)))
Ejemplo n.º 5
0
    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)
Ejemplo n.º 6
0
    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)
Ejemplo n.º 7
0
    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)
Ejemplo n.º 8
0
    def test_basis_fdatabasis_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(
            monomial.inner_product(bsplinefd).round(3),
            np.array([[2., 7., 12.], [1.29626206, 3.79626206, 6.29626206],
                      [0.96292873, 2.62959539, 4.29626206],
                      [0.7682873, 2.0182873, 3.2682873]]).round(3))
Ejemplo n.º 9
0
    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)
Ejemplo n.º 10
0
    def test_error_weights_negative(self):
        """ Test that none of the weights are negative. """

        x_fd = FDataBasis(Monomial(n_basis=7), np.identity(7))
        y = [1 for _ in range(7)]
        weights = [-1 for _ in range(7)]
        beta = Monomial(n_basis=7)

        scalar = LinearScalarRegression([beta])
        np.testing.assert_raises(ValueError, scalar.fit, [x_fd], y, weights)
Ejemplo n.º 11
0
    def test_basis_gram_matrix_monomial(self):

        basis = Monomial(n_basis=3)
        gram_matrix = basis.gram_matrix()
        gram_matrix_numerical = basis._gram_matrix_numerical()
        gram_matrix_res = np.array([[1, 1 / 2, 1 / 3], [1 / 2, 1 / 3, 1 / 4],
                                    [1 / 3, 1 / 4, 1 / 5]])

        np.testing.assert_allclose(gram_matrix, gram_matrix_res)
        np.testing.assert_allclose(gram_matrix_numerical, gram_matrix_res)
Ejemplo n.º 12
0
    def test_error_weights_lenght(self):
        """ Test that the number of weights is equal to the
        number of samples """

        x_fd = FDataBasis(Monomial(n_basis=7), np.identity(7))
        y = [1 for _ in range(7)]
        weights = [1 for _ in range(8)]
        beta = Monomial(n_basis=7)

        scalar = LinearScalarRegression([beta])
        np.testing.assert_raises(ValueError, scalar.fit, [x_fd], y, weights)
Ejemplo n.º 13
0
 def test_basis_constant_product(self):
     constant = Constant()
     monomial = Monomial()
     fourier = Fourier()
     bspline = BSpline(n_basis=5, order=3)
     self.assertEqual(constant.basis_of_product(monomial), monomial)
     self.assertEqual(constant.basis_of_product(fourier), fourier)
     self.assertEqual(constant.basis_of_product(bspline), bspline)
     self.assertEqual(monomial.basis_of_product(constant), monomial)
     self.assertEqual(fourier.basis_of_product(constant), fourier)
     self.assertEqual(bspline.basis_of_product(constant), bspline)
Ejemplo n.º 14
0
 def test_basis_basis_inprod(self):
     monomial = Monomial(nbasis=4)
     bspline = BSpline(nbasis=5, order=4)
     np.testing.assert_array_almost_equal(
         monomial.inner_product(bspline).round(3),
         np.array(
             [[0.12499983, 0.25000035, 0.24999965, 0.25000035, 0.12499983],
              [0.01249991, 0.07500017, 0.12499983, 0.17500017, 0.11249991],
              [0.00208338, 0.02916658, 0.07083342, 0.12916658, 0.10208338],
              [0.00044654, 0.01339264, 0.04375022, 0.09910693, 0.09330368]])
         .round(3)
     )
Ejemplo n.º 15
0
    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]])))
Ejemplo n.º 16
0
    def test_basis_inner_matrix(self):
        np.testing.assert_array_almost_equal(
            Monomial(n_basis=3)._inner_matrix(),
            [[1, 1 / 2, 1 / 3], [1 / 2, 1 / 3, 1 / 4], [1 / 3, 1 / 4, 1 / 5]])

        np.testing.assert_array_almost_equal(
            Monomial(n_basis=3)._inner_matrix(Monomial(n_basis=3)),
            [[1, 1 / 2, 1 / 3], [1 / 2, 1 / 3, 1 / 4], [1 / 3, 1 / 4, 1 / 5]])

        np.testing.assert_array_almost_equal(
            Monomial(n_basis=3)._inner_matrix(Monomial(n_basis=4)),
            [[1, 1 / 2, 1 / 3, 1 / 4], [1 / 2, 1 / 3, 1 / 4, 1 / 5],
             [1 / 3, 1 / 4, 1 / 5, 1 / 6]])
Ejemplo n.º 17
0
    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]]))
Ejemplo n.º 18
0
    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)
Ejemplo n.º 19
0
    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)
Ejemplo n.º 20
0
    def test_monomial_linear_diff_op(self):
        n_basis = 5

        basis = Monomial(n_basis=n_basis)

        linear_diff_op = [3]
        res = np.array([[0., 0., 0., 0., 3.], [0., 0., 0., 3., 0.],
                        [0., 0., 3., 0., 0.], [0., 3., 0., 0., 0.],
                        [3., 0., 0., 0., 0.]])

        np.testing.assert_allclose(
            _monomial_evaluate_constant_linear_diff_op(basis, linear_diff_op),
            res)

        linear_diff_op = [3, 2]
        res = np.array([[0., 0., 0., 0., 3.], [0., 0., 0., 3., 2.],
                        [0., 0., 3., 4., 0.], [0., 3., 6., 0., 0.],
                        [3., 8., 0., 0., 0.]])

        np.testing.assert_allclose(
            _monomial_evaluate_constant_linear_diff_op(basis, linear_diff_op),
            res)

        linear_diff_op = [3, 0, 5]
        res = np.array([[0., 0., 0., 0., 3.], [0., 0., 0., 3., 0.],
                        [0., 0., 3., 0., 10.], [0., 3., 0., 30., 0.],
                        [3., 0., 60., 0., 0.]])

        np.testing.assert_allclose(
            _monomial_evaluate_constant_linear_diff_op(basis, linear_diff_op),
            res)
Ejemplo n.º 21
0
 def test_basis_gram_matrix(self):
     np.testing.assert_array_almost_equal(
         Monomial(n_basis=3).gram_matrix(),
         [[1, 1 / 2, 1 / 3], [1 / 2, 1 / 3, 1 / 4], [1 / 3, 1 / 4, 1 / 5]])
     np.testing.assert_almost_equal(
         Fourier(n_basis=3).gram_matrix(), np.identity(3))
     np.testing.assert_almost_equal(
         BSpline(n_basis=6).gram_matrix().round(4),
         np.array([[
             4.760e-02, 2.920e-02, 6.200e-03, 4.000e-04, 0.000e+00,
             0.000e+00
         ],
                   [
                       2.920e-02, 7.380e-02, 5.210e-02, 1.150e-02,
                       1.000e-04, 0.000e+00
                   ],
                   [
                       6.200e-03, 5.210e-02, 1.090e-01, 7.100e-02,
                       1.150e-02, 4.000e-04
                   ],
                   [
                       4.000e-04, 1.150e-02, 7.100e-02, 1.090e-01,
                       5.210e-02, 6.200e-03
                   ],
                   [
                       0.000e+00, 1.000e-04, 1.150e-02, 5.210e-02,
                       7.380e-02, 2.920e-02
                   ],
                   [
                       0.000e+00, 0.000e+00, 4.000e-04, 6.200e-03,
                       2.920e-02, 4.760e-02
                   ]]))
Ejemplo n.º 22
0
    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)
Ejemplo n.º 23
0
    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])
Ejemplo n.º 24
0
    def test_monomial_penalty(self):
        basis = Monomial(n_basis=5, domain_range=(0, 3))

        # Theorethical result
        res = np.array([[0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.],
                        [0., 0., 12., 54., 216.], [0., 0., 54., 324., 1458.],
                        [0., 0., 216., 1458., 6998.4]])

        self._test_penalty(basis, linear_diff_op=2, result=res)

        basis = Monomial(n_basis=8, domain_range=(1, 5))

        self._test_penalty(basis, linear_diff_op=[1, 2, 3])
        self._test_penalty(basis, linear_diff_op=7)
        self._test_penalty(basis, linear_diff_op=0)
        self._test_penalty(basis, linear_diff_op=1)
        self._test_penalty(basis, linear_diff_op=27)
Ejemplo n.º 25
0
    def test_error_y_X_samples_different(self):
        """ Test that the number of response samples and explanatory samples
        are not different """

        x_fd = FDataBasis(Monomial(n_basis=7), np.identity(7))
        y = [1 for _ in range(8)]
        beta = Fourier(n_basis=5)

        scalar = LinearScalarRegression([beta])
        np.testing.assert_raises(ValueError, scalar.fit, [x_fd], y)

        x_fd = FDataBasis(Monomial(n_basis=8), np.identity(8))
        y = [1 for _ in range(7)]
        beta = Fourier(n_basis=5)

        scalar = LinearScalarRegression([beta])
        np.testing.assert_raises(ValueError, scalar.fit, [x_fd], y)
Ejemplo n.º 26
0
    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)))
Ejemplo n.º 27
0
    def test_domain_in_list_monomial(self):
        """Test the evaluation of FDataBasis"""

        for monomial in (Monomial(domain_range=[(0, 1)], n_basis=3),
                         Monomial(domain_range=((0, 1), ), n_basis=3),
                         Monomial(domain_range=np.array((0, 1)), n_basis=3),
                         Monomial(domain_range=np.array([(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)
Ejemplo n.º 28
0
 def setUp(self):
     grid_points = [1, 2, 3, 4, 5]
     self.fd = FDataGrid([[2, 3, 4, 5, 6], [1, 4, 9, 16, 25]],
                         grid_points=grid_points)
     basis = Monomial(n_basis=3, domain_range=(1, 5))
     self.fd_basis = FDataBasis(basis, [[1, 1, 0], [0, 0, 1]])
     self.fd_curve = self.fd.concatenate(self.fd, as_coordinates=True)
     self.fd_surface = make_multimodal_samples(n_samples=3,
                                               dim_domain=2,
                                               random_state=0)
Ejemplo n.º 29
0
    def test_basis_fdatabasis_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(monomial, bsplinefd),
            np.array([[2., 7., 12.],
                      [1.29626206, 3.79626206, 6.29626206],
                      [0.96292873, 2.62959539, 4.29626206],
                      [0.7682873, 2.0182873, 3.2682873]]), rtol=1e-4)
Ejemplo n.º 30
0
 def test_monomial_smoothing(self):
     # It does not have much sense to apply smoothing in this basic case
     # where the fit is very good but its just for testing purposes
     t = np.linspace(0, 1, 5)
     x = np.sin(2 * np.pi * t) + np.cos(2 * np.pi * t)
     basis = Monomial(nbasis=4)
     fd = FDataBasis.from_data(x, t, basis,
                               penalty_degree=2,
                               smoothness_parameter=1)
     # These results where extracted from the R package fda
     np.testing.assert_array_almost_equal(
         fd.coefficients.round(2), np.array([[0.61, -0.88, 0.06, 0.02]]))