Skip to content
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

Add rrule for logpdf of NegativeBinomial #1568

Closed
wants to merge 16 commits into from

Conversation

simsurace
Copy link
Contributor

This needs to be tested, and not entirely sure about the edge case.

Copy link
Member

@devmotion devmotion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add tests with ChainRulesTestUtils.test_rrule? It should cover in-support, out-support values, and the special case.

@codecov-commenter
Copy link

codecov-commenter commented Jun 22, 2022

Codecov Report

Merging #1568 (be49b02) into master (6ab4c1f) will increase coverage by 0.00%.
The diff coverage is 85.71%.

@@           Coverage Diff           @@
##           master    #1568   +/-   ##
=======================================
  Coverage   85.52%   85.52%           
=======================================
  Files         128      128           
  Lines        7861     7882   +21     
=======================================
+ Hits         6723     6741   +18     
- Misses       1138     1141    +3     
Impacted Files Coverage Δ
src/univariate/discrete/negativebinomial.jl 90.54% <85.71%> (-1.92%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6ab4c1f...be49b02. Read the comment docs.

simsurace and others added 2 commits June 22, 2022 14:59
Co-authored-by: David Widmann <[email protected]>
@simsurace
Copy link
Contributor Author

simsurace commented Jun 24, 2022

There are some errors in testing the rrule (as opposed to test failures). I'm not completely sure I'm using the test correctly, but the fact that the test is hitting negative parameters could be an issue of the finite differencing scheme, right?

@devmotion
Copy link
Member

Possibly. Probably you can fix these by using e.g. forward_fdm (https://github.com/JuliaDiff/FiniteDifferences.jl#dealing-with-singularities=) in the test_rrule (see e.g. https://github.com/JuliaStats/Distributions.jl/pull/1534/files).

@matbesancon
Copy link
Member

https://github.com/JuliaDiff/FiniteDifferences.jl#dealing-with-singularities
(the link had an equal at the end that broke it)

@simsurace
Copy link
Contributor Author

simsurace commented Jun 28, 2022

Tests for NegativeBinomial are passing locally.

EDIT: scratch that, apparently there are some parameters that still fail.

@simsurace
Copy link
Contributor Author

I managed to make all tests pass except the ones for p = 1 - eps(). I could not find any finite difference scheme that would give close-to-reasonable results. Maybe someone else knows how to fix this.

@simsurace
Copy link
Contributor Author

I made the following changes:

  • Use randomly sampled parameters
  • Include the r derivative in the tests for ForwardDiff
  • Test the edge case p=1, k=0 also for ForwardDiff
  • Compare the ForwardDiff to the finite difference result instead of the analytical formula

As it now turns out, the ForwardDiff derivative wrt. p for p=1 seems to be incorrect as well.
Do we need a diffrule for this?

Copy link
Member

@devmotion devmotion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not really happy with the changes of the tests. Generally, I think we should try to avoid making any unrelated changes also of the tests since that possibly unintentionally relaxes some existing tests and hence makes it easier for bugs to sneak in in the future.

My suggestion is to keep it really simple and just add some test_rrules that cover all branches. Due to the numerical issues of finite differencing we can't just not add it inside the loop for ForwardDiff. IMO that's fine, just test some other parameters in a separate loop (if you want to test multiple parameters).

The ForwardDiff issue might be fixed by JuliaDiff/ForwardDiff.jl#481 (assuming it's about a measure-zero branch). DiffRules won't help here since then the result will be non-deterministic: If users load ForwardDiff before Distributions, the rules won't be picked up by ForwardDiff.

@simsurace
Copy link
Contributor Author

Ok, I think I might have misunderstood your previous comment then:

I would prefer to use finite differencing in the tests - you don't want to use it in practice due to the numerical issues and inaccuracies but it's the simplest way to compute derivatives, and hence the one least likely to be systematically incorrect. ForwardDiff derivatives can be incorrect in some special cases but it can be tricky to notice and debug, and analytical derivatives can be wrong and were used to define the rules.

I took this to mean that while we're at it, we should use finite differencing also to test ForwardDiff against, given that previously it was tested against the analytical derivative, to have uniformity between ForwardDiff and the rrule.

The new ForwardDiff tests are more comprehensive because they now also cover edge cases and the derivative wrt. r. But they don't sample the more extreme values as well as the previous tests. Of course this is a consequence of changing to finite differencing to compare ForwardDiff results against.

Please let me know how you would like me to proceed. Some of the following options may not be independent of each other:

  • Would you like to keep the new parameter ranges for ForwardDiff, or have separate ranges for ForwardDiff and rrule?
  • Would you like to keep the new test of ForwardDiff for the derivative wrt. r?
  • Would you like to keep the new tests of ForwardDiff in the edge case?
  • Would you like to test ForwardDiff against analytical or finite differences?

@simsurace
Copy link
Contributor Author

simsurace commented Jul 1, 2022

I rearranged the tests. ForwardDiff is now tested against analytical derivatives and in the old parameters ranges as before, but additionally there are tests

  • In the edge case
  • For the derivative wrt. to r

As some of them fail for some parameters or for all parameters, they are marked with @test_skip or @test_broken, respectively.

Copy link
Member

@devmotion devmotion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that even the current iteration of the tests doesn't actually test all branches and contains changes unrelated to this PR. I thought maybe it's easier and faster if I implement directly what I had in mind, so I opened #1579 since I was not sure if you are OK with me pushing to this PR. I could also push my commit to this PR if you prefer. In any case I'd like to merge one of the PRs since I think the rrule is very useful 🙂

@simsurace
Copy link
Contributor Author

No worries, you can close this PR and merge the other one.

@simsurace
Copy link
Contributor Author

This is superseded by #1579

@simsurace simsurace closed this Jul 5, 2022
@simsurace simsurace deleted the patch-1 branch July 5, 2022 10:11
matbesancon pushed a commit that referenced this pull request Jul 8, 2022
* Add `rrule` for `logpdf` of `NegativeBinomial`

* Remove unnecessary module prefix

Co-authored-by: David Widmann <[email protected]>

* Use explicit division

Co-authored-by: David Widmann <[email protected]>

* Refator and correct pullback

* Add tests for rrule

* Use `forward_fdm` for testing `rrule`

* Fix tests

* Update test/negativebinomial.jl

* Fix tests (without `p = 1 - eps()`)

* Use FD for all tests, use random parameters

* Avoid type instability

Co-authored-by: David Widmann <[email protected]>

* Split and rearrange ForwardDiff and rrule tests

* Bump version

* Fix typo

* Clean tests (revert unrelated changes) and fix them

Co-authored-by: Simone Carlo Surace <[email protected]>
Co-authored-by: Simone Carlo Surace <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants