![]() |
Home | Libraries | People | FAQ | More |
當計算分位點的時候,離散分佈帶給我們一個問題:我們從一個連續的浮點值變量(real-valued variable)開始-概率-但結果(隨機變量的值)應當是真正的離散的。
考慮一個二項分佈,樣本大小為50,成功分數(success fraction)為0.5。我們有多種方式來為一個離散分佈作圖,但是如果我們把函數PDF作為一個階梯函數(step-function)來作圖,那麼函數圖像看起來像下面這樣:
現在假設用戶要求一個概率為0.05的分位點,如果我們在函數CDF上放大這個區域,我們將看到下面的圖像:
就像可以看到的那樣,並沒有一個隨機變量的概率對應於0.05,因此,就像在圖像中顯示的那樣,我們有兩個選擇:
實際上還有第三個選擇:我們可以「假裝」這個分佈是連續分佈並返回一個浮點值:在這種情況下,我們可以計算一個近似到18.701的結果(這就可以精確地反映出結果更接近於19而不是18)。
通過使用策略我們可以作出上面的任何一個選擇,但這仍留給我們一個問題:實際上應該怎麼做呢?
特殊一些:我們應當使用哪個策略作為缺省呢?
在回答問題之前我們應當意識到:
因此計算一個整數結果是一個很大的益處,並且從哲學的觀點看,這也是「正確的做法」。更進一步,如果有人需要一個在概率為0.05處的分位點,那麼我們通常可以假設他需要一個至少 95%的概率選擇右邊的一個值,且至多 5%的概率選擇左邊的值。
在上面的例子中,因此我們應當把結果向下捨入到18。
這種情況的反面應用於上分位點(upper-quantiles):如果概率大於0.5,那麼我們想要將結果向上捨入,使得至少要求的概率是在返回值的概率的左邊,且在返回值概率右邊的概率不超過1 - 要求的概率。
類似的,對於兩側區間( two-sided intervals),我們將會把下分位點(lower quantile)向下捨入,將上分位點(upper quantile)向上捨入。這確保在區間的中間區域我們至少有要求的概率並且在尾部區域的概率不超過1-要求的概率。
例如,使用樣本大小為50且成功分數(success fraction)為 0.5的二項分佈作為例子,如果我們想要一個雙側的(two-sided)的90%置信區間,那麼我們需要結果向外捨入的0.05 和 0.95 的分位點,使得至少90%的概率在中間區域。
直到目前為止還可以,但實際上有一個因為不小心面導致的陷阱:
quantile(binomial(50, 0.5), 0.05);
返回18 作為結果,這也是我們從上面的圖像中期待的結果,但實際上沒有大於18的x值使得:
cdf(binomial(50, 0.5), x) <= 0.05;
然而:
quantile(binomial(50, 0.5), 0.95);
返回 31,但實際上並沒有小於31的x值使得:
cdf(binomial(50, 0.5), x) >= 0.95;
我們可能簡單地希望對於這些對稱分佈,結果將會是 32 (因為 32 = 50 - 18),但是我們需要記住二項分佈的cdf函數是包含(inclusive) 隨機變量的。因此當左邊的區域包含 返回的分位點時,右邊的區域就排除了上分位點值:因為它屬於中心區域。
查看上面的圖像來觀察在這裡會發生什麼:下分們點18屬於左邊的尾部區域,因此任何的小於等於18的值都在左邊的尾部區域中。上分位點值31屬於中心區域,因此尾部區域實際上從32開始,因此任何大於31的值都在右邊的尾部區域中。
因此,如果 U 和 L 分別是上分位點和下分位點,那麼隨機變量X位於這個尾部區域-如果
X <= L || X > U
那麼我們將否決這個虛假設。
如果:
L < X <= U
那麼X位於中心區域。
這裡的寓意是(moral)當處理離散分佈時要時刻小心你所進行的操作, 如果不能肯定,那麼將你所進行的比較基於函數CDF。
就像你從策略這部分所期望的那樣,你不會驚訝還有其它的可用的策略:
這是缺省的策略,就像上面描述的那樣:下分位點(概率小於0.5)向下捨入,上分位點(概率大於0.5)向上捨入。
這就可以在尾部區域給定不超過要求的概率,在中心區域給定至少是要求的概率。
這個策略與缺省的策略是完全相反的:下分位點(概率小於0.5)向上捨入,上分位點(概率大於0.5)向下捨入。
這就可以在尾部區域給定至少是要求的概率,在中心區域給定不超過要求的概率。
這個策略將結果向下捨入而不管是上分位點還是下分位點。
這個策略將結果向上捨入而不管是上分位點還是下分位點。
這個策略將會把結果捨入到最接近的整數而不管是上分位點還是下分位點。
這個策略將會從離散分佈的分位點中返回浮點值:這通常要比查找一個整數結果更慢一些,但是卻允許更靈活的捨入策略。
為了理解捨入策略是如何應用於離散分佈的,我們將使用一個樣本大小為50,成功分數(success fraction)為0.5的二項分佈作為例子,並計算在0.05和0.95處的所有可能的分位點。
包含一些需要的頭文件:
#include <iostream> #include <boost/math/distributions/binomial.hpp>
接下來我們將需要的聲明引入作用域 ,並針對所有的可用的捨入策略定義分佈類型:
using namespace boost::math::policies; using namespace boost::math; typedef binomial_distribution< double, policy<discrete_quantile<integer_round_outwards> > > binom_round_outwards; typedef binomial_distribution< double, policy<discrete_quantile<integer_round_inwards> > > binom_round_inwards; typedef binomial_distribution< double, policy<discrete_quantile<integer_round_down> > > binom_round_down; typedef binomial_distribution< double, policy<discrete_quantile<integer_round_up> > > binom_round_up; typedef binomial_distribution< double, policy<discrete_quantile<integer_round_nearest> > > binom_round_nearest; typedef binomial_distribution< double, policy<discrete_quantile<real> > > binom_real_quantile;
現在讓我們調用這些分位點函數:
int main() { std::cout << "Testing rounding policies for a 50 sample binomial distribution,\n" "with a success fraction of 0.5.\n\n" "Lower quantiles are calculated at p = 0.05\n\n" "Upper quantiles at p = 0.95.\n\n"; std::cout << std::setw(25) << std::right << "Policy"<< std::setw(18) << std::right << "Lower Quantile" << std::setw(18) << std::right << "Upper Quantile" << std::endl; // 測試 integer_round_outwards: std::cout << std::setw(25) << std::right << "integer_round_outwards" << std::setw(18) << std::right << quantile(binom_round_outwards(50, 0.5), 0.05) << std::setw(18) << std::right << quantile(binom_round_outwards(50, 0.5), 0.95) << std::endl; // 測試 integer_round_inwards: std::cout << std::setw(25) << std::right << "integer_round_inwards" << std::setw(18) << std::right << quantile(binom_round_inwards(50, 0.5), 0.05) << std::setw(18) << std::right << quantile(binom_round_inwards(50, 0.5), 0.95) << std::endl; // 測試 integer_round_down: std::cout << std::setw(25) << std::right << "integer_round_down" << std::setw(18) << std::right << quantile(binom_round_down(50, 0.5), 0.05) << std::setw(18) << std::right << quantile(binom_round_down(50, 0.5), 0.95) << std::endl; // 測試 integer_round_up: std::cout << std::setw(25) << std::right << "integer_round_up" << std::setw(18) << std::right << quantile(binom_round_up(50, 0.5), 0.05) << std::setw(18) << std::right << quantile(binom_round_up(50, 0.5), 0.95) << std::endl; // 測試 integer_round_nearest: std::cout << std::setw(25) << std::right << "integer_round_nearest" << std::setw(18) << std::right << quantile(binom_round_nearest(50, 0.5), 0.05) << std::setw(18) << std::right << quantile(binom_round_nearest(50, 0.5), 0.95) << std::endl; // 測試 real: std::cout << std::setw(25) << std::right << "real" << std::setw(18) << std::right << quantile(binom_real_quantile(50, 0.5), 0.05) << std::setw(18) << std::right << quantile(binom_real_quantile(50, 0.5), 0.95) << std::endl; }
程序產生下面的輸出:
Testing rounding policies for a 50 sample binomial distribution,
with a success fraction of 0.5.
Lower quantiles are calculated at p = 0.05
Upper quantiles at p = 0.95.
Testing rounding policies for a 50 sample binomial distribution,
with a success fraction of 0.5.
Lower quantiles are calculated at p = 0.05
Upper quantiles at p = 0.95.
Policy Lower Quantile Upper Quantile
integer_round_outwards 18 31
integer_round_inwards 19 30
integer_round_down 18 30
integer_round_up 19 31
integer_round_nearest 19 30
real 18.701 30.299