Skip to content

Commit

Permalink
feat: add MCA
Browse files Browse the repository at this point in the history
  • Loading branch information
nicrie committed Aug 20, 2022
2 parents d5f6797 + 4fb881e commit 34a82d1
Show file tree
Hide file tree
Showing 57 changed files with 3,072 additions and 35 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
137 changes: 137 additions & 0 deletions docs/auto_examples/1eof/plot_bootstrap.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Significance teting of EOF analysis via bootstrap\n\nTesting the significance of individual modes and obtain confidence intervals\nfor both EOFs and PCs.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Load packages and data:\nimport numpy as np\nimport xarray as xr\nimport matplotlib.pyplot as plt\nfrom matplotlib.gridspec import GridSpec\nfrom cartopy.crs import Orthographic, PlateCarree\n\nfrom xeofs.xarray import EOF, Bootstrapper"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"t2m = xr.tutorial.load_dataset('air_temperature')['air']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Perform EOF analysis\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"model = EOF(t2m, n_modes=25, norm=False, dim='time')\nmodel.solve()\nexpvar = model.explained_variance_ratio()\neofs = model.eofs()\npcs = model.pcs()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Perform bootstrapping of the model to identy the number of significant modes.\nWe choose a significance level of alpha=0.05 and perform 25 bootstraps.\nNote - if computationallly feasible - you typically want to choose higher\nnumbers of bootstraps e.g. 100 or 1000.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"alpha = .05\nn_boot = 25\n\nbs = Bootstrapper(n_boot=n_boot, alpha=alpha)\nbs.bootstrap(model)\nn_significant_modes = bs.n_significant_modes()\nprint('{:} modes are significant at alpha={:.2}'.format(n_significant_modes, alpha))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The bootstrapping procedure identifies 5 significant modes. We can also\ncompute the 95 % confidence intervals of the EOFs/PCs and mask out\ninsignificant elements of the obtained EOFs.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"eofs_ci, eofs_mask = bs.eofs()\npcs_ci, pcs_mask = bs.pcs()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Summarize the results in a figure.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"lons, lats = np.meshgrid(eofs_mask.lon.values, eofs_mask.lat.values)\nproj = Orthographic(central_latitude=30, central_longitude=-80)\nkwargs = {\n 'cmap' : 'RdBu', 'vmin' : -.05, 'vmax': .05, 'transform': PlateCarree()\n}\n\nfig = plt.figure(figsize=(10, 16))\ngs = GridSpec(5, 2)\nax1 = [fig.add_subplot(gs[i, 0], projection=proj) for i in range(5)]\nax2 = [fig.add_subplot(gs[i, 1]) for i in range(5)]\n\nfor i, (a1, a2) in enumerate(zip(ax1, ax2)):\n a1.coastlines(color='.5')\n eofs.isel(mode=i).plot(ax=a1, **kwargs)\n a1.scatter(\n lons, lats, eofs_mask.isel(mode=i).values * .5,\n color='k', alpha=.5, transform=PlateCarree()\n )\n pcs_ci.isel(mode=i, quantile=0).plot(ax=a2, color='.3', lw='.5', label='2.5%')\n pcs_ci.isel(mode=i, quantile=1).plot(ax=a2, color='.3', lw='.5', label='97.5%')\n pcs.isel(mode=i).plot(ax=a2, lw='.5', alpha=.5, label='PC')\n a2.legend(loc=2)\n\nplt.tight_layout()\nplt.savefig('bootstrap.jpg')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.13"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""
Significance via bootstrap
==========================
Significance teting of EOF analysis via bootstrap
=================================================
Testing the significance of individual modes and obtain confidence intervals
for both EOFs and PCs.
Expand Down
1 change: 1 addition & 0 deletions docs/auto_examples/1eof/plot_bootstrap.py.md5
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
b574b07fccf1b8340da617a56baf5ea3
221 changes: 221 additions & 0 deletions docs/auto_examples/1eof/plot_bootstrap.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@

.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/1eof/plot_bootstrap.py"
.. LINE NUMBERS ARE GIVEN BELOW.
.. only:: html

.. note::
:class: sphx-glr-download-link-note

Click :ref:`here <sphx_glr_download_auto_examples_1eof_plot_bootstrap.py>`
to download the full example code

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_auto_examples_1eof_plot_bootstrap.py:


Significance teting of EOF analysis via bootstrap
=================================================

Testing the significance of individual modes and obtain confidence intervals
for both EOFs and PCs.

.. GENERATED FROM PYTHON SOURCE LINES 8-19
.. code-block:: default
# 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.xarray import EOF, Bootstrapper
.. GENERATED FROM PYTHON SOURCE LINES 20-23
.. code-block:: default
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:: default
model = EOF(t2m, n_modes=25, norm=False, dim='time')
model.solve()
expvar = model.explained_variance_ratio()
eofs = model.eofs()
pcs = model.pcs()
.. GENERATED FROM PYTHON SOURCE LINES 34-38
Perform bootstrapping of the model to identy the number of significant modes.
We choose a significance level of alpha=0.05 and perform 25 bootstraps.
Note - if computationallly feasible - you typically want to choose higher
numbers of bootstraps e.g. 100 or 1000.

.. GENERATED FROM PYTHON SOURCE LINES 38-47
.. code-block:: default
alpha = .05
n_boot = 25
bs = Bootstrapper(n_boot=n_boot, alpha=alpha)
bs.bootstrap(model)
n_significant_modes = bs.n_significant_modes()
print('{:} modes are significant at alpha={:.2}'.format(n_significant_modes, alpha))
.. rst-class:: sphx-glr-script-out

Out:

.. code-block:: none
Bootstrap: 0%| | 0/25 [00:00<?, ?it/s] Bootstrap: 4%|4 | 1/25 [00:00<00:06, 3.44it/s] Bootstrap: 8%|8 | 2/25 [00:00<00:08, 2.87it/s] Bootstrap: 12%|#2 | 3/25 [00:01<00:07, 2.91it/s] Bootstrap: 16%|#6 | 4/25 [00:01<00:07, 2.81it/s] Bootstrap: 20%|## | 5/25 [00:01<00:07, 2.69it/s] Bootstrap: 24%|##4 | 6/25 [00:02<00:06, 2.83it/s] Bootstrap: 28%|##8 | 7/25 [00:02<00:06, 2.78it/s] Bootstrap: 32%|###2 | 8/25 [00:03<00:07, 2.21it/s] Bootstrap: 36%|###6 | 9/25 [00:03<00:07, 2.16it/s] Bootstrap: 40%|#### | 10/25 [00:03<00:06, 2.39it/s] Bootstrap: 44%|####4 | 11/25 [00:04<00:05, 2.50it/s] Bootstrap: 48%|####8 | 12/25 [00:04<00:05, 2.45it/s] Bootstrap: 52%|#####2 | 13/25 [00:05<00:05, 2.33it/s] Bootstrap: 56%|#####6 | 14/25 [00:05<00:05, 2.17it/s] Bootstrap: 60%|###### | 15/25 [00:06<00:04, 2.39it/s] Bootstrap: 64%|######4 | 16/25 [00:06<00:03, 2.42it/s] Bootstrap: 68%|######8 | 17/25 [00:06<00:03, 2.25it/s] Bootstrap: 72%|#######2 | 18/25 [00:07<00:03, 2.27it/s] Bootstrap: 76%|#######6 | 19/25 [00:07<00:02, 2.36it/s] Bootstrap: 80%|######## | 20/25 [00:08<00:02, 2.45it/s] Bootstrap: 84%|########4 | 21/25 [00:08<00:01, 2.22it/s] Bootstrap: 88%|########8 | 22/25 [00:09<00:01, 2.25it/s] Bootstrap: 92%|#########2| 23/25 [00:09<00:00, 2.46it/s] Bootstrap: 96%|#########6| 24/25 [00:09<00:00, 2.65it/s] Bootstrap: 100%|##########| 25/25 [00:10<00:00, 2.75it/s] Bootstrap: 100%|##########| 25/25 [00:10<00:00, 2.47it/s]
3 modes are significant at alpha=0.05
.. GENERATED FROM PYTHON SOURCE LINES 48-51
The bootstrapping procedure identifies 5 significant modes. We can also
compute the 95 % confidence intervals of the EOFs/PCs and mask out
insignificant elements of the obtained EOFs.

.. GENERATED FROM PYTHON SOURCE LINES 51-55
.. code-block:: default
eofs_ci, eofs_mask = bs.eofs()
pcs_ci, pcs_mask = bs.pcs()
.. GENERATED FROM PYTHON SOURCE LINES 56-57
Summarize the results in a figure.

.. GENERATED FROM PYTHON SOURCE LINES 57-84
.. code-block:: default
lons, lats = np.meshgrid(eofs_mask.lon.values, eofs_mask.lat.values)
proj = Orthographic(central_latitude=30, central_longitude=-80)
kwargs = {
'cmap' : 'RdBu', 'vmin' : -.05, 'vmax': .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')
eofs.isel(mode=i).plot(ax=a1, **kwargs)
a1.scatter(
lons, lats, eofs_mask.isel(mode=i).values * .5,
color='k', alpha=.5, transform=PlateCarree()
)
pcs_ci.isel(mode=i, quantile=0).plot(ax=a2, color='.3', lw='.5', label='2.5%')
pcs_ci.isel(mode=i, quantile=1).plot(ax=a2, color='.3', lw='.5', label='97.5%')
pcs.isel(mode=i).plot(ax=a2, lw='.5', alpha=.5, label='PC')
a2.legend(loc=2)
plt.tight_layout()
plt.savefig('bootstrap.jpg')
.. image-sg:: /auto_examples/1eof/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/1eof/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 12.438 seconds)


.. _sphx_glr_download_auto_examples_1eof_plot_bootstrap.py:


.. only :: html
.. container:: sphx-glr-footer
:class: sphx-glr-footer-example
.. container:: sphx-glr-download sphx-glr-download-python
:download:`Download Python source code: plot_bootstrap.py <plot_bootstrap.py>`
.. container:: sphx-glr-download sphx-glr-download-jupyter
:download:`Download Jupyter notebook: plot_bootstrap.ipynb <plot_bootstrap.ipynb>`
.. only:: html

.. rst-class:: sphx-glr-signature

`Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
Expand Down
Binary file not shown.
6 changes: 4 additions & 2 deletions docs/auto_examples/1eof/sg_execution_times.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@

Computation times
=================
**00:01.629** total execution time for **auto_examples_1eof** files:
**00:12.438** total execution time for **auto_examples_1eof** files:

+--------------------------------------------------------------------------------------------+-----------+--------+
| :ref:`sphx_glr_auto_examples_1eof_plot_multivariate-eof.py` (``plot_multivariate-eof.py``) | 00:01.629 | 0.0 MB |
| :ref:`sphx_glr_auto_examples_1eof_plot_bootstrap.py` (``plot_bootstrap.py``) | 00:12.438 | 0.0 MB |
+--------------------------------------------------------------------------------------------+-----------+--------+
| :ref:`sphx_glr_auto_examples_1eof_plot_eof-smode.py` (``plot_eof-smode.py``) | 00:00.000 | 0.0 MB |
+--------------------------------------------------------------------------------------------+-----------+--------+
| :ref:`sphx_glr_auto_examples_1eof_plot_eof-tmode.py` (``plot_eof-tmode.py``) | 00:00.000 | 0.0 MB |
+--------------------------------------------------------------------------------------------+-----------+--------+
| :ref:`sphx_glr_auto_examples_1eof_plot_multivariate-eof.py` (``plot_multivariate-eof.py``) | 00:00.000 | 0.0 MB |
+--------------------------------------------------------------------------------------------+-----------+--------+
| :ref:`sphx_glr_auto_examples_1eof_plot_weighted-eof.py` (``plot_weighted-eof.py``) | 00:00.000 | 0.0 MB |
+--------------------------------------------------------------------------------------------+-----------+--------+
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 34a82d1

Please sign in to comment.