.. 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_02_chi_square_tests.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_02_chi_square_tests.py: Chi-square goodness-of-fit and independence tests ========================================================== Pearson's chi-square statistic applied two ways: testing whether a die's observed roll counts match a fair-die model (goodness-of-fit), and testing whether two categorical variables in a contingency table are independent. .. GENERATED FROM PYTHON SOURCE LINES 12-16 .. code-block:: Python import numpy as np from mathematicskit.statistics import chi_square_goodness_of_fit, chi_square_independence .. GENERATED FROM PYTHON SOURCE LINES 17-19 Goodness-of-fit: is this die fair? ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 19-29 .. code-block:: Python rng = np.random.default_rng(0) rolls = rng.choice(6, size=600, p=[0.14, 0.16, 0.15, 0.15, 0.17, 0.23]) observed = np.array([np.sum(rolls == face) for face in range(6)], dtype=float) expected = np.full(6, 100.0) gof_result = chi_square_goodness_of_fit(observed, expected) print(f"chi2={gof_result.statistic:.3f}, df={gof_result.df:.0f}, p={gof_result.p_value:.4f}") print("reject fair-die null at alpha=0.05:", gof_result.reject_null(alpha=0.05)) .. rst-class:: sphx-glr-script-out .. code-block:: none chi2=41.040, df=5, p=0.0000 reject fair-die null at alpha=0.05: True .. GENERATED FROM PYTHON SOURCE LINES 30-32 Independence: does treatment outcome depend on treatment group? ----------------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 32-37 .. code-block:: Python contingency_table = np.array([[30.0, 10.0], [20.0, 40.0]]) independence_result = chi_square_independence(contingency_table) print(f"chi2={independence_result.statistic:.3f}, df={independence_result.df:.0f}, p={independence_result.p_value:.6f}") print("expected counts under independence:\n", independence_result.extra["expected"]) .. rst-class:: sphx-glr-script-out .. code-block:: none chi2=16.667, df=1, p=0.000045 expected counts under independence: [[20. 20.] [30. 30.]] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.001 seconds) .. _sphx_glr_download_api_gallery_statistics_hypothesis_tests_plot_02_chi_square_tests.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_chi_square_tests.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_chi_square_tests.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_chi_square_tests.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_