纯Python反向错误函数

时间:2017-02-22 01:47:17

标签: python math scipy

是否存在逆错误函数的 pure-python 实现?

我知道SciPy有scipy.special.erfinv(),但这依赖于一些C扩展。我喜欢纯粹的python实现。

我尝试使用WikipediaWolfram引用来编写自己的引用,但是当arg为>时,它似乎总是偏离真实值。 0.9。

我还试图移植Scipy使用的基础C代码(ndtri.ccephes polevl.c函数),但这也没有通过我的单元测试。

编辑:根据要求,我已添加移植的代码。

Docstrings(和doctests)已被删除,因为它们比函数更长。我还没有付出太多努力让端口更加pythonic - 一旦我得到通过单元测试的东西,我就会担心。

支持cephes polevl.c

的功能
def polevl(x, coefs, N):
    ans = 0
    power = len(coefs) - 1
    for coef in coefs[:N]:
        ans += coef * x**power
        power -= 1

    return ans

def p1evl(x, coefs, N):
    return polevl(x, [1] + coefs, N)

主要反向错误功能

def inv_erf(z):
    if z < -1 or z > 1:
        raise ValueError("`z` must be between -1 and 1 inclusive")

    if z == 0:
        return 0
    if z == 1:
        return math.inf
    if z == -1:
        return -math.inf

    # From scipy special/cephes/ndrti.c
    def ndtri(y):
        # approximation for 0 <= abs(z - 0.5) <= 3/8
        P0 = [
            -5.99633501014107895267E1,
            9.80010754185999661536E1,
            -5.66762857469070293439E1,
            1.39312609387279679503E1,
            -1.23916583867381258016E0,
        ]

        Q0 = [
            1.95448858338141759834E0,
            4.67627912898881538453E0,
            8.63602421390890590575E1,
            -2.25462687854119370527E2,
            2.00260212380060660359E2,
            -8.20372256168333339912E1,
            1.59056225126211695515E1,
            -1.18331621121330003142E0,
        ]

        # Approximation for interval z = sqrt(-2 log y ) between 2 and 8
        # i.e., y between exp(-2) = .135 and exp(-32) = 1.27e-14.
        P1 = [
            4.05544892305962419923E0,
            3.15251094599893866154E1,
            5.71628192246421288162E1,
            4.40805073893200834700E1,
            1.46849561928858024014E1,
            2.18663306850790267539E0,
            -1.40256079171354495875E-1,
            -3.50424626827848203418E-2,
            -8.57456785154685413611E-4,
        ]

        Q1 = [
            1.57799883256466749731E1,
            4.53907635128879210584E1,
            4.13172038254672030440E1,
            1.50425385692907503408E1,
            2.50464946208309415979E0,
            -1.42182922854787788574E-1,
            -3.80806407691578277194E-2,
            -9.33259480895457427372E-4,
        ]

        # Approximation for interval z = sqrt(-2 log y ) between 8 and 64
        # i.e., y between exp(-32) = 1.27e-14 and exp(-2048) = 3.67e-890.
        P2 = [
            3.23774891776946035970E0,
            6.91522889068984211695E0,
            3.93881025292474443415E0,
            1.33303460815807542389E0,
            2.01485389549179081538E-1,
            1.23716634817820021358E-2,
            3.01581553508235416007E-4,
            2.65806974686737550832E-6,
            6.23974539184983293730E-9,
        ]

        Q2 = [
            6.02427039364742014255E0,
            3.67983563856160859403E0,
            1.37702099489081330271E0,
            2.16236993594496635890E-1,
            1.34204006088543189037E-2,
            3.28014464682127739104E-4,
            2.89247864745380683936E-6,
            6.79019408009981274425E-9,
        ]

        s2pi = 2.50662827463100050242
        code = 1

        if y > (1.0 - 0.13533528323661269189):      # 0.135... = exp(-2)
            y = 1.0 - y
            code = 0

        if y > 0.13533528323661269189:
            y = y - 0.5
            y2 = y * y
            x = y + y * (y2 * polevl(y2, P0, 4) / p1evl(y2, Q0, 8))
            x = x * s2pi
            return x

        x = math.sqrt(-2.0 * math.log(y))
        x0 = x - math.log(x) / x

        z = 1.0 / x
        if x < 8.0:                 # y > exp(-32) = 1.2664165549e-14
            x1 = z * polevl(z, P1, 8) / p1evl(z, Q1, 8)
        else:
            x1 = z * polevl(z, P2, 8) / p1evl(z, Q2, 8)

        x = x0 - x1
        if code != 0:
            x = -x

        return x

    result = ndtri((z + 1) / 2.0) / math.sqrt(2)

    return result

2 个答案:

答案 0 :(得分:4)

我认为你的代码中的错误是在polevl函数中的for循环系数中。如果你用下面的功能替换你所拥有的东西,一切似乎都有效。

[![if you need all navigation item in one line. you have to make display:inline-block,for navigation.

change your code to:

<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 nav-wrap">
                <ul class="pull-left"><li><a href="#" class="logo"></a></li>
                </ul>
                <ul class="pull-left">                  
                    <li class="formSearch">
                        <input type="text" placeholder="Find Freelancers" id="searchButton">
                    </li>               
                    <li><a href="#">Browse</a></li>
                    <li><a href="#">How it works</a></li>
                </ul>
                <ul class="pull-right">
                    <li>
                        <a href="#" class="signupHover">
                            <span class="icon signup"></span>
                            <a href="#">Sign Up
                            </a>
                        </a>
                    </li>
                    <li>
                        <a href="#" class="loginHover">
                            <span class="icon login"></span>
                            <a href="#">Login</a>
                        </a>
                    </li>
                </ul>
                <button type="button" id="buttonHeader">Become a freelancer</button>
            </div>

/*add below css*/

.nav-wrap ul li{display:inline-block;}][1]][1]

我已经使用以下代码对scipy的实现进行了测试:

def polevl(x, coefs, N):
    ans = 0
    power = len(coefs) - 1
    for coef in coefs:
        ans += coef * x**power
        power -= 1
    return ans

答案 1 :(得分:1)

sympy?可能需要进行一些挖掘,以了解其在内部实施的方式http://docs.sympy.org/latest/modules/functions/special.html#sympy.functions.special.error_functions.erfinv

from sympy import erfinv
erfinv(0.9).evalf(30)
1.16308715367667425688580351562