我试图在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)
,但它适用于随机变量,这不是我的情况。
因为你有很多点,也许你会发现相邻点之间的线性插值是可以接受的。做二分搜索,找到与你首先寻找的概率相邻的点。像这样,稍微整理一下:
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