.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/reliability/lifetimes/plot_05_kaplan_meier.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_05_kaplan_meier.py: Kaplan and Meier's product-limit estimator (1958) ================================================= Field data are incomplete: some units are still working when the data are collected, others were removed for reasons other than failure. Dropping them biases the life estimate low; counting them as failures biases it too. Kaplan and Meier (1958) multiplied the conditional survival :math:`1 - d_j/n_j` at each failure, where :math:`n_j` counts only units still under observation (:func:`~engineeringkit.reliability.kaplan_meier`). The example censors a Weibull sample at random and recovers the true survival curve. .. GENERATED FROM PYTHON SOURCE LINES 16-21 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from engineeringkit.reliability import kaplan_meier, weibull_reliability .. GENERATED FROM PYTHON SOURCE LINES 22-28 Censored field data ------------------- 200 pumps with Weibull lives (:math:`\eta` = 5000 h, :math:`\beta` = 1.8). Each enters service at a random date, so its observation ends at a uniformly distributed time up to 8000 h (time unit: hours). .. GENERATED FROM PYTHON SOURCE LINES 28-38 .. code-block:: Python rng = np.random.default_rng(1958) eta, beta, n = 5000.0, 1.8, 200 life = eta * rng.weibull(beta, n) window = rng.uniform(0, 8000, n) times = np.minimum(life, window) failed = (life <= window).astype(int) km = kaplan_meier(times, failed) print(km.summary()) .. rst-class:: sphx-glr-script-out .. code-block:: none {'n': 200, 'failures': 96, 'censored': 104, 'final_survival': 0.21319158745210554} .. GENERATED FROM PYTHON SOURCE LINES 39-43 Compare with the truth and with naive estimates ----------------------------------------------- "Ignore the censored units" treats only the failures as the sample. .. GENERATED FROM PYTHON SOURCE LINES 43-62 .. code-block:: Python t = np.linspace(0, 6000, 300) naive = np.array([np.mean(times[failed == 1] > x) for x in t]) error = np.abs(km.survival_at(t) - weibull_reliability(t, eta, beta)) band = 2 * km.std_error fig, ax = km.plot(label="Kaplan-Meier") ax.fill_between(km.times, km.survival - band, km.survival + band, step="post", alpha=0.2, label=r"$\pm 2$ s.e. (Greenwood)") ax.plot(t, weibull_reliability(t, eta, beta), "k--", label="true Weibull") ax.plot(t, naive, color="C3", label="failures only (biased)") ax.set(xlabel="time [h]", xlim=(0, 6000)) ax.legend() fig.tight_layout() naive_error = np.abs(naive - weibull_reliability(t, eta, beta)) print(f"largest error of Kaplan-Meier: {error.max():.3f}; of the naive estimate: {naive_error.max():.3f}") assert error.max() < 0.08 assert naive_error.max() > 2 * error.max() plt.show() .. image-sg:: /api/gallery/reliability/lifetimes/images/sphx_glr_plot_05_kaplan_meier_001.png :alt: plot 05 kaplan meier :srcset: /api/gallery/reliability/lifetimes/images/sphx_glr_plot_05_kaplan_meier_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none largest error of Kaplan-Meier: 0.057; of the naive estimate: 0.334 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.032 seconds) .. _sphx_glr_download_api_gallery_reliability_lifetimes_plot_05_kaplan_meier.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_05_kaplan_meier.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_05_kaplan_meier.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_05_kaplan_meier.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_05_kaplan_meier.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_