gpt4 book ai didi

c++ - Excel NORMDIST 函数的 C++ 实现中的魔数(Magic Number)

转载 作者:太空狗 更新时间:2023-10-29 20:00:47 25 4
gpt4 key购买 nike

在寻找 Excel 的 NORMDIST 的 C++ 实现时(累计)我找到的函数 this on a website :

static double normdist(double x, double mean, double standard_dev)
{
double res;
double x=(x - mean) / standard_dev;
if (x == 0)
{
res=0.5;
}
else
{
double oor2pi = 1/(sqrt(double(2) * 3.14159265358979323846));
double t = 1 / (double(1) + 0.2316419 * fabs(x));
t *= oor2pi * exp(-0.5 * x * x)
* (0.31938153 + t
* (-0.356563782 + t
* (1.781477937 + t
* (-1.821255978 + t * 1.330274429))));
if (x >= 0)
{
res = double(1) - t;
}
else
{
res = t;
}
}
return res;
}

我有限的数学知识让我想到了Taylor series ,但我无法确定这些数字的来源:

0.23164190.31938153-0.356563782,1.781477937-1.821255978,1.330274429

任何人都可以建议它们来自哪里以及如何获得它们吗?

最佳答案

查看数字食谱,第 6.2.2 章。近似值是标准的。回想一下

NormCdf(x) = 0.5 * (1 + erf(x / sqrt(2)))
erf(x) = 2 / (sqrt(pi)) integral(e^(-t^2) dt, t = 0..x)

并将erf写成

1 - erf x ~= t * exp(-x^2 + P(t))

对于正 x,其中

t = 2 / (2 + x)

并且由于 t 在 0 和 1 之间,您可以通过 Chebyshev approximation 找到 P一劳永逸(数值食谱,第 5.8 节)。您不使用泰勒展开:您希望近似值在整条实线上都很好,而泰勒展开无法保证。 Chebyshev 近似是 L^2 norm 中最好的多项式近似,这是很难找到的很好的替代品 minimax polynomial (= sup 范数中的最佳多项式逼近)。

这里的版本略有不同。相反,一个人写道

1 - erf x = t * exp(-x^2) * P(t)

但过程类似,直接计算normCdf,而不是erf。

特别是您正在使用的“实现”与文本中处理的“实现”非常相似,因为它的形式为 b*exp(-a*z^2) *y(t) 但它也是一个 Chevishev 大约。到 erfc(x) 函数,正如您在 Schonfelder(1978)[ http://www.ams.org/journals/mcom/1978-32-144/S0025-5718-1978-0494846-8/S0025-5718-1978-0494846-8.pdf 的这篇论文中看到的那样]

此外,在第 6.2.2 章的最后,在第 3 版的数值食谱中,他们提供了一个非常准确的类型 t*exp(-z^2 + c0 + c1*t+ c2t^2 + c3*t^3 + ... + c9t^9)

关于c++ - Excel NORMDIST 函数的 C++ 实现中的魔数(Magic Number),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4934217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com