Search code examples
matlabexponentialcdfgoodness-of-fit

Matlab Kolmogorov-Smirnov Test


I'm using MATLAB to analyze some neuroscience data, and I made an interspike interval distribution and fit an exponential to it. Then, I wanted to check this fit using a Kolmogorov-Smirnov test with MATLAB.

The data for the neuron spikes is just stored in a vector of spikes. The spikes vector is a 111 by 1 vector, where each entry is another vector. Each entry in thie spikes vector represents a trial. The number of spikes in each trial varies. For example, spikes{1} is a [1x116 double], meaning there are 116 spikes. The next has 115 spikes, then 108, etc.

Now, I understand that the kstest in MATLAB takes a couple of parameters. You enter the data in the first one, so I took all the interspike intervals and created a row vector alldiffs which stores all the interspike intervals. I want to set my CDF to that for an exponential function fit:

test_cdf = [transpose(alldiffs), transpose(1-exp(-alldiffs*firingrate))];

Note that the theoretical exponential (with which I fit the data) is r*exp(-rt) where r is the firing rate. I get a firing rate of about 0.2. Now, when I put this all together, I run the kstest:

[h,p] = kstest(alldiffs, 'CDF', test_cdf)

However, the result is a p value on the order of 1.4455e-126. I've tried redoing the test_cdf with another of the methods on Mathworks' website documentation:

test_cdf = [transpose(alldiffs), cdf('exp', transpose(alldiffs), 1/firingrate)];

This gives the exact same result! Is the fit just horrible? I don't know why I get such low p-values. Please help!

I would post an image of the fit, but I don't have enough reputation.

P.S. If there is a better place to post this, let me know and I'll repost.


Solution

  • Here is an example with fake data and yet another way to create the CDF:

    >> data = exprnd(.2, 100);
    >> test_cdf = makedist('exp', 'mu', .2);
    >> [h, p] = kstest(data, 'CDF', test_cdf)
    
    h =
    
         0
    
    
    p =
    
        0.3418
    

    However, why are you doing a KS Test?

    All models are wrong, some are useful.

    No neuron is perfectly a Poisson process and with enough data, you'll always have a significantly non-exponential ISI, as measured by a KS test. That doesn't mean you can't make the simplifying assumption of an exponential ISI, depending on what phenomena you're trying model.