def test_fourier_penalty(self): basis = Fourier(n_basis=5) np.testing.assert_array_almost_equal( basis.penalty(2).round(2), np.array([[0., 0., 0., 0., 0.], [0., 1558.55, 0., 0., 0.], [0., 0., 1558.55, 0., 0.], [0., 0., 0., 24936.73, 0.], [0., 0., 0., 0., 24936.73]]))
def test_hotelling_t2(self): fd1 = FDataGrid([[1, 1, 1], [1, 1, 1]]) fd2 = FDataGrid([[1, 1, 1], [2, 2, 2]]) self.assertAlmostEqual(hotelling_t2(fd1, fd1), 0) self.assertAlmostEqual(hotelling_t2(fd1, fd2), 1) fd1 = fd1.to_basis(Fourier(n_basis=3)) fd2 = fd2.to_basis(Fourier(n_basis=3)) self.assertAlmostEqual(hotelling_t2(fd1, fd1), 0) self.assertAlmostEqual(hotelling_t2(fd1, fd2), 1)
def test_hotelling_t2_args(self): fd1 = FDataGrid([[1, 1, 1]]) with self.assertRaises(TypeError): hotelling_t2(fd1, []) with self.assertRaises(TypeError): hotelling_t2([], fd1) with self.assertRaises(TypeError): hotelling_t2(fd1.to_basis(Fourier(n_basis=3)), fd1) with self.assertRaises(TypeError): hotelling_t2(fd1, fd1.to_basis(Fourier(n_basis=3)))
def test_basis_gram_matrix_fourier(self): basis = Fourier(n_basis=3) gram_matrix = basis.gram_matrix() gram_matrix_numerical = basis._gram_matrix_numerical() gram_matrix_res = np.identity(3) np.testing.assert_allclose( gram_matrix, gram_matrix_res) np.testing.assert_allclose( gram_matrix_numerical, gram_matrix_res, atol=1e-15, rtol=1e-15)
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_concatenate(self): sample1 = np.arange(0, 10) sample2 = np.arange(10, 20) fd1 = FDataGrid([sample1]).to_basis(Fourier(n_basis=5)) fd2 = FDataGrid([sample2]).to_basis(Fourier(n_basis=5)) fd = concatenate([fd1, fd2]) np.testing.assert_equal(fd.n_samples, 2) np.testing.assert_equal(fd.dim_codomain, 1) np.testing.assert_equal(fd.dim_domain, 1) np.testing.assert_array_equal(fd.coefficients, np.concatenate( [fd1.coefficients, fd2.coefficients]))
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_fdatabasis__mul__(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), [[2, 4, 6]])) np.testing.assert_equal(3 * monomial2, FDataBasis(Monomial(n_basis=3), [[3, 6, 9], [9, 12, 15]])) np.testing.assert_equal(3 * monomial2, monomial2 * 3) np.testing.assert_equal(monomial2 * [1, 2], FDataBasis(Monomial(n_basis=3), [[1, 2, 3], [6, 8, 10]])) np.testing.assert_equal([1, 2] * monomial2, FDataBasis(Monomial(n_basis=3), [[1, 2, 3], [6, 8, 10]])) with np.testing.assert_raises(TypeError): monomial2 * FDataBasis(Fourier(n_basis=3), [[2, 2, 3], [5, 4, 5]]) with np.testing.assert_raises(TypeError): monomial2 * monomial2
def test_asymptotic_behaviour(self): dataset = fetch_gait() fd = dataset['data'].coordinates[1] fd1 = fd[0:5] fd2 = fd[5:10] fd3 = fd[10:15] n_little_sim = 10 sims = np.array([oneway_anova( fd1, fd2, fd3, n_reps=500, random_state=i)[1] for i in range(n_little_sim)]) little_sim = np.mean(sims) big_sim = oneway_anova(fd1, fd2, fd3, n_reps=2000, random_state=100)[1] self.assertAlmostEqual(little_sim, big_sim, delta=0.05) fd = fd.to_basis(Fourier(n_basis=5)) fd1 = fd[0:5] fd2 = fd[5:10] sims = np.array([oneway_anova( fd1, fd2, n_reps=500, random_state=i)[1] for i in range(n_little_sim)]) little_sim = np.mean(sims) big_sim = oneway_anova(fd1, fd2, n_reps=2000, random_state=100)[1] self.assertAlmostEqual(little_sim, big_sim, delta=0.05)
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 ]]))
def test_basis_fpca_transform_result(self): n_basis = 9 n_components = 3 fd_data = fetch_weather()['data'].coordinates[0] fd_data = FDataGrid(np.squeeze(fd_data.data_matrix), np.arange(0.5, 365, 1)) # initialize basis data basis = Fourier(n_basis=n_basis, domain_range=(0, 365)) fd_basis = fd_data.to_basis(basis) fpca = FPCA(n_components=n_components, regularization=TikhonovRegularization( LinearDifferentialOperator(2), regularization_parameter=1e5)) fpca.fit(fd_basis) scores = fpca.transform(fd_basis) # results obtained using Ramsay's R package results = [[-7.68307641e+01, 5.69034443e+01, -1.22440149e+01], [-9.02873996e+01, 1.46262257e+01, -1.78574536e+01], [-8.21155683e+01, 3.19159491e+01, -2.56212328e+01], [-1.14163637e+02, 3.66425562e+01, -1.00810836e+01], [-6.97263223e+01, 1.22817168e+01, -2.39417618e+01], [-6.41886364e+01, -1.07261045e+01, -1.10587407e+01], [1.35824412e+02, 2.03484658e+01, -9.04815324e+00], [-1.46816399e+01, -2.66867491e+01, -1.20233465e+01], [1.02507511e+00, -2.29840736e+01, -9.06081296e+00], [-3.62936903e+01, -2.09520442e+01, -1.14799951e+01], [-4.20649313e+01, -1.13618094e+01, -6.24909009e+00], [-7.38115985e+01, -3.18423866e+01, -1.50298626e+01], [-6.69822456e+01, -3.35518632e+01, -1.25167352e+01], [-1.03534763e+02, -1.29513941e+01, -1.49103879e+01], [-1.04542036e+02, -1.36794907e+01, -1.41555965e+01], [-7.35863347e+00, -1.41171956e+01, -2.97562788e+00], [7.28804530e+00, -5.34421830e+01, -3.39823418e+00], [5.59974094e+01, -4.02154080e+01, 3.78800103e-01], [1.80778702e+02, 1.87798201e+01, -1.99043247e+01], [-3.69700617e+00, -4.19441020e+01, 6.45820740e+00], [3.76527216e+01, -4.23056953e+01, 1.04221757e+01], [1.23850646e+02, -4.24648130e+01, -2.22336786e-01], [-7.23588457e+00, -1.20579536e+01, 2.07502089e+01], [-4.96871011e+01, 8.88483448e+00, 2.02882768e+01], [-1.36726355e+02, -1.86472599e+01, 1.89076217e+01], [-1.83878661e+02, 4.12118550e+01, 1.78960356e+01], [-1.81568820e+02, 5.20817910e+01, 2.01078870e+01], [-5.08775852e+01, 1.34600555e+01, 3.18602712e+01], [-1.37633866e+02, 7.50809631e+01, 2.42320782e+01], [4.98276375e+01, 1.33401270e+00, 3.50611066e+01], [1.51149934e+02, -5.47417776e+01, 3.97592325e+01], [1.58366096e+02, -3.80762686e+01, -5.62415023e+00], [2.17139548e+02, 6.34055987e+01, -1.98853635e+01], [2.33615480e+02, -7.90787574e-02, 2.69069525e+00], [3.45371437e+02, 9.58703622e+01, 8.47570770e+00]] results = np.array(results) # compare results np.testing.assert_allclose(scores, results, atol=1e-7)
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_fourier_penalty(self): basis = Fourier(n_basis=5) res = np.array([[0., 0., 0., 0., 0.], [0., 1558.55, 0., 0., 0.], [0., 0., 1558.55, 0., 0.], [0., 0., 0., 24936.73, 0.], [0., 0., 0., 0., 24936.73]]) # Those comparisons require atol as there are zeros involved self._test_penalty(basis, linear_diff_op=2, atol=0.01, result=res) basis = Fourier(n_basis=9, domain_range=(1, 5)) self._test_penalty(basis, linear_diff_op=[1, 2, 3], atol=1e-7) self._test_penalty(basis, linear_diff_op=[2, 3, 0.1, 1], atol=1e-7) self._test_penalty(basis, linear_diff_op=0, atol=1e-7) self._test_penalty(basis, linear_diff_op=1, atol=1e-7) self._test_penalty(basis, linear_diff_op=3, atol=1e-7)
def test_v_stats(self): n_features = 50 weights = [1, 2, 3] t = np.linspace(0, 1, n_features) m1 = [1 for _ in range(n_features)] m2 = [2 for _ in range(n_features)] m3 = [3 for _ in range(n_features)] fd = FDataGrid([m1, m2, m3], sample_points=t) self.assertEqual(v_sample_stat(fd, weights), 7.0) self.assertAlmostEqual(v_sample_stat(fd.to_basis(Fourier(n_basis=5)), weights), 7.0) res = (1 - 2 * np.sqrt(1 / 2)) ** 2 + (1 - 3 * np.sqrt(1 / 3)) ** 2 \ + (2 - 3 * np.sqrt(2 / 3)) ** 2 self.assertAlmostEqual(v_asymptotic_stat(fd, weights), res) self.assertAlmostEqual(v_asymptotic_stat(fd.to_basis(Fourier( n_basis=5)), weights), res)
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)
def test_functional_response_basis(self): knnr = KNeighborsRegressor(weights='distance', n_neighbors=5) response = self.X.to_basis(Fourier(domain_range=(-1, 1), n_basis=10)) knnr.fit(self.X, response) res = knnr.predict(self.X) np.testing.assert_array_almost_equal(res.coefficients, response.coefficients)
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_domain_in_list_fourier(self): """Test the evaluation of FDataBasis""" for fourier in (Fourier(domain_range=[(0, 1)], nbasis=3), Fourier(domain_range=((0, 1),), nbasis=3), Fourier(domain_range=np.array((0, 1)), nbasis=3), Fourier(domain_range=np.array([(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 = np.array([0.905, 0.147, -1.05, 0.905, 0.303, 0.775, -1.024, 0.303]).reshape((2, 4)) 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_error_X_not_FData(self): """Tests that at least one of the explanatory variables is an FData object. """ x_fd = np.identity(7) y = np.zeros(7) scalar = LinearScalarRegression([Fourier(n_basis=5)]) np.testing.assert_raises(ValueError, scalar.fit, [x_fd], y)
def test_shift_registration_deltas(self): fd = make_sinusoidal_process(n_samples=2, error_std=0, random_state=1) deltas = shift_registration_deltas(fd).round(3) np.testing.assert_array_almost_equal(deltas, [-0.022, 0.03]) fd = fd.to_basis(Fourier()) deltas = shift_registration_deltas(fd).round(3) np.testing.assert_array_almost_equal(deltas, [-0.022, 0.03])
def test_fdatabasis_derivative_fourier(self): fourier = FDataBasis(Fourier(n_basis=7), [1, 5, 8, 9, 8, 4, 5]) fourier2 = FDataBasis( Fourier(n_basis=5), [[4, 9, 7, 4, 3], [1, 7, 9, 8, 5], [4, 6, 6, 6, 8]]) fou0 = fourier.derivative(order=0) fou1 = fourier.derivative() fou2 = fourier.derivative(order=2) np.testing.assert_equal(fou1.basis, fourier.basis) np.testing.assert_almost_equal( fou1.coefficients.round(5), np.atleast_2d([ 0, -50.26548, 31.41593, -100.53096, 113.09734, -94.24778, 75.39822 ])) np.testing.assert_equal(fou0, fourier) np.testing.assert_equal(fou2.basis, fourier.basis) np.testing.assert_almost_equal( fou2.coefficients.round(5), np.atleast_2d([ 0, -197.39209, -315.82734, -1421.22303, -1263.30936, -1421.22303, -1776.52879 ])) fou0 = fourier2.derivative(order=0) fou1 = fourier2.derivative() fou2 = fourier2.derivative(order=2) np.testing.assert_equal(fou1.basis, fourier2.basis) np.testing.assert_almost_equal( fou1.coefficients.round(5), [[0, -43.98230, 56.54867, -37.69911, 50.26548], [0, -56.54867, 43.98230, -62.83185, 100.53096], [0, -37.69911, 37.69911, -100.53096, 75.39822]]) np.testing.assert_equal(fou0, fourier2) np.testing.assert_equal(fou2.basis, fourier2.basis) np.testing.assert_almost_equal( fou2.coefficients.round(5), [[0, -355.30576, -276.34892, -631.65468, -473.74101], [0, -276.34892, -355.30576, -1263.30936, -789.56835], [0, -236.87051, -236.87051, -947.48202, -1263.30936]])
def test_error_X_not_FData(self): """Tests that at least one of the explanatory variables is an FData object. """ x_fd = np.identity(7) y = np.zeros(7) scalar = LinearRegression(coef_basis=[Fourier(n_basis=5)]) with np.testing.assert_warns(UserWarning): scalar.fit([x_fd], y)
def test_functional_response_custom_weights(self): def weights(weights): return np.array([w == 0 for w in weights], dtype=float) knnr = KNeighborsRegressor(weights=weights, n_neighbors=5) response = self.X.to_basis(Fourier(domain_range=(-1, 1), n_basis=10)) knnr.fit(self.X, response) res = knnr.predict(self.X) np.testing.assert_array_almost_equal(res.coefficients, response.coefficients)
def test_error_X_beta_len_distinct(self): """ Test that the number of beta bases and explanatory variables are not different """ x_fd = FDataBasis(Monomial(n_basis=7), np.identity(7)) y = [1 for _ in range(7)] beta = Fourier(n_basis=5) scalar = LinearScalarRegression([beta]) np.testing.assert_raises(ValueError, scalar.fit, [x_fd, x_fd], y) scalar = LinearScalarRegression([beta, beta]) np.testing.assert_raises(ValueError, scalar.fit, [x_fd], y)
def test_basis_fourier_product(self): # Test when periods are the same fourier = Fourier(n_basis=5) fourier2 = Fourier(n_basis=3) prod = Fourier(n_basis=7) self.assertEqual(fourier.basis_of_product(fourier2), prod) # Test when periods are different fourier2 = Fourier(n_basis=3, period=2) prod = BSpline(n_basis=9, order=8) self.assertEqual(fourier.basis_of_product(fourier2), prod)
def test_score_functional_response(self): neigh = KNeighborsRegressor() y = 5 * self.X + 1 neigh.fit(self.X, y) r = neigh.score(self.X, y) np.testing.assert_almost_equal(r, 0.962651178452408) # Weighted case and basis form y = y.to_basis(Fourier(domain_range=y.domain_range[0], n_basis=5)) neigh.fit(self.X, y) r = neigh.score(self.X[:7], y[:7], sample_weight=4 * [1. / 5] + 3 * [1. / 15]) np.testing.assert_almost_equal(r, 0.9982527586114364)
def test_basis_fpca_fit_attributes(self): fpca = FPCA() with self.assertRaises(AttributeError): fpca.fit(None) basis = Fourier(n_basis=1) # check that if n_components is bigger than the number of samples then # an exception should be thrown fd = FDataBasis(basis, [[0.9]]) with self.assertRaises(AttributeError): fpca.fit(fd) # check that n_components must be smaller than the number of elements # of target basis fd = FDataBasis(basis, [[0.9], [0.7], [0.5]]) with self.assertRaises(AttributeError): fpca.fit(fd)
def test_fdatabasis__add__(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 + monomial2).equals( FDataBasis(Monomial(n_basis=3), [[2, 4, 6], [4, 6, 8]]))) self.assertTrue((monomial2 + 1).equals( FDataBasis(Monomial(n_basis=3), [[2, 2, 3], [4, 4, 5]]))) self.assertTrue((1 + monomial2).equals( FDataBasis(Monomial(n_basis=3), [[2, 2, 3], [4, 4, 5]]))) self.assertTrue((monomial2 + [1, 2]).equals( FDataBasis(Monomial(n_basis=3), [[2, 2, 3], [5, 4, 5]]))) self.assertTrue(([1, 2] + monomial2).equals( FDataBasis(Monomial(n_basis=3), [[2, 2, 3], [5, 4, 5]]))) with np.testing.assert_raises(TypeError): monomial2 + FDataBasis(Fourier(n_basis=3), [[2, 2, 3], [5, 4, 5]])
def test_fdatabasis__sub__(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 - monomial2).equals( FDataBasis(Monomial(n_basis=3), [[0, 0, 0], [-2, -2, -2]]))) self.assertTrue((monomial2 - 1).equals( FDataBasis(Monomial(n_basis=3), [[0, 2, 3], [2, 4, 5]]))) self.assertTrue((1 - monomial2).equals( FDataBasis(Monomial(n_basis=3), [[0, -2, -3], [-2, -4, -5]]))) self.assertTrue((monomial2 - [1, 2]).equals( FDataBasis(Monomial(n_basis=3), [[0, 2, 3], [1, 4, 5]]))) self.assertTrue(([1, 2] - monomial2).equals( FDataBasis(Monomial(n_basis=3), [[0, -2, -3], [-1, -4, -5]]))) with np.testing.assert_raises(TypeError): monomial2 - FDataBasis(Fourier(n_basis=3), [[2, 2, 3], [5, 4, 5]])
def test_evaluation_point_fourier(self): """Test the evaluation of a single point FDataBasis""" 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) # Test different ways of call f with a point res = np.array([-0.903918107989282, -0.267163981229459] ).reshape((2, 1)).round(4) np.testing.assert_array_almost_equal(f([0.5]).round(4), res) np.testing.assert_array_almost_equal(f((0.5,)).round(4), res) np.testing.assert_array_almost_equal(f(0.5).round(4), res) np.testing.assert_array_almost_equal(f(np.array([0.5])).round(4), res)