1import numpy as np2from fractions import Fraction3from pamle.jackknife import (vandermonde_weights_exact, vandermonde_weights, leading_bias_coefficient,4 amplification_constant, vandermonde_condition_number, combine_levels, DEFAULT_SPLITS)567def test_moment_constraints_exact():8 for s in [(1, 2), (1, 2, 4), (1, 2, 4, 8), (1, 2, 3), (1, 3, 9), (1, 2, 4, 8, 16)]:9 w = vandermonde_weights_exact(s)10 assert sum(w) == 111 for a in range(1, len(s)):12 assert sum(wj * Fraction(sj) ** a for wj, sj in zip(w, s)) == 0131415def test_known_weights():16 assert vandermonde_weights_exact((1, 2)) == [2, -1]17 assert vandermonde_weights_exact((1, 2, 4)) == [Fraction(8, 3), -2, Fraction(1, 3)]18 assert np.allclose(vandermonde_weights((1, 2, 4)), [8 / 3, -2, 1 / 3])192021def test_float_moment_constraints():22 for r in range(1, 6):23 s = DEFAULT_SPLITS[r]; w = vandermonde_weights(s); sv = np.array(s, float)24 assert abs(w.sum() - 1) < 1e-1225 for a in range(1, r):26 assert abs(np.sum(w * sv ** a)) < 1e-9 * np.sum(np.abs(w) * sv ** a)272829def test_leading_coefficients_and_amplification():30 assert leading_bias_coefficient((1, 2)) == -231 assert leading_bias_coefficient((1, 2, 4)) == 832 assert leading_bias_coefficient((1, 2, 4, 8)) == -6433 assert amplification_constant((1, 2)) == 4 and amplification_constant((1, 2, 4)) == 834 assert vandermonde_condition_number((1, 2, 4, 8)) > vandermonde_condition_number((1, 2))353637def test_combine_levels_preserves_mean():38 rng = np.random.default_rng(0)39 K, p = 5, 340 common = rng.standard_normal((K, p))41 levels = np.stack([common, common, common]) # identical level means -> J equals them (sum w = 1)42 J = combine_levels(levels, vandermonde_weights((1, 2, 4)))43 assert np.allclose(J, common)44