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
01:11
created

incomplete_gamma()   B

Complexity

Conditions 4

Size

Total Lines 27

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 27
rs 8.5806
1
# Copyright (c) 2014, Salesforce.com, Inc.  All rights reserved.
2
# Copyright (c) 2015-2016, 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
"""
31
This module computes the chi^2 survival function and the required
32
functions.
33
34
This was implemented so that we could use the goftests module
35
with pypy. Given that the only scipy dependence inside goftests
36
was from the chi^2 survival function (1 - cdf), and then gamma
37
function.
38
39
"""
40
41
from __future__ import division
42
import math
43
import sys
44
from numpy import inf
45
46
47
def log(x):
48
    if x > sys.float_info.min:
49
        value = math.log(x)
50
    else:
51
        value = -inf
52
    return value
53
54
55
def incomplete_gamma(x, s):
56
    r"""
57
    This function computes the incomplete lower gamma function
58
    using the series expansion:
59
    \gamma(x, s) = x^s \Gamma(s)e^{-x}\sum^\infty_{k=0}
60
                    \frac{x^k}{\Gamma(s + k + 1)}
61
    This series will converge strongly because the Gamma
62
    function grows factorially.
63
64
    Because the Gamma function does grow so quickly, we can
65
    run into numerical stability issues. I have chosen to carry
66
    out as much math as possible in the log domain to reduce
67
    numerical error. This function matches the results from
68
    scipy to numerical precision.
69
    """
70
    value = 0.0
71
    if x < 0.0:
72
        return 1.0
73
    if x > 1e3:
74
        return math.gamma(s)
75
    log_gamma_s = log(math.gamma(s))
76
    log_x = log(x)
77
    for k in range(0, 100):
78
        log_num = (k + s)*log_x + (-x) + log_gamma_s
79
        log_denom = log(math.gamma(k + s + 1))
80
        value += math.exp(log_num - log_denom)
81
    return value
82
83
84
def chi2sf(x, s):
85
    r"""
86
    This function returns the survival function of the chi^2
87
    distribution. The survival function is given as:
88
    1 - CDF
89
    where rhe chi^2 CDF is given as
90
    F(x; s) = \frac{ \gamma( x/2, s/2 ) }{ \Gamma(s/2) },
91
    with $\gamma$ is the incomplete gamma function defined above.
92
    Therefore, the survival probability is givne by:
93
    1 - \frac{ \gamma( x/2, s/2 ) }{ \Gamma(s/2) }.
94
    This function matches the results from
95
    scipy to numerical precision.
96
    """
97
    top = incomplete_gamma(x/2.0, s/2.0)
98
    bottom = math.gamma(s/2.0)
99
    value = top/bottom
100
    return 1.0 - value
101