12

从文档来看 boost 似乎为正态分布和伽马分布提供分位数函数(逆 cdf 函数),但我不清楚如何实际使用它们。有人可以粘贴一个例子吗?

4

2 回答 2

9

分位数计算是作为自由函数实现的。这是一个例子:

#include <boost/math/distributions/normal.hpp>

boost::math::normal dist(0.0, 1.0);

// 95% of distribution is below q:
double q = quantile(dist, 0.95);

您还可以使用以下方法获得补码(右侧分位数):

// 95% of distribution is above qc:
double qc = quantile(complement(dist, 0.05));

这里有一些类似的工作示例:

http://www.boost.org/doc/libs/1_46_1/libs/math/doc/sf_and_dist/html/math_toolkit/dist/stat_tut/weg.html

编辑:由于 ADL,在免费功能上不需要命名空间

于 2011-04-06T14:43:07.183 回答
3

QuantCorner上有一个可行的例子。

// Édouard Tallent @ TaGoMa.Tech
// September 2012

#include<boost/math/distributions.hpp>
#include<iostream>
using std::cout;
using std::endl;

double inverseNormal(double prob, double mean, double sd){
        boost::math::normal_distribution<>myNormal (mean, sd);
        return quantile(myNormal, prob);
}

int main (int, char*[])
{
        try
        {                
                double myProb = 0.1;    // the 10% quantile
                double myMean = 0.07;   // a 7% mean 
                double myVol = 0.14;    // a 14% volatility 

        cout << inverseNormal(myProb, myMean, myVol)  << endl;
        }

                catch(std::exception& e)
        {
                cout << "Error message: " << e.what() << endl;
        }
return 0;
}
于 2012-09-13T08:16:27.990 回答