.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "content/user_guide/auto_examples/4validation/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_content_user_guide_auto_examples_4validation_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-18 .. code-block:: default # Load packages and data: import matplotlib.pyplot as plt import numpy as np import xarray as xr from cartopy.crs import Orthographic, PlateCarree from matplotlib.gridspec import GridSpec import xeofs as xe .. GENERATED FROM PYTHON SOURCE LINES 19-22 .. code-block:: default t2m = xr.tutorial.load_dataset("air_temperature")["air"] .. GENERATED FROM PYTHON SOURCE LINES 23-24 Perform EOF analysis .. GENERATED FROM PYTHON SOURCE LINES 24-32 .. code-block:: default model = xe.single.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 33-37 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 37-54 .. code-block:: default n_boot = 50 bs = xe.validation.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 66-67 Summarize the results in a figure. .. GENERATED FROM PYTHON SOURCE LINES 67-96 .. code-block:: default 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:: /content/user_guide/auto_examples/4validation/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: /content/user_guide/auto_examples/4validation/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 29.972 seconds) .. _sphx_glr_download_content_user_guide_auto_examples_4validation_plot_bootstrap.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_bootstrap.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_bootstrap.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_