GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#10)
by
unknown
02:20
created

test_chi2cdf()   A

Complexity

Conditions 4

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
c 1
b 0
f 0
dl 0
loc 9
rs 9.2
1
# Copyright (c) 2014, Salesforce.com, Inc.  All rights reserved.
2
# Copyright (c) 2015, Gamelan Labs, Inc.
3
# Copyright (c) 2016, Google, Inc.
4
#
5
# Redistribution and use in source and binary forms, with or without
6
# modification, are permitted provided that the following conditions
7
# are met:
8
#
9
# - Redistributions of source code must retain the above copyright
10
#   notice, this list of conditions and the following disclaimer.
11
# - Redistributions in binary form must reproduce the above copyright
12
#   notice, this list of conditions and the following disclaimer in the
13
#   documentation and/or other materials provided with the distribution.
14
# - Neither the name of Salesforce.com nor the names of its contributors
15
#   may be used to endorse or promote products derived from this
16
#   software without specific prior written permission.
17
#
18
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
22
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
25
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
27
# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
28
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30
from __future__ import division
31
try:
32
    from itertools import izip as zip
33
except ImportError:
34
    pass
35
import numpy
36
import scipy.stats
37
from numpy import pi
38
from numpy.testing import rand
39
from nose import SkipTest
40
from nose.tools import assert_almost_equal
41
from nose.tools import assert_equal
42
from nose.tools import assert_greater
43
from nose.tools import assert_less
44
from goftests import seed_all
45
from goftests import get_dim
46
from goftests import multinomial_goodness_of_fit
47
from goftests import discrete_goodness_of_fit
48
from goftests import auto_density_goodness_of_fit
49
from goftests import mixed_density_goodness_of_fit
50
from goftests import split_discrete_continuous
51
from goftests import volume_of_sphere
52
from goftests import chi2sf
53
54
NUM_BASE_SAMPLES = 250
55
56
NUM_SAMPLES_SCALE = 1000
57
58
TEST_FAILURE_RATE = 5e-4
59
60
61
def test_chi2cdf(xmin=0.0, xmax=100.0, nx=500, smin=1, smax=41, sstep=1.5):
62
    xlist = numpy.linspace(xmin, xmax, nx)
63
    slist = numpy.arange(smin, smax, sstep)
64
    for s in slist:
65
        for x in xlist:
66
            delta = scipy.stats.chi2.sf(x, s) - chi2sf(x, s)
67
            if delta > 1e-12:
68
                print s, x, delta, scipy.stats.chi2.sf(x, s), chi2sf(x, s)
69
            assert_almost_equal(delta, 0.0)
70
71
72
def test_multinomial_goodness_of_fit():
73
    for dim in range(2, 20):
74
        yield _test_multinomial_goodness_of_fit, dim
75
76
77
def _test_multinomial_goodness_of_fit(dim):
78
    seed_all(0)
79
    sample_count = int(1e5)
80
    probs = numpy.random.dirichlet([1] * dim)
81
82
    counts = numpy.random.multinomial(sample_count, probs)
83
    p_good = multinomial_goodness_of_fit(probs, counts, sample_count)
84
    assert_greater(p_good, TEST_FAILURE_RATE)
85
86
    unif_counts = numpy.random.multinomial(sample_count, [1. / dim] * dim)
87
    p_bad = multinomial_goodness_of_fit(probs, unif_counts, sample_count)
88
    assert_less(p_bad, TEST_FAILURE_RATE)
89
90
91
def test_volume_of_sphere():
92
    for r in [0.1, 1.0, 10.0]:
93
        assert_almost_equal(volume_of_sphere(1, r), 2.0 * r)
94
        assert_almost_equal(volume_of_sphere(2, r), pi * r ** 2)
95
        assert_almost_equal(volume_of_sphere(3, r), 4 / 3.0 * pi * r ** 3)
96
97
98
split_examples = [
99
    {'mixed': False, 'discrete': False, 'continuous': []},
100
    {'mixed': 0, 'discrete': 0, 'continuous': []},
101
    {'mixed': 'abc', 'discrete': 'abc', 'continuous': []},
102
    {'mixed': 0.0, 'discrete': None, 'continuous': [0.0]},
103
    {'mixed': (), 'discrete': (), 'continuous': []},
104
    {'mixed': [], 'discrete': (), 'continuous': []},
105
    {'mixed': (0,), 'discrete': (0, ), 'continuous': []},
106
    {'mixed': [0, ], 'discrete': (0, ), 'continuous': []},
107
    {'mixed': (0.0, ), 'discrete': (None, ), 'continuous': [0.0]},
108
    {'mixed': [0.0, ], 'discrete': (None, ), 'continuous': [0.0]},
109
    {
110
        'mixed': [True, 1, 'xyz', 3.14, [None, (), ([2.71],)]],
111
        'discrete': (True, 1, 'xyz', None, (None, (), ((None,),))),
112
        'continuous': [3.14, 2.71],
113
    },
114
    {
115
        'mixed': numpy.zeros(3),
116
        'discrete': (None, None, None),
117
        'continuous': [0.0, 0.0, 0.0],
118
    },
119
]
120
121
122
def split_example(i):
123
    example = split_examples[i]
124
    discrete, continuous = split_discrete_continuous(example['mixed'])
125
    assert_equal(discrete, example['discrete'])
126
    assert_almost_equal(continuous, example['continuous'])
127
128
129
def test_split_continuous_discrete():
130
    for i in range(len(split_examples)):
131
        yield split_example, i
132
133
134
seed_all(0)
135
default_params = {
136
    'bernoulli': [(0.2,)],
137
    'beta': [
138
        (0.5, 0.5),
139
        (0.5, 1.5),
140
        (0.5, 2.5),
141
    ],
142
    'binom': [(40, 0.4)],
143
    'dirichlet': [
144
        ([2.0, 2.5],),
145
        ([2.0, 2.5, 3.0],),
146
        ([2.0, 2.5, 3.0, 3.5],),
147
    ],
148
    'erlang': [(7,)],
149
    'dlaplace': [(0.8,)],
150
    'frechet': [tuple(2 * rand(1)) + (0,) + tuple(2 * rand(2))],
151
    'geom': [(0.1,)],
152
    'hypergeom': [(40, 14, 24)],
153
    'logser': [(0.9,)],
154
    'multivariate_normal': [
155
        (numpy.ones(1), numpy.eye(1)),
156
        (numpy.ones(2), numpy.eye(2)),
157
        (numpy.ones(3), numpy.eye(3)),
158
    ],
159
    'nbinom': [(40, 0.4)],
160
    'ncf': [(27, 27, 0.415784417992)],
161
    'planck': [(0.51,)],
162
    'poisson': [(20,)],
163
    'reciprocal': [tuple(numpy.array([0, 1]) + rand(1)[0])],
164
    'trapz': [(0.333, 0.666)],
165
    'triang': [tuple(rand(1))],
166
    'truncnorm': [(0.1, 2.0)],
167
    'vonmises': [tuple(1.0 + rand(1))],
168
    'wrapcauchy': [(0.5,)],
169
    'zipf': [(1.2,)],
170
}
171
172
known_failures = set([
173
    'alpha',
174
    'boltzmann',
175
    'gausshyper',  # very slow
176
    'ksone',  # ???
177
    'levy_stable',  # ???
178
    'ortho_group',  # matrix
179
    'randint',  # too sparse
180
    'random_correlation',  # matrix
181
    'rv_continuous',  # abstract
182
    'rv_discrete',  # abstract
183
    'special_ortho_group',  # matrix
184
    'zipf',  # bug?
185
    'invwishart',  # matrix
186
    'wishart',  # matrix
187
    'matrix_normal',  # matrix
188
])
189
190
191
def transform_dirichlet(ps):
192
    dim = len(ps)
193
    assert dim > 1
194
    # return ps[:-1] - ps[-1] * (dim ** 0.5 - 1.0) / (dim - 1.0)
195
    return ps[:-1]
196
197
198
transforms = {
199
    'dirichlet': transform_dirichlet,
200
}
201
202
203
def _test_scipy_stats(name):
204
    if name in known_failures:
205
        raise SkipTest('known failure')
206
    dist = getattr(scipy.stats, name)
207
    try:
208
        params = default_params[name]
209
    except KeyError:
210
        params = [tuple(1.0 + rand(dist.numargs))]
211
    for param in params:
212
        print('param = {}'.format(param))
213
        dim = get_dim(dist.rvs(*param, size=2)[0])
214
        sample_count = NUM_BASE_SAMPLES + NUM_SAMPLES_SCALE * dim
215
        samples = list(dist.rvs(*param, size=sample_count))
216
        if name in transforms:
217
            transformed = list(map(transforms[name], samples))
218
        else:
219
            transformed = samples
220
221
        if hasattr(dist, 'pmf'):
222
            probs = [dist.pmf(sample, *param) for sample in samples]
223
            probs_dict = dict(zip(samples, probs))
224
            gof = discrete_goodness_of_fit(transformed, probs_dict, plot=True)
225
        else:
226
            probs = [dist.pdf(sample, *param) for sample in samples]
227
            gof = auto_density_goodness_of_fit(transformed, probs, plot=True)
228
        assert_greater(gof, TEST_FAILURE_RATE)
229
230
        gof = mixed_density_goodness_of_fit(transformed, probs, plot=True)
231
        assert_greater(gof, TEST_FAILURE_RATE)
232
233
234
def test_scipy_stats():
235
    seed_all(0)
236
    for name in dir(scipy.stats):
237
        if hasattr(getattr(scipy.stats, name), 'rvs'):
238
            yield _test_scipy_stats, name
239