.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "api/gallery/statistics/shrinkage/plot_01_james_stein.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_shrinkage_plot_01_james_stein.py: Stein's paradox ==================== Estimates p unrelated normal means from one observation each. Shrinking all the observations toward zero by the James-Stein factor lowers the total squared error for every p >= 3, even though no single observation carries information about the others. .. GENERATED FROM PYTHON SOURCE LINES 12-17 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mathematicskit.statistics import james_stein_estimator .. GENERATED FROM PYTHON SOURCE LINES 18-20 Risk as a function of dimension ----------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 20-39 .. code-block:: Python rng = np.random.default_rng(0) dims = np.arange(3, 31) raw_risk, js_risk = [], [] for p in dims: theta = rng.normal(scale=1.5, size=p) x = theta + rng.normal(size=(2000, p)) shrunk = np.array([james_stein_estimator(row) for row in x]) raw_risk.append(np.mean(np.sum((x - theta) ** 2, axis=1))) js_risk.append(np.mean(np.sum((shrunk - theta) ** 2, axis=1))) if p in (3, 10, 30): print(f"p={p:2d}: raw risk {raw_risk[-1]:6.2f}, James-Stein risk {js_risk[-1]:6.2f}") fig, ax = plt.subplots() ax.plot(dims, raw_risk, "o-", label="x (risk = p)") ax.plot(dims, js_risk, "s-", label="James-Stein") ax.set_xlabel("dimension p") ax.set_ylabel("mean total squared error") ax.legend() .. image-sg:: /api/gallery/statistics/shrinkage/images/sphx_glr_plot_01_james_stein_001.png :alt: plot 01 james stein :srcset: /api/gallery/statistics/shrinkage/images/sphx_glr_plot_01_james_stein_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none p= 3: raw risk 2.97, James-Stein risk 1.95 p=10: raw risk 10.03, James-Stein risk 7.10 p=30: raw risk 29.92, James-Stein risk 21.74 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.102 seconds) .. _sphx_glr_download_api_gallery_statistics_shrinkage_plot_01_james_stein.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_james_stein.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_james_stein.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_james_stein.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_