累积分布函数python 累积分布函数例题

如何在Python中实现这五类强大的概率分布R编程语言已经成为统计分析中的事实标准 。但在这篇文章中,我将告诉你在Python中实现统计学概念会是如此容易 。我要使用Python实现一些离散和连续的概率分布 。虽然我不会讨论这些分布的数学细节,但我会以链接的方式给你一些学习这些统计学概念的好资料 。在讨论这些概率分布之前,我想简单说说什么是随机变量(random variable) 。随机变量是对一次试验结果的量化 。
举个例子 , 一个表示抛硬币结果的随机变量可以表示成
Python
1
2
X = {1 如果正面朝上,
2 如果反面朝上}
随机变量是一个变量,它取值于一组可能的值(离散或连续的),并服从某种随机性 。随机变量的每个可能取值的都与一个概率相关联 。随机变量的所有可能取值和与之相关联的概率就被称为概率分布(probability distributrion) 。
我鼓励大家仔细研究一下scipy.stats模块 。
概率分布有两种类型:离散(discrete)概率分布和连续(continuous)概率分布 。
离散概率分布也称为概率质量函数(probability mass function) 。离散概率分布的例子有伯努利分布(Bernoulli distribution)、二项分布(binomial distribution)、泊松分布(Poisson distribution)和几何分布(geometric distribution)等 。
连续概率分布也称为概率密度函数(probability density function),它们是具有连续取值(例如一条实线上的值)的函数 。正态分布(normal distribution)、指数分布(exponential distribution)和β分布(beta distribution)等都属于连续概率分布 。
若想了解更多关于离散和连续随机变量的知识 , 你可以观看可汗学院关于概率分布的视频 。
二项分布(Binomial Distribution)
服从二项分布的随机变量X表示在n个独立的是/非试验中成功的次数,其中每次试验的成功概率为p 。
E(X) = np, Var(X) = np(1?p)
如果你想知道每个函数的原理 , 你可以在IPython笔记本中使用help file命令 。E(X)表示分布的期望或平均值 。
键入stats.binom?了解二项分布函数binom的更多信息 。
二项分布的例子:抛掷10次硬币,恰好两次正面朝上的概率是多少?
假设在该试验中正面朝上的概率为0.3 , 这意味着平均来说 , 我们可以期待有3次是硬币正面朝上的 。我定义掷硬币的所有可能结果为k = np.arange(0,11):你可能观测到0次正面朝上、1次正面朝上,一直到10次正面朝上 。我使用stats.binom.pmf计算每次观测的概率质量函数 。它返回一个含有11个元素的列表(list),这些元素表示与每个观测相关联的概率值 。
您可以使用.rvs函数模拟一个二项随机变量,其中参数size指定你要进行模拟的次数 。我让Python返回10000个参数为n和p的二项式随机变量 。我将输出这些随机变量的平均值和标准差,然后画出所有的随机变量的直方图 。
泊松分布(Poisson Distribution)
一个服从泊松分布的随机变量X , 表示在具有比率参数(rate parameter)λ的一段固定时间间隔内,事件发生的次数 。参数λ告诉你该事件发生的比率 。随机变量X的平均值和方差都是λ 。
E(X) = λ, Var(X) = λ
泊松分布的例子:已知某路口发生事故的比率是每天2次,那么在此处一天内发生4次事故的概率是多少?
让我们考虑这个平均每天发生2起事故的例子 。泊松分布的实现和二项分布有些类似 , 在泊松分布中我们需要指定比率参数 。泊松分布的输出是一个数列,包含了发生0次、1次、2次,直到10次事故的概率 。我用结果生成了以下图片 。
你可以看到,事故次数的峰值在均值附近 。平均来说 , 你可以预计事件发生的次数为λ 。尝试不同的λ和n的值,然后看看分布的形状是怎么变化的 。
现在我来模拟1000个服从泊松分布的随机变量 。
正态分布(Normal Distribution)
正态分布是一种连续分布,其函数可以在实线上的任何地方取值 。正态分布由两个参数描述:分布的平均值μ和方差σ2。
E(X) = μ, Var(X) = σ2
正态分布的取值可以从负无穷到正无穷 。你可以注意到,我用stats.norm.pdf得到正态分布的概率密度函数 。
β分布(Beta Distribution)
β分布是一个取值在 [0, 1] 之间的连续分布 , 它由两个形态参数α和β的取值所刻画 。
β分布的形状取决于α和β的值 。贝叶斯分析中大量使用了β分布 。
当你将参数α和β都设置为1时,该分布又被称为均匀分布(uniform distribution) 。尝试不同的α和β取值,看看分布的形状是如何变化的 。
指数分布(Exponential Distribution)
指数分布是一种连续概率分布,用于表示独立随机事件发生的时间间隔 。比如旅客进入机场的时间间隔、打进客服中心电话的时间间隔、中文维基百科新条目出现的时间间隔等等 。
我将参数λ设置为0.5 , 并将x的取值范围设置为 $[0, 15]$。
接着,我在指数分布下模拟1000个随机变量 。scale参数表示λ的倒数 。函数np.std中 , 参数ddof等于标准偏差除以 $n-1$ 的值 。
结语(Conclusion)
概率分布就像盖房子的蓝图,而随机变量是对试验事件的总结 。我建议你去看看哈佛大学数据科学课程的讲座,Joe Blitzstein教授给了一份摘要 , 包含了你所需要了解的关于统计模型和分布的全部 。
怎样用python的matplotlib模块画累积分布图下面的程序绘制随机变量X的累积分布函数和数组p的累加结果
pl.plot(t, X.cdf(t))
pl.plot(t2, np.add.accumulate(p)*(t2[1]-t2[0]))
如何使用python做统计分析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/λ.
虽然一个一般累积分布函数python的连续随机变量可以被位移和伸缩通过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.)
让累积分布函数python我们检查伽马分布的形态参数的名字的数量 。(我们知道从上面知道其应该为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:
注意我们也可以以关键字的方式指定形态参数累积分布函数python:
gamma(a=1, scale=2.).stats(moments="mv")
(array(2.0), array(4.0))
Freezing a Distribution
【累积分布函数python 累积分布函数例题】冻结分布
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(prb1e-8, M, n, N)
array([ 1.,3.,5.,7.])
hypergeom.ppf(prb - 1e-8, M, n, N)
array([ 0.,2.,4.,6.])
使用Python构造经验累积分布函数(ECDF)对于一个样本序列累积分布函数python,经验累积分布函数 (Empirical Cumulative Distribution Function)可被定义为
其中是一个指示函数累积分布函数python,如果,指示函数取值为1,否则取值为0,因此能反映在样本中小于的元素数量占比 。
根据格利文科定理(Glivenko–Cantelli Theorem),如果一个样本满足独立同分布(IID),那么其经验累积分布函数会趋近于真实的累积分布函数。
首先定义一个类,命名为ECDF累积分布函数python:
累积分布函数python我们采用均匀分布(Uniform)进行验证,导入 uniform 包,然后进行两轮抽样,第一轮抽取10次,第二轮抽取1000次,比较输出的结果 。
输出结果为累积分布函数python:
而我们知道,在真实的0到1均匀分布中 , 时, , 从模拟结果可以看出,样本量越大,最终的经验累积分布函数值也越接近于真实的累积分布函数值,因此格利文科定理得以证明 。
累积分布函数python的介绍就聊到这里吧,感谢你花时间阅读本站内容 , 更多关于累积分布函数例题、累积分布函数python的信息别忘了在本站进行查找喔 。

    推荐阅读