-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Improve ExGaussian logcdf and refactor logp #4407
Merged
Merged
Conversation
This file contains 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
ricardoV94
changed the title
- Improve ExGaussian logcdf and refactor logp
Improve ExGaussian logcdf and refactor logp
Jan 5, 2021
pre-commit is failing with:
Should it really be removed? |
Codecov Report
@@ Coverage Diff @@
## master #4407 +/- ##
==========================================
+ Coverage 87.91% 87.98% +0.07%
==========================================
Files 88 88
Lines 14331 14325 -6
==========================================
+ Hits 12599 12604 +5
+ Misses 1732 1721 -11
|
Yes you should remove the unused import. |
junpenglao
approved these changes
Jan 5, 2021
ricardoV94
force-pushed
the
ExGaussian_issue
branch
from
January 5, 2021 16:02
2a12ce7
to
b49ba9f
Compare
twiecki
approved these changes
Jan 5, 2021
twiecki
reviewed
Jan 5, 2021
@@ -1851,6 +1854,9 @@ def test_ex_gaussian(self, value, mu, sigma, nu, logp): | |||
(15.0, 5.000, 7.500, 7.500, -0.4545255), | |||
(50.0, 50.000, 10.000, 10.000, -1.433714), | |||
(1000.0, 500.000, 10.000, 20.000, -1.573708e-11), | |||
(0.01, 0.01, 100.0, 0.01, -0.69314718), # Fails in scipy version | |||
(-0.43402407, 0.0, 0.1, 0.1, -13.59615423), # Previous 32-bit version failed here | |||
(-0.72402009, 0.0, 0.1, 0.1, -31.26571842), # Previous 64-bit version failed here |
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.
These tests are really neat! 💪
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.
This PR fixes #4295
logcdf
The logcdf method was reliably returning nan for some configurations due to numerical inaccuracies in the
std_cdf
method. By replacing it with the more numerical stablenormal_lcdf
the problems seem to vanish.In addition the method was originally written in the natural scale and only at the end converted to log, but it was straightforward to rewrite it in log terms, which simplifies things quite a bit, and further allows for the use of
logdiffexp
, which should provide further numerical stability.I added unit tests on values that were systematically failing in the 32- and 64-bit implementations. I also added one test that fails with the scipy
exponnorm
implementation, which is numerically unstable in different ways.logp
I thought it would be nice to also add the
normal_lcdf
to the logp method. This seems to not only solve the issues that were addressed by #4050 (method would wrongly return-inf
for several configurations), but it is also more numerically accurate (using the R implementation as the benchmark).I added a unittest based on the output of the
gamlss::exGAUS
R function that fails on master but not in this PR. I also added two tests that fail with the scipyexponnorm
implementation, which is numerically unstable in different ways.The only downside, is that the
normal_lcdf
relies ontt.erfcx
which does not havec_code
support (as mentioned by warnings in the tests). However, since the same function is being used in the logp of theTruncatedNormal
, I thought it would be okay to use it here as well. Maybe we can addc_code
support fortt.erfcx
in the future?Indirect benefit
The code for the logp and logcdf methods look much more clean now, and it is much easier to see the similarities and differences between them!