-
Notifications
You must be signed in to change notification settings - Fork 3
fix(families): raise ValueError for x outside support in exponential and uniform #118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -272,8 +272,7 @@ def _base_score(parameters: Parametrization, x: NumericArray) -> NumericArray: | |
| ∂/∂a log f = 1/(b - a) | ||
| ∂/∂b log f = -1/(b - a) | ||
|
|
||
| For points outside the support, the gradient is set to 0 (since density is zero, | ||
| but the score is typically considered undefined; we return 0 for numerical safety). | ||
| For points outside the support, there is ValueError. | ||
|
|
||
| Parameters | ||
| ---------- | ||
|
|
@@ -291,11 +290,12 @@ def _base_score(parameters: Parametrization, x: NumericArray) -> NumericArray: | |
| params = cast(_Standard, parameters) | ||
| a = params.lower_bound | ||
| b = params.upper_bound | ||
| width = b - a | ||
| if np.any((x < a) | (x > b)): | ||
| raise ValueError(f"Score is undefined for x outside support [{a}, {b}]. Got x = {x}") | ||
|
Comment on lines
+293
to
+294
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This support check does not reject |
||
|
|
||
| inside = (x >= a) & (x <= b) | ||
| grad_a = np.where(inside, 1.0 / width, 0.0) | ||
| grad_b = np.where(inside, -1.0 / width, 0.0) | ||
| width = b - a | ||
| grad_a = np.full_like(x, 1.0 / width) | ||
| grad_b = np.full_like(x, -1.0 / width) | ||
|
Comment on lines
+297
to
+298
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| return np.stack([grad_a, grad_b], axis=-1) | ||
|
|
||
| Uniform = ParametricFamily( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -438,6 +438,9 @@ def score(self, parameters: Parametrization, x: NumericArray) -> NumericArray: | |
| NumericArray | ||
| Gradient with respect to the parameters of the given parametrization. | ||
| Shape is (..., d), where d is the number of parameters of the parametrization. | ||
|
|
||
| ValueError | ||
| If any value in `x` lies outside the distribution's support. | ||
|
Comment on lines
+442
to
+443
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be documented under a NumPy-style |
||
| """ | ||
| if self._base_score is None: | ||
| raise ValueError( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check only rejects
x < 0, but the family support excludes non-finite values as well. For example,ContinuousSupport(left=0.0).contains(np.inf)is false, whilescore([np.inf])currently returns-infinstead of raising. Reject non-finitexvalues, ideally with a test fornp.infandnp.nan.