fix(random): correct six distribution bugs found in audit#211
Merged
Conversation
Audit of brainstate.random surfaced six reachable correctness bugs, each
contradicting the function's own NumPy-style docstring. All are fixed with
test-first regression coverage (TestAuditRegressions in _fun_test.py).
- standard_t: with array `df` and `size=None`, output shape was always ()
(dead `u.math.shape(size)` branch) and raised ValueError. Now infers the
shape from `df`, matching the sibling `t` and the docstring.
- weibull_min: divided by `scale` instead of multiplying. Now `r * scale`,
matching scipy.stats.weibull_min and the `weibull` docstring (lambda scale).
- triangular: was `2*bernoulli-1` (Rademacher ±1) with a size-only signature,
so the documented `triangular(-3, 0, 8, N)` raised TypeError. Reimplemented
as the real triangular(left, mode, right, size) via inverse-CDF, with
shared-unit support like `uniform`.
- geometric: off-by-one (support {0,1,...} instead of {1,2,...}) and returned
float. Now `floor(...) + 1` cast to an integer dtype, so P(k==1)==p.
- randint_like: default `high = max(input)` used the Python builtin and raised
on >1-D templates. Now uses `u.math.max`.
- chisquare: summed `df` squared normals, rejecting non-integer scalar `df`
(TypeError) and array `df` with `size=None` (NotImplementedError). Now uses
the `2 * Gamma(df/2)` relation, valid for any positive real / array `df`.
Tests encoding the old buggy behavior (triangular ±1, chisquare
NotImplementedError) are updated to assert the corrected contracts.
Contributor
There was a problem hiding this comment.
Sorry @chaoming0625, you have reached your weekly rate limit of 500000 diff characters.
Please try again later or upgrade to continue using Sourcery
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Merged
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
Audited
brainstate.randomfor errors, bugs, and edge cases. Found six reachable correctness bugs, each contradicting the function's own NumPy-style docstring. All are fixed and covered by test-first regression tests (TestAuditRegressionsin_fun_test.py), with statistical checks gated behind--run-slow.standard_tdf+size=NoneraisedValueError(deadu.math.shape(size)branch always yielded())df(matches siblingt+ docstring)weibull_minscaleinstead of multiplyingr * scale(matchesscipy.stats.weibull_min+weibulldocstring)triangular2*bernoulli-1(Rademacher ±1) with a size-only signature; the documentedtriangular(-3, 0, 8, N)raisedTypeErrortriangular(left, mode, right, size)via inverse-CDF, with shared-unit support likeuniformgeometric{0,1,…}not{1,2,…}) and returned floatfloor(…) + 1, cast to integer dtype, soP(k==1)==prandint_likehigh = max(input)(Python builtin) raised on >1-D templatesu.math.maxchisquaredfsquared normals — rejected non-integer scalardf(TypeError) and arraydfwithsize=None(NotImplementedError)2 * Gamma(df/2), valid for any positive real / arraydf(mirrorst,noncentral_chisquare)Tests that encoded the old buggy behavior (triangular ±1; chisquare
NotImplementedError) are updated to assert the corrected contracts.Out of scope (documented in audit, not changed here)
ir_compilation=Truebreaks everynamed_scope-decorated method (evennormal) becausestatic_argnumsindices referencesize/dtype/keypositions passed as kwargs. This is a transform-layer concern (brainstate/transform/_named_scope.py+jit), off by default, with no random test coverage — a fix belongs there with its own tests.seed_contextclaims it touches NumPy state;__init__.pyshuffleexample implies in-place mutation).Testing
pytest brainstate/random/ --run-slow→ 539 passed.brainstate/random/references the changed functions.🤖 Generated with Claude Code