示例#1
0
    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)
示例#2
0
    def test_fdatabasis_derivative_constant(self):
        constant = FDataBasis(Constant(), [[1], [2], [3], [4]])

        self.assertTrue(constant.derivative().equals(
            FDataBasis(Constant(), [[0], [0], [0], [0]])))
        self.assertTrue(
            constant.derivative(order=0).equals(
                FDataBasis(Constant(), [[1], [2], [3], [4]])))
示例#3
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)
    def test_vector_valued_constant(self):

        basis_first = Constant()
        basis_second = Constant()

        basis = VectorValued([basis_first, basis_second])

        fd = FDataBasis(basis=basis, coefficients=[[1, 2], [3, 4]])

        self.assertEqual(fd.dim_codomain, 2)

        res = np.array([[[1, 2]], [[3, 4]]])

        np.testing.assert_allclose(fd(0), res)
示例#5
0
    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")
示例#6
0
    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")
示例#7
0
    def test_regression_predict_multiple_explanatory(self):
        y = [1, 2, 3, 4, 5, 6, 7]

        x0 = FDataBasis(Constant(domain_range=(0, 1)), np.ones((7, 1)))
        x1 = FDataBasis(Monomial(n_basis=7), np.identity(7))

        beta0 = Constant(domain_range=(0, 1))
        beta1 = BSpline(domain_range=(0, 1), n_basis=5)

        scalar = LinearScalarRegression([beta0, beta1])

        scalar.fit([x0, x1], y)

        betas = scalar.beta_

        np.testing.assert_array_almost_equal(betas[0].coefficients.round(4),
                                             np.array([[32.6518]]))

        np.testing.assert_array_almost_equal(
            betas[1].coefficients.round(4),
            np.array([[-28.6443, 80.3996, -188.587, 236.5832, -481.3449]]))
示例#8
0
    def test_init_list_int(self):
        """Tests initializations with integer weights."""

        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.weights, list(fd),
            "Wrong list of weight functions of the linear operator")
示例#9
0
    def test_init_integer(self):
        # Checks for a zero order Lfd object
        lfd_0 = LinearDifferentialOperator(order=0)
        weightfd = [FDataBasis(Constant((0, 1)), 1)]

        np.testing.assert_equal(lfd_0.order, 0,
                                "Wrong deriv order of the linear operator")
        np.testing.assert_equal(
            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((0, 1)), np.identity(4)[3].reshape(-1, 1))
        bwtlist3 = consfd.to_list()

        np.testing.assert_equal(lfd_3.order, 3,
                                "Wrong deriv order of the linear operator")
        np.testing.assert_equal(
            lfd_3.weights, bwtlist3,
            "Wrong list of weight functions of the linear operator")

        np.testing.assert_raises(ValueError, LinearDifferentialOperator, -1)
    def test_vector_valued_constant_monomial(self):

        basis_first = Constant(domain_range=(0, 5))
        basis_second = Monomial(n_basis=3, domain_range=(0, 5))

        basis = VectorValued([basis_first, basis_second])

        fd = FDataBasis(basis=basis, coefficients=[[1, 2, 3, 4], [3, 4, 5, 6]])

        self.assertEqual(fd.dim_codomain, 2)

        np.testing.assert_allclose(fd.domain_range[0], (0, 5))

        res = np.array([[[1, 2], [1, 9], [1, 24]], [[3, 4], [3, 15], [3, 38]]])

        np.testing.assert_allclose(fd([0, 1, 2]), res)
示例#11
0
    def test_init_list_fdatabasis(self):
        weights = np.arange(4 * 5).reshape((5, 4))
        monomial = Monomial((0, 1), nbasis=4)
        fd = FDataBasis(monomial, weights)

        fdlist = [FDataBasis(monomial, weights[i])
                  for i in range(len(weights))]

        lfd = LinearDifferentialOperator(weights=fdlist)

        np.testing.assert_equal(lfd.order, 4,
                                "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")

        contant = Constant((0, 2))
        fdlist.append(FDataBasis(contant, 1))
        np.testing.assert_raises(ValueError, LinearDifferentialOperator,
                                 None, fdlist)
    def test_tensor_monomial_constant(self):

        basis = Tensor([Monomial(n_basis=2), Constant()])

        fd = FDataBasis(basis=basis, coefficients=[1, 1])

        self.assertEqual(fd.dim_domain, 2)
        self.assertEqual(fd.dim_codomain, 1)

        np.testing.assert_allclose(fd([0., 0.]), [[[1.]]])

        np.testing.assert_allclose(fd([0.5, 0.5]), [[[1.5]]])

        np.testing.assert_allclose(fd([(0., 0.), (0.5, 0.5)]),
                                   [[[1.0], [1.5]]])

        fd_grid = fd.to_grid()

        fd2 = fd_grid.to_basis(basis)

        np.testing.assert_allclose(fd.coefficients, fd2.coefficients)
示例#13
0
    def _argcheck(self, y, x, weights=None):
        """Do some checks to types and shapes"""
        if all(not isinstance(i, FData) for i in x):
            raise ValueError("All the dependent variable are scalar.")
        if any(isinstance(i, FData) for i in y):
            raise ValueError(
                "Some of the independent variables are not scalar")

        ylen = len(y)
        xlen = len(x)
        blen = len(self.beta_basis)
        domain_range = ([i for i in x if isinstance(i, FData)][0].domain_range)

        if blen != xlen:
            raise ValueError("Number of regression coefficients does"
                             " not match number of independent variables.")

        for j in range(xlen):
            if isinstance(x[j], list):
                xjcoefs = np.array(x[j]).reshape((-1, 1))
                x[j] = FDataBasis(Constant(domain_range), xjcoefs)

        if any(ylen != xfd.n_samples for xfd in x):
            raise ValueError("The number of samples on independent and "
                             "dependent variables should be the same")

        if any(not isinstance(b, Basis) for b in self.beta_basis):
            raise ValueError("Betas should be a list of Basis.")

        if weights is None:
            weights = [1 for _ in range(ylen)]

        if len(weights) != ylen:
            raise ValueError("The number of weights should be equal to the "
                             "independent samples.")

        if np.any(np.array(weights) < 0):
            raise ValueError("The weights should be non negative values")

        return y, x, weights
示例#14
0
    def test_init_list_fdatabasis(self):
        """Test initialization with functional weights."""

        n_basis = 4
        n_weights = 6

        monomial = Monomial((0, 1), n_basis=n_basis)

        weights = np.arange(n_basis * n_weights).reshape((n_weights, n_basis))

        fd = FDataBasis(monomial, weights)

        fdlist = [FDataBasis(monomial, w) for w in weights]
        lfd = LinearDifferentialOperator(weights=fdlist)

        np.testing.assert_equal(
            lfd.weights, list(fd),
            "Wrong list of weight functions of the linear operator")

        # Check failure if intervals do not match
        constant = Constant((0, 2))
        fdlist.append(FDataBasis(constant, 1))
        with np.testing.assert_raises(ValueError):
            LinearDifferentialOperator(weights=fdlist)
示例#15
0
    def test_constant_penalty(self):
        basis = Constant(domain_range=(0, 3))

        res = np.array([[12]])

        self._test_penalty(basis, linear_diff_op=[2, 3, 4], result=res)