spectral_connectivity.statistics.power_confidence_intervals#

power_confidence_intervals(n_observations: int | None = None, power: NDArray[floating] | float = 1, confidence_level: float = 0.95, *, n_tapers: int | None = None, ci: float | None = None, real_valued: bool | NDArray[bool] = False) → tuple[NDArray[floating] | float, NDArray[floating] | float][source]#

Compute confidence intervals for multitaper power spectrum estimates.

Uses chi-squared distribution to compute confidence bounds for power spectral density estimates from multitaper analysis.

Changed in version 3.0: The count is named n_observations and the level confidence_level, as in jackknife(); both keep their positions, so positional calls are unchanged. n_tapers= and ci= still work through 3.x with a DeprecationWarning and are removed in 4.0.

Parameters:
  • n_observations (int) – Number of independent observations averaged into each power estimate: tapers times trials (Connectivity.n_observations), not the taper count alone unless a single trial was averaged. It sets the chi-squared degrees of freedom 2 * n_observations, as in power_bias() and power_variance(). Passing only the taper count for a multi-trial average makes the interval far too wide (about 100% coverage instead of 95% for 5 tapers x 5 trials).

  • power (NDArray[floating] or float, default=1) – Power spectrum estimates. Can be array of values or scalar.

  • confidence_level (float, default=0.95) – The probability mass inside the two-sided interval (0.95 for a 95% interval), in the range [0.5, 1.0).

  • n_tapers (int, optional) – Deprecated name of n_observations, by keyword only; removed in 4.0. Giving both raises ValueError.

  • ci (float, optional) – Deprecated name of confidence_level, by keyword only; removed in 4.0. Giving both raises ValueError.

  • real_valued (bool or NDArray[bool], default=False) – Which estimates average real-valued Fourier coefficients, a boolean array broadcast against power; see real_valued_frequencies for which bins and why. For Connectivity.power(), shaped (..., n_frequencies, n_signals), pass real_valued_frequencies[:, np.newaxis]. Those bins use n_observations degrees of freedom; with 2 * n_observations there a nominal 95% interval from five observations covers 83%.

Returns:

  • lower_bound (NDArray[floating]) – Lower confidence bounds for power estimates.

  • upper_bound (NDArray[floating]) – Upper confidence bounds for power estimates.

Examples

>>> import numpy as np
>>> # Single power estimate from 5 observations (5 tapers x 1 trial)
>>> lower, upper = power_confidence_intervals(
...     n_observations=5, power=1.0, confidence_level=0.95
... )
>>> print(f"95% CI: [{lower:.3f}, {upper:.3f}]")
95% CI: [0.488, 3.080]
>>> # Averaging 5 tapers x 5 trials gives 25 observations, a much tighter interval
>>> lower, upper = power_confidence_intervals(
...     n_observations=25, power=1.0, confidence_level=0.95
... )
>>> print(f"95% CI: [{lower:.3f}, {upper:.3f}]")
95% CI: [0.700, 1.545]
>>> # Multiple power estimates
>>> power_vals = np.array([0.5, 1.0, 2.0, 5.0])
>>> lower, upper = power_confidence_intervals(5, power_vals, 0.95)
>>> np.round(lower, 3).tolist()
[0.244, 0.488, 0.976, 2.441]
>>> # The 0 Hz bin of a real signal: half the degrees of freedom, a wider interval
>>> lower, upper = power_confidence_intervals(5, 1.0, 0.95, real_valued=True)
>>> print(f"95% CI: [{lower:.3f}, {upper:.3f}]")
95% CI: [0.390, 6.015]

From a Connectivity: pass its n_observations and mark its real 0 Hz and Nyquist bins:

>>> from spectral_connectivity import Connectivity, Multitaper
>>> rng = np.random.default_rng(0)
>>> time_series = rng.standard_normal((1000, 10, 2))  # (time, trials, signals)
>>> multitaper = Multitaper(time_series, sampling_frequency=500, time_window_duration=0.5)
>>> connectivity = Connectivity.from_transform(multitaper)
>>> power = connectivity.power(one_sided=True)
>>> power.shape  # (n_time_windows, n_frequencies, n_signals)
(4, 126, 2)
>>> lower, upper = power_confidence_intervals(
...     connectivity.n_observations,  # 10 trials x 5 tapers = 50
...     power,
...     confidence_level=0.95,
...     # (n_frequencies,) -> (n_frequencies, 1): broadcasts over time windows and signals
...     real_valued=connectivity.real_valued_frequencies[:, np.newaxis],
... )
>>> lower.shape
(4, 126, 2)
>>> connectivity.frequencies[[0, 10]].tolist()
[0.0, 20.0]
>>> # Upper bound over the estimate: wider at the real 0 Hz bin
>>> np.round(upper[0, [0, 10], 0] / power[0, [0, 10], 0], 2).tolist()
[1.55, 1.35]

References

[1]

Kramer, M.A., and Eden, U.T. (2016). Case studies in neural data analysis: a guide for the practicing neuroscientist (MIT Press).