-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
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
fix moments for invgamma #2849
fix moments for invgamma #2849
Conversation
n-th moment only exists for n < a.
my guess is that the first moments should be inf and not nan |
also .stats has explicit formulas, see wikipedia |
OK, yes, makes sense: it's definitely a positive infinity. Same can be sensibly argued for skew/kurtosis (the higher the moment, the faster the divergence). Have just pushed the commit with explicit stats and slightly better tests. |
the pattern in other distributions is stats = but I never checked the details there (what's skew and kurtosis in weird cases) |
Okay, the last commit follows the pattern (whether it's possible / is worth it to enforce it across the board is a separate question; but here it's easy). |
def _munp(self, n, a): | ||
return exp(gamln(a-n) - gamln(a)) | ||
def _stats(self, a, moments='mvsk'): | ||
a1, a2, a3, a4 = a - 1., a -2., a - 3., a - 4. |
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.
one missing space a - 2 :)
looks good now |
Here's a problem:
|
@WarrenWeckesser IMO, this looks like a generic problem not of invgamma Does this work with all distributions? I never systematically checked broadcasting in |
Is it a conscious design decision to raise warnings when an E.g. (with numpy 1.7.1):
(A problem with this design is that the decision is being left to whatever numpy decides to do. The default behavior of numpy has changed over the years, and in fact it can depend on the platform.) If the function is documented to return |
@josef-pkt: I haven't checked lately, but I'm pretty sure that not all the distributions' |
separate issue/bug in my reading: |
But
So, does not seem to be consistent. Also, all the errors I've seen so far (haven't done a systematic sweep) seem to come from the generic part of the |
I agree. We should silence warnings (or not create them in the first place) in these cases. |
@WarrenWeckesser can you open a new issue with some examples for vectorized/broadcasting in .stats? |
@WarrenWeckesser (warnings) No, not a conscious decsion, just reliance on numpy. There's not much more to the warning-generating code than what's printed in the traceback. It's literally Now, I would argue that relying on numpy is a good thing here. It might not be consistent across numpy installations, but it's consistent across code paths and scripts on any given machine. IMO all divisions by zero should be handled in a uniform way, so that a user can control them all at once. |
@WarrenWeckesser: IMHO, if inf/nan are expected possible results, there should be no warnings. Also, for the practical reasons you state, the warnings generated by Numpy are probably not very useful in any case. |
I don't understand the failure from reading the code |
@josef-pkt |
|
Here:
|
@EvgeniBurovski wrote
Not if the division by zero is an implementation detail, which is the case here. For example, the formula for the mean, |
@josef-pkt oops, scratch that.
|
This looks good, however I don't see where the generic code get's the wrong shape (broadcast error) in Warren's example. (scale or loc is (2,) ? sounds weird) But this is not a problem of invgamma in this PR. the ._stats return looks good |
back to my earlier bug: argsreduce is after the call to |
argsreduce is used in all methods to screen out invalid arguments and to broadcast the valid arguments to have common shape before calling the private methods. In this case a=0 is removed (we set nans in the generic part), the two valid arguments together with loc and scale are broadcasted together. In this case, the usage of argsreduce after the call to ._stats is screwed up. |
@josef-pkt [yes, took me half an hour more (and a debugger) to figure that out :-(]. A matter for a separate issue/PR/ a slew of test? |
Yes, but as in your example and what Warren mentioned, there are also other problems before stats really works vectorized. |
The last commit avoids division-by-zero warnings. |
So separate PR needed for |
Yes, good to merge. |
Yesterday I've erroneously reported that gh-1866 has been fixed. This patch actually fixes it.