Skip to content

FEAT: log densities for the spin models - #145

Merged
ColmTalbot merged 1 commit into
mainfrom
ln-spin-models
Oct 23, 2026
Merged

ColmTalbot merged 1 commit into
mainfrom
ln-spin-models

Conversation

@ColmTalbot

Copy link
Copy Markdown
Owner

Add ln_ versions of gaussian_chi_eff, gaussian_chi_p, the beta spin magnitude models (iid and independent), the isotropic + Gaussian orientation mixtures and iid_spin, and GaussianChiEffChiP.ln_prob. hyperpe.Model uses these, so in single precision the densities no longer underflow to zero far from narrow peaks: in a float32 survey (10k samples x 10 draws per model), gaussian_chi_eff and GaussianChiEffChiP lost 6% of points to underflow and now lose none.

  • utils.ln_beta_dist, with beta_dist now exp(ln_beta_dist). It uses xlogy, so a Beta(1, b) density at a = 0 (and Beta(a, 1) at a = amax) is now finite rather than 0; elsewhere values agree to rounding.
  • utils.ln_unnormalized_2d_gaussian, the exponent of unnormalized_2d_gaussian, which is now its exp. GaussianChiEffChiP's log density is this minus the log of the unchanged grid normalization.
  • The orientation mixtures use logaddexp; as in the linear model, the isotropic component is not truncated.

test/ln_model_test.py checks each log density against the log of the density (numpy and jax backends) and -inf outside the support, and the jit-compiled float32 log density against float64 on the same rounded inputs. It also checks that the linear float32 density loses points for the hyperparameters used, so the comparison covers underflow.

Add ln_ versions of gaussian_chi_eff, gaussian_chi_p, the beta spin
magnitude models (iid and independent), the isotropic + Gaussian
orientation mixtures and iid_spin, and GaussianChiEffChiP.ln_prob.
hyperpe.Model uses these, so in single precision the densities no longer
underflow to zero far from narrow peaks: in a float32 survey (10k
samples x 10 draws per model), gaussian_chi_eff and GaussianChiEffChiP
lost 6% of points to underflow and now lose none.

- utils.ln_beta_dist, with beta_dist now exp(ln_beta_dist). It uses
  xlogy, so a Beta(1, b) density at a = 0 (and Beta(a, 1) at a = amax)
  is now finite rather than 0; elsewhere values agree to rounding.
- utils.ln_unnormalized_2d_gaussian, the exponent of
  unnormalized_2d_gaussian, which is now its exp. GaussianChiEffChiP's
  log density is this minus the log of the unchanged grid normalization.
- The orientation mixtures use logaddexp; as in the linear model, the
  isotropic component is not truncated.

test/ln_model_test.py checks each log density against the log of the
density (numpy and jax backends) and -inf outside the support, and the
jit-compiled float32 log density against float64 on the same rounded
inputs. It also checks that the linear float32 density loses points for
the hyperparameters used, so the comparison covers underflow.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.21%. Comparing base (4da2034) to head (2a87a52).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #145      +/-   ##
==========================================
+ Coverage   95.12%   95.21%   +0.10%     
==========================================
  Files          14       14              
  Lines        1085     1107      +22     
==========================================
+ Hits         1032     1054      +22     
  Misses         53       53              
Flag Coverage Δ
python3.10 95.21% <100.00%> (+0.10%) ⬆️
python3.11 95.21% <100.00%> (+0.10%) ⬆️
python3.12 95.21% <100.00%> (+0.10%) ⬆️
python3.13 95.21% <100.00%> (+0.10%) ⬆️

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 added this pull request to stack #147 October 23, 2026 17:55
@ColmTalbot
ColmTalbot merged commit e56105a into main Oct 23, 2026
9 checks passed
@ColmTalbot
ColmTalbot deleted the ln-spin-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