.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/statistics/likelihood/plot_02_wilks_likelihood_ratio.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_likelihood_plot_02_wilks_likelihood_ratio.py: Wilks's theorem and the likelihood-ratio test =================================================== Repeatedly tests the true null hypothesis "the normal mean is 0" with the likelihood-ratio statistic 2(l1 - l0). Wilks's theorem says the statistic's null distribution approaches chi-square with one degree of freedom, the number of parameters the null hypothesis fixes. .. GENERATED FROM PYTHON SOURCE LINES 12-18 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from scipy import stats from mathematicskit.statistics import likelihood_ratio_test, maximum_likelihood_fit .. GENERATED FROM PYTHON SOURCE LINES 19-21 Simulating the statistic under the null ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 21-38 .. code-block:: Python rng = np.random.default_rng(0) statistics = [] for _ in range(1000): data = rng.normal(loc=0.0, scale=1.0, size=50) null = maximum_likelihood_fit(data, "norm", floc=0.0) alt = maximum_likelihood_fit(data, "norm") statistics.append(likelihood_ratio_test(null.log_likelihood, alt.log_likelihood, df=1).statistic) statistics = np.array(statistics) print(f"fraction above the chi2(1) 95% point: {np.mean(statistics > stats.chi2.ppf(0.95, 1)):.3f} (nominal 0.05)") fig, ax = plt.subplots() ax.hist(statistics, bins=40, density=True, alpha=0.6, label="simulated 2(l1 - l0)") grid = np.linspace(0.05, 10, 200) ax.plot(grid, stats.chi2.pdf(grid, 1), label=r"$\chi^2_1$ density") ax.set_ylim(0, 1.5) ax.legend() .. image-sg:: /api/gallery/statistics/likelihood/images/sphx_glr_plot_02_wilks_likelihood_ratio_001.png :alt: plot 02 wilks likelihood ratio :srcset: /api/gallery/statistics/likelihood/images/sphx_glr_plot_02_wilks_likelihood_ratio_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none fraction above the chi2(1) 95% point: 0.066 (nominal 0.05) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.082 seconds) .. _sphx_glr_download_api_gallery_statistics_likelihood_plot_02_wilks_likelihood_ratio.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_wilks_likelihood_ratio.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_wilks_likelihood_ratio.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_wilks_likelihood_ratio.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_