.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/statistics/nonparametric/plot_02_wilcoxon_mann_whitney.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_02_wilcoxon_mann_whitney.py: Wilcoxon and Mann-Whitney rank tests ========================================= Rank tests throw away the raw values and keep only their order, so a few extreme observations cannot dominate. With heavy-tailed data they detect a shift that the t-test misses. .. GENERATED FROM PYTHON SOURCE LINES 11-16 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.statistics import mann_whitney_u_test, two_sample_t_test, wilcoxon_signed_rank_test .. GENERATED FROM PYTHON SOURCE LINES 17-19 Two independent samples with heavy tails ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 19-28 .. code-block:: Python rng = np.random.default_rng(1) a = rng.standard_cauchy(size=60) b = rng.standard_cauchy(size=60) + 1.0 mw = mann_whitney_u_test(a, b) tt = two_sample_t_test(a, b, equal_var=False) print(f"Mann-Whitney: U = {mw.statistic:.0f} of {a.size * b.size}, p = {mw.p_value:.4f}") print(f"Welch t-test: t = {tt.statistic:.3f}, p = {tt.p_value:.4f}") .. rst-class:: sphx-glr-script-out .. code-block:: none Mann-Whitney: U = 1416 of 3600, p = 0.0441 Welch t-test: t = -0.584, p = 0.5605 .. GENERATED FROM PYTHON SOURCE LINES 29-31 Paired measurements ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 31-42 .. code-block:: Python before = rng.normal(loc=120.0, scale=10.0, size=15) after = before - 4.0 + rng.standard_t(df=2, size=15) * 3.0 paired = wilcoxon_signed_rank_test(before, after) print(f"Wilcoxon signed-rank on before - after: p = {paired.p_value:.4f}") fig, ax = plt.subplots() ax.boxplot([a, b]) ax.set_xticks([1, 2], ["sample a", "sample b (shifted by 1)"]) ax.set_ylim(-10, 10) ax.set_title("Cauchy samples, clipped to [-10, 10]") .. image-sg:: /api/gallery/statistics/nonparametric/images/sphx_glr_plot_02_wilcoxon_mann_whitney_001.png :alt: Cauchy samples, clipped to [-10, 10] :srcset: /api/gallery/statistics/nonparametric/images/sphx_glr_plot_02_wilcoxon_mann_whitney_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Wilcoxon signed-rank on before - after: p = 0.0125 Text(0.5, 1.0, 'Cauchy samples, clipped to [-10, 10]') .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.024 seconds) .. _sphx_glr_download_api_gallery_statistics_nonparametric_plot_02_wilcoxon_mann_whitney.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_wilcoxon_mann_whitney.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_wilcoxon_mann_whitney.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_wilcoxon_mann_whitney.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_