.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_data_analysis/statistical_tests/plot_smirnov_test.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_data_analysis_statistical_tests_plot_smirnov_test.py: Test identical distributions ============================ .. GENERATED FROM PYTHON SOURCE LINES 8-21 In this example we are going to estimate whether two samples follow the same distribution using the two samples Kolmogorov-Smirnov test and the graphical QQ-plot test. The Smirnov test relies on the maximum distance between the cumulative distribution function. If :math:`F_{n_1}^{*}` and :math:`F_{n_2}^{*}` are the empirical cumulative density functions of both samples of size :math:`n_1` and :math:`n_2`, the Smirnov test evaluates the decision variable: .. math:: D^2 = \displaystyle \sqrt{\frac{n_1n_2}{n_1+n_2}} \sup_{x}|F_{n_1}^{*}(x) - F_{n_2}^{*}(x)| which tends towards the Kolmogorov distribution. The hypothesis of same distribution is rejected if :math:`D^2` is too high (depending on the p-value threshold). The QQ-plot graph plots empirical quantiles levels from two samples. If both samples correspond to the same probability distribution the curve should be close to the diagonal. .. GENERATED FROM PYTHON SOURCE LINES 23-29 .. code-block:: default from __future__ import print_function import openturns as ot import openturns.viewer as viewer from matplotlib import pylab as plt ot.Log.Show(ot.Log.NONE) .. GENERATED FROM PYTHON SOURCE LINES 30-31 Generate 3 samples, sample1 and sample2 arise from the same distribution .. GENERATED FROM PYTHON SOURCE LINES 31-39 .. code-block:: default distribution1 = ot.Gumbel(0.2, 0.5) distribution2 = ot.Uniform() ot.RandomGenerator.SetSeed(5) sample1 = distribution1.getSample(100) sample2 = distribution1.getSample(100) sample3 = distribution2.getSample(100) .. GENERATED FROM PYTHON SOURCE LINES 40-41 Visually compare sample1 and sample2 using QQ-plot .. GENERATED FROM PYTHON SOURCE LINES 41-44 .. code-block:: default graph = ot.VisualTest.DrawQQplot(sample1, sample2) view = viewer.View(graph) .. image-sg:: /auto_data_analysis/statistical_tests/images/sphx_glr_plot_smirnov_test_001.png :alt: Two sample QQ-plot :srcset: /auto_data_analysis/statistical_tests/images/sphx_glr_plot_smirnov_test_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 45-46 Visually compare sample1 and sample3 using QQ-plot .. GENERATED FROM PYTHON SOURCE LINES 46-49 .. code-block:: default graph = ot.VisualTest.DrawQQplot(sample1, sample3) view = viewer.View(graph) .. image-sg:: /auto_data_analysis/statistical_tests/images/sphx_glr_plot_smirnov_test_002.png :alt: Two sample QQ-plot :srcset: /auto_data_analysis/statistical_tests/images/sphx_glr_plot_smirnov_test_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 50-51 Numerically test sample1 against sample2 .. GENERATED FROM PYTHON SOURCE LINES 51-56 .. code-block:: default test_result = ot.HypothesisTest.TwoSamplesKolmogorov(sample1, sample2) print('Samples follow the same distribution?', test_result.getBinaryQualityMeasure(), 'p-value=%.6g' % test_result.getPValue(), 'threshold=%.6g' % test_result.getThreshold()) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Samples follow the same distribution? True p-value=0.190264 threshold=0.05 .. GENERATED FROM PYTHON SOURCE LINES 57-58 Numerically test sample1 against sample3 .. GENERATED FROM PYTHON SOURCE LINES 58-62 .. code-block:: default test_result = ot.HypothesisTest.TwoSamplesKolmogorov(sample1, sample3) print('Samples follow the same distribution?', test_result.getBinaryQualityMeasure(), 'p-value=%.6g' % test_result.getPValue(), 'threshold=%.6g' % test_result.getThreshold()) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Samples follow the same distribution? False p-value=9.86999e-15 threshold=0.05 .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.238 seconds) .. _sphx_glr_download_auto_data_analysis_statistical_tests_plot_smirnov_test.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_smirnov_test.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_smirnov_test.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_