.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/3validation/plot_bootstrap.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_auto_examples_3validation_plot_bootstrap.py: Significance testing of EOF analysis via bootstrap =================================================== Test the significance of individual modes and obtain confidence intervals for both EOFs and PCs. .. GENERATED FROM PYTHON SOURCE LINES 8-19 .. code-block:: Python # Load packages and data: import numpy as np import xarray as xr import matplotlib.pyplot as plt from matplotlib.gridspec import GridSpec from cartopy.crs import Orthographic, PlateCarree from xeofs.models import EOF from xeofs.validation import EOFBootstrapper .. GENERATED FROM PYTHON SOURCE LINES 20-23 .. code-block:: Python t2m = xr.tutorial.load_dataset("air_temperature")["air"] .. GENERATED FROM PYTHON SOURCE LINES 24-25 Perform EOF analysis .. GENERATED FROM PYTHON SOURCE LINES 25-33 .. code-block:: Python model = EOF(n_modes=5, standardize=False) model.fit(t2m, dim="time") expvar = model.explained_variance_ratio() components = model.components() scores = model.scores() .. GENERATED FROM PYTHON SOURCE LINES 34-38 Perform bootstrapping of the model to identy the number of significant modes. We perform 50 bootstraps. Note - if computationallly feasible - you typically want to choose higher numbers of bootstraps e.g. 1000. .. GENERATED FROM PYTHON SOURCE LINES 38-55 .. code-block:: Python n_boot = 50 bs = EOFBootstrapper(n_bootstraps=n_boot) bs.fit(model) bs_expvar = bs.explained_variance() ci_expvar = bs_expvar.quantile([0.025, 0.975], "n") # 95% confidence intervals q025 = ci_expvar.sel(quantile=0.025) q975 = ci_expvar.sel(quantile=0.975) is_significant = q025 - q975.shift({"mode": -1}) > 0 n_significant_modes = ( is_significant.where(is_significant is True).cumsum(skipna=False).max().fillna(0) ) print("{:} modes are significant at alpha=0.05".format(n_significant_modes.values)) .. rst-class:: sphx-glr-script-out .. code-block:: none 0%| | 0/50 [00:00 0 .. GENERATED FROM PYTHON SOURCE LINES 67-68 Summarize the results in a figure. .. GENERATED FROM PYTHON SOURCE LINES 68-97 .. code-block:: Python lons, lats = np.meshgrid(is_sig_comps.lon.values, is_sig_comps.lat.values) proj = Orthographic(central_latitude=30, central_longitude=-80) kwargs = {"cmap": "RdBu", "vmin": -0.05, "vmax": 0.05, "transform": PlateCarree()} fig = plt.figure(figsize=(10, 16)) gs = GridSpec(5, 2) ax1 = [fig.add_subplot(gs[i, 0], projection=proj) for i in range(5)] ax2 = [fig.add_subplot(gs[i, 1]) for i in range(5)] for i, (a1, a2) in enumerate(zip(ax1, ax2)): a1.coastlines(color=".5") components.isel(mode=i).plot(ax=a1, **kwargs) a1.scatter( lons, lats, is_sig_comps.isel(mode=i).values * 0.5, color="k", alpha=0.5, transform=PlateCarree(), ) ci_scores.isel(mode=i, quantile=0).plot(ax=a2, color=".3", lw=".5", label="2.5%") ci_scores.isel(mode=i, quantile=1).plot(ax=a2, color=".3", lw=".5", label="97.5%") scores.isel(mode=i).plot(ax=a2, lw=".5", alpha=0.5, label="PC") a2.legend(loc=2) plt.tight_layout() plt.savefig("bootstrap.jpg") .. image-sg:: /auto_examples/3validation/images/sphx_glr_plot_bootstrap_001.png :alt: mode = 1, mode = 2, mode = 3, mode = 4, mode = 5, mode = 1, mode = 2, mode = 3, mode = 4, mode = 5 :srcset: /auto_examples/3validation/images/sphx_glr_plot_bootstrap_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 16.004 seconds) .. _sphx_glr_download_auto_examples_3validation_plot_bootstrap.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_bootstrap.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_bootstrap.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_