用于计算正态分布标准差的标准 C 或 Python 库

发布于 2024-07-10 01:28:59 字数 112 浏览 13 评论 0 原文

假设我们有正态分布 n(x):mean=0 且 \int_{-a}^{a} n(x) = P。

计算这种分布的标准差的最简单方法是什么? 可能有适合该任务的 python 或 C 标准库吗?

Say we have normal distribution n(x): mean=0 and \int_{-a}^{a} n(x) = P.

What is the easiest way to compute standard deviation of such distribution? May be there are standard libraries for python or C, that are suitable for that task?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

鸠魁 2024-07-17 01:28:59

SciPy 有一个 stats 子包。

SciPy has a stats sub-package.

不顾 2024-07-17 01:28:59

看看 sciPy 项目,它应该有你需要的。

Take a look at the sciPy Project, it should have what you need.

困倦 2024-07-17 01:28:59

Pr(-a < X < a) = P 的均值零高斯分布的标准差是

a/(sqrt(2)*inverseErf(P))

您要查找的表达式,其中 inverseErf 是误差函数的反函数(通常称为 erf) 。

对于 C 语言,Gnu Scientific Library (GSL) 是一个很好的资源。 然而它只有 erf,没有 inverseErf,所以你必须自己反转它(一个简单的二分搜索就可以了)。 或者,这里有一个近似 erf 和 inverseErf 的好方法:

http:// /homepages.phyk.uni-muenchen.de/~Winitzki/erf-approx.pdf

对于 Python,inverseErf 在 SciPy 库中以 erfinv 形式提供,因此以下给出标准差:

a/(math.sqrt(2)*erfinv(P))

PS: Stackoverflow 的 URL 呈现存在某种错误,它不允许我链接到上面的 GSL: http://www.gnu.org/software/gsl
当我将上面带有 pdf 的 URL 设置为正确的链接时,它也会呈现错误。

The standard deviation of a mean-zero gaussian distribution with Pr(-a < X < a) = P is

a/(sqrt(2)*inverseErf(P))

which is the expression you're looking for, where inverseErf is the inverse of the error function (commonly known as erf).

For C, the Gnu Scientific Library (GSL) is a good resource. However it only has erf, not inverseErf, so you'd have to invert it yourself (a simple binary search would do the trick). Alternatively, here's a nice way to approximate erf and inverseErf:

http://homepages.physik.uni-muenchen.de/~Winitzki/erf-approx.pdf

For Python, inverseErf is available as erfinv in the SciPy library, so the following gives the standard deviation:

a/(math.sqrt(2)*erfinv(P))

PS: There's some kind of bug in Stackoverflow's URL rendering and it wouldn't let me link to GSL above: http://www.gnu.org/software/gsl.
It also renders wrong when I make the URL above with a pdf a proper link.

你好,陌生人 2024-07-17 01:28:59

如果 X 是均值 0 和标准差 sigma 的正态分布,则它必须保持

P = Prob[ -a <= X <= a ] = Prob[ -a/sigma <= N <= a/sigma ]
  = 2 Prob[ 0 <= N <= a/sigma ]
  = 2 ( Prob[ N <= a/sigma ] - 1/2 )

N 是均值 0 和标准差 1 的正态分布。因此,

P/2 + 1/2 = Prob[ N <= a/sigma ] = Phi(a/sigma)

其中 Phi 是均值 0 和标准差 1 的正态变量的累积分布函数 (cdf)。现在我们需要正态 cdf(或“百分点函数”),在 Python 中为 scipy.stats.norm.ppf()。 示例代码:

from scipy.stats import norm
P = 0.3456
a = 3.0

a_sigma = float(norm.ppf(P/2 + 0.5))   # a/sigma
sigma = a/a_sigma   # Here is the standard deviation

例如,我们知道 N(0,1) 变量落入区间 [-1.1] 的概率约为 0.682(此图)。 如果设置 P = 0.682 且 a = 1.0,您将获得 sigma ~ 1.0,这确实是标准差。

If X is normal with mean 0 and standard deviation sigma, it must hold

P = Prob[ -a <= X <= a ] = Prob[ -a/sigma <= N <= a/sigma ]
  = 2 Prob[ 0 <= N <= a/sigma ]
  = 2 ( Prob[ N <= a/sigma ] - 1/2 )

where N is normal with mean 0 and standard deviation 1. Hence

P/2 + 1/2 = Prob[ N <= a/sigma ] = Phi(a/sigma)

Where Phi is the cumulative distribution function (cdf) of a normal variable with mean 0 and stddev 1. Now we need the inverse normal cdf (or the "percent point function"), which in Python is scipy.stats.norm.ppf(). Sample code:

from scipy.stats import norm
P = 0.3456
a = 3.0

a_sigma = float(norm.ppf(P/2 + 0.5))   # a/sigma
sigma = a/a_sigma   # Here is the standard deviation

For example, we know that the probability of a N(0,1) variable falling int the interval [-1.1] is ~ 0.682 (the dark blue area in this figure). If you set P = 0.682 and a = 1.0 you obtain sigma ~ 1.0, which is indeed the standard deviation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文