對于一個樣本序列 ,經驗累積分布函數 (Empirical Cumulative Distribution Function)可被定義為
創新互聯建站基于成都重慶香港及美國等地區分布式IDC機房數據中心構建的電信大帶寬,聯通大帶寬,移動大帶寬,多線BGP大帶寬租用,是為眾多客戶提供專業成都多線服務器托管報價,主機托管價格性價比高,為金融證券行業服務器托管,ai人工智能服務器托管提供bgp線路100M獨享,G口帶寬及機柜租用的專業成都idc公司。
其中 是一個指示函數,如果 ,指示函數取值為1,否則取值為0,因此 能反映在樣本中小于 的元素數量占比。
根據格利文科定理(Glivenko–Cantelli Theorem),如果一個樣本滿足獨立同分布(IID),那么其經驗累積分布函數 會趨近于真實的累積分布函數 。
首先定義一個類,命名為ECDF:
我們采用均勻分布(Uniform)進行驗證,導入 uniform 包,然后進行兩輪抽樣,第一輪抽取10次,第二輪抽取1000次,比較輸出的結果。
輸出結果為:
而我們知道,在真實的0到1均勻分布中, 時, ,從模擬結果可以看出,樣本量越大,最終的經驗累積分布函數值也越接近于真實的累積分布函數值,因此格利文科定理得以證明。
下面的程序繪制隨機變量X的累積分布函數和數組p的累加結果
pl.plot(t, X.cdf(t))
pl.plot(t2, np.add.accumulate(p)*(t2[1]-t2[0]))
Shape Parameters
形態參數
While a general continuous random variable can be shifted and scaled
with the loc and scale parameters, some distributions require additional
shape parameters. For instance, the gamma distribution, with density
γ(x,a)=λ(λx)a?1Γ(a)e?λx,
requires the shape parameter a. Observe that setting λ can be obtained by setting the scale keyword to 1/λ.
雖然一個一般的連續隨機變量可以被位移和伸縮通過loc和scale參數,但一些分布還需要額外的形態參數。作為例子,看到這個伽馬分布,這是它的密度函數
γ(x,a)=λ(λx)a?1Γ(a)e?λx,
要求一個形態參數a。注意到λ的設置可以通過設置scale關鍵字為1/λ進行。
Let’s check the number and name of the shape parameters of the gamma
distribution. (We know from the above that this should be 1.)
讓我們檢查伽馬分布的形態參數的名字的數量。(我們知道從上面知道其應該為1)
from scipy.stats import gamma
gamma.numargs
1
gamma.shapes
'a'
Now we set the value of the shape variable to 1 to obtain the
exponential distribution, so that we compare easily whether we get the
results we expect.
現在我們設置形態變量的值為1以變成指數分布。所以我們可以容易的比較是否得到了我們所期望的結果。
gamma(1, scale=2.).stats(moments="mv")
(array(2.0), array(4.0))
Notice that we can also specify shape parameters as keywords:
注意我們也可以以關鍵字的方式指定形態參數:
gamma(a=1, scale=2.).stats(moments="mv")
(array(2.0), array(4.0))
Freezing a Distribution
凍結分布
Passing the loc and scale keywords time and again can become quite
bothersome. The concept of freezing a RV is used to solve such problems.
不斷地傳遞loc與scale關鍵字最終會讓人厭煩。而凍結RV的概念被用來解決這個問題。
rv = gamma(1, scale=2.)
By using rv we no longer have to include the scale or the shape
parameters anymore. Thus, distributions can be used in one of two ways,
either by passing all distribution parameters to each method call (such
as we did earlier) or by freezing the parameters for the instance of the
distribution. Let us check this:
通過使用rv我們不用再更多的包含scale與形態參數在任何情況下。顯然,分布可以被多種方式使用,我們可以通過傳遞所有分布參數給對方法的每次調用(像我們之前做的那樣)或者可以對一個分布對象凍結參數。讓我們看看是怎么回事:
rv.mean(), rv.std()
(2.0, 2.0)
This is indeed what we should get.
這正是我們應該得到的。
Broadcasting
廣播
The basic methods pdf and so on satisfy the usual numpy broadcasting
rules. For example, we can calculate the critical values for the upper
tail of the t distribution for different probabilites and degrees of
freedom.
像pdf這樣的簡單方法滿足numpy的廣播規則。作為例子,我們可以計算t分布的右尾分布的臨界值對于不同的概率值以及自由度。
stats.t.isf([0.1, 0.05, 0.01], [[10], [11]])
array([[ 1.37218364, 1.81246112, 2.76376946],
[ 1.36343032, 1.79588482, 2.71807918]])
Here, the first row are the critical values for 10 degrees of freedom
and the second row for 11 degrees of freedom (d.o.f.). Thus, the
broadcasting rules give the same result of calling isf twice:
這里,第一行是以10自由度的臨界值,而第二行是以11為自由度的臨界值。所以,廣播規則與下面調用了兩次isf產生的結果相同。
stats.t.isf([0.1, 0.05, 0.01], 10)
array([ 1.37218364, 1.81246112, 2.76376946])
stats.t.isf([0.1, 0.05, 0.01], 11)
array([ 1.36343032, 1.79588482, 2.71807918])
If the array with probabilities, i.e, [0.1, 0.05, 0.01] and the array of
degrees of freedom i.e., [10, 11, 12], have the same array shape, then
element wise matching is used. As an example, we can obtain the 10% tail
for 10 d.o.f., the 5% tail for 11 d.o.f. and the 1% tail for 12 d.o.f.
by calling
但是如果概率數組,如[0.1,0.05,0.01]與自由度數組,如[10,11,12]具有相同的數組形態,則元素對應捕捉被作用,我們可以分別得到10%,5%,1%尾的臨界值對于10,11,12的自由度。
stats.t.isf([0.1, 0.05, 0.01], [10, 11, 12])
array([ 1.37218364, 1.79588482, 2.68099799])
Specific Points for Discrete Distributions
離散分布的特殊之處
Discrete distribution have mostly the same basic methods as the
continuous distributions. However pdf is replaced the probability mass
function pmf, no estimation methods, such as fit, are available, and
scale is not a valid keyword parameter. The location parameter, keyword
loc can still be used to shift the distribution.
離散分布的簡單方法大多數與連續分布很類似。當然像pdf被更換為密度函數pmf,沒有估計方法,像fit是可用的。而scale不是一個合法的關鍵字參數。Location參數,關鍵字loc則仍然可以使用用于位移。
The computation of the cdf requires some extra attention. In the case of
continuous distribution the cumulative distribution function is in most
standard cases strictly monotonic increasing in the bounds (a,b) and
has therefore a unique inverse. The cdf of a discrete distribution,
however, is a step function, hence the inverse cdf, i.e., the percent
point function, requires a different definition:
ppf(q) = min{x : cdf(x) = q, x integer}
Cdf的計算要求一些額外的關注。在連續分布的情況下,累積分布函數在大多數標準情況下是嚴格遞增的,所以有唯一的逆。而cdf在離散分布,無論如何,是階躍函數,所以cdf的逆,分位點函數,要求一個不同的定義:
ppf(q) = min{x : cdf(x) = q, x integer}
For further info, see the docs here.
為了更多信息可以看這里。
We can look at the hypergeometric distribution as an example
from scipy.stats import hypergeom
[M, n, N] = [20, 7, 12]
我們可以看這個超幾何分布的例子
from scipy.stats import hypergeom
[M, n, N] = [20, 7, 12]
If we use the cdf at some integer points and then evaluate the ppf at
those cdf values, we get the initial integers back, for example
如果我們使用在一些整數點使用cdf,它們的cdf值再作用ppf會回到開始的值。
x = np.arange(4)*2
x
array([0, 2, 4, 6])
prb = hypergeom.cdf(x, M, n, N)
prb
array([ 0.0001031991744066, 0.0521155830753351, 0.6083591331269301,
0.9897832817337386])
hypergeom.ppf(prb, M, n, N)
array([ 0., 2., 4., 6.])
If we use values that are not at the kinks of the cdf step function, we get the next higher integer back:
如果我們使用的值不是cdf的函數值,則我們得到一個更高的值。
hypergeom.ppf(prb + 1e-8, M, n, N)
array([ 1., 3., 5., 7.])
hypergeom.ppf(prb - 1e-8, M, n, N)
array([ 0., 2., 4., 6.])
新聞標題:累積分布函數python 累積分布函數是什么意思
文章地址:http://www.yijiale78.com/article30/hgcdso.html
成都網站建設公司_創新互聯,為您提供自適應網站、網站策劃、靜態網站、定制開發、小程序開發、
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯