.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/reliability/life_data/plot_01_johnson_median_ranks.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_life_data_plot_01_johnson_median_ranks.py: Johnson's median ranks (1951) ============================= Where should the :math:`i`-th of :math:`n` failures be plotted? The population fraction failed at the :math:`i`-th smallest life is itself random, with a :math:`\mathrm{Beta}(i, n - i + 1)` distribution. Leonard Johnson (1951), working on bearing fatigue at General Motors, proposed its median, the *median rank*, as the plotting position, and Benard's :math:`(i - 0.3)/(n + 0.4)` approximates it closely (:func:`~engineeringkit.reliability.median_ranks`). For censored data Johnson's adjusted order numbers (:func:`~engineeringkit.reliability.adjusted_ranks`) let units removed early still move the later ranks. A least-squares line on Weibull axes then gives :math:`\beta` and :math:`\eta` (:func:`~engineeringkit.reliability.weibull_rank_regression`). .. GENERATED FROM PYTHON SOURCE LINES 19-25 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from scipy import stats from engineeringkit.reliability import adjusted_ranks, median_ranks, weibull_rank_regression .. GENERATED FROM PYTHON SOURCE LINES 26-32 The median rank is the median of a beta distribution ---------------------------------------------------- Simulate many samples of ten lives from any continuous distribution: the fraction failed at the third failure, :math:`F(t_{(3)})`, is uniform order statistic number three of ten. .. GENERATED FROM PYTHON SOURCE LINES 32-49 .. code-block:: Python n = 10 rng = np.random.default_rng(1951) u = np.sort(rng.random((100_000, n)), axis=1) simulated = np.median(u, axis=0) exact, benard = median_ranks(n), median_ranks(n, method="benard") for i in (1, 3, 5, 10): print(f"i = {i:2d}: simulated {simulated[i - 1]:.4f}, beta median {exact[i - 1]:.4f}, Benard {benard[i - 1]:.4f}, i/n {i / n:.2f}") fig, axes = plt.subplots(1, 2, figsize=(9, 3.5)) x = np.linspace(0, 1, 300) for i in (1, 3, 5, 10): axes[0].plot(x, stats.beta.pdf(x, i, n - i + 1), label=f"i = {i}") axes[0].axvline(exact[i - 1], ls=":", color="gray") axes[0].set(xlabel="fraction failed F", ylabel="density", title="Distribution of F at the i-th failure", ylim=(0, 10)) axes[0].legend() .. image-sg:: /api/gallery/reliability/life_data/images/sphx_glr_plot_01_johnson_median_ranks_001.png :alt: Distribution of F at the i-th failure :srcset: /api/gallery/reliability/life_data/images/sphx_glr_plot_01_johnson_median_ranks_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none i = 1: simulated 0.0667, beta median 0.0670, Benard 0.0673, i/n 0.10 i = 3: simulated 0.2582, beta median 0.2586, Benard 0.2596, i/n 0.30 i = 5: simulated 0.4518, beta median 0.4517, Benard 0.4519, i/n 0.50 i = 10: simulated 0.9330, beta median 0.9330, Benard 0.9327, i/n 1.00 .. GENERATED FROM PYTHON SOURCE LINES 50-56 Censored bearings on Weibull paper ---------------------------------- Twelve bearings (:math:`\eta` = 1000 h, :math:`\beta` = 2) are tested; four are removed for other reasons before they fail. Their removal raises the order numbers of the failures that follow. .. GENERATED FROM PYTHON SOURCE LINES 56-72 .. code-block:: Python times = np.array([180, 310, 390, 450, 520, 610, 700, 760, 830, 980, 1150, 1400.0]) failed = np.array([1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1]) failure_times, order = adjusted_ranks(times, failed) print("adjusted order numbers:", np.round(order, 2)) fit = weibull_rank_regression(times, failed) print(f"rank regression: beta = {fit.beta:.2f}, eta = {fit.eta:.0f} h, B10 life = {fit.b_life(0.1):.0f} h") fit.plot(ax=axes[1]) fig.tight_layout() assert np.allclose(simulated, exact, atol=0.003) assert np.max(np.abs(benard - exact)) < 0.003 assert np.isclose(order[0], 1) and np.all(np.diff(order) >= 1) and order[-1] < times.size + 1 assert np.allclose(median_ranks(times.size, order), fit.median_ranks) plt.show() .. rst-class:: sphx-glr-script-out .. code-block:: none adjusted order numbers: [ 1. 2. 3.1 4.2 5.46 6.71 8.29 10.64] rank regression: beta = 1.69, eta = 968 h, B10 life = 256 h .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.180 seconds) .. _sphx_glr_download_api_gallery_reliability_life_data_plot_01_johnson_median_ranks.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/life_data/plot_01_johnson_median_ranks.ipynb :alt: Launch JupyterLite :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_johnson_median_ranks.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_johnson_median_ranks.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_johnson_median_ranks.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_