-
Notifications
You must be signed in to change notification settings - Fork 4
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 rand
safe version of Poisson and Negative binomial distributions
#418
Conversation
Try this Pull Request!Open Julia and type: import Pkg
Pkg.activate(temp=true)
Pkg.add(url="https://github.com/CDCgov/Rt-without-renewal", rev="safe-discrete-dists", subdir="EpiAware")
using EpiAware |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## remove-nan-handling #418 +/- ##
=======================================================
- Coverage 93.71% 88.67% -5.05%
=======================================================
Files 54 56 +2
Lines 557 715 +158
=======================================================
+ Hits 522 634 +112
- Misses 35 81 +46 ☔ View full report in Codecov by Sentry. |
Benchmark resultJudge resultBenchmark Report for /home/runner/work/Rt-without-renewal/Rt-without-renewalJob Properties
ResultsA ratio greater than
Benchmark Group ListHere's a list of all the benchmark groups executed by this job:
Julia versioninfoTarget
Baseline
Target resultBenchmark Report for /home/runner/work/Rt-without-renewal/Rt-without-renewalJob Properties
ResultsBelow is a table of this job's results, obtained by running the benchmarks.
Benchmark Group ListHere's a list of all the benchmark groups executed by this job:
Julia versioninfo
Baseline resultBenchmark Report for /home/runner/work/Rt-without-renewal/Rt-without-renewalJob Properties
ResultsBelow is a table of this job's results, obtained by running the benchmarks.
Benchmark Group ListHere's a list of all the benchmark groups executed by this job:
Julia versioninfo
Runtime information
|
Note that this is branched from We can either address that with a new issue, or use that as an opportunity to test using the new dists? |
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 think this looks good and I think we should replace our usage everywhere with this (so that i.e Documenter passes). I also think we should push ideas about reducing copy and paste here into a issue but definitely think it should be investigated as it should help with our partial pooling problems
This needs a benchmark before we merge it. |
If we call |
I got irritated by a stochastic CI failure on how different the sample variance is from sampling from |
Benchmark resultJudge resultBenchmark Report for /home/runner/work/Rt-without-renewal/Rt-without-renewalJob Properties
ResultsA ratio greater than
Benchmark Group ListHere's a list of all the benchmark groups executed by this job:
Julia versioninfoTarget
Baseline
Target resultBenchmark Report for /home/runner/work/Rt-without-renewal/Rt-without-renewalJob Properties
ResultsBelow is a table of this job's results, obtained by running the benchmarks.
Benchmark Group ListHere's a list of all the benchmark groups executed by this job:
Julia versioninfo
Baseline resultBenchmark Report for /home/runner/work/Rt-without-renewal/Rt-without-renewalJob Properties
ResultsBelow is a table of this job's results, obtained by running the benchmarks.
Benchmark Group ListHere's a list of all the benchmark groups executed by this job:
Julia versioninfo
Runtime information
|
Benchmark resultJudge resultBenchmark Report for /home/runner/work/Rt-without-renewal/Rt-without-renewalJob Properties
ResultsA ratio greater than
Benchmark Group ListHere's a list of all the benchmark groups executed by this job:
Julia versioninfoTarget
Baseline
Target resultBenchmark Report for /home/runner/work/Rt-without-renewal/Rt-without-renewalJob Properties
ResultsBelow is a table of this job's results, obtained by running the benchmarks.
Benchmark Group ListHere's a list of all the benchmark groups executed by this job:
Julia versioninfo
Baseline resultBenchmark Report for /home/runner/work/Rt-without-renewal/Rt-without-renewalJob Properties
ResultsBelow is a table of this job's results, obtained by running the benchmarks.
Benchmark Group ListHere's a list of all the benchmark groups executed by this job:
Julia versioninfo
Runtime information
|
Documenter CI works now. To recap:
|
Benchmark resultJudge resultBenchmark Report for /home/runner/work/Rt-without-renewal/Rt-without-renewalJob Properties
ResultsA ratio greater than
Benchmark Group ListHere's a list of all the benchmark groups executed by this job:
Julia versioninfoTarget
Baseline
Target resultBenchmark Report for /home/runner/work/Rt-without-renewal/Rt-without-renewalJob Properties
ResultsBelow is a table of this job's results, obtained by running the benchmarks.
Benchmark Group ListHere's a list of all the benchmark groups executed by this job:
Julia versioninfo
Baseline resultBenchmark Report for /home/runner/work/Rt-without-renewal/Rt-without-renewalJob Properties
ResultsBelow is a table of this job's results, obtained by running the benchmarks.
Benchmark Group ListHere's a list of all the benchmark groups executed by this job:
Julia versioninfo
Runtime information
|
p = μ / (μ + ex_σ²) | ||
r = μ² / ex_σ² | ||
return NegativeBinomial(r, p) | ||
σ² = μ + α * μ² |
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.
lol woopps
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.
Nice looks good. As we discussed its a lot of copy and paste so ideally we should have an issue to track changes we can make to reduce this (either here or upstream).
* remove nan handling * get rid of all NB padding and clamping * remove nan handling * get rid of all NB padding and clamping * remove overflow test * Add `rand` safe version of Poisson and Negative binomial distributions (#418) * SafePoisson with safety for large means * better selection for conversion to Int or BigInt * add SafeNegativeBinomial * add unit tests to doctests * reformat * Add type promotion so AD works with distribution constructor * Add logpdf grad call unit tests for Safe discrete dists * reformat * change neg bin param to (r, p) * Update utils.jl * reformat * change empirical var test to more principled approach * add default nadapts rather than just 50% of target sampling * Update NUTSampler.jl * set dist check_args = false * Set nadapts to Turing Default * reformat --------- Co-authored-by: Samuel Brand <[email protected]>
This PR contributes a possible solution to #415 by creating distributions that work with
Distributions
but don't trigger aInexactError
whenrand
is called on them with a large mean value (noting that a large mean value can triggered by random walks on the log-mean). These distributions are:SafePoisson
SafeNegativeBinomial
Largely this was boilerplate (if we do more of this we should consider a macro to avoid this), with the difference that I replace
floor(Int, x)
calls with a helper function that determines whetherfloor(BigInt, x)
should be used rather than throwing anInexactError
.Notable features/limitations
PoissonRandom
after checking that its still faster thanDistribution.Poisson
sampling.SafeNegativeBinomial
I'm using the parameterisation with meanMy reasoning here is in the doc string; I think this is the easiest parameterisation to reason on priors because$\alpha \approx \text{std} / \text{mean}$ when the mean is larger than $1/ \alpha$ . This differs from
NegativeBinomialMeanClust
so I'm happy to change.ReverseDiff
andForwardDiff
to the test env. This was because we need to be sure that we can backprop throughlogpdf
calls withSafePoisson
orSafeNegativeBinomial
distribution structs. I've added unit tests here with very large mean values to check validity in extreme parameter regimes.rand
calls are slower to these distributions (but not done large formal analysis).NegativeBinomialMeanClust
.I know @damonbayer has been interested in Neg bin safety too.