From 74bb9498b4e97aca9c14d75e04688ae42a1bbd24 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 22 May 2026 09:43:42 +0000 Subject: [PATCH 1/2] Use truncnorm for bounded Gaussian prior --- src/pint/models/priors.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/pint/models/priors.py b/src/pint/models/priors.py index aa4b68ed0d..7bcf0e3f2d 100644 --- a/src/pint/models/priors.py +++ b/src/pint/models/priors.py @@ -8,7 +8,7 @@ """ import numpy as np -from scipy.stats import rv_continuous, uniform +from scipy.stats import rv_continuous, truncnorm, uniform class Prior: @@ -145,10 +145,9 @@ def GaussianBoundedRV(loc=0.0, scale=1.0, lower_bound=-np.inf, upper_bound=np.in upper_bound : number Upper bound of allowed parameter range - Returns a frozen rv_continuous instance with a gaussian probability - inside the range lower_bound to upper_bound and 0.0 outside + Returns a frozen truncnorm instance with a gaussian probability inside + the range lower_bound to upper_bound and 0.0 outside """ ymin = (lower_bound - loc) / scale ymax = (upper_bound - loc) / scale - n = GaussianRV_gen(name="bounded_gaussian", a=ymin, b=ymax) - return n(loc=loc, scale=scale) + return truncnorm(a=ymin, b=ymax, loc=loc, scale=scale) From a54bc48713e5f050e4ec9e20fa9b3accac50c293 Mon Sep 17 00:00:00 2001 From: Dotan Gazith Date: Sun, 31 May 2026 11:49:50 +0200 Subject: [PATCH 2/2] added changelog, modified and added to `test_gaussian_bounded` --- CHANGELOG-unreleased.md | 1 + tests/test_priors.py | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/CHANGELOG-unreleased.md b/CHANGELOG-unreleased.md index 90fd8b64e0..b03627e820 100644 --- a/CHANGELOG-unreleased.md +++ b/CHANGELOG-unreleased.md @@ -24,4 +24,5 @@ the released changes. - Make `get_prefix_timeranges` work for SWX. - Some of the `gridutils` functions had improper logging behavior - Fixed bug in changing epoch for ELL1k model +- Fixed bug in `GaussianRV_gen`, where the probability distribution function was not normalized correctly. Changed to use `scipy.stats.truncnorm` instead of the custom `GaussianRV_gen`. ### Removed diff --git a/tests/test_priors.py b/tests/test_priors.py index 0f6b8a64d8..9d6128b50d 100644 --- a/tests/test_priors.py +++ b/tests/test_priors.py @@ -72,15 +72,17 @@ def test_inclination_prior(self): def test_gaussian_bounded(self): print("test_gaussian_bounded") self.m.M2.prior = Prior( - GaussianBoundedRV(loc=0.26, scale=0.10, lower_bound=0.0, upper_bound=0.6) + GaussianBoundedRV(loc=0.26, scale=0.20, lower_bound=0.06, upper_bound=0.46) ) assert self.m.M2.prior_pdf(-0.1) == 0.0 assert self.m.M2.prior_pdf(0.7) == 0.0 assert self.m.M2.prior_pdf(-0.1, logpdf=True) == -np.inf assert self.m.M2.prior_pdf(0.7, logpdf=True) == -np.inf assert np.isclose( - self.m.M2.prior_pdf(0.26 + 0.1) / self.m.M2.prior_pdf(0.26), + self.m.M2.prior_pdf(0.26 + 0.2) / self.m.M2.prior_pdf(0.26), 0.60653065971263342, ) # Test that integral is 1.0, not safe since using _rv private var assert np.isclose(self.m.M2.prior._rv.cdf(0.6), 1.0) + # Test that integral is 0.5 at the mean, not safe since using _rv private var + assert np.isclose(self.m.M2.prior._rv.cdf(0.26), 0.5)