|
| 1 | +#!/usr/bin/env python |
| 2 | +# encoding: utf-8 |
| 3 | + |
| 4 | +# This Source Code Form is subject to the terms of the Mozilla Public |
| 5 | +# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 6 | +# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 7 | + |
| 8 | +""" |
| 9 | +This module implements test coverage for the stats functions in stats.py. |
| 10 | +""" |
| 11 | +import itertools |
| 12 | + |
| 13 | +import numpy.random |
| 14 | +import scipy.stats |
| 15 | + |
| 16 | +import pytest |
| 17 | +from moztelemetry import stats |
| 18 | + |
| 19 | + |
| 20 | +def l2d(values): |
| 21 | + # Convert a list of values to a histogram representation. |
| 22 | + d = {} |
| 23 | + for v in values: |
| 24 | + d[v] = d.get(v, 0) + 1 |
| 25 | + return d |
| 26 | + |
| 27 | + |
| 28 | +# A normally distributed sample set. |
| 29 | +norm1 = list(numpy.random.normal(5, 3.25, 1000)) |
| 30 | +norm2 = list(numpy.random.normal(6, 2.5, 1000)) |
| 31 | + |
| 32 | +# A uniformly distributed sample set. |
| 33 | +uni1 = numpy.random.randint(1, 100, 1000) |
| 34 | +uni2 = numpy.random.randint(10, 120, 900) |
| 35 | + |
| 36 | +# A skewed normal distribution. |
| 37 | +skew1 = list(scipy.stats.skewnorm.rvs(10, size=1000)) |
| 38 | +skew2 = list(scipy.stats.skewnorm.rvs(5, size=900)) |
| 39 | + |
| 40 | + |
| 41 | +samples = { |
| 42 | + 'normalized': (norm1, norm2), |
| 43 | + 'uniform': (uni1, uni2), |
| 44 | + 'skewed': (skew1, skew2), |
| 45 | +} |
| 46 | + |
| 47 | + |
| 48 | +def test_rank(): |
| 49 | + assert stats._rank({1: 1}) == {1: 1.0} |
| 50 | + assert stats._rank({1: 5, 2: 4, 3: 3, 4: 2, 5: 1}) == { |
| 51 | + 1: 3.0, |
| 52 | + 2: 7.5, |
| 53 | + 3: 11.0, |
| 54 | + 4: 13.5, |
| 55 | + 5: 15.0, |
| 56 | + } |
| 57 | + |
| 58 | + |
| 59 | +def test_tie_correct(): |
| 60 | + assert stats._tie_correct({}) == 1.0 |
| 61 | + assert stats._tie_correct({1: 1}) == 1.0 |
| 62 | + |
| 63 | + |
| 64 | +def test_ndtr(): |
| 65 | + # Test invalid values raise an error. |
| 66 | + with pytest.raises(TypeError): |
| 67 | + stats.ndtr(None) |
| 68 | + with pytest.raises(ValueError): |
| 69 | + stats.ndtr('a') |
| 70 | + |
| 71 | + assert round(stats.ndtr(0), 6) == 0.5 |
| 72 | + assert round(stats.ndtr(1), 6) == 0.841345 |
| 73 | + assert round(stats.ndtr(2), 6) == 0.977250 |
| 74 | + assert round(stats.ndtr(3), 6) == 0.998650 |
| 75 | + |
| 76 | + assert round(stats.ndtr(0), 6) == round(scipy.special.ndtr(0), 6) |
| 77 | + assert round(stats.ndtr(1), 6) == round(scipy.special.ndtr(1), 6) |
| 78 | + assert round(stats.ndtr(1.5), 6) == round(scipy.special.ndtr(1.5), 6) |
| 79 | + assert round(stats.ndtr(2), 6) == round(scipy.special.ndtr(2), 6) |
| 80 | + assert round(stats.ndtr(3), 6) == round(scipy.special.ndtr(3), 6) |
| 81 | + |
| 82 | + |
| 83 | +def test_mann_whitney_u(): |
| 84 | + distribution_types = ('normalized', 'uniform', 'skewed') |
| 85 | + |
| 86 | + # Test different distributions against each other, including |
| 87 | + # like distributions against themselves. |
| 88 | + for sample1, sample2 in itertools.product(distribution_types, repeat=2): |
| 89 | + |
| 90 | + arr1, arr2 = samples[sample1][0], samples[sample2][1] |
| 91 | + hist1, hist2 = l2d(arr1), l2d(arr2) |
| 92 | + |
| 93 | + # Basic test, with defaults. |
| 94 | + res = stats.mann_whitney_u(hist1, hist2) |
| 95 | + sci = scipy.stats.mannwhitneyu(arr1, arr2) |
| 96 | + assert res.u == sci.statistic |
| 97 | + assert round(res.p, 6) == round(sci.pvalue, 6) |
| 98 | + |
| 99 | + # Test that order of samples doesn't matter. |
| 100 | + res = stats.mann_whitney_u(hist2, hist1) |
| 101 | + sci = scipy.stats.mannwhitneyu(arr1, arr2) |
| 102 | + assert res.u == sci.statistic |
| 103 | + assert round(res.p, 6) == round(sci.pvalue, 6) |
| 104 | + |
| 105 | + # Test exact same samples. |
| 106 | + res = stats.mann_whitney_u(hist1, hist1) |
| 107 | + sci = scipy.stats.mannwhitneyu(arr1, arr1) |
| 108 | + assert res.u == sci.statistic |
| 109 | + assert round(res.p, 6) == round(sci.pvalue, 6) |
| 110 | + |
| 111 | + # Test with use_continuity = False. |
| 112 | + res = stats.mann_whitney_u(hist1, hist2, False) |
| 113 | + sci = scipy.stats.mannwhitneyu(arr1, arr2, False) |
| 114 | + assert res.u == sci.statistic |
| 115 | + assert round(res.p, 6) == round(sci.pvalue, 6) |
0 commit comments