.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/statistics/hypothesis_tests/plot_03_fisher_exact_lady_tea.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_hypothesis_tests_plot_03_fisher_exact_lady_tea.py: Fisher's exact test and the lady tasting tea ================================================= Fisher's thought experiment: a lady claims she can tell whether milk or tea was poured first. She is given 8 cups, 4 of each, and told so. If she is only guessing, the number of milk-first cups she identifies correctly follows a hypergeometric distribution, and getting all 4 right happens with probability exactly 1/70. .. GENERATED FROM PYTHON SOURCE LINES 13-19 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from scipy import stats from mathematicskit.statistics import fisher_exact_test .. GENERATED FROM PYTHON SOURCE LINES 20-22 The null distribution of correct identifications ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 22-34 .. code-block:: Python k = np.arange(5) pmf = stats.hypergeom.pmf(k, 8, 4, 4) for correct in range(5): table = [[correct, 4 - correct], [4 - correct, correct]] p = fisher_exact_test(table, alternative="greater").p_value print(f"{correct} of 4 correct: P(exactly) = {pmf[correct] * 70:.0f}/70, one-sided p = {p:.4f}") fig, ax = plt.subplots() ax.bar(k, pmf * 70) ax.set_xlabel("milk-first cups correctly identified") ax.set_ylabel("ways out of 70") .. image-sg:: /api/gallery/statistics/hypothesis_tests/images/sphx_glr_plot_03_fisher_exact_lady_tea_001.png :alt: plot 03 fisher exact lady tea :srcset: /api/gallery/statistics/hypothesis_tests/images/sphx_glr_plot_03_fisher_exact_lady_tea_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 0 of 4 correct: P(exactly) = 1/70, one-sided p = 1.0000 1 of 4 correct: P(exactly) = 16/70, one-sided p = 0.9857 2 of 4 correct: P(exactly) = 36/70, one-sided p = 0.7571 3 of 4 correct: P(exactly) = 16/70, one-sided p = 0.2429 4 of 4 correct: P(exactly) = 1/70, one-sided p = 0.0143 Text(46.722222222222214, 0.5, 'ways out of 70') .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.019 seconds) .. _sphx_glr_download_api_gallery_statistics_hypothesis_tests_plot_03_fisher_exact_lady_tea.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_03_fisher_exact_lady_tea.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_03_fisher_exact_lady_tea.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_03_fisher_exact_lady_tea.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_