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

[On hold] New formulas for derivatives of neg_binomial_lpmf after alpha cutoff #1579

Conversation

martinmodrak
Copy link
Contributor

@martinmodrak martinmodrak commented Jan 3, 2020

Summary

Currently on-hold. If PR #1497 gets merged, neg_binomial_lpmf should probably be rewritten using the same approach

The formulas for derivatives of neg_binomial_lpmf after delegating to poisson (for alpha > 1e10) are replaced with Taylor expansions of the exact derivatives for alpha -> Inf obtained in Mathematica via:

     nb[n_,alpha_,beta_]:= LogGamma[n + alpha] - LogGamma[n + 1] - 
        LogGamma[alpha ] + alpha * Log[beta/ (1 + beta)] - n * Log[1 + beta];
      nbdalpha[n_,alpha_,beta_]= D[nb[n, alpha, beta],alpha];
      nbdbeta[n_,alpha_,beta_]= D[nb[n, alpha, beta],beta];
      Series[nbdalpha[n, alpha, beta],{alpha, Infinity, 1}]
      Series[nbdbeta[n, alpha, beta],{alpha, Infinity, 0}]

(the lowest order of the series that let's me pass the tests was chosen)

Tests

Tests are adapted from #1497 where new tests for neg_binomial_2_lpmf were written. Namely

  • Created a set of test values and derivatives in Mathematica (Wolfram Cloud) and test against those
  • Testing the derivatives against complex step derivatives. This is important for large alpha, as Mathematica refuses to compute exact derivatives in this case.
  • Over a grid of beta and n values test whether the change in the derivatives of neg_binomial_lpmf at alpha_cutoff +/- 1e-8 is negligible.
  • Test whether there is no drastic change in neg_binomial_lpmf<propto=true, int, var, double> between alpha just below and just above the cutoff.

Side Effects

Not any.

Checklist

@stan-buildbot
Copy link
Contributor

(stat_comp_benchmarks/benchmarks/gp_pois_regr/gp_pois_regr.stan, 1.02)
(stat_comp_benchmarks/benchmarks/low_dim_corr_gauss/low_dim_corr_gauss.stan, 0.98)
(stat_comp_benchmarks/benchmarks/irt_2pl/irt_2pl.stan, 1.0)
(stat_comp_benchmarks/benchmarks/pkpd/one_comp_mm_elim_abs.stan, 1.04)
(stat_comp_benchmarks/benchmarks/eight_schools/eight_schools.stan, 1.02)
(stat_comp_benchmarks/benchmarks/gp_regr/gp_regr.stan, 0.99)
(stat_comp_benchmarks/benchmarks/arK/arK.stan, 0.99)
(performance.compilation, 1.02)
(stat_comp_benchmarks/benchmarks/low_dim_gauss_mix_collapse/low_dim_gauss_mix_collapse.stan, 0.99)
(stat_comp_benchmarks/benchmarks/low_dim_gauss_mix/low_dim_gauss_mix.stan, 1.0)
(stat_comp_benchmarks/benchmarks/sir/sir.stan, 0.97)
(stat_comp_benchmarks/benchmarks/pkpd/sim_one_comp_mm_elim_abs.stan, 0.92)
(stat_comp_benchmarks/benchmarks/garch/garch.stan, 1.0)
(stat_comp_benchmarks/benchmarks/gp_regr/gen_gp_data.stan, 0.94)
(stat_comp_benchmarks/benchmarks/arma/arma.stan, 1.0)
Result: 0.99186497357
Commit hash: 095974e

@martinmodrak
Copy link
Contributor Author

@bob-carpenter May I ask for review on this? The tests here are similar to those written for #1497 (although the problem addressed is different).

Copy link
Contributor

@bob-carpenter bob-carpenter left a comment

Choose a reason for hiding this comment

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

Looks good. My only requests were around doc, which shouldn't be first person and should be written so that someone who knows C++ but not Mathematica can understand it if you want to leave it in the code.

Also, have you checked that the IP is OK for us to include Mathematica code in comments and use the output of their approximations? I can't see why it wouldn't be, but it'd be good to check before we go too far down the road of using a proprietary software package.

@@ -15,6 +15,12 @@
namespace stan {
namespace math {

namespace internal {
// Exposing to let me use in tests
Copy link
Contributor

Choose a reason for hiding this comment

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

Comments shouldn't be first person. I'd suggest just eliminating line 19.

Line 20 should say what the value is for---in this case, the threshold beyond which a Poisson approximation is used. It's also OK to say why it's 1e10, but I couldn't follow the logic of why it's set to 1e10.

Copy link
Member

Choose a reason for hiding this comment

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

@bob-carpenter, would it be ok if L20 said something like:
// Set to 1e10 based on empirical evaluation. At 1e9, the approximation isn't close enough

// reduces numerically to Poisson
// The derivatives are obtained via Taylor series at alpha -> Inf
// via Mathematica as:
// nb[n_,alpha_,beta_]:= LogGamma[n + alpha] - LogGamma[n + 1] -
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we should assume the readers of comments understand mathematica. So this should be put in Stan notation or just eliminated. I couldn't understand what LogGamma is or Log or D. If these comments stay, the functions used need to be explained.

@stan-buildbot
Copy link
Contributor

(stat_comp_benchmarks/benchmarks/gp_pois_regr/gp_pois_regr.stan, 1.0)
(stat_comp_benchmarks/benchmarks/low_dim_corr_gauss/low_dim_corr_gauss.stan, 1.01)
(stat_comp_benchmarks/benchmarks/irt_2pl/irt_2pl.stan, 1.01)
(stat_comp_benchmarks/benchmarks/pkpd/one_comp_mm_elim_abs.stan, 0.99)
(stat_comp_benchmarks/benchmarks/eight_schools/eight_schools.stan, 0.99)
(stat_comp_benchmarks/benchmarks/gp_regr/gp_regr.stan, 1.0)
(stat_comp_benchmarks/benchmarks/arK/arK.stan, 1.0)
(performance.compilation, 1.0)
(stat_comp_benchmarks/benchmarks/low_dim_gauss_mix_collapse/low_dim_gauss_mix_collapse.stan, 1.0)
(stat_comp_benchmarks/benchmarks/low_dim_gauss_mix/low_dim_gauss_mix.stan, 1.0)
(stat_comp_benchmarks/benchmarks/sir/sir.stan, 1.0)
(stat_comp_benchmarks/benchmarks/pkpd/sim_one_comp_mm_elim_abs.stan, 1.01)
(stat_comp_benchmarks/benchmarks/garch/garch.stan, 1.0)
(stat_comp_benchmarks/benchmarks/gp_regr/gen_gp_data.stan, 1.01)
(stat_comp_benchmarks/benchmarks/arma/arma.stan, 1.0)
Result: 1.00075771834
Commit hash: ce88c83

@martinmodrak martinmodrak mentioned this pull request Jan 14, 2020
5 tasks
@bob-carpenter
Copy link
Contributor

bob-carpenter commented Jan 26, 2020 via email

@martinmodrak
Copy link
Contributor Author

Thx @syclik and @bob-carpenter for the notes. I should've marked this PR as "on hold" because if #1497 turns out to be on the right track, I would probably rewrite neg_binomial_lpmf in the same spirit (eliminating the Poisson branch completely). So sorry for wasting your time here.

@martinmodrak martinmodrak changed the title New formulas for derivatives of neg_binomial_lpmf after alpha cutoff [On hold] New formulas for derivatives of neg_binomial_lpmf after alpha cutoff Jan 27, 2020
@syclik
Copy link
Member

syclik commented Apr 21, 2020

@martinmodrak, do you want to close this PR? #1497 was merged and from reading through the PR, it seems like you are favoring a different implementation.

@martinmodrak
Copy link
Contributor Author

Thanks for reminidng me, yes this should be closed.

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.

None yet

4 participants