fix(harmonic): correct BB radial kernel + add fine-k interpolation - #19
Open
MinhMPA wants to merge 1 commit into
Open
fix(harmonic): correct BB radial kernel + add fine-k interpolation#19MinhMPA wants to merge 1 commit into
MinhMPA wants to merge 1 commit into
Conversation
Two compounding bugs in `compute_cl_bb` produced clax/CLASS C_l^BB ratios
in [0.4×, 22×] depending on l. Both fixed; primordial BB now matches CLASS
sub-percent at l<=200 and ~2% at l=300.
Bug 1: clax used `sqrt[l(l-1)(l+1)(l+2)] * j_l(x)/x²`, which is CLASS's
TENSOR_TEMPERATURE_2 kernel (transfer.c:4241-4249), not the BB kernel.
Replaced with TENSOR_POLARISATION_B (transfer.c:4263-4272 flat-space):
K_l^B(x) = 0.5 * (j_l'(x) + 2 * j_l(x) / x)
Bug 2: BB integrated over the raw 160-mode perturbation k-grid. Bessel
oscillations at the BB recombination peak alias against the log-uniform
k-spacing, producing 6-30% trapezoidal-rule errors with l-dependent sign.
Added cubic-spline interpolation of source_p to a fine log-uniform k-grid
(n_k_fine=2000 default), mirroring compute_cls_all_fast.
Test bounds tightened from [0.05, 20.0] to [0.95, 1.05] in
test_cl_bb_vs_class. Existing positional API unchanged.
3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two compounding bugs in
compute_cl_bbwere producing clax/CLASS C_l^BB ratios in[0.4×, 22×]depending on l. Both are fixed; primordial BB now matches CLASS sub-percent at l ≤ 200 and ~2% at l = 300.Bugs
1. Wrong radial kernel.
compute_cl_bbused√[l(l-1)(l+1)(l+2)] · j_l(x)/x², which is CLASS'sTENSOR_TEMPERATURE_2kernel (transfer.c:4241-4249), not the BB kernel. Replaced withTENSOR_POLARISATION_B(transfer.c:4263-4272, flat-space limitK=0wherecscKgen=cotKgen=1/xandrescale_function=1):The derivative is computed via the recurrence
j_l'(x) = j_{l-1}(x) - (l+1)/x · j_l(x), usingspherical_jl_backwardfor bothj_landj_{l-1}.2. k-grid undersampling for BB integration.
compute_cl_bbintegratedP_T(k)·|B_l(k)|²directly over the perturbation k-grid (~160 modes). The Bessel-driven oscillation period at the BB recombination peak (k ≈ 0.005-0.05 Mpc⁻¹) is comparable to the log-uniform k-mode spacing, so adjacent modes can sample opposing peaks of|B_l(k)|²and produce trapezoidal-rule errors of 6-30% with sign that flips with l. Added cubic-spline interpolation ofsource_pfrom the perturbation k-grid to a fine log-uniform k-grid (n_k_fine=2000default), mirroring the pattern already used bycompute_cls_all_fastfor scalar T,E.Both bugs are necessary and together sufficient — fixing only the kernel leaves 30-40% residuals at l ≈ 80-200; fixing only the k-grid leaves the kernel-form pathology (a spurious peak at the same scales).
Validation
tests/test_tensor.py::TestClBB::test_cl_bb_vs_class— ratio band tightened from[0.05, 20.0]to[0.95, 1.05]at l=2,10. Pass at the test's reducedTENSOR_PREC(l_max_g=10, k/decade=10, rtol=1e-3): l=2 → 0.993, l=10 → 1.006.At production precision (l_max_g=30, 40 k/decade, rtol=1e-6, n_k_fine=2000), clax/CLASS BB ratios:
Sub-percent across l ≤ 200; ~2% at l = 300.
Why this didn't show up before
tests/test_tensor.pyonly checked0.05 < ratio < 20.0— wide enough to pass even with the 22× kernel error at l=80-150.n_k_finesource interpolation was already in place for scalar T,E incompute_cls_all_fast—compute_cl_bbhad been written before that pattern was generalized.API
compute_cl_bbnow takes an additional keyword-onlyn_k_fine=2000argument. Existing positional callerscompute_cl_bb(tpt, params, bg, l_values)continue to work unchanged.Blast radius
grep -rn "compute_cl_bb\|from clax.harmonic" tests/showscompute_cl_bbis consumed only bytests/test_tensor.py. Other tests intest_harmonic.pyandtest_high_l.pyusecompute_cl_tt,compute_cl_ee,compute_cl_te,compute_cls_all— all unaffected by this change.Test plan
tests/test_tensor.py::TestClBB— all 4 tests pass at TENSOR_PREC (28s).test_cl_bb_vs_classto ±5% — passes at l=2,10.tests/test_tensor.py tests/test_harmonic.py tests/test_lensing.py tests/test_constants.py tests/test_interpolation.py tests/test_background.py tests/test_thermodynamics.py tests/test_nonlinear.py tests/test_end_to_end.py --fast -x): 20 tests passed, 0 failures before the run was interrupted on a slow fixture; no regression observed.Files
clax/harmonic.py— rewrittencompute_cl_bb(kernel + fine-k).tests/test_tensor.py— tightenedtest_cl_bb_vs_classratio band.README.md— updated the "Known limitations" line for primordial BB.CHANGELOG.md— entry under May 4, 2026.