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
Handmade distributions have the option not to check for validity #20120
Conversation
✅ Hi, I am the SymPy bot (v161). I'm here to help you write a release notes entry. Please read the guide on how to write release notes.
Click here to see the pull request description that was parsed.
|
Codecov Report
@@ Coverage Diff @@
## master #20120 +/- ##
=============================================
- Coverage 75.854% 75.847% -0.008%
=============================================
Files 671 671
Lines 173815 173788 -27
Branches 41046 41065 +19
=============================================
- Hits 131847 131814 -33
+ Misses 36221 36220 -1
- Partials 5747 5754 +7 |
Thanks for the PR! |
|
@czgdp1807 @oscarbenjamin Please have a look if this can be merged. |
sympy/stats/drv_types.py
Outdated
@@ -74,6 +75,14 @@ def DiscreteRV(symbol, density, set=S.Integers): | |||
set : set | |||
Represents the region where the pdf is valid, by default is real line. | |||
|
|||
Other Parameters | |||
================ |
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.
We shouldn't have a section called "Other parameters".
The expected sections are listed here:
https://docs.sympy.org/latest/documentation-style-guide.html#docstring-sections
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 have added it under the "Parameters" section in that case.
I found an error in the guide: they state that one needs an extra section for Returns
but Returns
is not part of the list of the valid sections. Probably not a big deal though.
I'm not sure it's necessary to have a check at all but if it is then shouldn't we just fix the check so it doesn't incorrectly return False? We could use e.g. |
I'll add that as well. But I fear that for some complicated distributions, Edit: Never mind. I see that you changed the condition now. But maybe |
It's probably better not to use integrate by default anyway. Checking should be something that a user chooses to do if it isn't guaranteed to be cheap. Or perhaps there could be an API that "normalises" a pdf to produce a distribution. |
This is the goal of this PR. My handmade distributions had this problem.
I think this could be done by the users on their own since its only 1 line of work: |
Then we could just remove the checking. I'd rather do that than have a "check" argument. |
What if the default was not to check? I'm sure some users indirectly rely on the check. |
How would they rely on the check? Doesn't it just throw an exception (incorrectly some of the time). |
One uses of a custom RV is so that a developer can design an API for clients that can input their own distributions. The developer might assume that these checks are done by sympy and has a try-catch ready for incorrect user input. If we change this, their code might break since their functions are running with an invalid distribution. Even though the checks fail for complicated distributions, they don't fail for most of the usual ones. |
Is that a hypothetical use case or do you know of anyone doing that? I don't think we need to worry about checking. It should be up to users to pass in valid input because sympy can not check that in general. We can provide a function for checking or otherwise an API that explicitly states that it will normalise a pdf. |
I am the only one I know doing this. This is how I found out about the problem. I can add that extra normalising functionality as an extra keyword arg in this PR. I can also set the default for the check to false. Should I do these two things? |
I'll let someone more familiar with stats and how it is used comment. My views here are just in general for sympy:
|
Agreed. |
@@ -355,7 +355,7 @@ def compute_moment_generating_function(self, **kwargs): | |||
""" | |||
x, t = symbols('x, t', real=True, cls=Dummy) | |||
pdf = self.pdf(x) | |||
mgf = integrate(exp(t * x) * pdf, (x, -oo, oo)) | |||
mgf = integrate(exp(t * x) * pdf, (x, self.set)) |
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.
+1
LGTM. Will merge tomorrow if no objections. |
References to other Issues or PRs
Fixes #19959
Brief description of what is fixed or changed
(stats module)
Some handmade distributions raised errors saying that the density function did not integrate to 1. This was simply because the
integrate
function did not integrate to 1 exactly, but rather an expression that is simplifies to 1. I have given the option for the user to allow SymPy to check the validity.Other comments
On request, I will add similar functionality for
drv_types.py
,frv_types.py
and possibly some vector and matrix handmade distributions.I can also add a
.simplify()
after integrating.Release Notes
NO ENTRY