.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/statistics/descriptive/plot_02_gauss_error_law.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_descriptive_plot_02_gauss_error_law.py: Gauss's normal law of errors ============================= Repeated measurements of one quantity scatter around its true value. Gauss (1809) asked which error law makes the arithmetic mean of the measurements the most probable value of the quantity, and found the normal law. This example simulates noisy measurements, compares their errors with the normal curve, and checks that the mean both maximizes the normal error probability and minimizes the sum of squared errors. .. GENERATED FROM PYTHON SOURCE LINES 14-20 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from scipy import stats from mathematicskit.statistics import descriptive_stats, maximum_likelihood_fit .. GENERATED FROM PYTHON SOURCE LINES 21-23 Repeated measurements of a known length ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 23-34 .. code-block:: Python rng = np.random.default_rng(0) true_value = 100.0 sigma = 0.5 measurements = true_value + rng.normal(scale=sigma, size=400) errors = measurements - true_value summary = descriptive_stats(errors) print(f"error mean = {summary.mean:+.4f}, error std = {summary.std:.4f} (sigma = {sigma})") print(f"skewness = {summary.skewness:+.3f}, excess kurtosis = {summary.kurtosis:+.3f} (normal: 0, 0)") .. rst-class:: sphx-glr-script-out .. code-block:: none error mean = -0.0183, error std = 0.4982 (sigma = 0.5) skewness = -0.037, excess kurtosis = +0.310 (normal: 0, 0) .. GENERATED FROM PYTHON SOURCE LINES 35-42 The mean is the most probable value ----------------------------------------------------- Under the normal error law, the probability of the observed errors for a candidate true value :math:`\mu` is proportional to :math:`\exp(-\sum_i (x_i - \mu)^2 / 2\sigma^2)`, so maximizing it is the same as minimizing the sum of squared errors -- least squares. .. GENERATED FROM PYTHON SOURCE LINES 42-51 .. code-block:: Python fit = maximum_likelihood_fit(measurements, "norm") print(f"most probable value = {fit.params[0]:.4f}, arithmetic mean = {measurements.mean():.4f}") candidates = np.linspace(99.8, 100.2, 401) log_prob = np.array([np.sum(stats.norm.logpdf(measurements, loc=m, scale=sigma)) for m in candidates]) sse = np.array([np.sum((measurements - m) ** 2) for m in candidates]) print(f"argmax log-probability = {candidates[np.argmax(log_prob)]:.3f}, argmin squared error = {candidates[np.argmin(sse)]:.3f}") .. rst-class:: sphx-glr-script-out .. code-block:: none most probable value = 99.9817, arithmetic mean = 99.9817 argmax log-probability = 99.982, argmin squared error = 99.982 .. GENERATED FROM PYTHON SOURCE LINES 52-54 Plot the error law and the most-probable-value curve ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 54-69 .. code-block:: Python fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4)) ax1.hist(errors, bins=30, density=True, alpha=0.6, label="measurement errors") grid = np.linspace(-4 * sigma, 4 * sigma, 300) ax1.plot(grid, stats.norm.pdf(grid, scale=sigma), "C1", label="Gauss's normal law") ax1.set_xlabel("error $x_i - \\mu$") ax1.set_ylabel("density") ax1.legend() ax2.plot(candidates, log_prob) ax2.axvline(measurements.mean(), color="C1", ls="--", label="arithmetic mean") ax2.set_xlabel("candidate true value $\\mu$") ax2.set_ylabel("log-probability of the errors") ax2.legend() fig.tight_layout() .. image-sg:: /api/gallery/statistics/descriptive/images/sphx_glr_plot_02_gauss_error_law_001.png :alt: plot 02 gauss error law :srcset: /api/gallery/statistics/descriptive/images/sphx_glr_plot_02_gauss_error_law_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.086 seconds) .. _sphx_glr_download_api_gallery_statistics_descriptive_plot_02_gauss_error_law.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_gauss_error_law.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_gauss_error_law.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_gauss_error_law.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_