.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/statistics/nonparametric/plot_01_kolmogorov_smirnov.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_api_gallery_statistics_nonparametric_plot_01_kolmogorov_smirnov.py: The Kolmogorov-Smirnov test ================================ Draws a sample from a Student t distribution with 3 degrees of freedom and compares its empirical CDF with the standard normal CDF. The KS statistic is the largest vertical gap between the two curves. .. GENERATED FROM PYTHON SOURCE LINES 11-17 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from scipy import stats from mathematicskit.statistics import kolmogorov_smirnov_test .. GENERATED FROM PYTHON SOURCE LINES 18-20 One sample against a model ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 20-38 .. code-block:: Python rng = np.random.default_rng(0) data = np.sort(rng.standard_t(df=3, size=800)) result = kolmogorov_smirnov_test(data, "norm") print(f"vs. N(0,1): D = {result.statistic:.4f}, p = {result.p_value:.4f}") print(f"vs. t(3): p = {kolmogorov_smirnov_test(data, stats.t(df=3).cdf).p_value:.4f}") ecdf = np.arange(1, data.size + 1) / data.size fig, ax = plt.subplots() ax.step(data, ecdf, where="post", label="empirical CDF") ax.plot(data, stats.norm.cdf(data), label="normal CDF") model = stats.norm.cdf(data) above, below = ecdf - model, model - (ecdf - 1 / data.size) k = int(np.argmax(np.maximum(above, below))) top = ecdf[k] if above[k] >= below[k] else ecdf[k] - 1 / data.size ax.vlines(data[k], model[k], top, color="C3", lw=3, label=f"D = {result.statistic:.3f}") ax.legend() .. image-sg:: /api/gallery/statistics/nonparametric/images/sphx_glr_plot_01_kolmogorov_smirnov_001.png :alt: plot 01 kolmogorov smirnov :srcset: /api/gallery/statistics/nonparametric/images/sphx_glr_plot_01_kolmogorov_smirnov_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none vs. N(0,1): D = 0.0539, p = 0.0184 vs. t(3): p = 0.6969 .. GENERATED FROM PYTHON SOURCE LINES 39-41 Two samples ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 41-45 .. code-block:: Python other = rng.normal(size=300) two = kolmogorov_smirnov_test(data, other) print(f"two-sample: D = {two.statistic:.4f}, p = {two.p_value:.4f}") .. rst-class:: sphx-glr-script-out .. code-block:: none two-sample: D = 0.0708, p = 0.2139 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.025 seconds) .. _sphx_glr_download_api_gallery_statistics_nonparametric_plot_01_kolmogorov_smirnov.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_kolmogorov_smirnov.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_kolmogorov_smirnov.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_kolmogorov_smirnov.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_