C++ 正态分布、概率累积密度函数的使用(boost库)

在C++11以上中在# include 头文件中包含了std::normal_distribution<>b;正态分布函数。

当然也可以下载boost库,里面已经有包装好的正态分布函数。

1.boost库可以在https://www.boost.org/中进行下载最新版的

2.boost库导入C++工程的方法可以参照博客https://blog.csdn.net/loongsking/article/details/80136004

3.导入好了boost库后示例代码如下

#include 
#include 
#include 
#include 
int main() {
	//std::normal_distribution<>b;
	
	boost::math::normal_distribution<> norm(0, 1);//期望,方差
	double PI = acos(-1);
	std::cout<

其中,要引入normal.hpp才能够使用正态分布,norm(0,1)代表(期望,方差),cdf用来求概率密度,pdf用来求正太分布函数对应的y值。

其实手动实现标准正太分布的概率累积函数也是可以的,也许更快,但是精度准确性有点差。我想到的累积函数是正态分布函数的积分,思路大概就是在x=0处进行泰勒展开,展开到五六阶倒数后精度可以大概到后5位小数左右。

(从其他地方拷贝过来的标准正态分布下的概率累积代码可以参考参考)

 

double phi(double x)
{
    // constants
    double a1 =  0.254829592;
    double a2 = -0.284496736;
    double a3 =  1.421413741;
    double a4 = -1.453152027;
    double a5 =  1.061405429;
    double p  =  0.3275911;

    // Save the sign of x
    int sign = 1;
    if (x < 0)
        sign = -1;
    x = fabs(x)/sqrt(2.0);

    // A&S formula 7.1.26
    double t = 1.0/(1.0 + p*x);
    double y = 1.0 - (((((a5*t + a4)*t) + a3)*t + a2)*t + a1)*t*exp(-x*x);

    return 0.5*(1.0 + sign*y);
}

 

你可能感兴趣的:(C++,C++,boost,正态分布,概率密度,累积概率)