boost::math::pdf() 与C++概率密度函数

来源:互联网 发布:外贸书籍推荐 知乎 编辑:程序博客网 时间:2024/06/05 19:11

转载请注明出处:http://my.csdn.NET/ye_shen_wei_mian

最近在一份开源代码中接触到概率密度函数,该概率密度函数是正态(高斯)分布,是基于boost库实现的,非常方便易用。

官网对该函数的解释是:

pdf(my_dist, x);  // Returns PDF (density) at point x of distribution my_dist.
至于用法也很简单:

boost::math::normal_distribution<float> nd(seed->mu, norm_scale);float result = boost::math::pdf(nd, x);
对于 pdf 和 cdf 函数的详情,可以参考http://www.boost.org/doc/libs/1_63_0/libs/math/doc/html/math_toolkit/stat_tut/overview/generic.html 这个网址

寻思着boost作为C++的准标准库组成部分,在C++标准库中是否有同样的实现呢?

很遗憾,经过搜集资料发现,好像是没有的,在stackoverflow中参详这个问题下的被接收的回答

Probability Density Function using the standard library?(网址:http://stackoverflow.com/questions/19387222/probability-density-function-using-the-standard-library)

其实要自己写出常用的概率分布密度函数也并不难,完全可以自己去写。在stackoverflow找到一个正态(高斯)分布的别人的实现,供大家参考下:

Using the gaussian probability density function in C++

(网址:http://stackoverflow.com/questions/10847007/using-the-gaussian-probability-density-function-in-c#)
作者给出了两种实现:
float normal_pdf(float x, float m, float s){    static const float inv_sqrt_2pi = 0.3989422804014327;    float a = (x - m) / s;    return inv_sqrt_2pi / s * std::exp(-0.5f * a * a);}
或者做成模板函数也行:
template <typename T>T normal_pdf(T x, T m, T s){    static const T inv_sqrt_2pi = 0.3989422804014327;    T a = (x - m) / s;    return inv_sqrt_2pi / s * std::exp(-T(0.5) * a * a);}

至于如何使用std自带的概率分布来产生正态分布或者其他分布的随机数,可以参考正态分布下的随机数产生方法:
http://www.cplusplus.com/reference/random/normal_distribution/?kw=normal_distribution

0 0
原创粉丝点击