stan-dev / math

The Stan Math Library is a C++ template library for automatic differentiation of any order using forward, reverse, and mixed modes. It includes a range of built-in functions for probabilistic modeling, linear algebra, and equation solving.
https://mc-stan.org
BSD 3-Clause "New" or "Revised" License
752 stars 188 forks source link

Add loglogistic distribution #2320

Closed spinkney closed 2 years ago

spinkney commented 3 years ago

Here's Stan code I put together. The rng function includes lower and upper bounds because I needed a truncated distribution but can be removed to be consistent with other rng functions.

  real loglogistic_lpdf (real y, real alpha, real beta){
    real lalpha = log(alpha);
    real numerator = log(beta) - lalpha + (beta - 1) * (log(y) - lalpha);
    return numerator - 2 * (log1p( (y/alpha)^beta ));
  }

  real loglogistic_lcdf (real y, real alpha, real beta) {
    return -log1p((y / alpha) ^-beta);
  }

  real loglogistic_cdf (real y, real alpha, real beta) {
    return 1/(1 + (y / alpha) ^-beta);
  }

  real loglogistic_lccdf (real y, real alpha, real beta){
    return -log1p( (y / alpha)^beta);
  }

  real loglogistic_rng (real alpha, real beta, real lb, real ub) {
    real p_ub = loglogistic_cdf(ub, alpha, beta);
    real p_lb = loglogistic_cdf(lb, alpha, beta);
    real r = uniform_rng(p_lb, p_ub);
    return alpha * (r / (1 - r) )^(1/beta);
  }
adamhaber commented 3 years ago

I can try... this involves adding loglogistic_x.hpp functions to prim/prob, right?

rok-cesnovar commented 3 years ago

That is correct. Thanks!

adamhaber commented 3 years ago

No problem. Two questions:

  1. Would the logistic_x.hpp be a good starting point for this?
  2. Other than these files, this means adding something like loglogistic_test.cpp unit test? Any other expected outputs?
rok-cesnovar commented 3 years ago

logistic_x is probably a good starting point I would say.

Maybe take a look at a recent PR adding a new distribution to see the requirements: https://github.com/stan-dev/math/pull/2271

Tagging @bbbales2 that has been the designated reviewer for these kind of PRs recently.

bbbales2 commented 3 years ago

@adamhaber am back! The distribution functions are pretty involved. If you wanna do em', yeah have a look at #2271.

Reverse mode implementations of some existing functions might be an easier place to start though (less moving parts). Can dig up a couple good ones if you want.

adamhaber commented 3 years ago

@bbbales2 thanks for the suggestion, I'll have a look.

Reverse mode implementations of some existing functions might be an easier place to start though (less moving parts). Can dig up a couple good ones if you want.

You mean as a separate, simpler PR "to start with"?

bbbales2 commented 3 years ago

@adamhaber yeah yeah. The distributions are tough. I was trying to write down some notes on how to write functions using the new autodiff, but it's been a few days and I haven't finished so don't wanna keep you waiting.

diag_pre_multipy and diag_post_multiply both could use reverse mode autodiff implementations.

I think use elt_multiply as a template.

Prim implementation here Rev implementation here Prim tests here Autodiff tests here

That's not much information -- there's a lot of weird stuff in those functions. Hit me up on the slack when stuff starts acting weird (cuz it almost certainly will): https://discourse.mc-stan.org/t/mc-stan-community-slack/2410

This feeds into the stuff we're doing converting everything to support the varmat autodiff types, but also some other stuff, so will be good things to know.

gregorp90 commented 3 years ago

@bbbales2 I can do the reverse mode autodiff implementations of diag_pre_multiply and diag_post_multiply, if @adamhaber didn't already start on that.

bbbales2 commented 3 years ago

@gregorp90 go for it. Probably just start with one (and then the second one will be easy). @adamhaber is working on a different function.

gregorp90 commented 3 years ago

Great, thanks!

gregorp90 commented 3 years ago

Hi, is anyone working on this @bbbales2? Maybe @adamhaber? If not, then I can try it out.

rok-cesnovar commented 3 years ago

No one has commented here that they are working on it or opened a Work-in-progress PR, so I would say go for it.

gregorp90 commented 3 years ago

Ok great, thank you!

SteveBronder commented 3 years ago

I started working on some docs for adding new distributions, got about halfway through it today (and tbh stopped at the hard part of explaining all the whistles and bells) but I'll try to get this finished tomorrow. I'm using loglogistic_cdf as an example since everything we do there should work with the other functions.

You can checkout the branch below and run make doxygen to make the site locally

https://github.com/stan-dev/math/compare/docs/distributions

gregorp90 commented 3 years ago

@SteveBronder Thanks, I skimmed through it and it looks very helpful. I started implementing the loglogistic_lpdf, so I will definitely go through this help file in more detail and let you know if I have any comments.

Just a note, I guess you decided not to use loglogistic_cdf but normal_lpdf instead? Because I don't see the loglogistic_cdf in the help file I generated.

I also looked at the "Testing New Distributions" section of this help file. What I miss there are the instructions to add GENERATE_DISTRIBUTION_TESTS=true to the make/local file. Maybe you could add a note on this, if you are updating these help files. Tadej told me I need this. If he didn't I would probably lose a couple of hours in trying to figure out why the testing isn't working, so it might be helpful to mention it.

SteveBronder commented 3 years ago

Just a note, I guess you decided not to use loglogistic_cdf but normal_lpdf instead? Because I don't see the loglogistic_cdf in the help file I generated.

Oh yeah sorry so I changed it up because I could just use some of the examples from the Stan math paper for normal_lpdf

I also looked at the "Testing New Distributions" section of this help file. What I miss there are the instructions to add GENERATE_DISTRIBUTION_TESTS=true to the make/local file. Maybe you could add a note on this, if you are updating these help files. Tadej told me I need this. If he didn't I would probably lose a couple of hours in trying to figure out why the testing isn't working, so it might be helpful to mention it.

Good catch! I didn't see that is not set for windows and I'll add that note to the distribution tests docs

spinkney commented 3 years ago

should I close this now that @gregorp90's pr is merged?

SteveBronder commented 3 years ago

We can leave it open until the cdf etc is added

gregorp90 commented 3 years ago

I'll start on the other functions as soon as I have some time. It will probably go a bit quicker now.

danielinteractive commented 2 years ago

Thanks all for working on this - it will be very useful! Just a naive question from a newcomer - when do you think this will be available in stan / cmdstan / cmdstanr? :-)

rok-cesnovar commented 2 years ago

The loglogistic_lpdf should be available for the next release coming at the end of January. The cdf, lcdf and other functions typically available with distributions will not be available though.

danielinteractive commented 2 years ago

thx @rok-cesnovar ! just to understand: the other functions are not ready? since I saw that #2488 implemented some?

rok-cesnovar commented 2 years ago

Oh yeah, the loglogistic_cdf one was done. So this is only missing the loglogistic_lccdf and loglogistic_lcdf before this issue can be closed. loglogistic_lpdf, loglogistic_cdf and loglogistic_rng should be available in 2.29 end of January.

rok-cesnovar commented 2 years ago

Closing this as we opened a more general CDF function issue: #2657