2011-04-06 365 views
10

從文檔判斷boost似乎爲normal和gamma分佈提供了分位數函數(反cdf函數),但是對於我來說它不是很清楚,我怎麼才能真正使用它們。有人可以粘貼一個例子嗎?分位數函數boost(C++)

+0

[本頁](http://www.boost.org/doc/libs/1_46_1/libs/math/doc/sf_and_dist/html/math_toolkit/dist/stat_tut/weg/normal_example/normal_misc.html )包含一個計算正態分佈分位數的例子。它看起來非常簡單。這對你有用嗎? – 2011-04-06 11:39:07

回答

8

分位數計算是作爲一個自由函數實現的。這裏有一個例子:使用

#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

+0

任何想法爲什麼有'normal'和'normal_distribution'類?這讓我感到困惑。 – Grzenio 2011-04-07 09:18:28

+0

我認爲'normal'只是'normal_distribution '的typedef' – Inverse 2011-04-07 18:46:28

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