【问题标题】:Need code for Inverse Error Function in Python/Spark需要 Python/Spark 中的逆误差函数代码
【发布时间】:2021-10-29 20:58:41
【问题描述】:

我知道提供 erfinv 功能的库,如 scipy、symph 和 pyerf。 但是我正在寻找纯数学公式中的代码。 这个公式中需要替换erfinv(math.sqrt(2) * erfinv(x*2-1))

【问题讨论】:

  • 也许是同情而不是同情?
  • 你需要用其他表达式替换erfinv(x)吗?
  • n=math.sqrt(2)*erfinv(2*0.008-1) print(n) -->-2.4089155458154616
  • n=math.sqrt(2)*erfinv(2*0.008-1) print(n) -->-2.4089155458154616......需要替换这个方程中的erfinv函数跨度>

标签: python python-3.x apache-spark pyspark


【解决方案1】:

使用有理切比雪夫近似 尽管有几种极端情况,但您可能能够实现误差函数的逆函数,因此解决方案可能非常复杂;我简化了 scipy 用于计算 erfinv 的 C 代码,以展示更简单的 python 实现的样子。

import math

def erfinv(x):
    lower_b = -1
    upper_b = 1

    threshold = pow(1,-7)

    if -threshold < x && x < threshold:
        #using a simpler Taylor expansion for small values        
        return x / (2/sqrt(math.pi))
    
    elif lower_b < x && x < upper_b:
        #requires Gaussian cum distribution implementations (requires indefinite integration)
        return Gaussian_cumulative_distribution_function(0.5 * (x+1)) * (1/sqrt(2))

    elif x == lower_b:
        #show negative infinity, somehow
        pass
    
    elif y == domain_ub:
       #show positive infinity, somehow
        pass
    
    else:
        #unique solution cannot be guaranteed outside of the established domain
        raise Exception('Error')

考虑到所有这些,我想说,当您面临如此复杂的数字计算时,在大多数情况下,您可能最好使用 scipy 之类的库;因此,我会向任何想要执行相同计算的人推荐这种方法。

我能想出的最简单的解决方案需要数值不定积分,而使用我在此答案开头提到的近似值的解决方案将需要具有许多系数的不同程度的有理函数(这不一定很难实现,但可能非常耗时)

我在撰写此答案时阅读了几篇研究论文,如果您决定采用更“纯粹”的解决方案,它们可能会为您提供指导:

Rational Chebyshev Approximations for the Inverse of the Error Function

Approximations to inverse error functions

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多