.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_tutorials/plot_11_pcmci.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_tutorials_plot_11_pcmci.py: ================================================= Lagged links with PCMCI: conditions and lags ================================================= PCMCI (Runge et al. 2019) answers a question close to conditional Granger causality: does the past of one signal carry information about another, given the relevant past of the rest? It gets there differently. It first selects, for every signal, a small set of lagged *parents* with a sequence of conditional-independence tests, and then tests every lagged link ``i (t - tau) -> j (t)`` conditioned on the parents of both ends (the *MCI* test). With the partial-correlation test (ParCorr) used here, the dependence is linear, and every link comes with the lag at which it is strongest. PCMCI needs the optional package tigramite: ``pip install dirconn[pcmci]``. .. GENERATED FROM PYTHON SOURCE LINES 18-22 Setup ----- Four signals generated with numpy: A drives B at a lag of 2 samples and C at a lag of 4, and C drives D at a lag of 1 with a negative sign. B and C share a driver but do not interact. .. GENERATED FROM PYTHON SOURCE LINES 22-48 .. code-block:: Python import os import matplotlib.pyplot as plt import numpy as np import dirconn from dirconn import GC, PCMCI, TimeSeries QUICK = os.environ.get("DIRCONN_QUICK") == "1" # the tests set this; readers get the full version SEED = 0 print(f"dirconn {dirconn.__version__}, numpy {np.__version__}") SFREQ = 100.0 N_TIMES = 2000 if QUICK else 6000 NAMES = ["A", "B", "C", "D"] rng = np.random.default_rng(SEED) x = np.zeros((4, N_TIMES + 100)) e = rng.standard_normal(x.shape) for t in range(4, x.shape[1]): x[0, t] = 0.6 * x[0, t - 1] + e[0, t] x[1, t] = 0.3 * x[1, t - 1] + 0.5 * x[0, t - 2] + e[1, t] # A -> B, lag 2 x[2, t] = 0.3 * x[2, t - 1] + 0.5 * x[0, t - 4] + e[2, t] # A -> C, lag 4 x[3, t] = 0.3 * x[3, t - 1] - 0.5 * x[2, t - 1] + e[3, t] # C -> D, lag 1, negative data = TimeSeries.from_array(x[:, 100:], SFREQ, names=NAMES) .. rst-class:: sphx-glr-script-out .. code-block:: none dirconn 0.2.0, numpy 2.5.5 .. GENERATED FROM PYTHON SOURCE LINES 49-56 PCMCI, its values and its lags ------------------------------ ``tau_max`` is the largest lag tested. By default it is the order that the Bayesian information criterion picks for a VAR model of all signals (as for :class:`~dirconn.GC`); here we set it, so that the true lag of 4 is inside the range. Each cell is the MCI partial correlation of a link at the lag where it is strongest. The values are signed, so the colour scale runs from -1 (blue) to 1 (red) and the negative link from C to D shows in blue: .. GENERATED FROM PYTHON SOURCE LINES 56-65 .. code-block:: Python result = PCMCI(tau_max=6).fit(data) result.plot() lags = np.array(result.provenance.details["lag"], dtype=float) for source, target in ((0, 1), (0, 2), (2, 3), (1, 2)): print( f"{NAMES[source]} -> {NAMES[target]}: MCI {result.values[source, target, 0, 0]:+.3f}" f" at lag {lags[source, target]:.0f}" ) .. image-sg:: /auto_tutorials/images/sphx_glr_plot_11_pcmci_001.png :alt: plot 11 pcmci :srcset: /auto_tutorials/images/sphx_glr_plot_11_pcmci_001.png, /auto_tutorials/images/sphx_glr_plot_11_pcmci_001_2_00x.png 2.00x :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none A -> B: MCI +0.449 at lag 2 A -> C: MCI +0.396 at lag 4 C -> D: MCI -0.434 at lag 1 B -> C: MCI +0.012 at lag 6 .. GENERATED FROM PYTHON SOURCE LINES 66-68 The lag of every link is part of the result. The full lag profile is in the provenance; for the link from A to C it peaks at the true lag: .. GENERATED FROM PYTHON SOURCE LINES 68-76 .. code-block:: Python profile = np.array(result.provenance.details["val_matrix"], dtype=float)[0, 2] fig, ax = plt.subplots(layout="constrained") ax.bar(np.arange(1, profile.size + 1), profile) ax.axhline(0.0, color="0.5", linewidth=0.8) ax.set_xlabel("lag (samples)") ax.set_ylabel("MCI partial correlation") ax.set_title("A -> C at every lag") .. image-sg:: /auto_tutorials/images/sphx_glr_plot_11_pcmci_002.png :alt: A -> C at every lag :srcset: /auto_tutorials/images/sphx_glr_plot_11_pcmci_002.png, /auto_tutorials/images/sphx_glr_plot_11_pcmci_002_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 77-82 The shared driver ----------------- B and C both follow A, so B's past predicts C a little when A is ignored. Pairwise Granger causality reports that; PCMCI conditions on the parents it selected (A among them) and, like conditional Granger causality, does not: .. GENERATED FROM PYTHON SOURCE LINES 82-88 .. code-block:: Python pairwise = GC(order=6, conditional=False, check_order_stability=False).fit(data) conditional = GC(order=6, check_order_stability=False).fit(data) print(f"B -> C: pairwise GC {pairwise.values[1, 2, 0, 0]:.4f}") print(f"B -> C: conditional GC {conditional.values[1, 2, 0, 0]:.4f}") print(f"B -> C: PCMCI {result.values[1, 2, 0, 0]:+.4f}") .. rst-class:: sphx-glr-script-out .. code-block:: none B -> C: pairwise GC 0.0849 B -> C: conditional GC 0.0012 B -> C: PCMCI +0.0115 .. GENERATED FROM PYTHON SOURCE LINES 89-96 Significance ------------ PCMCI's own test gives a p-value for every link and lag. :func:`dirconn.pcmci_test` reports, per pair, the smallest p-value over the lags multiplied by the number of lags (a Bonferroni correction over the lags: picking the best of several lags would otherwise make the p-value too small). The result works with the usual correction across pairs. Passed to the plot as ``significance``, it marks with ``*`` the links that pass the FDR correction across pairs: .. GENERATED FROM PYTHON SOURCE LINES 96-105 .. code-block:: Python test = dirconn.pcmci_test(PCMCI(tau_max=6), data) print(test.null) test.observed.plot(significance=test) significant = test.significant(method="fdr_bh")[:, :, 0, 0] for source in range(4): for target in range(4): if significant[source, target]: print(f"significant after FDR: {NAMES[source]} -> {NAMES[target]}") .. image-sg:: /auto_tutorials/images/sphx_glr_plot_11_pcmci_003.png :alt: plot 11 pcmci :srcset: /auto_tutorials/images/sphx_glr_plot_11_pcmci_003.png, /auto_tutorials/images/sphx_glr_plot_11_pcmci_003_2_00x.png 2.00x :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none momentary conditional independence (ParCorr): at every lag, the source's past is linearly independent of the target given the parents that PCMCI selected for both significant after FDR: A -> B significant after FDR: A -> C significant after FDR: C -> D .. GENERATED FROM PYTHON SOURCE LINES 106-107 The three true links pass the correction, and no other pair does. .. GENERATED FROM PYTHON SOURCE LINES 109-114 The limit: what happens at zero lag ----------------------------------- Only lagged links are tested. A zero-lag mixture, such as volume conduction between two sensors, is not a link PCMCI can represent, and it changes the lagged links. Mixing B into C at zero lag makes PCMCI report a lagged link from A to the mixed signal at B's lag as well: .. GENERATED FROM PYTHON SOURCE LINES 114-121 .. code-block:: Python mixed = np.array(data.data)[0].copy() mixed[2] = mixed[2] + 0.8 * mixed[1] # C now contains B, at zero lag mixed_result = PCMCI(tau_max=6).fit(TimeSeries.from_array(mixed, SFREQ, names=NAMES)) profile_mixed = np.array(mixed_result.provenance.details["val_matrix"], dtype=float)[0, 2] print("A -> C lag profile without mixing:", np.round(profile, 2)) print("A -> C lag profile with mixing: ", np.round(profile_mixed, 2)) .. rst-class:: sphx-glr-script-out .. code-block:: none A -> C lag profile without mixing: [-0.01 0. 0.02 0.4 -0.03 -0. ] A -> C lag profile with mixing: [-0. 0.3 0.01 0.35 -0.02 -0.02] .. GENERATED FROM PYTHON SOURCE LINES 122-135 Limitations ----------- * On a simulated linear system PCMCI found the three links with their lags and signs and did not report the shared-driver pair. This is one system and one seed (a demonstration, not an evaluation); the validation report (``docs/method_validation.md``) runs it on the reference scenarios. * The values are partial correlations given the selected parents: they depend on what was selected (``pc_alpha``) and on ``tau_max``, and the p-values do not account for either choice. * Zero-lag dependence is not represented and changes the lagged links; PCMCI+ (contemporaneous links) is not implemented here. ParCorr sees linear dependence only. * It is predictive dependence given the recorded signals, not causation: an unrecorded common driver can produce a link. Each result lists its assumptions; ``not_checked`` means the library did not test one, not that it holds: .. GENERATED FROM PYTHON SOURCE LINES 135-138 .. code-block:: Python for check in result.provenance.assumptions.checks: print(f"{check.name:24s} {check.status:12s} {check.detail}") .. rst-class:: sphx-glr-script-out .. code-block:: none stationarity not_checked linearity not_checked sampling_adequacy not_checked instantaneous_coupling not_checked causal_sufficiency not_checked filtering not_checked .. GENERATED FROM PYTHON SOURCE LINES 139-141 * Not shown here: resampling tests (any estimator works with :func:`dirconn.significance_test`, tutorial 4, but PCMCI is slow inside them), non-linear tests and more signals. .. GENERATED FROM PYTHON SOURCE LINES 141-142 .. code-block:: Python plt.close("all") .. _sphx_glr_download_auto_tutorials_plot_11_pcmci.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_11_pcmci.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_11_pcmci.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_11_pcmci.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_