本文介绍了如何在 C/C++ 中将百分比转换为正态分布的 z 分数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标是说:这些值位于正态分布中平均值周围 95% 的值范围内."

现在,我正在尝试将百分比转换为 z 分数,这样我就可以获得精确的值范围.类似于 就足够了.

所以我需要类似的东西

double z_score(双倍百分比){//...}//...//根据 https://en.wikipedia.org/wiki/68–95–99.7_rulez_score(68.27) == 1z_score(95.45) == 2z_score(99.73) == 3

我发现了一篇文章解释了如何去做使用来自 boost 库的 函数,但是

double z_score( 双倍百分比) {返回 - sqrt( 2 )/boost::math::erfc_inv( 2 * 百分比/100 );}

无法正常工作并返回奇怪的值.

z_score(95) == 1.21591//而不是 1.96

而且 boost 库有点重,我打算将它用于 Ruby gem,所以它应该尽可能轻量级.

有人有想法吗?

解决方案

我说你足够接近"了.

#include #include #include <cmath>双 z_score(双百分比){返回 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.0000295.45 299.73 2.99998

我不知道你是怎么把 - 放在前面的,它是 erf>>c<<_inv 而它是 sqrt(2) 除以.从 这里 wiki Normal_distribution#Standard_deviation_and_coverage 我读到:

p 

boost 库也有点重

5 分钟的搜索结果是 thisthis C 实现 erf_inv.

The goal is to say: "These values lie within a band of 95 % of values around the mean in a normal distribution."

Now, I am trying to convert percentage to z-score, so then I can get the precise range of values. Something like <lower bound , upper bound> would be enough.

So I need something like

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

I found an article explaining how to do it with a function from boost library, but

double z_score( double percentage ) {
    return - sqrt( 2 ) / boost::math::erfc_inv( 2 * percentage / 100 );
}

does not work properly and it returns weird values.

z_score(95) == 1.21591 // instead of 1.96

Also the boost library is kinda heavy and I plan to use it for Ruby gem, so it should be as lightweight as possible.

Does anyone have an idea?

解决方案

I say you were "close enough".

#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);
}

outputs:

68.27 1.00002
95.45 2
99.73 2.99998

I do not know how you got that - in front, and that it's erf>>c<<_inv and that it's sqrt(2) divided by. From here wiki Normal_distribution#Standard_deviation_and_coverage I read that:

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)

A 5 min search resulted in this and this C implementations erf_inv.

这篇关于如何在 C/C++ 中将百分比转换为正态分布的 z 分数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-26 10:49