scala breeze 中的二项分布

scala breeze 是类似于 matlab R numpy  的数值计算软件。其中的stats.distributions 中包括了《概率论与数理统计》

的常用分布函数

下面回顾一下二项分布。

二项分布的取样

import breeze.stats.distributions

var bi = breeze.stats.distributions.Binomial(12, 0.2)

val st = new Array[Int](100)

for(i<- 0 to 100)st(i) = bi.sample()

我们将n:试验的次数 设为12次 p:结果为true的概率 设为0.2 。st 是对该二项分布100次取样的结果。

由于bi 的期望,方差为 np, np(1-p); 我们观察到st 满足一定的统计性质。

 

二项分布的可加性

var bj = breeze.stats.distributions.Binomial(8, 0.2)
var bk = breeze.stats.distributions.Binomial(20, 0.2)

val stl = new Array[Int](100)

for(i<- 0 to 100)st(i) = bi.sample()+bj.sample()

for(i<- 0 to 100)stl(i) = bk.sample()

println(st.reduce((x,y)=>x+y), stl.reduce((x,y) => x+y))

 

二项分布的泊松近似

当试验的次数趋于无穷大,而乘积np固定时,二项分布收敛于泊松分布。因此参数为λ = np的泊松分布可以作为二项分布B(np)的近似,如果n足够大,而p足够小。

 

例子

poj 3984

posted @ 2015-12-31 20:54  中国邮政777号  Views(679)  Comments(0)    收藏  举报