Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python scipy.stats.binom库能否返回一个值";";对于一个特定的;k";及;p";_Python_Numpy_Scipy - Fatal编程技术网

Python scipy.stats.binom库能否返回一个值";";对于一个特定的;k";及;p";

Python scipy.stats.binom库能否返回一个值";";对于一个特定的;k";及;p";,python,numpy,scipy,Python,Numpy,Scipy,我有一个我希望是一个简单的问题。我正试图将SciPy作为一种业余爱好来学习(是的,我真的应该多出去玩),目前我正在玩SciPy.stats.binom二项式概率函数(我学习统计学已经20多年了,这对我没有帮助) 因此,我给自己设置了以下问题 """ Binomial probability problem The planet Klom is famous for two things. The superabundance of dilithium crystals which are so

我有一个我希望是一个简单的问题。我正试图将SciPy作为一种业余爱好来学习(是的,我真的应该多出去玩),目前我正在玩SciPy.stats.binom二项式概率函数(我学习统计学已经20多年了,这对我没有帮助)

因此,我给自己设置了以下问题

"""
Binomial probability problem

The planet Klom is famous for two things. The superabundance of
dilithium crystals which are so common they are simply lying all
over the ground; and their notoriously poor quality as only 10%
of them are any good. Good and bad crystals are indistinguishable
to the naked eye.

Captain Kirk requires four good crystals to power his spaceship
and beams down to the surface. He can only carry 12 crystals back
to his ship.

What is the probability of him returning with 0, 1, 2, .. 12
good crystals.

Plot the probability

How many crystals should he bring back if he wants a better than 50,
90, 95 and 99% probability of having at least four working ones.
"""

import scipy.stats
import numpy
import matplotlib.pyplot

[N, p] = [12, 0.1]
rv = scipy.stats.binom(N, p)
x = numpy.arange(0, N+1)
pmf = rv.pmf(x)
for k in range(len(pmf)):
    print("{:3d} {:10.6f}".format(k, pmf[k]))

fig = matplotlib.pyplot.figure()
ax = fig.add_subplot(111)
ax.plot(x, pmf, 'bo')
ax.vlines(x, 0, pmf, lw=2)
ax.set_xlabel('Number of Good crystals')
ax.set_ylabel('Binomial PDF')
matplotlib.pyplot.show()
现在我显然可以解决问题的第一部分了。但是调用
scipy.stats.binom(N,p)
会创建一个冻结的PMF,其中N和p的值被冻结,并为给定的k计算概率

我如何(除了使用蛮力循环)算出N的值,这给了柯克50%,90%的机会?(即,有效地计算给定k和p的N)。我确信一定有一些函数可以做到这一点,但我不能确定是哪个函数

干杯


Tim.

因为我们在这里处理的是一个离散分布,所以我们需要使用
scipy.optimize

第一部分很简单:

In [131]:

import scipy.stats as ss
import scipy.optimize as so
In [132]:

ss.binom.pmf(range(12), n=12, p=0.1)
Out[132]:
array([  2.82429536e-01,   3.76572715e-01,   2.30127770e-01,
         8.52325076e-02,   2.13081269e-02,   3.78811145e-03,
         4.91051484e-04,   4.67668080e-05,   3.24769500e-06,
         1.60380000e-07,   5.34600000e-09,   1.08000000e-10])
对于第二部分,我们需要通过
scipy.optimize
从方程PMF(4,N,0.1)=0.5/0.1/0.05/0.01中求解N,我们需要
Nelder-Mead
优化器,它不需要来自导数的信息,因此适用于我们面临的离散问题

In [133]:

for P in [0.5, 0.1, 0.05, 0.01]:
    N=int(so.fmin(lambda n, p: (ss.binom.pmf(4, int(n), 0.1)-p)**2, 40, args=(P,), disp=0)) 
    #the initial guess is 40, because 40*0.1=4
    print N,
    print ss.binom.pmf(4, N, p=0.1)
39 0.205887043441
67 0.100410451946
81 0.0498607360095
107 0.00999262321 #need to pick 107 crystals in order to be sure 99% of time that there are at least 4 usable crystals.

谢谢你的回答,但我可以问一个补充问题吗?为什么在优化例程中如此。fmim有必要对pmf进行平方处理?欢迎您的帮助,很高兴为您提供帮助。我们使用Nelder-Mead(
so.fmin
)是因为我们需要一种无导数的优化方法。你看,PMF只支持整数。因此,梯度/导数函数不是平滑函数,我们无法使用
scipy
提供的许多其他方法。其中许多都需要平滑导数才能工作。