import numpy as np from repro_beyond_model_ranking.spectral import linear_utilization_ratio, scp def test_scp_separates_coherent_signal_from_independent_noise(): rng = np.random.default_rng(7) n = 8192 x = rng.normal(size=n) coherent_y = 0.8 * x + 0.2 * rng.normal(size=n) independent_y = rng.normal(size=n) coherent = scp(x, coherent_y) independent = scp(x, independent_y) assert coherent.p_coherence > 0.85 assert independent.p_coherence < 0.35 assert coherent.p_coherence > independent.p_coherence + 0.5 def test_scp_matches_gaussian_intrinsic_predictability_as_n_grows(): rng = np.random.default_rng(11) n = 65536 coefficient = 0.75 noise_std = 0.5 x = rng.normal(size=n) y = coefficient * x + noise_std * rng.normal(size=n) theoretical = coefficient**2 / (coefficient**2 + noise_std**2) # A smaller fractional window increases the number of Welch segments, # which is the effective sample size referenced by the consistency claim. estimate = scp(x, y, win_frac=1 / 64) assert abs(estimate.p_coherence - theoretical) < 0.04 def test_paper_formula_can_leave_unit_interval_with_boundary_mean_shift(): rng = np.random.default_rng(19) x = rng.normal(0.0, 1.0, size=8192) y = x + 4.0 estimate = scp(x, y) assert estimate.mean_shift_mse > estimate.var_y assert estimate.p_xy_raw < 0.0 assert estimate.p_xy_clipped == 0.0 assert estimate.bound_violated def test_lur_distinguishes_underuse_saturation_and_beyond_linear(): rng = np.random.default_rng(23) n = 8192 x = rng.normal(size=n) y = x + 0.5 * rng.normal(size=n) under = rng.normal(size=n) saturated = x beyond = y under_lur = linear_utilization_ratio(x, y, under).lur saturated_lur = linear_utilization_ratio(x, y, saturated).lur beyond_lur = linear_utilization_ratio(x, y, beyond).lur assert under_lur < 0.5 assert 0.9 < saturated_lur < 1.1 assert beyond_lur > 1.1 def test_band_lur_returns_frequency_resolved_values(): n = 8192 t = np.arange(n) low = np.sin(2 * np.pi * 0.02 * t) high = 0.5 * np.sin(2 * np.pi * 0.32 * t) x = low + high y = low + high prediction = low result = linear_utilization_ratio( x, y, prediction, bands=((0.0, 0.1), (0.1, 0.5)), ) assert len(result.band_lur) == 2 assert result.band_lur[0] > result.band_lur[1] assert result.band_lur[0] > 0.9 assert result.band_lur[1] < 0.2