Ejemplo n.º 1
0
    def test_vector_valued_smoothing(self):
        X, _ = skfda.datasets.fetch_weather(return_X_y=True)

        basis_dim = skfda.representation.basis.Fourier(
            n_basis=7, domain_range=X.domain_range)
        basis = skfda.representation.basis.VectorValued([basis_dim] * 2)

        for method in smoothing.BasisSmoother.SolverMethod:
            with self.subTest(method=method):

                basis_smoother = smoothing.BasisSmoother(
                    basis,
                    regularization=TikhonovRegularization(
                        LinearDifferentialOperator(2)),
                    return_basis=True,
                    smoothing_parameter=1,
                    method=method)

                basis_smoother_dim = smoothing.BasisSmoother(
                    basis_dim,
                    regularization=TikhonovRegularization(
                        LinearDifferentialOperator(2)),
                    return_basis=True,
                    smoothing_parameter=1,
                    method=method)

                X_basis = basis_smoother.fit_transform(X)

                self.assertEqual(X_basis.dim_codomain, 2)

                self.assertEqual(X_basis.coordinates[0].basis, basis_dim)
                np.testing.assert_allclose(
                    X_basis.coordinates[0].coefficients,
                    basis_smoother_dim.fit_transform(
                        X.coordinates[0]).coefficients)

                self.assertEqual(X_basis.coordinates[1].basis, basis_dim)
                np.testing.assert_allclose(
                    X_basis.coordinates[1].coefficients,
                    basis_smoother_dim.fit_transform(
                        X.coordinates[1]).coefficients)
Ejemplo n.º 2
0
 def test_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)
     fd = FDataGrid(data_matrix=x, sample_points=t)
     smoother = smoothing.BasisSmoother(basis=basis,
                                        smoothing_parameter=10,
                                        penalty=2,
                                        method='qr',
                                        return_basis=True)
     fd_basis = smoother.fit_transform(fd)
     np.testing.assert_array_almost_equal(
         fd_basis.coefficients.round(2),
         np.array([[0.60, 0.47, 0.20, -0.07, -0.20]]))
Ejemplo n.º 3
0
 def test_cholesky(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)
     fd = FDataGrid(data_matrix=x, sample_points=t)
     smoother = smoothing.BasisSmoother(
         basis=basis,
         smoothing_parameter=10,
         regularization=TikhonovRegularization(
             LinearDifferentialOperator(2)),
         method='cholesky',
         return_basis=True)
     fd_basis = smoother.fit_transform(fd)
     np.testing.assert_array_almost_equal(
         fd_basis.coefficients.round(2),
         np.array([[0.60, 0.47, 0.20, -0.07, -0.20]]))
Ejemplo n.º 4
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(n_basis=4)
     fd = FDataGrid(data_matrix=x, sample_points=t)
     smoother = smoothing.BasisSmoother(basis=basis,
                                        smoothing_parameter=1,
                                        penalty=2,
                                        return_basis=True)
     fd_basis = smoother.fit_transform(fd)
     # These results where extracted from the R package fda
     np.testing.assert_array_almost_equal(
         fd_basis.coefficients.round(2),
         np.array([[0.61, -0.88, 0.06, 0.02]]))