有效矢量化 scipy.stats.beta 函数

efficiently vectorize scipy.stats.beta function

我想拟合在公式

中包含beta分布的曲线(scipy.curve_fit
def f(X,a,b):
    # X.shape == (2, 100)
    # X[0] is the column 0 of the matrix X, 
    # the following line doesn't work because a must be a float not an array 
    beta_cdf = beta.cdf([0,0.5,1], a=a*X[0], b=b)) 
    beta_dif = np.diff(beta_cdf)
    return beta_dif*X[1]

问题是 scipy.stats.beta.cdf 只接受一个浮点数作为 a 参数,我需要 beta 函数的形状取决于 X[0] 列,我知道我可以用循环解决这个问题,但是因为我使用的是 scipy.curve_fit method/solver,所以我需要非常快速地评估 f(X,a,b)。我如何“矢量化” scipy.stats.beta.cdf 以便它可以接收 NumPy 数组而不是 float ?或者有没有办法“创建”矢量化 beta?

谢谢!

最终证明 scipy.stats.beta.cdf 能够接收带有参数列表的列向量(感谢@Michael Szczesny 指出这一点)。

def f(X:np.ndarray, a:float, b:float):
    beta_cdf = beta.cdf([0,0.5,1], 
                        a= a*X[0].reshape((-1, 1)), 
                        b=b)) 
    # where a*X[0].reshape((-1, 1)) its a column wise array = [[1], [2], ..]
    beta_dif = np.diff(beta_cdf)
    return beta_dif*X[1]