Note
Go to the end to download the full example code.
Is this value more than noise?#
An estimator always returns numbers, also for signals that have nothing to do with each other. So the first question about any value is whether it is more than noise would give. This tutorial shows the tests dirconn offers for that (surrogate data, block permutation and an analytic test for Granger causality), how to correct for testing many cells at once, and how to put an interval around a value with the bootstrap. It ends on what a significant value does not tell you.
Setup and data#
The same simulated chain as in tutorial 1: the first region drives the second, the second drives the third, and there is no direct influence from the first on the third.
import os
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import dirconn
from dirconn import GC, Edge, Network, dk_node
from dirconn.simulation import NodeDynamics, simulate
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__}")
A, B, C = "cuneus-rh", "precuneus-rh", "insula-rh"
network = Network(
nodes=[dk_node(A), dk_node(B), dk_node(C)],
edges=[Edge(A, B, strength=0.4, delay_ms=10.0), Edge(B, C, strength=0.4, delay_ms=10.0)],
name="chain",
)
dynamics = {
A: NodeDynamics(kind="oscillator", freq_hz=10.0),
B: NodeDynamics(kind="oscillator", freq_hz=20.0),
C: NodeDynamics(kind="oscillator", freq_hz=15.0),
}
SFREQ = 100.0
sim = simulate(
network,
sfreq=SFREQ,
n_times=int((30.0 if QUICK else 120.0) * SFREQ),
seed=SEED,
dynamics=dynamics,
snr_db=10.0,
n_background=0,
node_scale="unit",
)
data = sim.roi_observations(snr_db=10.0, seed=SEED + 1)
print(data)
dirconn 0.2.0, numpy 2.5.5
TimeSeries(n_trials=1, n_channels=3, n_times=12000, sfreq=100, segmentation='continuous')
A test with surrogate data#
The idea: make many copies of the data in which any relation between signals is destroyed but
each signal keeps its own spectrum, estimate on every copy, and see how often the copies give a
value as large as the one observed. That fraction is the p-value. phase_randomize makes the
copies by shuffling the phases of every signal independently.
The estimator is built once and handed to the test. The order is fixed here so that every copy is fitted with the same model; the number of copies sets how small a p-value can be: with 199 copies the smallest possible p-value is 1/200 = 0.005.
estimator = GC(order=5, check_order_stability=False)
surrogate = dirconn.significance_test(
estimator,
data,
test="surrogate",
surrogate="phase_randomize",
n_surrogates=199,
seed=SEED + 2,
)
print(surrogate)
print("what this test assumes and cannot see:")
for line in surrogate.limitations:
print(" -", line)
SurrogateTestResult(method='gc', shape=3x3x1x1, surrogate='phase_randomize', n_surrogates=199, null='connectivity is explained by individual-channel power spectra')
what this test assumes and cannot see:
- Assumes each channel is a stationary linear Gaussian process; amplitude fluctuations over time and non-Gaussian amplitude distributions are not kept.
- Destroys phase relations within a channel (bursts, waveform shape, cross-frequency coupling), so a channel with such structure can look significant for that reason alone.
- A trial-locked evoked response is also scrambled; for epoched data this can inflate significance when the coupling is driven by the evoked response.
The results are arrays of the same layout as the estimate: pvalues and observed have one
entry per (source, target) cell. As a table:
observed = surrogate.observed.values[:, :, 0, 0]
pvalues = surrogate.pvalues[:, :, 0, 0]
names = surrogate.observed.source
rows = []
for i, source in enumerate(names):
for j, target in enumerate(names):
if i != j:
rows.append(
{
"source": source,
"target": target,
"value (nats)": observed[i, j],
"p": pvalues[i, j],
"real edge": bool(sim.truth.values[i, j, 0, 0] == 1),
}
)
table = pd.DataFrame(rows)
print(table.round(3).to_string(index=False))
source target value (nats) p real edge
cuneus-rh precuneus-rh 0.446 0.005 True
cuneus-rh insula-rh 0.005 0.005 False
precuneus-rh cuneus-rh 0.004 0.005 False
precuneus-rh insula-rh 0.245 0.005 True
insula-rh cuneus-rh 0.001 0.350 False
insula-rh precuneus-rh 0.002 0.005 False
Many cells, many tests#
A matrix of three regions has six directed cells, and each cell is a test. At the usual level of
0.05, one test in twenty on pure noise comes out “significant”, so with many cells some of them
will. adjust_pvalues corrects for that: "bonferroni" is the strictest and controls the
chance of any false positive; "fdr_bh" (Benjamini-Hochberg) controls the share of false
positives among the cells declared significant and is the default of significant.
adjusted = {
method: dirconn.adjust_pvalues(surrogate.pvalues, method=method)[:, :, 0, 0]
for method in ("none", "fdr_bh", "bonferroni")
}
counts = {m: int(np.sum(p[np.isfinite(p)] < 0.05)) for m, p in adjusted.items()}
print("cells below 0.05:", counts)
significant = surrogate.significant() # fdr_bh at alpha = 0.05
cells below 0.05: {'none': 5, 'fdr_bh': 5, 'bonferroni': 5}
The result as a picture, next to the truth. significance= takes the test result and marks
with a * the cells that significant() declares significant (FDR, 0.05).

The two true connections (dark grey on the left) have by far the largest values and both carry a
*. The four other cells stay below 0.01. Whether the test marks some of them as well differs
between the two versions of this tutorial: none with the 30 s recording of the quick version,
three with the full 120 s. The next section looks at what such marks mean.
A significant value is not necessarily a large one#
A p-value says that a value is unlikely to come from the null model of the test, here signals that have no relation to each other. It does not say how large the value is. Sorted by value, the two real influences stand far above every other cell, whatever the p-values of the rest say:
table["significant (FDR)"] = [
bool(significant[names.index(row.source), names.index(row.target), 0, 0])
for row in table.itertuples()
]
print(table.sort_values("value (nats)", ascending=False).round(3).to_string(index=False))
source target value (nats) p real edge significant (FDR)
cuneus-rh precuneus-rh 0.446 0.005 True True
precuneus-rh insula-rh 0.245 0.005 True True
cuneus-rh insula-rh 0.005 0.005 False True
precuneus-rh cuneus-rh 0.004 0.005 False True
insula-rh precuneus-rh 0.002 0.005 False True
insula-rh cuneus-rh 0.001 0.350 False False
If cells without a real influence are marked significant in your run, look at their values: they are tiny next to those of the real influences. A longer recording gives a test more power, so it can flag smaller and smaller values; the size of the value is the second thing to look at, next to the p-value. Whether such small values come from something in the simulation, for example the observation noise that is added to every region, was not tested here.
An interval around a value#
The bootstrap asks a different question: how much would the value move if the recording had been slightly different? It resamples blocks of the recording (continuous data) or whole trials (epoched data), estimates on each resample and reports the spread. Blocks must be long compared with the memory of the signals; 1 s is used here, and the library warns when there are fewer than 20 blocks to draw from.
boot = dirconn.bootstrap(estimator, data, n_boot=200, seed=SEED + 3, block_size=100)
rows = []
for i, source in enumerate(names):
for j, target in enumerate(names):
if i != j:
rows.append(
{
"source": source,
"target": target,
"value": boot.observed.values[i, j, 0, 0],
"lower": boot.lower[i, j, 0, 0],
"upper": boot.upper[i, j, 0, 0],
}
)
print(f"{boot.ci:.0%} intervals from {boot.n_boot} resamples")
print(pd.DataFrame(rows).round(3).to_string(index=False))
95% intervals from 200 resamples
source target value lower upper
cuneus-rh precuneus-rh 0.446 0.422 0.459
cuneus-rh insula-rh 0.005 0.003 0.009
precuneus-rh cuneus-rh 0.004 0.002 0.007
precuneus-rh insula-rh 0.245 0.225 0.261
insula-rh cuneus-rh 0.001 0.000 0.002
insula-rh precuneus-rh 0.002 0.001 0.005
Granger causality cannot be negative, so the interval of a cell without influence sits just above zero. The interval describes how much the estimate varies; it does not say whether the cell is a real influence.
Other tests#
The same call runs a block permutation test (test="block_perm") and, for Granger causality
only, an analytic F test (test="analytic", which needs no copies). They make different
assumptions, and each result lists them:
block = dirconn.significance_test(
estimator, data, test="block_perm", block_size=100, n_surrogates=199, seed=SEED + 4
)
analytic = dirconn.significance_test(GC(order=5, ridge=0.0), data, test="analytic")
for label, result in (
("surrogate", surrogate),
("block permutation", block),
("analytic", analytic),
):
n_significant = int(np.nansum(result.significant()[:, :, 0, 0]))
print(f"{label}: {n_significant} of {len(rows)} cells significant (FDR, 0.05)")
for line in result.limitations:
print(" -", line)
surrogate: 5 of 6 cells significant (FDR, 0.05)
- Assumes each channel is a stationary linear Gaussian process; amplitude fluctuations over time and non-Gaussian amplitude distributions are not kept.
- Destroys phase relations within a channel (bursts, waveform shape, cross-frequency coupling), so a channel with such structure can look significant for that reason alone.
- A trial-locked evoked response is also scrambled; for epoched data this can inflate significance when the coupling is driven by the evoked response.
block permutation: 5 of 6 cells significant (FDR, 0.05)
- The observed result is computed on the blocks too (as separate trials), so a model loses the regression rows across block boundaries; with blocks short relative to the model order this costs data and changes the estimate.
- Assumes stationarity and exchangeable blocks: a drift, a change of variance or a block-locked event makes shuffled blocks differ from the original for reasons unrelated to coupling.
- Dependence longer than a block is destroyed in the surrogates, so the null also includes 'no coupling on time scales longer than block_size'; too short a block inflates significance.
- A trial-locked evoked response is moved to other blocks in some channels and not in others, so coupling driven by it is destroyed with it.
analytic: 5 of 6 cells significant (FDR, 0.05)
- The F distribution is exact only for a stationary linear VAR with independent, homoscedastic Gaussian residuals; heavy tails, bursts or changing variance make the p-values wrong in either direction.
- The p-values are conditional on the chosen model order. When the order is selected from the data (a criterion) the selection uncertainty is ignored; a too-low order leaves autocorrelated residuals, which inflates significance.
- Trials are pooled as independent observations of one VAR model; trial-to-trial changes in the process or serial dependence between trials are not accounted for.
- Predictive influence is tested, not causation: common input, a hidden driver or volume conduction can produce a significant value.
Limitations#
A significant cell means that its value is unlikely under the null model of the test; it does not mean that the influence is real, large or causal. The tests here ask whether signals are related at all; a relation through a third region or through a shared input can pass them (tutorial 5).
The three tests make different assumptions, listed above, and a violated assumption can make the p-values wrong in either direction. This tutorial does not check how often each test is wrong; the benchmark report (
docs/benchmark_results.md) measures that under stated conditions.Correcting for the six cells of a three-region matrix is simple; with many regions, many frequencies and many time points the family of tests grows quickly and the choice of family is yours to make and to report.
Every result lists the assumptions it rests on;
not_checkedmeans that the library did not test an assumption, not that it holds:
stationarity not_checked
linearity not_checked
sampling_adequacy pass 15 coefficients per equation are estimated from 11995 observations (ratio 0.00125; the rule of thumb is at most 0.1).
instantaneous_coupling not_checked
causal_sufficiency not_checked
filtering not_checked
residual_whiteness pass Hosking portmanteau statistic 279.2 over lags 1 to 15 (df 90, p = 3.03e-21); largest absolute residual autocorrelation 0.0661 (warning when p < 0.05 and it exceeds 0.1).
Not tested here: the test of one recording says nothing about a group of participants; and frequency-resolved and time-resolved results need a test over every frequency or time point as well, which this tutorial does not do.
plt.close("all")