-
-
Notifications
You must be signed in to change notification settings - Fork 11.2k
BUG: random: Fix handling of very small p in Generator.binomial. #29522
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
Conversation
Use `log1p` to compute an expression of `1 - p` with more precision. To not change the legacy interface, a copy of the unaltered function `random_binomial_inversion` is used in the legacy distributions code.
Hmmm... why are we flagging an expression such as FWIW, chatgpt gets it right: Question:
Answer: According to PEP 8, the expression: [... yada yada ...] Final answer:
|
ruff rule E226 isn't smart enough to understand the nuance of PEP 8's whitespace recommendations. Any objections to disabling it? |
IMO the version ruff suggests is clearer. I agree we're being more strict than PEP 8 but IMO that isn't a reason not to format stuff in a uniform way. I also sometimes am annoyed by formatting choices in codebases but then I remember how much time I'm saving not having nitpicky arguments about formatting in code review or getting confused by different formatting conventions in different parts of a codebase. |
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.
I don't know the underlying statistics well enough to review the math but the implementation looks fine to me.
I'm fine with following guidelines that I don't necessarily agree with that have been adopted by the community, but I guess I'm not sure the current behavior was explicitly agreed to by the core devs (my activity waxes and wanes, so I might have missed an essential discussion), or is something that we ended up with accidentally, because it just happened to be the ruff default. If everyone really prefers "whitespace around all the operators"--e.g. (B u t t o m e , i t i s j u s t t o o m u c h w h i t e s p a c e !) |
IIRC, it came in when I committed the reformatting :) We are slowly working our way towards automatic formatting compatibility. If the original formatting is clearly better than the replacement, exceptions can be added. |
I don't mind too much either way - happy to defer to Warren's tastes on formatting math expressions :) @WarrenWeckesser would you mind opening a separate PR for the ruff config change? And maybe ping @jorenham and @DimitriPapadopoulos since they did the bulk of the formatting PRs. |
I'm OK with it. While I like this rule in the context of smaller projects with fewer calculations, I tend to agree it might not fit projects such as NumPy. I just used the default rule set here, and probably shouldn't have. |
Another hmmm... Perhaps someday--maybe someday soon!--we'll use a clever LLM for linting, and it will "understand" the readability talked about in PEP 8 well enough to know that, say, So I will conform, and not lead the revolt against E226. |
Thanks Warren. |
Use
log1p
to compute an expression of1 - p
with more precision. To not change the legacy interface, a copy of the unaltered functionrandom_binomial_inversion
is used in the legacy distribution code.This example is from the unit test added in the PR. Before this fix, or when using the legacy interface, the values in
x
will all be 0:With the fixed
Generator.binomial
method: