stan-dev / stan-dev/math

Add Boost's `tgamma_lower` and `tgamma(a,z)` functions

Open
#2,769 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
839
Forks
220
Avg merge
2d 4h
Merged PRs (30d)
14

Description

Description

The Math library currently has the normalised incomplete gamma functions (gamma_p and gamma_q), which return values within [0, 1]. The log of the gamma_p function is used for the gamma_lcdf function, but given that gamma_p normalises the result to [0,1], we lose resolution around the boundaries of the LCDF.

If we add the tgamma(a,z) function (incomplete gamma, not normalised), then we can define a log_gamma_p function which should be more numerically stable.

Where the normalised lower incomplete gamma (gamma_p) is defined:

$$
P(a,z) = \frac{\Gamma(a) - \Gamma(a,z)}{\Gamma(a)}
$$

With the tgamma_lower function, we can keep this on the log scale as:

double lgamma_a = lgamma(a);

log_diff_exp(lgamma_a, log(tgamma(a,z)) - lgamma_a;

With an analogous approach for gamma_q and log_gamma_q

Current Version:

v4.4.0

EDIT: The definition should have used tgamma, not tgamma_lower!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the existing gamma_p and gamma_q implementations and the Boost incomplete-gamma APIs they wrap. Trace how gamma_lcdf and the corresponding log-scale functions use these results. Done means the requested tgamma_lower and tgamma(a,z) functionality supports the more stable log-scale gamma calculations described in the issue, with matching coverage for gamma_p and gamma_q.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.