# scipy.stats.ks_2samp

scipy.stats.ks_2samp(data1, data2, alternative='two-sided', mode='auto')

  Performs the two-sample Kolmogorov-Smirnov test for goodness of fit.

  This test compares the underlying continuous distributions F(x) and G(x) of two independent samples. See Notes for a description of the available null and alternative hypotheses.

  Parameters
    data1, data2 array_like, 1-Dimensional

      Two arrays of sample observations assumed to be drawn from a continuous distribution, sample sizes can be different.

    alternative{‘two-sided’, ‘less’, ‘greater’}, optional

      Defines the null and alternative hypotheses. Default is ‘two-sided’. Please see explanations in the Notes below.

    mode{‘auto’, ‘exact’, ‘asymp’}, optional

      Defines the method used for calculating the p-value. The following options are available (default is ‘auto’):

      > - ‘auto’ : use ‘exact’ for small size arrays, ‘asymp’ for large

      > - ‘exact’ : use exact distribution of test statistic

      > - ‘asymp’ : use asymptotic distribution of test statistic

  Returns
    statistic float

      KS statistic.

    pvalue float

      One-tailed or two-tailed p-value.

  See also

  `kstest`, `ks_1samp`, `epps_singleton_2samp`, `anderson_ksamp`

  Notes

  There are three options for the null and corresponding alternative hypothesis that can be selected using the alternative parameter.

  - two-sided: The null hypothesis is that the two distributions are identical, F(x)=G(x) for all x; the alternative is that they are not identical.

  - less: The null hypothesis is that F(x) >= G(x) for all x; the alternative is that F(x) < G(x) for at least one x.

  - greater: The null hypothesis is that F(x) <= G(x) for all x; the alternative is that F(x) > G(x) for at least one x.

  Note that the alternative hypotheses describe the CDFs of the underlying distributions, not the observed values. For example, suppose x1 ~ F and x2 ~ G. If F(x) > G(x) for all x, the values in x1 tend to be less than those in x2.

  If the KS statistic is small or the p-value is high, then we cannot reject the null hypothesis in favor of the alternative.

  If the mode is ‘auto’, the computation is exact if the sample sizes are less than 10000. For larger sizes, the computation uses the Kolmogorov-Smirnov distributions to compute an approximate value.

  The ‘two-sided’ ‘exact’ computation computes the complementary probability and then subtracts from 1. As such, the minimum probability it can return is about 1e-16. While the algorithm itself is exact, numerical errors may accumulate for large sample sizes. It is most suited to situations in which one of the sample sizes is only a few thousand.

  We generally follow Hodges’ treatment of Drion/Gnedenko/Korolyuk [1].

  References

  1

    Hodges, J.L. Jr., “The Significance Probability of the Smirnov Two-Sample Test,” Arkiv fiur Matematik, 3, No. 43 (1958), 469-86.

  Examples

      >>> from scipy import stats
      >>> rng = np.random.default_rng()

      >>> n1 = 200  # size of first sample
      >>> n2 = 300  # size of second sample

  For a different distribution, we can reject the null hypothesis since the pvalue is below 1%:

      >>> rvs1 = stats.norm.rvs(size=n1, loc=0., scale=1, random_state=rng)
      >>> rvs2 = stats.norm.rvs(size=n2, loc=0.5, scale=1.5, random_state=rng)
      >>> stats.ks_2samp(rvs1, rvs2)
       KstestResult(statistic=0.24833333333333332, pvalue=5.846586728086578e-07)

  For a slightly different distribution, we cannot reject the null hypothesis at a 10% or lower alpha since the p-value at 0.144 is higher than 10%

      >>> rvs3 = stats.norm.rvs(size=n2, loc=0.01, scale=1.0, random_state=rng)
      >>> stats.ks_2samp(rvs1, rvs3)
      KstestResult(statistic=0.07833333333333334, pvalue=0.4379658456442945)

  For an identical distribution, we cannot reject the null hypothesis since the p-value is high, 41%:

      >>> rvs4 = stats.norm.rvs(size=n2, loc=0.0, scale=1.0, random_state=rng)
      >>> stats.ks_2samp(rvs1, rvs4)
      KstestResult(statistic=0.12166666666666667, pvalue=0.05401863039081145)

scipy.stats.ks_1samp scipy.stats.epps_singleton_2samp