mass函数Python mass函数

如何在Python中实现这五类强大的概率分布Python – 伯乐在线
首页所有文章观点与动态基础知识系列教程实践项目工具与框架工具资源Python小组伯乐在线Python - 伯乐在线所有文章实践项目如何在Python中实现这五类强大的概率分布如何在Python中实现这五类强大的概率分布
2015/04/25 · 实践项目 · 概率分布
分享到: 12
本文由 伯乐在线 - feigao.me 翻译,Daetalus 校稿 。未经许可,禁止转载!
英文出处: 。欢迎加入翻译组 。
R编程语言已经成为统计分析中的事实标准 。但在这篇文章中 , 我将告诉你在Python中实现统计学概念会是如此容易 。我要使用Python实现一些离散和连续的概率分布 。虽然我不会讨论这些分布的数学细节,但我会以链接的方式给你一些学习这些统计学概念的好资料 。在讨论这些概率分布之前,我想简单说说什么是随机变量(random variable) 。随机变量是对一次试验结果的量化 。
举个例子 , 一个表示抛硬币结果的随机变量可以表示成Python
X = {1 如果正面朝上,
2 如果反面朝上}
12X = {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做统计分析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/λ进行 。
【mass函数Python mass函数】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(prb1e-8, M, n, N)
array([ 1.,3.,5.,7.])
hypergeom.ppf(prb - 1e-8, M, n, N)
array([ 0.,2.,4.,6.])
程序数据toodata中 参数mass是指我觉得参数,也百叫参变量 , 是一个变量 。我们在研究当前问题的时候,关心某几个变量的变化以及它们之间的相互关系 , 其中度有一个或一些叫自变量,另一个或另一些叫因变量 。参数就是用来代替一个数的未知数 比如你定义时间,用t做参数 当你要计算路程vt(其中v设为常量),当要算一段时间的路程 , 只要用这个公式,再带入一个t的值就可以了 。总之,参数就是一个符号,没有实际意义,要让他有实际意义,就给参数附一个值就可以了 形参就是没有实际意义的参数 比如上面的t 实参就是有实际意义的参数 比如把上面的t赋值的那个常量 二维数组就是既有行又有列的 。
比如:你的程序为Untitled1.exe
打开命令提示符:
转到你的Untitled1.exe的目录,
输入下列命令:
Untitled1.exe a basdfsa 1314324 -k -f
回车就可以运行了 。
这时,在Main函数里定义的
int main(int argc, char *args[])
里的argc = 6,就是表示有六个参数,
对应的
args[0] = "Untitled1.exe";
args[1] = "a";
args[2] = "basdfsa";
args[3] = "1314324";
args[4] = "-k";
args[5] = "-f";
函数调用时需注意的东西 。函数定义中函数首部中的参数叫形参,调用函数时使用的参数叫实参 。C函数调用采用“值传递”,比如有函数:
int max(int x,int y)
{if(xy)
return x;
return y;
}
在调用该函数时,有max(a,b);则该调用只把a(实参)的值赋给x(形参),把b(实参)的值赋给y(形参) 。函数中对x,y的任何动作不改变a,b的值 。值得注意的是如果用指针作参数,虽然符合上面原则,但对形参指向的数据的更改会影响实参指向数据的值 。如函数:
int swap(int *x.int *y)/*交换*/
{int temp=*x;
*x=*y;
*y=temp;
}
则调用后swap第一个实参指向数据变为原第二个实参指向的数据,原第二个实参指向的数据变为第一个实参指向数据(因为指针的值是其指向数据的地址,该形参和实参指向同一个内存地址,形参对该地址存储值的改变,当然会影响到实参指向的地址存储值),但两指针指向(指向为其值)不变(上面的原则) 。
关于mass函数Python和mass函数的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读