egttools.utils.calculate_spectral_gap

calculate_spectral_gap(transition_matrix, tol=1e-10)[source]

Compute the spectral gap of a Markov chain transition matrix.

The spectral gap is 1 - |lambda_2| where |lambda_2| is the second-largest eigenvalue modulus (the largest eigenvalue of an ergodic chain is always 1).

A larger gap means faster mixing. The chain needs approximately ceil(log(epsilon) / log(1 - gap)) steps from the worst initial state to be epsilon-close to stationarity, providing a principled lower bound for the transitory parameter in Monte Carlo estimation.

Uses scipy.sparse.linalg.eigs (ARPACK) to avoid dense conversion; falls back to a 2x2 dense solve only when the matrix is too small for ARPACK (n <= 3).

Parameters:
  • transition_matrix (numpy.ndarray, csr_matrix, or csc_matrix) – Square row- or column-stochastic transition matrix of shape (n, n). Eigenvalues are invariant to transposition so either convention works.

  • tol (float, optional) – ARPACK convergence tolerance (default 1e-10).

Returns:

Spectral gap in (0, 1]. Returns 1.0 for a single-state chain (n <= 1).

Return type:

float