Source code for mercurial.hierarchy.coupling

"""Cross‑level adjacency optimization (LADDER F.1, Definition 3.5)."""

from typing import List

import numpy as np


[docs] class LevelCoupling: """ Implements hierarchical coupling strength between levels: K_ij = K_0 * exp(-|i - j| / l_adj) Where: - i, j are LADDER level indices (0-18) - K_0 is baseline coupling strength - l_adj is adjacency decay length (typically 1.0-2.0) """
[docs] def __init__(self, baseline_coupling: float = 0.1, decay_length: float = 1.0): """ Parameters ---------- baseline_coupling : float K_0, maximum coupling when i = j. decay_length : float ℓ_adj, characteristic distance for exponential decay. """ self.K0 = baseline_coupling self.l_adj = decay_length
[docs] def coupling_strength(self, level_i: int, level_j: int) -> float: """Compute K_ij = K_0 * exp(-|i - j| / ℓ_adj).""" distance = abs(level_i - level_j) return self.K0 * np.exp(-distance / self.l_adj)
[docs] def coupling_matrix(self, levels: List[int]) -> np.ndarray: """Compute full coupling matrix for a list of levels.""" n = len(levels) K = np.zeros((n, n)) for i in range(n): for j in range(n): K[i, j] = self.coupling_strength(levels[i], levels[j]) return K
[docs] def effective_similarity(self, base_similarity: float, level_i: int, level_j: int) -> float: """ Combine base isomorphism with hierarchical coupling. Used as σ_eff = σ_base * K_ij. """ return base_similarity * self.coupling_strength(level_i, level_j)