Home > Mobile >  efficiently vectorize scipy.stats.beta function
efficiently vectorize scipy.stats.beta function

Time:01-31

I wanted to fit a curve (with scipy.curve_fit) that contains a beta distribution in the formula

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]

the problem is that scipy.stats.beta.cdf only accepts a float as the a parameter, and I need that the shape of the beta function depends on the X[0] column, I know that I can solve that with a loop, but because I'm using the scipy.curve_fit method/solver, I need that the f(X,a,b) is evaluated very fast. How can I "vectorize" the scipy.stats.beta.cdf so it can receive a NumPy array instead of a float ? or is there a way to "create" a vectorized beta?

Thanks !

CodePudding user response:

finally turns out that scipy.stats.beta.cdf is able to receive a column vector with a list of parameters (thanks @Michael Szczesny for pointing that out).

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]
  •  Tags:  
  • Related