从文档上看,Boost似乎提供了正态分布和伽马分布的分位数函数(反向累积分布函数),但我不清楚如何实际使用它们。请问有人能够提供一个示例吗?
分位数计算是实现为一个自由函数。以下是一个示例:
#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));
这里有一些类似的工作示例:
编辑: 由于ADL,自由函数不需要名称空间。
normal
和normal_distribution
两个类?这让我感到困惑。 - Grzenionormal
只是normal_distribution<double>
的一个typedef。 - Inverse// É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;
}