Exemplo n.º 1
0
 def test_fit_eval2(self):
     """Polynomial1DFit: Basic fitting and evaluation (non-zero-mean)."""
     interp = Polynomial1DFit(2)
     interp.fit(self.x2, self.y2)
     y2 = interp(self.x2)
     assert_almost_equal(interp.poly, self.poly, decimal=10)
     assert_almost_equal(y2, self.y2, decimal=10)
Exemplo n.º 2
0
 def test_randomised_polyfit(self):
     """Randomise: Randomise the fit of a polynomial fitter."""
     interp = Polynomial1DFit(2)
     # Perfect fit (no noise)
     interp.fit(self.x, self.y)
     random_interp = randomise(interp, self.x, self.y, 'unknown')
     y = random_interp(self.x)
     assert_almost_equal(random_interp.poly, self.poly, decimal=10)
     assert_almost_equal(y, self.y, decimal=10)
     random_interp = randomise(interp, self.x, self.y, 'shuffle')
     y = random_interp(self.x)
     assert_almost_equal(random_interp.poly, self.poly, decimal=10)
     assert_almost_equal(y, self.y, decimal=10)
     # Fit polynomials to a set of noisy samples
     noisy_poly = []
     for yn in self.y_noisy:
         interp.fit(self.x, yn)
         noisy_poly.append(interp.poly)
     noisy_poly = np.array(noisy_poly)
     # Randomise polynomial fit to first noisy sample in various ways
     # pylint: disable-msg=W0612
     shuffle_poly = np.array([randomise(interp, self.x, self.y_noisy[0], 'shuffle').poly
                              for n in range(self.num_runs)])
     assert_almost_equal(shuffle_poly.mean(axis=0), noisy_poly[0], decimal=2)
     assert_almost_equal(shuffle_poly.std(axis=0), noisy_poly.std(axis=0), decimal=2)
     normal_poly = np.array([randomise(interp, self.x, self.y_noisy[0], 'normal').poly
                             for n in range(self.num_runs)])
     assert_almost_equal(normal_poly.mean(axis=0), noisy_poly[0], decimal=2)
     assert_almost_equal(normal_poly.std(axis=0), noisy_poly.std(axis=0), decimal=2)
     boot_poly = np.array([randomise(interp, self.x, self.y_noisy[0], 'bootstrap').poly
                           for n in range(self.num_runs)])
     assert_almost_equal(boot_poly.mean(axis=0), noisy_poly[0], decimal=2)
     assert_almost_equal(boot_poly.std(axis=0), noisy_poly.std(axis=0), decimal=2)
Exemplo n.º 3
0
 def test_fit_eval(self):
     """Polynomial1DFit: Basic function fitting + evaluation (zero-mean)."""
     interp = Polynomial1DFit(2)
     self.assertRaises(NotFittedError, interp, self.x)
     interp.fit(self.x, self.y)
     y = interp(self.x)
     self.assertAlmostEqual(interp._mean, 0.0, places=10)
     assert_almost_equal(interp.poly, self.poly, decimal=10)
     assert_almost_equal(y, self.y, decimal=10)
Exemplo n.º 4
0
 def test_vs_numpy(self):
     """Polynomial1DFit: Compare fitter to np.polyfit and np.polyval."""
     x, p = self.randx, self.randp
     y = p[0] * (x**3) + p[1] * (x**2) + p[2] * x + p[3]
     interp = Polynomial1DFit(3)
     interp.fit(x, y)
     interp_y = interp(x)
     np_poly = np.polyfit(x, y, 3)
     np_y = np.polyval(np_poly, x)
     self.assertAlmostEqual(interp._mean, self.randx.mean(), places=10)
     assert_almost_equal(interp.poly, np_poly, decimal=10)
     assert_almost_equal(interp_y, np_y, decimal=10)
Exemplo n.º 5
0
 def test_fit_eval(self):
     """Independent1DFit: Basic function fitting and evaluation using data from a known function."""
     interp = Independent1DFit(Polynomial1DFit(2), self.axis)
     self.assertRaises(NotFittedError, interp, self.x)
     self.assertRaises(ValueError, interp.fit, self.x, self.y_too_low_dim)
     self.assertRaises(ValueError, interp.fit, self.x, self.y_wrong_size)
     interp.fit(self.x, self.y)
     y = interp(self.x)
     self.assertEqual(interp._axis, self.axis)
     self.assertEqual(interp._interps.shape, (2, 3))
     assert_almost_equal(interp._interps[0, 0].poly, self.poly1, decimal=10)
     assert_almost_equal(interp._interps[0, 1].poly, self.poly2, decimal=10)
     assert_almost_equal(interp._interps[0, 2].poly, self.poly1, decimal=10)
     assert_almost_equal(interp._interps[1, 0].poly, self.poly2, decimal=10)
     assert_almost_equal(interp._interps[1, 1].poly, self.poly1, decimal=10)
     assert_almost_equal(interp._interps[1, 2].poly, self.poly2, decimal=10)
     assert_almost_equal(y, self.y, decimal=10)
Exemplo n.º 6
0
 def test_cov_params(self):
     """Polynomial1DFit: Compare parameter stats to covariance matrix."""
     interp = Polynomial1DFit(2)
     std_y = 1.3
     M = 200
     poly_set = np.zeros((len(self.poly), M))
     for n in range(M):
         yn = self.y2 + std_y * np.random.randn(len(self.y2))
         interp.fit(self.x2, yn, std_y)
         poly_set[:, n] = interp.poly
     mean_poly = poly_set.mean(axis=1)
     norm_poly = poly_set - mean_poly[:, np.newaxis]
     cov_poly = np.dot(norm_poly, norm_poly.T) / M
     std_poly = np.sqrt(np.diag(interp.cov_poly))
     self.assertTrue(
         (np.abs(mean_poly - self.poly) / std_poly < 0.5).all(),
         "Sample mean coefficient vector differs too much from true value")
     self.assertTrue(
         (np.abs(cov_poly - interp.cov_poly) / np.abs(interp.cov_poly) <
          0.5).all(),
         "Sample coefficient covariance matrix differs too much")
Exemplo n.º 7
0
 def test_reduce_degree(self):
     """Polynomial1DFit: Reduce polynomial degree if too few data points."""
     interp = Polynomial1DFit(2)
     interp.fit([1.0], [1.0])
     assert_almost_equal(interp.poly, [1.0], decimal=10)