def test_compute_stats(): np = pytest.importorskip("numpy") np.random.seed(1) assert statistics.compute_stats([], 1) == (None, None) assert statistics.compute_stats([15.0], 1) == (15.0, { 'ci_99_a': -math.inf, 'ci_99_b': math.inf, 'number': 1, 'q_25': 15.0, 'q_75': 15.0, 'repeat': 1 }) for nsamples, true_mean in product([10, 50, 250], [0, 0.3, 0.6]): samples = np.random.randn(nsamples) + true_mean result, stats = statistics.compute_stats(samples, 42) assert stats['repeat'] == len(samples) assert stats['number'] == 42 assert np.allclose(stats['q_25'], np.percentile(samples, 25)) assert np.allclose(stats['q_75'], np.percentile(samples, 75)) assert np.allclose(result, np.median(samples)) ci = stats['ci_99_a'], stats['ci_99_b'] assert ci[0] <= true_mean <= ci[1] w = 12.0 * np.std(samples) / np.sqrt(len(samples)) assert ci[1] - ci[0] < w err = statistics.get_err(result, stats) iqr = np.percentile(samples, 75) - np.percentile(samples, 25) assert np.allclose(err, iqr / 2)
def test_compute_stats(): np.random.seed(1) assert statistics.compute_stats([]) == (None, None) assert statistics.compute_stats([15.0]) == (15.0, None) for nsamples, true_mean in product([10, 50, 250], [0, 0.3, 0.6]): samples = np.random.randn(nsamples) + true_mean result, stats = statistics.compute_stats(samples) assert np.allclose(stats['systematic'], 0) assert np.allclose(stats['n'], len(samples)) assert np.allclose(stats['mean'], np.mean(samples)) assert np.allclose(stats['q_25'], np.percentile(samples, 25)) assert np.allclose(stats['q_75'], np.percentile(samples, 75)) assert np.allclose(stats['min'], np.min(samples)) assert np.allclose(stats['max'], np.max(samples)) assert np.allclose(stats['std'], np.std(samples, ddof=0)) assert np.allclose(result, np.median(samples)) ci = stats['ci_99'] assert ci[0] <= true_mean <= ci[1] w = 12.0 * np.std(samples) / np.sqrt(len(samples)) assert ci[1] - ci[0] < w err = statistics.get_err(result, stats) iqr = np.percentile(samples, 75) - np.percentile(samples, 25) assert np.allclose(err, iqr/2)
def test_is_different(): np.random.seed(1) # Smoke test is_different for true_mean, n, significant in [(0.05, 10, False), (0.05, 100, True), (0.1, 10, True)]: samples_a = 0 + 0.1 * np.random.rand(n) samples_b = true_mean + 0.1 * np.random.rand(n) result_a, stats_a = statistics.compute_stats(samples_a) result_b, stats_b = statistics.compute_stats(samples_b) assert statistics.is_different(stats_a, stats_b) == significant
def test_is_different(): np = pytest.importorskip("numpy") np.random.seed(1) # Smoke test is_different for true_mean, n, significant in [(0.01, 10, False), (0.05, 100, True), (0.1, 10, True)]: samples_a = 0 + 0.1 * np.random.rand(n) samples_b = true_mean + 0.1 * np.random.rand(n) _, stats_a = statistics.compute_stats(samples_a, 1) _, stats_b = statistics.compute_stats(samples_b, 1) assert statistics.is_different(None, None, stats_a, stats_b) == significant assert statistics.is_different(samples_a, samples_b, stats_a, stats_b) == significant
def test_compute_stats(): np.random.seed(1) assert statistics.compute_stats([], 1) == (None, None) assert statistics.compute_stats([15.0], 1) == (15.0, { 'ci_99': [-inf, inf], 'max': 15.0, 'mean': 15.0, 'min': 15.0, 'number': 1, 'q_25': 15.0, 'q_75': 15.0, 'repeat': 1, 'std': 0.0 }) for nsamples, true_mean in product([10, 50, 250], [0, 0.3, 0.6]): samples = np.random.randn(nsamples) + true_mean result, stats = statistics.compute_stats(samples, 42) assert stats['repeat'] == len(samples) assert stats['number'] == 42 assert np.allclose(stats['mean'], np.mean(samples)) assert np.allclose(stats['q_25'], np.percentile(samples, 25)) assert np.allclose(stats['q_75'], np.percentile(samples, 75)) assert np.allclose(stats['min'], np.min(samples)) assert np.allclose(stats['max'], np.max(samples)) assert np.allclose(stats['std'], np.std(samples, ddof=0)) assert np.allclose(result, np.median(samples)) ci = stats['ci_99'] assert ci[0] <= true_mean <= ci[1] w = 12.0 * np.std(samples) / np.sqrt(len(samples)) assert ci[1] - ci[0] < w err = statistics.get_err(result, stats) iqr = np.percentile(samples, 75) - np.percentile(samples, 25) assert np.allclose(err, iqr / 2)