![]() |
Home | Libraries | People | FAQ | More |
參考distribution_construction.cpp 中的完整代碼。
這個庫中的分佈的結構(structure of distributions)與其它的統計庫有很大的不同,例如,在類似於FORTRAN 和 C等非面向對象的語言(less object-oriented language)中,為每個自由函數(Free Function)提供了一些參數。這個庫將每個分佈作為一個C++類模板( template C++ class)而提供,每個分佈使用一些參數來構造,然後使用成員和非成員函數來查找分佈的相關值,通常是帶有一個隨機變量的函數。
首先我們要包含一些頭文件來訪問負二項分佈( negative binomial distribution) (以及 二項分佈,β分佈和γ分佈)。
#include <boost/math/distributions/negative_binomial.hpp> // 負二項分佈(negative_binomial_distribution) using boost::math::negative_binomial_distribution; // 缺省類型是double. using boost::math::negative_binomial; // typedef 提供的缺省類型是 double. #include <boost/math/distributions/binomial.hpp> // 二項分佈(binomial_distribution). #include <boost/math/distributions/beta.hpp> // 分佈(beta_distribution). #include <boost/math/distributions/gamma.hpp> // 分佈(gamma_distribution). #include <boost/math/distributions/normal.hpp> // 正態分佈(normal_distribution).
在下面有一些構造分佈的例子:
首先,試驗成功次數為8,成功分數為(success fraction)為0.25,25%或者1/4的負二項分佈(negative binomial distribution)是這樣構造的:
boost::math::negative_binomial_distribution<double> mydist0(8., 0.25);
但是這太長了,因此通過:
using namespace boost::math;
或
using boost::math::negative_binomial_distribution;
我們可以簡寫。
因為大多數應用程序會使用double精度,分佈類型的模板參數(RealType)的缺省類型為double,因此我們也可以寫作:
negative_binomial_distribution<> mydist9(8., 0.25); // 使用缺省的 RealType = double.
但是"negative_binomial_distribution"這個名字仍然太長了,所以,對於大多數的分佈類型,提供了一個方便的 typedef ,例如:
typedef negative_binomial_distribution<double> negative_binomial; // 為 double 類型保留名字.
![]() |
注意 |
|---|---|
如果與函數的名字發生了衝突,那麼就沒有提供這個方便的typedef :當前只有"beta" 和 "gamma" 屬於這個類別。 |
因此,在使用了語句:
using boost::math::negative_binomial;
後,我們就有了一個方便的 typedef : negative_binomial_distribution<double>
negative_binomial mydist(8., 0.25);
使用方便的 typedef的更多例子:
negative_binomial mydist10(5., 0.4); // 兩個參數都是 double.
並且進行自動轉換,所以你可以使用使用整數和浮點數(floats):
negative_binomial mydist11(5, 0.4); //使用已提供的 typedef double, int 和 double 參數.
這可能是最常見的使用
negative_binomial mydist12(5., 0.4F); // double 和 float 參數. negative_binomial mydist13(5, 1); // 兩個參數都是整數.
其它的分佈也類似於二項分佈的情況。
binomial mybinomial(1, 0.5); // 整數與浮點數 binomial_distribution<> mybinomd1(1, 0.5);//更精確
對於 typdef 分佈的名字會和數學特殊函數的名字發生衝突的情況(當前只有β分佈和γ分佈) ,有意沒有提供 typedef ,因此必須使用分佈類型名字較長的版本。例如,不要使用:
using boost::math::beta; beta mybetad0(1, 0.5); // Error beta 是一個數學函數名字!
將會產生下面的錯誤信息:
error C2146: syntax error : missing ';' before identifier 'mybetad0' warning C4551: function call missing argument list error C3861: 'mybetad0': identifier not found
反之,你應當使用:
using boost::math::beta_distribution; beta_distribution<> mybetad1(1, 0.5);
或者對於γ分佈( gamma distribution):
gamma_distribution<> mygammad1(1, 0.5);
當然,我們可以明確地提供這個類型:
// 明確的 double 精度: negative_binomial_distribution<double> mydist1(8., 0.25); // 明確的 float 精度, double 參數截斷為 float: negative_binomial_distribution<float> mydist2(8., 0.25); // 明確的 float 精度, integer & double 參數轉換為 float. negative_binomial_distribution<float> mydist3(8, 0.25); // 明確的 float 精度, float 參數, 因此沒有轉換: negative_binomial_distribution<float> mydist4(8.F, 0.25F); // 明確的 float 精度, integer 參數提升為 float. negative_binomial_distribution<float> mydist5(8, 1); // 明確的 double 精度: negative_binomial_distribution<double> mydist6(8., 0.25); // 明確的 long double 精度: negative_binomial_distribution<long double> mydist7(8., 0.25);
如果你自己的 RealType 稱作 MyFPType,例如NTL RR (一個任意精度類型),那麼我們可以寫作:
negative_binomial_distribution<MyFPType> mydist6(8, 1); // 整數參數 -> MyFPType.
注意:對於一些分佈類型的構造函數提供了缺省的參數。所以,如果你錯誤地假定了缺省參數,那麼你將得到一個錯誤信息,例如:
negative_binomial_distribution<> mydist8;
error C2512 no appropriate default constructor available.
沒有為負二項分佈(negative_binomial_distribution)的構造函數提供缺省參數,因為很難為負二項分佈(negative_binomial_distribution)提供有意義的缺省參數。對於其它的分佈類型,類似於正態分佈(normal distribution),提供標準的缺省的均值(mean)和標準差(standard deviation)明顯是很有用的:
normal_distribution(RealType mean = 0, RealType sd = 1);
所以在這種情況下我們可以寫作:
using boost::math::normal; normal norm1; // 標準正態分佈(Standard normal distribution). normal norm2(2); // 均值(Mean) = 2, 標準差(std deviation) = 1. normal norm3(2, 3); // 均值(Mean) = 2, 標準差(std deviation) = 3. return 0; } // int main()
當然,對於這個程序沒有有用的輸出結果。