如何将百分比转换为 C/C++ 中正态分布的 z 分数?
How to convert percentage to z-score of normal distribution in C/C++?
目标是说:“这些值位于正态分布均值周围 95% 的值范围内。”
现在,我正在尝试将百分比转换为 z 分数,这样我就可以获得精确的值范围。 <lower bound , upper bound>
这样的东西就足够了。
所以我需要类似的东西
double z_score(double percentage) {
// ...
}
// ...
// according to https://en.wikipedia.org/wiki/68–95–99.7_rule
z_score(68.27) == 1
z_score(95.45) == 2
z_score(99.73) == 3
我找到了 article explaining how to do it with a function from boost library,但是
double z_score( double percentage ) {
return - sqrt( 2 ) / boost::math::erfc_inv( 2 * percentage / 100 );
}
无法正常工作并且它 returns 奇怪的值。
z_score(95) == 1.21591 // instead of 1.96
此外,boost 库有点重,我打算将其用于 Ruby gem,因此它应该尽可能轻便。
有人知道吗?
我说你“足够接近”了。
#include <iostream>
#include <boost/math/special_functions/erf.hpp>
#include <cmath>
double z_score(double percentage) {
return sqrt(2) * boost::math::erf_inv(percentage / 100);
}
int main() {
#define _(x) std::cout << x << " " << z_score(x) << "\n"
_(68.27);
_(95.45);
_(99.73);
}
输出:
68.27 1.00002
95.45 2
99.73 2.99998
我不知道你怎么得到前面的-
,然后是erf>>c<<_inv
,然后是sqrt(2)
。从 here wiki Normal_distribution#Standard_deviation_and_coverage 我读到:
p <- this is probability, ie. your input
u <- mean value
o <- std dev
n <- the count of std deviations from mean, ie. 1, 2, 3 etc.
p = F(u + no) - F(u + no) = fi(n) - fi(-n) = erf(n / sqrt(2))
p = erf(n / sqrt(2))
erf_inv(p) = n / sqrt(2)
erf_inv(p) * sqrt(2) = n
n = sqrt(2) * erf_inv(p)
Also the boost library is kinda heavy
目标是说:“这些值位于正态分布均值周围 95% 的值范围内。”
现在,我正在尝试将百分比转换为 z 分数,这样我就可以获得精确的值范围。 <lower bound , upper bound>
这样的东西就足够了。
所以我需要类似的东西
double z_score(double percentage) {
// ...
}
// ...
// according to https://en.wikipedia.org/wiki/68–95–99.7_rule
z_score(68.27) == 1
z_score(95.45) == 2
z_score(99.73) == 3
我找到了 article explaining how to do it with a function from boost library,但是
double z_score( double percentage ) {
return - sqrt( 2 ) / boost::math::erfc_inv( 2 * percentage / 100 );
}
无法正常工作并且它 returns 奇怪的值。
z_score(95) == 1.21591 // instead of 1.96
此外,boost 库有点重,我打算将其用于 Ruby gem,因此它应该尽可能轻便。
有人知道吗?
我说你“足够接近”了。
#include <iostream>
#include <boost/math/special_functions/erf.hpp>
#include <cmath>
double z_score(double percentage) {
return sqrt(2) * boost::math::erf_inv(percentage / 100);
}
int main() {
#define _(x) std::cout << x << " " << z_score(x) << "\n"
_(68.27);
_(95.45);
_(99.73);
}
输出:
68.27 1.00002
95.45 2
99.73 2.99998
我不知道你怎么得到前面的-
,然后是erf>>c<<_inv
,然后是sqrt(2)
。从 here wiki Normal_distribution#Standard_deviation_and_coverage 我读到:
p <- this is probability, ie. your input
u <- mean value
o <- std dev
n <- the count of std deviations from mean, ie. 1, 2, 3 etc.
p = F(u + no) - F(u + no) = fi(n) - fi(-n) = erf(n / sqrt(2))
p = erf(n / sqrt(2))
erf_inv(p) = n / sqrt(2)
erf_inv(p) * sqrt(2) = n
n = sqrt(2) * erf_inv(p)
Also the boost library is kinda heavy