如何在Python

时间:2016-11-06 13:39:48

标签: python numpy inverse cdf

我试图在Python中找到离散概率分布的逆向CDF函数,然后绘制它。我的CDF来自以下numpy输出:

array([ 0.228157,  0.440671,  0.588515,  0.683326,  0.740365,  0.783288,
        0.81362 ,  0.840518,  0.859213,  0.876764,  0.889355,  0.89813 ,
        0.909194,  0.916443,  0.9256  ,  0.930369,  0.938572,  0.942387,
        0.946012,  0.951353,  0.954405,  0.956694,  0.965088,  0.966614,
        0.96814 ,  0.969475,  0.970047,  0.971001,  0.971573,  0.973099,
        0.974816,  0.975388,  0.977105,  0.984163,  0.984354,  0.984736,
        0.98569 ,  0.985881,  0.986072,  0.986644,  0.990269,  0.990651,
        0.990842,  0.993322,  0.993704,  0.994467,  0.995039,  0.995802,
        0.996184,  0.996375,  0.996566,  0.996757,  0.997329,  0.99752 ,
        0.997711,  0.997902,  0.998093,  0.998284,  0.998475,  0.998666,
        0.998857,  0.999239,  0.999621,  0.999812,  1.00000])

我尝试rv_discrete.ppf(q, *args, **kwds),但它适用于随机变量,这不是我的情况。

1 个答案:

答案 0 :(得分:1)

由于你有很多点,你可能会发现相邻点之间可以接受线性插值。进行二元搜索以找到与您首先寻找的概率相邻的点。像这样,有些整理:

import numpy as np

CDF = np.array([ 0.228157, 0.440671, 0.588515, 0.683326, 0.740365, 0.783288, 0.81362 , 0.840518, 0.859213, 0.876764, 0.889355, 0.89813 , 0.909194, 0.916443, 0.9256 , 0.930369, 0.938572, 0.942387, 0.946012, 0.951353, 0.954405, 0.956694, 0.965088, 0.966614, 0.96814 , 0.969475, 0.970047, 0.971001, 0.971573, 0.973099, 0.974816, 0.975388, 0.977105, 0.984163, 0.984354, 0.984736, 0.98569 , 0.985881, 0.986072, 0.986644, 0.990269, 0.990651, 0.990842, 0.993322, 0.993704, 0.994467, 0.995039, 0.995802, 0.996184, 0.996375, 0.996566, 0.996757, 0.997329, 0.99752 , 0.997711, 0.997902, 0.998093, 0.998284, 0.998475, 0.998666, 0.998857, 0.999239, 0.999621, 0.999812, 1.00000] )

## inverse of .3
index = np.searchsorted(CDF, .3) 
print ( index )
print ( (.3 - CDF [ index-1 ] ) / ( CDF [ index ] - CDF [ index-1 ] ) )

输出就是这个。

1
0.338062433534