Exemple #1
0
    def test_nanvar_nans(self):
        samples = np.nan * np.ones(2 * self.samples.shape[0])
        samples[::2] = self.samples

        actual_variance = nanops.nanvar(samples, skipna=True)
        tm.assert_almost_equal(actual_variance, self.variance, check_less_precise=2)

        actual_variance = nanops.nanvar(samples, skipna=False)
        tm.assert_almost_equal(actual_variance, np.nan, check_less_precise=2)
Exemple #2
0
    def test_nanvar_nans(self):
        samples = np.nan * np.ones(2 * self.samples.shape[0])
        samples[::2] = self.samples

        actual_variance = nanops.nanvar(samples, skipna=True)
        np.testing.assert_almost_equal(actual_variance, self.variance, decimal=2)

        actual_variance = nanops.nanvar(samples, skipna=False)
        np.testing.assert_almost_equal(actual_variance, np.nan, decimal=2)
Exemple #3
0
 def var(self, axis=None, dtype=None, out=None, ddof=1, keepdims=False,
         skipna=True):
     nv.validate_stat_ddof_func((), dict(dtype=dtype, out=out,
                                         keepdims=keepdims),
                                fname='var')
     return nanops.nanvar(self._ndarray, axis=axis, skipna=skipna,
                          ddof=ddof)
Exemple #4
0
    def test_ground_truth(self):
        # Test against values that were precomputed with Numpy.
        samples = np.empty((4, 4))
        samples[:3, :3] = np.array([[0.97303362, 0.21869576, 0.55560287
                                     ], [0.72980153, 0.03109364, 0.99155171],
                                    [0.09317602, 0.60078248, 0.15871292]])
        samples[3] = samples[:, 3] = np.nan

        # Actual variances along axis=0, 1 for ddof=0, 1, 2
        variance = np.array([[[0.13762259, 0.05619224, 0.11568816
                               ], [0.20643388, 0.08428837, 0.17353224],
                              [0.41286776, 0.16857673, 0.34706449]],
                             [[0.09519783, 0.16435395, 0.05082054
                               ], [0.14279674, 0.24653093, 0.07623082],
                              [0.28559348, 0.49306186, 0.15246163]]])

        # Test nanvar.
        for axis in range(2):
            for ddof in range(3):
                var = nanops.nanvar(samples, skipna=True, axis=axis, ddof=ddof)
                tm.assert_almost_equal(var[:3], variance[axis, ddof])
                self.assertTrue(np.isnan(var[3]))

        # Test nanstd.
        for axis in range(2):
            for ddof in range(3):
                std = nanops.nanstd(samples, skipna=True, axis=axis, ddof=ddof)
                tm.assert_almost_equal(std[:3], variance[axis, ddof] ** 0.5)
                self.assertTrue(np.isnan(std[3]))
Exemple #5
0
    def test_nanvar_ddof(self):
        n = 5
        samples = self.prng.uniform(size=(10000, n + 1))
        samples[:, -1] = np.nan  # Force use of our own algorithm.

        variance_0 = nanops.nanvar(samples, axis=1, skipna=True, ddof=0).mean()
        variance_1 = nanops.nanvar(samples, axis=1, skipna=True, ddof=1).mean()
        variance_2 = nanops.nanvar(samples, axis=1, skipna=True, ddof=2).mean()

        # The unbiased estimate.
        var = 1.0 / 12
        np.testing.assert_almost_equal(variance_1, var, decimal=2)
        # The underestimated variance.
        np.testing.assert_almost_equal(variance_0, (n - 1.0) / n * var, decimal=2)
        # The overestimated variance.
        np.testing.assert_almost_equal(variance_2, (n - 1.0) / (n - 2.0) * var, decimal=2)
Exemple #6
0
    def test_nanvar_axis(self):
        # Generate some sample data.
        samples_norm = self.samples
        samples_unif = self.prng.uniform(size=samples_norm.shape[0])
        samples = np.vstack([samples_norm, samples_unif])

        actual_variance = nanops.nanvar(samples, axis=1)
        tm.assert_almost_equal(actual_variance, np.array([self.variance, 1.0 / 12]), check_less_precise=2)
Exemple #7
0
    def test_nanvar_ddof(self):
        n = 5
        samples = self.prng.uniform(size=(10000, n + 1))
        samples[:, -1] = np.nan  # Force use of our own algorithm.

        variance_0 = nanops.nanvar(samples, axis=1, skipna=True, ddof=0).mean()
        variance_1 = nanops.nanvar(samples, axis=1, skipna=True, ddof=1).mean()
        variance_2 = nanops.nanvar(samples, axis=1, skipna=True, ddof=2).mean()

        # The unbiased estimate.
        var = 1.0 / 12
        tm.assert_almost_equal(variance_1, var, rtol=1e-2)

        # The underestimated variance.
        tm.assert_almost_equal(variance_0, (n - 1.0) / n * var, rtol=1e-2)

        # The overestimated variance.
        tm.assert_almost_equal(variance_2, (n - 1.0) / (n - 2.0) * var, rtol=1e-2)
Exemple #8
0
    def test_nanstd_nans(self):
        samples = np.nan * np.ones(2 * self.samples.shape[0])
        samples[::2] = self.samples

        actual_std = nanops.nanstd(samples, skipna=True)
        tm.assert_almost_equal(actual_std, self.variance ** 0.5, rtol=1e-2)

        actual_std = nanops.nanvar(samples, skipna=False)
        tm.assert_almost_equal(actual_std, np.nan, rtol=1e-2)
Exemple #9
0
    def test_nanvar_axis(self):
        # Generate some sample data.
        samples_norm = self.samples
        samples_unif = self.prng.uniform(size=samples_norm.shape[0])
        samples = np.vstack([samples_norm, samples_unif])

        actual_variance = nanops.nanvar(samples, axis=1)
        tm.assert_almost_equal(actual_variance, np.array(
            [self.variance, 1.0 / 12]), check_less_precise=2)
Exemple #10
0
 def var(
     self,
     *,
     axis=None,
     dtype: Optional[NpDtype] = None,
     out=None,
     ddof=1,
     keepdims=False,
     skipna=True,
 ):
     nv.validate_stat_ddof_func(
         (), {"dtype": dtype, "out": out, "keepdims": keepdims}, fname="var"
     )
     result = nanops.nanvar(self._ndarray, axis=axis, skipna=skipna, ddof=ddof)
     return self._wrap_reduction_result(axis, result)
Exemple #11
0
 def var(self,
         axis=None,
         dtype=None,
         out=None,
         ddof=1,
         keepdims=False,
         skipna=True):
     nv.validate_stat_ddof_func((),
                                dict(dtype=dtype,
                                     out=out,
                                     keepdims=keepdims),
                                fname="var")
     return nanops.nanvar(self._ndarray,
                          axis=axis,
                          skipna=skipna,
                          ddof=ddof)
Exemple #12
0
    def test_ground_truth(self):
        # Test against values that were precomputed with Numpy.
        samples = np.empty((4, 4))
        samples[:3, :3] = np.array(
            [
                [0.97303362, 0.21869576, 0.55560287],
                [0.72980153, 0.03109364, 0.99155171],
                [0.09317602, 0.60078248, 0.15871292],
            ]
        )
        samples[3] = samples[:, 3] = np.nan

        # Actual variances along axis=0, 1 for ddof=0, 1, 2
        variance = np.array(
            [
                [
                    [0.13762259, 0.05619224, 0.11568816],
                    [0.20643388, 0.08428837, 0.17353224],
                    [0.41286776, 0.16857673, 0.34706449],
                ],
                [
                    [0.09519783, 0.16435395, 0.05082054],
                    [0.14279674, 0.24653093, 0.07623082],
                    [0.28559348, 0.49306186, 0.15246163],
                ],
            ]
        )

        # Test nanvar.
        for axis in range(2):
            for ddof in range(3):
                var = nanops.nanvar(samples, skipna=True, axis=axis, ddof=ddof)
                tm.assert_almost_equal(var[:3], variance[axis, ddof])
                assert np.isnan(var[3])

        # Test nanstd.
        for axis in range(2):
            for ddof in range(3):
                std = nanops.nanstd(samples, skipna=True, axis=axis, ddof=ddof)
                tm.assert_almost_equal(std[:3], variance[axis, ddof] ** 0.5)
                assert np.isnan(std[3])
Exemple #13
0
 def test_nanvar_all_finite(self):
     samples = self.samples
     actual_variance = nanops.nanvar(samples)
     np.testing.assert_almost_equal(actual_variance,
                                    self.variance,
                                    decimal=2)
Exemple #14
0
 def test_nanvar_all_finite(self):
     samples = self.samples
     actual_variance = nanops.nanvar(samples)
     tm.assert_almost_equal(actual_variance, self.variance,
                            check_less_precise=2)
Exemple #15
0
 def test_nanvar_all_finite(self):
     samples = self.samples
     actual_variance = nanops.nanvar(samples)
     tm.assert_almost_equal(actual_variance, self.variance, rtol=1e-2)
Exemple #16
0
 def test_nanvar_all_finite(self):
     samples = self.samples
     actual_variance = nanops.nanvar(samples)
     tm.assert_almost_equal(actual_variance,
                            self.variance,
                            check_less_precise=2)
Exemple #17
0
 def test_nanvar_all_finite(self):
     samples = self.samples
     actual_variance = nanops.nanvar(samples)
     np.testing.assert_almost_equal(
         actual_variance, self.variance, decimal=2)