.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/statistics/likelihood/plot_01_maximum_likelihood.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_01_maximum_likelihood.py: Fisher's maximum likelihood ================================ Fits an exponential and a gamma distribution to the same skewed data by maximum likelihood, and plots the exponential model's log-likelihood as a function of its scale: the curve peaks exactly at the sample mean, the closed-form MLE. .. 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 maximum_likelihood_fit .. GENERATED FROM PYTHON SOURCE LINES 19-21 Fitting two candidate models ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 21-30 .. code-block:: Python rng = np.random.default_rng(0) data = rng.gamma(shape=2.0, scale=1.5, size=300) expon_fit = maximum_likelihood_fit(data, "expon", floc=0) gamma_fit = maximum_likelihood_fit(data, "gamma", floc=0) print(f"exponential: scale={expon_fit.params[1]:.3f} (sample mean {data.mean():.3f}), AIC={expon_fit.aic():.1f}") print(f"gamma: shape={gamma_fit.params[0]:.3f}, scale={gamma_fit.params[2]:.3f}, AIC={gamma_fit.aic():.1f}") .. rst-class:: sphx-glr-script-out .. code-block:: none exponential: scale=2.908 (sample mean 2.908), AIC=1242.4 gamma: shape=2.243, scale=1.296, AIC=1154.0 .. GENERATED FROM PYTHON SOURCE LINES 31-33 The log-likelihood curve ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 33-43 .. code-block:: Python scales = np.linspace(1.0, 6.0, 200) loglik = [np.sum(stats.expon.logpdf(data, scale=s)) for s in scales] fig, ax = plt.subplots() ax.plot(scales, loglik) ax.axvline(expon_fit.params[1], color="C1", ls="--", label="MLE = sample mean") ax.set_xlabel("exponential scale") ax.set_ylabel("log-likelihood") ax.legend() .. image-sg:: /api/gallery/statistics/likelihood/images/sphx_glr_plot_01_maximum_likelihood_001.png :alt: plot 01 maximum likelihood :srcset: /api/gallery/statistics/likelihood/images/sphx_glr_plot_01_maximum_likelihood_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.024 seconds) .. _sphx_glr_download_api_gallery_statistics_likelihood_plot_01_maximum_likelihood.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_maximum_likelihood.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_maximum_likelihood.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_maximum_likelihood.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_