Search code examples
pythonscipyexponentialkolmogorov-smirnov

Evaluate goodness of fit with kolmogorov-smirnov test for a custom exponential equation


I know that there are several similar questions already here, but none really answer my specific question.

I have a an array of individual values (black square in the picture). And now want to check, if the black squares significantly differ from the red line, which I created by fitting an exponential equation to the data. The fit was created like this:

def exponential_equation(x, k, c):
    return np.exp(-(x - c) / k)


def fit_exp(x, y):
    popt, pcov = curve_fit(exponential_equation, x, y, p0=[0.1, 0.1])
    k = popt[0]
    c = popt[1]
    return k, c

k, c = fit_exp(x_array, y_array) 

As you can see, the exponential equation I used is slightly different from the "standard" exponential equation, thus running something like pval = scipy.stats.kstest(y_array, "expon")[1] doesnt work. I though something similar to pval = scipy.stats.kstest(y_array, exponential_equation, args=(k,c)[1] would work, but this also returns a pvalue of 2.68e-104 and by evaluating the fit by eye, it seems like the pvalue should be above 0.05..

If anyone could tell me what I am doing wrong or could point me in the right direction, I would be very happy!

Cheers!

enter image description here


Solution

  • In case someone is looking this up later, this is what I ended up doing, and I think the results look decent:

    def exponential_equation(x, k, c):
        return np.exp(-(x - c) / k)
    
    
    def cdf_exp(b, k, c):
        max_cdf = quad(exponential_equation, 0, np.inf, args=(k, c))[0]
        return [quad(exponential_equation, 0, i, args=(k, c))[0] / max_cdf for i in b]
    
    k = 0.2
    c = 0.01
    pvalue = scipy.stats.kstest(test_array, cdf_exp, args=(k,c))[1]
    

    In case someone sees a mistake, please let me know, but I am fairly sure this does what I want.