Directed connectivity on published autoregressive models#

Goal: compare directed measures on vector autoregressive models from the literature, whose coefficients give the true connections. The measures are pairwise spectral Granger prediction, the directed transfer function, partial directed coherence and its generalized form, and the direct directed transfer function.

You need: the getting-started tutorial. Some familiarity with Granger causality helps; each section gives its model’s equations.

Each grid of plots shows every measure for every ordered pair of signals, titled source → target, followed by the power spectrum. The models come from:

  • Baccalá, L.A., and Sameshima, K. (2001). Partial directed coherence: a new concept in neural structure determination. Biological Cybernetics 84, 463–474. https://doi.org/10.1007/PL00007990

  • Ding, M., Chen, Y., and Bressler, S.L. (2006). Granger causality: basic theory and application to neuroscience. In Handbook of Time Series Analysis, pp. 437–460. https://doi.org/10.1002/9783527609970.ch17

  • Dhamala, M., Rangarajan, G., and Ding, M. (2008). Analyzing information flow in brain networks with nonparametric Granger causality. NeuroImage 41, 354–362. https://doi.org/10.1016/j.neuroimage.2008.02.020

  • Nedungadi, A.G., Ding, M., and Rangarajan, G. (2011). Block coherence: a method for measuring the interdependence between two blocks of neurobiological time series. Biological Cybernetics 104, 197–207. https://doi.org/10.1007/s00422-011-0429-7

  • Wen, X., Rangarajan, G., and Ding, M. (2013). Multivariate Granger causality: an estimation framework based on factorization of the spectral density matrix. Philosophical Transactions of the Royal Society A 371, 20110610. https://doi.org/10.1098/rsta.2011.0610

import warnings

import matplotlib.pyplot as plt
import numpy as np

from spectral_connectivity import (
    Connectivity,
    DirectedOrientationWarning,
    Multitaper,
    SidednessWarning,
)
from spectral_connectivity.simulate import simulate_MVAR
from spectral_connectivity.transforms import prepare_time_series

# This tutorial indexes directed measures source first ([..., i, j] is i -> j)
# and plots the one-sided power, so the reminders meant for code written for
# 2.x do not apply here.
warnings.filterwarnings("ignore", category=DirectedOrientationWarning)
warnings.filterwarnings("ignore", category=SidednessWarning)


def plot_directional(time_series, sampling_frequency, time_halfbandwidth_product=2):
    m = Multitaper(
        prepare_time_series(time_series),
        sampling_frequency=sampling_frequency,
        time_halfbandwidth_product=time_halfbandwidth_product,
        start_time=0,
    )
    c = Connectivity(
        fourier_coefficients=m.fft(), frequencies=m.frequencies, time=m.time
    )

    measures = {
        "pairwise_spectral_granger": c.pairwise_spectral_granger_prediction(),
        "directed_transfer_function": c.directed_transfer_function(),
        "partial_directed_coherence": c.partial_directed_coherence(),
        "generalized_partial_directed_coherence": c.generalized_partial_directed_coherence(),
        "direct_directed_transfer_function": c.direct_directed_transfer_function(),
    }

    n_signals = time_series.shape[-1]
    signal_ind2, signal_ind1 = np.meshgrid(np.arange(n_signals), np.arange(n_signals))

    _fig, axes = plt.subplots(
        n_signals, n_signals, figsize=(n_signals * 3, n_signals * 3), sharex=True
    )
    for ind1, ind2, ax in zip(signal_ind1.ravel(), signal_ind2.ravel(), axes.ravel()):
        for measure_name, measure in measures.items():
            ax.plot(
                c.frequencies,
                measure[0, :, ind1, ind2],
                label=measure_name,
                linewidth=5,
                alpha=0.8,
            )
        ax.set_title(f"x{ind1 + 1} → x{ind2 + 1}", fontsize=15)
        ax.set_ylim((0, np.max([np.nanmax(np.stack(list(measures.values()))), 1.05])))

    axes[0, -1].legend()
    plt.tight_layout()

    __fig, axes = plt.subplots(1, 1, figsize=(3, 3), sharex=True, sharey=True)
    axes.plot(c.frequencies, c.power().squeeze())
    plt.title("Power")

Baccala Example 2#

Baccalá, L.A., and Sameshima, K. (2001). Partial directed coherence: a new concept in neural structure determination. Biological Cybernetics 84, 463–474.

\[x_1 = 0.5x_1(n-1) + 0.3x_2(n-1) + 0.4x_3(n-1) + w_1(n)\]
\[x_2 = -0.5x_1(n-1) + 0.3x_2(n-1) + 1.0x_3(n-1) + w_2(n)\]
\[x_3 = -0.3x_2(n-1) - 0.2x_3(n-1) + w_3(n)\]

image.png

def baccala_example2():
    """Baccalá, L.A., and Sameshima, K. (2001). Partial directed coherence:
    a new concept in neural structure determination. Biological
    Cybernetics 84, 463–474.
    """
    sampling_frequency = 200
    n_time_samples, _n_lags, n_signals = 1000, 1, 3

    coefficients = np.array([[[0.5, 0.3, 0.4], [-0.5, 0.3, 1.0], [0.0, -0.3, -0.2]]])

    noise_covariance = np.eye(n_signals)

    return (
        simulate_MVAR(
            coefficients,
            noise_covariance=noise_covariance,
            n_time_samples=n_time_samples,
            n_trials=500,
            n_burnin_samples=500,
            random_state=0,
        ),
        sampling_frequency,
    )


plot_directional(*baccala_example2(), time_halfbandwidth_product=1)
../_images/c71bab180899ad51e0ff723e036779891ac25975386c1b3c8c7e7631fb57e1fd.png ../_images/fdcb85e717e5b23d1c85c900c2371432d7811f5b6f54caac0f3f8af439ce34ba.png

Baccala Example 3#

Baccalá, L.A., and Sameshima, K. (2001). Partial directed coherence: a new concept in neural structure determination. Biological Cybernetics 84, 463–474.

image.png

def baccala_example3():
    """Baccalá, L.A., and Sameshima, K. (2001). Partial directed coherence:
    a new concept in neural structure determination. Biological
    Cybernetics 84, 463–474.
    """
    sampling_frequency = 500
    n_time_samples, n_lags, n_signals = 510, 3, 5
    coefficients = np.zeros((n_lags, n_signals, n_signals))

    coefficients[0, 0, 0] = 0.95 * np.sqrt(2)
    coefficients[1, 0, 0] = -0.9025

    coefficients[1, 1, 0] = 0.50
    coefficients[2, 2, 0] = -0.40

    coefficients[1, 3, 0] = -0.5
    coefficients[0, 3, 3] = 0.25 * np.sqrt(2)
    coefficients[0, 3, 4] = 0.25 * np.sqrt(2)

    coefficients[0, 4, 3] = -0.25 * np.sqrt(2)
    coefficients[0, 4, 4] = 0.25 * np.sqrt(2)

    noise_covariance = None

    return (
        simulate_MVAR(
            coefficients,
            noise_covariance=noise_covariance,
            n_time_samples=n_time_samples,
            n_trials=500,
            n_burnin_samples=500,
            random_state=0,
        ),
        sampling_frequency,
    )


plot_directional(*baccala_example3(), time_halfbandwidth_product=3)
../_images/3ec9242a687050df1212fa2d62871f2f728f5131b79c7622afced7ee1879112c.png ../_images/eebc4dac662e24207a0f0e41630d08d2261242895b2587cb00313199a24edfe8.png

Baccala Example 4#

Baccalá, L.A., and Sameshima, K. (2001). Partial directed coherence: a new concept in neural structure determination. Biological Cybernetics 84, 463–474.

image.png

def baccala_example4():
    """Baccalá, L.A., and Sameshima, K. (2001). Partial directed coherence:
    a new concept in neural structure determination. Biological
    Cybernetics 84, 463–474.
    """
    sampling_frequency = 200
    n_time_samples, n_lags, n_signals = 100, 2, 5
    coefficients = np.zeros((n_lags, n_signals, n_signals))

    coefficients[0, 0, 0] = 0.95 * np.sqrt(2)
    coefficients[1, 0, 0] = -0.9025
    coefficients[0, 1, 0] = -0.50
    coefficients[1, 2, 1] = 0.40
    coefficients[0, 3, 2] = -0.50
    coefficients[0, 3, 3] = 0.25 * np.sqrt(2)
    coefficients[0, 3, 4] = 0.25 * np.sqrt(2)
    coefficients[0, 4, 3] = -0.25 * np.sqrt(2)
    coefficients[0, 4, 4] = 0.25 * np.sqrt(2)

    noise_covariance = None

    return (
        simulate_MVAR(
            coefficients,
            noise_covariance=noise_covariance,
            n_time_samples=n_time_samples,
            n_trials=500,
            n_burnin_samples=500,
            random_state=0,
        ),
        sampling_frequency,
    )


plot_directional(*baccala_example4(), time_halfbandwidth_product=1)
../_images/5482c06410f183563d7186a7787038b063c588920666e58b282a7444c44364d6.png ../_images/10e933fb787a4ccbc2f4b37c01fb5cba68fc4879bc2eac545db27029f79180de.png

Baccala Example 5#

Baccalá, L.A., and Sameshima, K. (2001). Partial directed coherence: a new concept in neural structure determination. Biological Cybernetics 84, 463–474.

image.png

def baccala_example5():
    """Baccalá, L.A., and Sameshima, K. (2001). Partial directed coherence:
    a new concept in neural structure determination. Biological
    Cybernetics 84, 463–474.
    """
    sampling_frequency = 200
    n_time_samples, n_lags, n_signals = 510, 2, 5
    coefficients = np.zeros((n_lags, n_signals, n_signals))

    coefficients[0, 0, 0] = 0.95 * np.sqrt(2)
    coefficients[1, 0, 0] = -0.9025
    coefficients[1, 0, 4] = 0.5
    coefficients[0, 1, 0] = -0.50
    coefficients[1, 2, 1] = 0.40
    coefficients[0, 3, 2] = -0.50
    coefficients[0, 3, 3] = 0.25 * np.sqrt(2)
    coefficients[0, 3, 4] = 0.25 * np.sqrt(2)
    coefficients[0, 4, 3] = -0.25 * np.sqrt(2)
    coefficients[0, 4, 4] = 0.25 * np.sqrt(2)

    noise_covariance = None

    return (
        simulate_MVAR(
            coefficients,
            noise_covariance=noise_covariance,
            n_time_samples=n_time_samples,
            n_trials=500,
            n_burnin_samples=500,
            random_state=0,
        ),
        sampling_frequency,
    )


plot_directional(*baccala_example5(), time_halfbandwidth_product=1)
../_images/0cbd9f3dc99b9336e26857309887a4f9de0a181e73534d9d236635b7d9c63f31.png ../_images/716a0f8e7e54384c5adb99f1cef2e2f5eb753cdde6da48b73ba3953d1b67bae6.png

Baccala Example 6#

Baccalá, L.A., and Sameshima, K. (2001). Partial directed coherence: a new concept in neural structure determination. Biological Cybernetics 84, 463–474.

image.png

def baccala_example6():
    """Baccalá, L.A., and Sameshima, K. (2001). Partial directed coherence:
    a new concept in neural structure determination. Biological
    Cybernetics 84, 463–474.
    """
    sampling_frequency = 200
    n_time_samples, n_lags, n_signals = 100, 4, 5
    coefficients = np.zeros((n_lags, n_signals, n_signals))

    coefficients[0, 0, 0] = 0.95 * np.sqrt(2)
    coefficients[1, 0, 0] = -0.9025
    coefficients[0, 1, 0] = -0.50
    coefficients[3, 2, 1] = 0.10
    coefficients[1, 2, 1] = -0.40
    coefficients[0, 3, 2] = -0.50
    coefficients[0, 3, 3] = 0.25 * np.sqrt(2)
    coefficients[0, 3, 4] = 0.25 * np.sqrt(2)
    coefficients[0, 4, 3] = -0.25 * np.sqrt(2)
    coefficients[0, 4, 4] = 0.25 * np.sqrt(2)

    noise_covariance = None

    return (
        simulate_MVAR(
            coefficients,
            noise_covariance=noise_covariance,
            n_time_samples=n_time_samples,
            n_trials=500,
            n_burnin_samples=500,
            random_state=0,
        ),
        sampling_frequency,
    )


plot_directional(*baccala_example6(), time_halfbandwidth_product=1)
../_images/d47263745a2e07a491f0b86c600acbcb3f4152475a43574f0dae14057c3c3a3e.png ../_images/225344065567a6e3ef66d7fe597e8b957796ab0b3c72aafa338d223dad2e9429.png

Ding Example 1#

Ding, M., Chen, Y., and Bressler, S.L. (2006). Granger causality: basic theory and application to neuroscience. In Handbook of Time Series Analysis, pp. 437–460.

\[ x_1 = 0.9x_1(n-1) -0.5x_1(n-2) + w_1 \]
\[ x_2 = 0.8x_2(n-1) -0.5x_2(n-2) + 0.16x_1(n-1) -0.2x_1(n-2) + w_2 \]

image.png

def ding_example1():
    """Ding, M., Chen, Y., and Bressler, S.L. (2006). Granger causality:
    basic theory and application to neuroscience. In Handbook of Time Series
    Analysis, pp. 437–460.
    """
    sampling_frequency = 200
    n_time_samples, n_lags, n_signals = 1000, 2, 2
    coefficients = np.zeros((n_lags, n_signals, n_signals))

    coefficients[0, ...] = np.array([[0.90, 0.00], [0.16, 0.80]])
    coefficients[1, ...] = np.array([[-0.50, 0.00], [-0.20, -0.50]])

    noise_covariance = np.array([[1.0, 0.4], [0.4, 0.7]])

    return (
        simulate_MVAR(
            coefficients,
            noise_covariance=noise_covariance,
            n_time_samples=n_time_samples,
            n_trials=500,
            n_burnin_samples=500,
            random_state=0,
        ),
        sampling_frequency,
    )


plot_directional(*ding_example1(), time_halfbandwidth_product=3)
../_images/f4c57d7c2ebff418679b31f4bdbf3cda06567d0c19064d447d9e2a999938ddec.png ../_images/8f96d22b605242c70237b3e9dcd86986c83203ccfc146c822804869219ebc983.png

Ding Example 2a#

Ding, M., Chen, Y., and Bressler, S.L. (2006). Granger causality: basic theory and application to neuroscience. In Handbook of Time Series Analysis, pp. 437–460.

\[ x_1 = 0.8x_1(n-1) - 0.5x_1(n-2) + 0.4x_3(n-1) + w_1 \]
\[ x_2 = 0.9x_2(n-1) - 0.8x_2(n-2) + w_2\]
\[ x_3 = 0.5x_3(n-1) - 0.2x_3(n-2) + 0.5x_2(n-1) + w_3\]

image.png

dashed curves showing the results for the first model and the solid curves for the second model

def ding_example2a():
    """Ding, M., Chen, Y., and Bressler, S.L. (2006). Granger causality:
    basic theory and application to neuroscience. In Handbook of Time Series
    Analysis, pp. 437–460."""
    sampling_frequency = 200
    n_time_samples, n_lags, n_signals = 500, 2, 3
    coefficients = np.zeros((n_lags, n_signals, n_signals))

    coefficients[0, :, :] = np.array(
        [[0.8, 0.0, 0.4], [0.0, 0.9, 0.0], [0.0, 0.5, 0.5]]
    )
    coefficients[1, :, :] = np.array(
        [[-0.5, 0.0, 0.0], [0.0, -0.8, 0.0], [0.0, 0.0, -0.2]]
    )

    noise_covariance = np.eye(n_signals) * [0.3, 1, 0.2]

    return (
        simulate_MVAR(
            coefficients,
            noise_covariance=noise_covariance,
            n_time_samples=n_time_samples,
            n_trials=500,
            n_burnin_samples=500,
            random_state=0,
        ),
        sampling_frequency,
    )


plot_directional(*ding_example2a())
../_images/a14c675fab3b392ac66e386461863fed91e098e0aa91c0d848a945ac595f0197.png ../_images/bc42dcaae7297285f8646b45c335612a772619cdf790556136fdb53f70921856.png

Ding Example 2b#

Ding, M., Chen, Y., and Bressler, S.L. (2006). Granger causality: basic theory and application to neuroscience. In Handbook of Time Series Analysis, pp. 437–460.

\[ x_1 = 0.8x_1(n-1) - 0.5x_1(n-2) + 0.4x_3(n-1) + 0.2x_2(n-2) + w_1 \]
\[ x_2 = 0.9x_2(n-1) - 0.8x_2(n-2) + w_2\]
\[ x_3 = 0.5x_3(n-1) - 0.2x_3(n-2) + 0.5x_2(n-1) + w_3\]

image.png

dashed curves showing the results for the first model and the solid curves for the second model

def ding_example2b():
    """Ding, M., Chen, Y., and Bressler, S.L. (2006). Granger causality:
    basic theory and application to neuroscience. In Handbook of Time Series
    Analysis, pp. 437–460."""
    sampling_frequency = 200
    n_time_samples, n_lags, n_signals = 500, 2, 3
    coefficients = np.zeros((n_lags, n_signals, n_signals))

    coefficients[0, :, :] = np.array(
        [[0.8, 0.0, 0.4], [0.0, 0.9, 0.0], [0.0, 0.5, 0.5]]
    )
    coefficients[1, :, :] = np.array(
        [[-0.5, 0.2, 0.0], [0.0, -0.8, 0.0], [0.0, 0.0, -0.2]]
    )

    noise_covariance = np.eye(n_signals) * [0.3, 1.0, 0.2]

    return (
        simulate_MVAR(
            coefficients,
            noise_covariance=noise_covariance,
            n_time_samples=n_time_samples,
            n_trials=500,
            n_burnin_samples=100,
            random_state=0,
        ),
        sampling_frequency,
    )


plot_directional(*ding_example2b(), time_halfbandwidth_product=2)
../_images/034127fa8c253826224d6236f7e02e04a2ae7dc39fceb891138515256c33f6d4.png ../_images/2986aee24f2f389aeecdee41f506a2fdcdc35cbc41b544fcf8090ad76010869a.png

Ding Example 3#

Ding, M., Chen, Y., and Bressler, S.L. (2006). Granger causality: basic theory and application to neuroscience. In Handbook of Time Series Analysis, pp. 437–460.

\[ x_1 = 0.95\sqrt2x_1(n-1) - 0.9025x_1(n-2) + w_1 \]
\[ x_2 = 0.5x_1(n-2) + w_2 \]
\[ x_3 = -0.4x_1(n-3) + w_3 \]
\[ x_4 = -0.5x_1(n-2) + 0.25\sqrt2x_4(n-1) + 0.25\sqrt2x_5(n-1) + w_4 \]
\[ x_5 = -0.25\sqrt2x_4(n-1) + 0.25\sqrt2x_5(n-1) + w_5 \]

image.png

def ding_example3():
    """Ding, M., Chen, Y., and Bressler, S.L. (2006). Granger causality:
    basic theory and application to neuroscience. In Handbook of Time Series
    Analysis, pp. 437–460."""
    sampling_frequency = 200
    n_time_samples, n_lags, n_signals = 1000, 3, 5
    coefficients = np.zeros((n_lags, n_signals, n_signals))

    coefficients[0, 0, 0] = 0.95 * np.sqrt(2)
    coefficients[1, 0, 0] = -0.9025

    coefficients[1, 1, 0] = 0.50
    coefficients[2, 2, 0] = -0.40

    coefficients[1, 3, 0] = -0.50
    coefficients[0, 3, 3] = 0.25 * np.sqrt(2)
    coefficients[0, 3, 4] = 0.25 * np.sqrt(2)

    coefficients[0, 4, 3] = -0.25 * np.sqrt(2)
    coefficients[0, 4, 4] = 0.25 * np.sqrt(2)

    noise_covariance = np.eye(n_signals) * [0.6, 0.5, 0.3, 0.3, 0.6]

    return (
        simulate_MVAR(
            coefficients,
            noise_covariance=noise_covariance,
            n_time_samples=n_time_samples,
            n_trials=500,
            n_burnin_samples=500,
            random_state=0,
        ),
        sampling_frequency,
    )


plot_directional(*ding_example3(), time_halfbandwidth_product=1)
../_images/8964f98ace2fd6ad17f10c79067f674cf9e552eee180e1e82264533a4867227e.png ../_images/6d771ccebaa1bc3e1dd647c2e109e071500329e4948b6b4572328a7fb1566863.png

Nedungadi Example 1#

Nedungadi, A.G., Ding, M., and Rangarajan, G. (2011). Block coherence: a method for measuring the interdependence between two blocks of neurobiological time series. Biological Cybernetics 104, 197–207.

def Nedungadi_example1():
    """Nedungadi, A.G., Ding, M., and Rangarajan, G. (2011).
    Block coherence: a method for measuring the interdependence
    between two blocks of neurobiological time series. Biological
    Cybernetics 104, 197–207.
    """
    sampling_frequency = 200
    n_time_samples, n_lags, n_signals = 500, 1, 3
    coefficients = np.zeros((n_lags, n_signals, n_signals))

    coefficients[0, :, :] = np.array(
        [[0.1, 0.0, 0.9], [0.0, 0.1, 0.9], [0.0, 0.0, 0.1]]
    )

    noise_covariance = np.array([[0.9, 0.6, 0.0], [0.6, 0.9, 0.0], [0.0, 0.0, 0.9]])

    return (
        simulate_MVAR(
            coefficients,
            noise_covariance=noise_covariance,
            n_time_samples=n_time_samples,
            n_trials=1000,
            n_burnin_samples=500,
            random_state=0,
        ),
        sampling_frequency,
    )


plot_directional(*Nedungadi_example1(), time_halfbandwidth_product=3)
../_images/ae148e2ceac23b10197dc01afb0ac0d02d824de72eee47832f0e414f07df1d4e.png ../_images/d3faa864bde34cd7f2522a7f2251883473ed95802b5c46ef081fda41336c3e2f.png

Nedungadi Example 2#

Nedungadi, A.G., Ding, M., and Rangarajan, G. (2011). Block coherence: a method for measuring the interdependence between two blocks of neurobiological time series. Biological Cybernetics 104, 197–207.

def Nedungadi_example2():
    """Nedungadi, A.G., Ding, M., and Rangarajan, G. (2011).
    Block coherence: a method for measuring the interdependence
    between two blocks of neurobiological time series. Biological
    Cybernetics 104, 197–207.
    """
    sampling_frequency = 200
    n_time_samples, n_lags, n_signals = 500, 1, 3
    coefficients = np.zeros((n_lags, n_signals, n_signals))

    coefficients[0, :, :] = np.array(
        [[0.1, 0.0, 0.9], [0.0, 0.1, 0.9], [0.0, 0.0, 0.1]]
    )

    noise_covariance = np.array([[0.9, 0.0, 0.0], [0.0, 0.9, 0.0], [0.0, 0.0, 0.9]])

    return (
        simulate_MVAR(
            coefficients,
            noise_covariance=noise_covariance,
            n_time_samples=n_time_samples,
            n_trials=1000,
            n_burnin_samples=500,
            random_state=0,
        ),
        sampling_frequency,
    )


plot_directional(*Nedungadi_example2(), time_halfbandwidth_product=3)
../_images/1b814e9c11aa29a7d632f22f2f41180070b1d2f2347a21fd02c2bad10a4755d3.png ../_images/cc98d22f2a91d435711033ef22b5e5ee623adf4d138ec36ea24994437f8aec2d.png

Wen Example 1#

Wen, X., Rangarajan, G., and Ding, M. (2013). Multivariate Granger causality: an estimation framework based on factorization of the spectral density matrix. Philosophical Transactions of the Royal Society A: Mathematical, Physical and Engineering Sciences 371, 20110610.

image.png

def Wen_example1():
    sampling_frequency = 200
    n_time_samples, n_lags, n_signals = 500, 4, 5
    coefficients = np.zeros((n_lags, n_signals, n_signals))

    coefficients[0, 0, 0] = 0.55
    coefficients[1, 0, 0] = -0.70
    coefficients[0, 1, 1] = 0.56
    coefficients[1, 1, 1] = -0.75
    coefficients[0, 1, 0] = 0.60
    coefficients[0, 2, 2] = 0.57
    coefficients[1, 2, 2] = -0.80
    coefficients[1, 2, 0] = 0.40
    coefficients[0, 3, 3] = 0.58
    coefficients[1, 3, 3] = -0.85
    coefficients[2, 3, 0] = 0.50
    coefficients[0, 4, 4] = 0.59
    coefficients[1, 4, 4] = -0.90
    coefficients[3, 4, 0] = 0.80

    noise_covariance = np.eye(n_signals) * [1.0, 2.0, 0.8, 1.0, 1.5]

    return (
        simulate_MVAR(
            coefficients,
            noise_covariance=noise_covariance,
            n_time_samples=n_time_samples,
            n_trials=500,
            n_burnin_samples=500,
            random_state=0,
        ),
        sampling_frequency,
    )


plot_directional(*Wen_example1(), time_halfbandwidth_product=1)
../_images/07a15ae75a4c8f960703adc6d74a0e4a506ab766a23adc8471a2d123104d50aa.png ../_images/86a9cb0b9a9aeeab7b69e4956978092920f877fbed2d1d9f3aa85ee30e140b4c.png

Wen Example 2#

Wen, X., Rangarajan, G., and Ding, M. (2013). Multivariate Granger causality: an estimation framework based on factorization of the spectral density matrix. Philosophical Transactions of the Royal Society A: Mathematical, Physical and Engineering Sciences 371, 20110610.

image.png

All five signals in this model resonate near 40 Hz, within 0.6 Hz of each other, and their noise is correlated, so the trials have to be long enough for the tapers to tell the resonances apart. Tapers average the spectrum over time_halfbandwidth_product / trial duration on either side of each frequency, which is 0.15 Hz for the 20 s trials simulated here. With 5 s trials (0.6 Hz) the same analysis shows a partial directed coherence from x3 to x5 of 0.19 at 40 Hz, an influence the model does not have.

def Wen_example2():
    sampling_frequency = 200
    n_time_samples, n_lags, n_signals = 4000, 4, 5
    coefficients = np.zeros((n_lags, n_signals, n_signals))

    coefficients[0, 0, 0] = 0.55
    coefficients[1, 0, 0] = -0.70
    coefficients[0, 1, 1] = 0.56
    coefficients[1, 1, 1] = -0.75
    coefficients[0, 1, 0] = 0.60
    coefficients[0, 2, 2] = 0.57
    coefficients[1, 2, 2] = -0.80
    coefficients[1, 2, 0] = 0.40
    coefficients[0, 3, 3] = 0.58
    coefficients[1, 3, 3] = -0.85
    coefficients[2, 3, 0] = 0.50
    coefficients[0, 4, 4] = 0.59
    coefficients[1, 4, 4] = -0.90
    coefficients[3, 4, 0] = 0.80

    coefficients[0, 2, 3] = -0.50
    coefficients[0, 4, 3] = -0.50

    noise_covariance = np.ones((n_signals, n_signals)) * 0.5
    diagonal_ind = np.arange(0, n_signals)
    noise_covariance[diagonal_ind, diagonal_ind] = [1.0, 2.0, 0.8, 1.0, 1.5]

    return (
        simulate_MVAR(
            coefficients,
            noise_covariance=noise_covariance,
            n_time_samples=n_time_samples,
            n_trials=200,
            n_burnin_samples=100,
            random_state=0,
        ),
        sampling_frequency,
    )


plot_directional(*Wen_example2(), time_halfbandwidth_product=3)
../_images/0ca7c95fe121d187e77e669fc17c55bddec692a251902bbdda290e814d07191a.png ../_images/2ddf9cb53feb7e64cdd5ad54b3a371ca37eb4999ab223362e1239380d8cb3a68.png

Dhamala Example 1#

Dhamala, M., Rangarajan, G., and Ding, M. (2008). Analyzing information flow in brain networks with nonparametric Granger causality. NeuroImage 41, 354–362.

image.png

def Dhamala_example1():
    sampling_frequency = 200
    n_time_samples, n_lags, n_signals = 4000, 2, 3
    coefficients = np.zeros((n_lags, n_signals, n_signals))

    coefficients[0, 0, 0] = 0.80
    coefficients[1, 0, 0] = -0.50
    coefficients[0, 0, 2] = 0.40
    coefficients[0, 1, 1] = 0.53
    coefficients[1, 1, 1] = -0.80
    coefficients[0, 2, 2] = 0.50
    coefficients[1, 2, 2] = -0.20
    coefficients[0, 2, 1] = 0.50

    noise_covariance = np.eye(n_signals) * [0.25, 1.00, 0.25]
    return (
        simulate_MVAR(
            coefficients,
            noise_covariance=noise_covariance,
            n_time_samples=n_time_samples,
            n_trials=4000,
            n_burnin_samples=1000,
            random_state=0,
        ),
        sampling_frequency,
    )


plot_directional(*Dhamala_example1(), time_halfbandwidth_product=1)
../_images/5ed082208febf803652ddd0138250ee9b213d184192279c4502d7eda5aa036f5.png ../_images/51f3bbc257f55784d5310c469e095bca796fe5df47c10032d181ab8d5e7a653b.png

Dhamala Example 2#

Dhamala, M., Rangarajan, G., and Ding, M. (2008). Analyzing information flow in brain networks with nonparametric Granger causality. NeuroImage 41, 354–362.

def Dhamala_example2a():
    sampling_frequency = 200
    n_time_samples, n_lags, n_signals = 450, 2, 2
    coefficients = np.zeros((n_lags, n_signals, n_signals))

    coefficients[0, 0, 0] = 0.53
    coefficients[1, 0, 0] = -0.80
    coefficients[0, 0, 1] = 0.50
    coefficients[0, 1, 1] = 0.53
    coefficients[1, 1, 1] = -0.80
    coefficients[0, 1, 0] = 0.00

    noise_covariance = np.eye(n_signals) * [0.25, 0.25]
    return (
        simulate_MVAR(
            coefficients,
            noise_covariance=noise_covariance,
            n_time_samples=n_time_samples,
            n_trials=1000,
            n_burnin_samples=1000,
            random_state=0,
        ),
        sampling_frequency,
    )


def Dhamala_example2b():
    sampling_frequency = 200
    n_time_samples, n_lags, n_signals = 450, 2, 2
    coefficients = np.zeros((n_lags, n_signals, n_signals))

    coefficients[0, 0, 0] = 0.53
    coefficients[1, 0, 0] = -0.80
    coefficients[0, 0, 1] = 0.00
    coefficients[0, 1, 1] = 0.53
    coefficients[1, 1, 1] = -0.80
    coefficients[0, 1, 0] = 0.50

    noise_covariance = np.eye(n_signals) * [0.25, 0.25]
    return (
        simulate_MVAR(
            coefficients,
            noise_covariance=noise_covariance,
            n_time_samples=n_time_samples,
            n_trials=1000,
            n_burnin_samples=1000,
            random_state=0,
        ),
        sampling_frequency,
    )


time_series1, sampling_frequency = Dhamala_example2a()
time_series2, _ = Dhamala_example2b()
time_series = np.concatenate((time_series1, time_series2), axis=0)

time_halfbandwidth_product = 1

m = Multitaper(
    prepare_time_series(time_series),
    sampling_frequency=sampling_frequency,
    time_halfbandwidth_product=time_halfbandwidth_product,
    start_time=0,
    time_window_duration=0.500,
    time_window_step=0.250,
)
c = Connectivity.from_transform(m)
granger = c.pairwise_spectral_granger_prediction()

fig, ax = plt.subplots(2, 1, sharex=True, sharey=True)

ax[0].pcolormesh(
    c.time, c.frequencies, granger[..., :, 0, 1].T, cmap="viridis", shading="auto"
)
ax[0].set_title("x1 -> x2")
ax[0].set_ylabel("Frequency")
ax[1].pcolormesh(
    c.time, c.frequencies, granger[..., :, 1, 0].T, cmap="viridis", shading="auto"
)
ax[1].set_title("x2 -> x1")
ax[1].set_xlabel("Time")
ax[1].set_ylabel("Frequency")
Text(0, 0.5, 'Frequency')
../_images/66acb18ea56e7778d3366161b37181614643f5325cc29a1a7aa0443750ec2f36.png
dtf = c.directed_transfer_function()

fig, ax = plt.subplots(2, 1, sharex=True, sharey=True)

ax[0].pcolormesh(
    c.time, c.frequencies, dtf[..., :, 0, 1].T, cmap="viridis", shading="auto"
)
ax[0].set_title("x1 -> x2")
ax[0].set_ylabel("Frequency")
ax[1].pcolormesh(
    c.time, c.frequencies, dtf[..., :, 1, 0].T, cmap="viridis", shading="auto"
)
ax[1].set_title("x2 -> x1")
ax[1].set_xlabel("Time")
ax[1].set_ylabel("Frequency")
Text(0, 0.5, 'Frequency')
../_images/d9ff4e3bce95a36171eeac7f5530803590026502ac655896ed7250aeabfcc5ba.png
pdc = c.partial_directed_coherence()

fig, ax = plt.subplots(2, 1, sharex=True, sharey=True)

ax[0].pcolormesh(c.time, c.frequencies, pdc[..., :, 0, 1].T, cmap="viridis")
ax[0].set_title("x1 -> x2")
ax[0].set_ylabel("Frequency")
ax[1].pcolormesh(c.time, c.frequencies, pdc[..., :, 1, 0].T, cmap="viridis")
ax[1].set_title("x2 -> x1")
ax[1].set_xlabel("Time")
ax[1].set_ylabel("Frequency")
Text(0, 0.5, 'Frequency')
../_images/d9ff4e3bce95a36171eeac7f5530803590026502ac655896ed7250aeabfcc5ba.png