【问题标题】:PDF and CDF without SciPy没有 SciPy 的 PDF 和 CDF
【发布时间】:2012-05-05 08:45:05
【问题描述】:

我需要在我正在编程的 Python 应用程序中使用概率和累积密度函数。 SciPy 两者都提供,但对于这两个函数来说似乎过于依赖了。没有 SciPy,PDF 似乎很容易实现。 (From the docs:)

范数的概率密度函数为:

norm.pdf(x) = exp(-x**2/2)/sqrt(2*pi)

有没有办法在使用 SciPy 的情况下获得 CDF?

【问题讨论】:

  • 手工集成!有一个简单的公式……对吧? [提示:否]

标签: python statistics scipy


【解决方案1】:

您正在寻找“错误函数”,请参阅the math module。它没有以elementary functions 表示的封闭形式。

请注意,math.erf(x) 是在 python 2.7 中引入的。如果您使用的是早期版本,则必须使用 an approximation

【讨论】:

    【解决方案2】:

    this post:

    from math import *
    def erfcc(x):
        """Complementary error function."""
        z = abs(x)
        t = 1. / (1. + 0.5*z)
        r = t * exp(-z*z-1.26551223+t*(1.00002368+t*(.37409196+
            t*(.09678418+t*(-.18628806+t*(.27886807+
            t*(-1.13520398+t*(1.48851587+t*(-.82215223+
            t*.17087277)))))))))
        if (x >= 0.):
            return r
        else:
            return 2. - r
    
    def ncdf(x):
        return 1. - 0.5*erfcc(x/(2**0.5))
    

    【讨论】:

      猜你喜欢
      • 2015-02-10
      • 2022-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-14
      • 1970-01-01
      • 2019-11-22
      相关资源
      最近更新 更多