Skip to content

FEAT: log densities for the smoothed and spline models - #148

Merged
ColmTalbot merged 1 commit into
ln-mass-modelsfrom
ln-smoothed-models
Oct 23, 2026
Merged

ColmTalbot merged 1 commit into
ln-mass-modelsfrom
ln-smoothed-models

Conversation

@ColmTalbot

Copy link
Copy Markdown
Owner

BaseSmoothedMassDistribution gains ln_prob, ln_p_m1, ln_p_q and ln_smoothing, and an ln_primary_model class attribute, the log of primary_model taking the same arguments (the log of primary_model if unset); its four subclasses and InterpolatedPowerlaw set it. The terms for each sample are computed in log space, the smoothing window as a log-sigmoid, -logaddexp(0, exponent); the normalizations are computed as for call. InterpolatedNoBaseModelIdentical, and so the spline spin models, gains ln_prob, ln_p_x_identical and ln_p_x_unnormed: the spline perturbation minus the log of its normalization.

The linear path is refactored, with identical values:

  • the mmin/mmax check moves to _check_mass_limits, and its error messages are now f-strings (they were printed unformatted);
  • the cached mass ratio normalization moves to _norm_p_q_at, shared by p_q and ln_p_q, which also re-caches when the cached interpolant's shape differs from mass_1 (before, a shape that still broadcast silently used stale normalizations);
  • the smoothing exponent moves to _smoothing_exponent.

In a float32 survey (10k samples x 10 draws per model), the points lost to underflow drop from 0.3-2.6% to almost none. The exception is primary masses within ~delta_m / 87 of mmin, where the linear mass ratio normalization itself underflows (7 of 500k points, all with ln p below -170). Close to mmin the float32 error grows like (ln p)^2, as the smoothing is evaluated at m - mmin; it is below 5e-3 where ln p > -84.

BaseSmoothedMassDistribution gains ln_prob, ln_p_m1, ln_p_q and
ln_smoothing, and an ln_primary_model class attribute, the log of
primary_model taking the same arguments (the log of primary_model if
unset); its four subclasses and InterpolatedPowerlaw set it. The terms
for each sample are computed in log space, the smoothing window as a
log-sigmoid, -logaddexp(0, exponent); the normalizations are computed
as for __call__. InterpolatedNoBaseModelIdentical, and so the spline
spin models, gains ln_prob, ln_p_x_identical and ln_p_x_unnormed: the
spline perturbation minus the log of its normalization.

The linear path is refactored, with identical values:
- the mmin/mmax check moves to _check_mass_limits, and its error
  messages are now f-strings (they were printed unformatted);
- the cached mass ratio normalization moves to _norm_p_q_at, shared by
  p_q and ln_p_q, which also re-caches when the cached interpolant's
  shape differs from mass_1 (before, a shape that still broadcast
  silently used stale normalizations);
- the smoothing exponent moves to _smoothing_exponent.

In a float32 survey (10k samples x 10 draws per model), the points lost
to underflow drop from 0.3-2.6% to almost none. The exception is primary
masses within ~delta_m / 87 of mmin, where the linear mass ratio
normalization itself underflows (7 of 500k points, all with ln p below
-170). Close to mmin the float32 error grows like (ln p)^2, as the
smoothing is evaluated at m - mmin; it is below 5e-3 where ln p > -84.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ColmTalbot
ColmTalbot added this pull request to stack #147 October 23, 2026 18:03
@codecov

codecov Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.50562% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 95.61%. Comparing base (5dfefe4) to head (c8f2e74).

Files with missing lines Patch % Lines
gwpopulation/models/mass.py 94.52% 4 Missing ⚠️
Additional details and impacted files
@@                Coverage Diff                 @@
##           ln-mass-models     #148      +/-   ##
==================================================
+ Coverage           95.50%   95.61%   +0.11%     
==================================================
  Files                  14       14              
  Lines                1156     1230      +74     
==================================================
+ Hits                 1104     1176      +72     
- Misses                 52       54       +2     
Flag Coverage Δ
python3.10 95.61% <95.51%> (+0.11%) ⬆️
python3.11 95.61% <95.51%> (+0.11%) ⬆️
python3.12 95.61% <95.51%> (+0.11%) ⬆️
python3.13 95.61% <95.51%> (+0.11%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ColmTalbot
ColmTalbot merged commit 2370ec9 into main Oct 23, 2026
16 checks passed
@ColmTalbot
ColmTalbot deleted the ln-smoothed-models branch October 23, 2026 18:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant