.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/reliability/lifetimes/plot_02_poisson_failures.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 or to run this example in your browser via JupyterLite. .. rst-class:: sphx-glr-example-title .. _sphx_glr_api_gallery_reliability_lifetimes_plot_02_poisson_failures.py: Poisson's law of rare failures (1837) ===================================== Poisson (1837) derived the distribution of the number of rare events among many trials, :math:`P(k) = (\lambda T)^k e^{-\lambda T}/k!` (:func:`~engineeringkit.reliability.poisson_failures_probability`). A fleet of units failing independently at a constant rate :math:`\lambda` has :math:`\lambda T` expected failures in total operating time :math:`T` (:func:`~engineeringkit.reliability.expected_failures`), and the count is Poisson. The example simulates failures as exponential gaps and checks the counts, then sizes a spares kit. .. GENERATED FROM PYTHON SOURCE LINES 16-21 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from engineeringkit.reliability import expected_failures, poisson_failures_probability .. GENERATED FROM PYTHON SOURCE LINES 22-27 Simulate failure counts ----------------------- 50 units, each failing at :math:`2\times10^{-5}` per hour, run for a year of 8760 h; every failed unit is replaced at once. Time unit: hours. .. GENERATED FROM PYTHON SOURCE LINES 27-41 .. code-block:: Python rng = np.random.default_rng(1837) lam, units, hours = 2e-5, 50, 8760.0 mean = expected_failures(lam, units * hours) trials = 20_000 gaps = rng.exponential(1 / lam, size=(trials, units, 8)) counts = (np.cumsum(gaps, axis=2) <= hours).sum(axis=(1, 2)) k = np.arange(0, 25) pmf = np.array([poisson_failures_probability(int(i), lam, units * hours) for i in k]) observed = np.bincount(counts, minlength=k.size)[: k.size] / trials print(f"expected failures per year: {mean:.2f}; simulated mean {counts.mean():.2f}") assert np.isclose(pmf.sum(), 1, atol=1e-6) assert np.abs(observed - pmf).max() < 0.01 .. rst-class:: sphx-glr-script-out .. code-block:: none expected failures per year: 8.76; simulated mean 8.77 .. GENERATED FROM PYTHON SOURCE LINES 42-47 How many spares? ---------------- Stock enough spares that the year's failures exceed them with probability under 1%. .. GENERATED FROM PYTHON SOURCE LINES 47-62 .. code-block:: Python cumulative = np.cumsum(pmf) spares = int(np.argmax(cumulative >= 0.99)) print(f"spares for 99% cover: {spares} (P = {cumulative[spares]:.4f})") fig, ax = plt.subplots(figsize=(6.5, 3.5)) ax.bar(k, observed, alpha=0.5, label="simulated fleet") ax.plot(k, pmf, "o-", color="C1", label="Poisson") ax.axvline(spares + 0.5, color="gray", ls="--", label=f"{spares} spares") ax.set(xlabel="failures in one year", ylabel="probability") ax.legend() fig.tight_layout() assert cumulative[spares] >= 0.99 > cumulative[spares - 1] plt.show() .. image-sg:: /api/gallery/reliability/lifetimes/images/sphx_glr_plot_02_poisson_failures_001.png :alt: plot 02 poisson failures :srcset: /api/gallery/reliability/lifetimes/images/sphx_glr_plot_02_poisson_failures_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none spares for 99% cover: 16 (P = 0.9913) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.070 seconds) .. _sphx_glr_download_api_gallery_reliability_lifetimes_plot_02_poisson_failures.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: lite-badge .. image:: images/jupyterlite_badge_logo.svg :target: ../../../../lite/lab/index.html?path=api/gallery/reliability/lifetimes/plot_02_poisson_failures.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_poisson_failures.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_poisson_failures.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_poisson_failures.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_