Skip to content

Per-distribution S3 interface: migrate gamma (PoC for #41)#43

Open
sbfnk-bot wants to merge 3 commits into
mainfrom
refactor-gamma-dispatch
Open

Per-distribution S3 interface: migrate gamma (PoC for #41)#43
sbfnk-bot wants to merge 3 commits into
mainfrom
refactor-gamma-dispatch

Conversation

@sbfnk-bot

@sbfnk-bot sbfnk-bot commented Jul 9, 2026

Copy link
Copy Markdown
Collaborator

Proof-of-concept for the per-distribution refactor (#41, first checklist box). It introduces the dispatch machinery and migrates gamma only, so we can see the pattern (and review) on something small before doing the rest.

What it does

  • R/distribution.Rnew_dist(name, params) dispatch objects + the interface generics (dist_natural_params, dist_cdf, plus reuse of the existing mean/sd generics). Discretisation is an optional capability (dist_cdf.default errors), not a type split.
  • R/gamma.R — everything gamma in one file: dist_natural_params.gamma, dist_cdf.gamma, mean.gamma, sd.gamma.
  • The gamma arms of the discrete_pmf (pdist), natural_params, mean.dist_spec and sd.dist_spec switches now delegate to those methods. Every other distribution is untouched.

Notes / scope

  • Behaviour-preserving: full test suite green, lint_package() = 0. mean/sd/natural_params/discretise for gamma verified identical.
  • Kept to internal generics (no public-API change) for this slice. mean/sd are used directly (they're generics); natural_params's own dispatch and the stored-component representation change are deliberately deferred.
  • Once every distribution is migrated, each switch collapses to a single <generic>(new_dist(distribution)) call and disappears — and R/template.R + the "how to add a distribution" doc follow.

Draft, for design feedback on the shape before the remaining distributions.

Summary by CodeRabbit

  • New Features

    • Added support for gamma distribution calculations, including mean, standard deviation, and CDF-based operations.
    • Expanded distribution handling so more calculations now use a consistent dispatch mechanism.
  • Bug Fixes

    • Improved gamma distribution support in discretised calculations, making results more consistent across related functions.

First slice of #41: adds new_dist() dispatch objects and the distribution interface (dist_natural_params, dist_cdf, mean, sd), with gamma migrated into R/gamma.R. The gamma arms of the discrete_pmf/natural_params/mean/sd switches now delegate; all other distributions unchanged. Behaviour-preserving (tests green, 0 lints).

Co-authored-by: sbfnk <sebastian.funk@lshtm.ac.uk>
@sbfnk-bot sbfnk-bot requested a review from sbfnk July 9, 2026 09:50
@coderabbitai

coderabbitai Bot commented Jul 9, 2026

Copy link
Copy Markdown

Review Change Stack

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 8ae26c7c-7334-48d0-939f-0c466cbb9038

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Walkthrough

This PR introduces a new distribution dispatch interface (new_dist, dist_natural_params, dist_cdf) in R/distribution.R, implements it for the gamma distribution in R/gamma.R, and refactors gamma-specific logic in R/dist_spec.R (mean, sd, natural_params, discrete_pmf CDF) to delegate through this interface instead of inline formulas. NAMESPACE is updated accordingly.

Changes

Gamma distribution dispatch interface

Layer / File(s) Summary
Distribution dispatch interface
R/distribution.R
Adds new_dist() constructor, dist_natural_params() and dist_cdf() generics, and dist_cdf.default() which errors via cli::cli_abort() when a distribution has no CDF method.
Gamma distribution implementation
R/gamma.R
Implements dist_natural_params.gamma, dist_cdf.gamma (using pgamma), mean.gamma (shape/rate), and sd.gamma (sqrt(shape/rate^2)).
Gamma delegation in dist_spec.R
R/dist_spec.R
Replaces inline gamma formulas in discrete_pmf(), mean.dist_spec(), sd.dist_spec(), and natural_params() with calls through new_dist("gamma", ...) and the dist_cdf/dist_natural_params interface.
NAMESPACE registrations
NAMESPACE
Registers new S3 methods for dist_cdf (default, gamma), dist_natural_params (gamma), mean (gamma), and sd (gamma).

Possibly related issues

Possibly related PRs

  • epiforecasts/distspec#32: Both PRs touch sd()/natural_params() in R/dist_spec.R; this PR changes the gamma implementations to delegate through the new distribution interface at the same functions.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately reflects the main change: introducing a per-distribution S3 interface and migrating gamma as a proof of concept.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch refactor-gamma-dispatch

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@sbfnk-bot sbfnk-bot added the no news needed PR intentionally has no NEWS.md entry label Jul 9, 2026
@sbfnk sbfnk marked this pull request as ready for review July 9, 2026 10:08
sbfnk added 2 commits July 9, 2026 11:15
Co-authored-by: sbfnk <sebastian.funk@lshtm.ac.uk>
Co-authored-by: sbfnk <sebastian.funk@lshtm.ac.uk>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no news needed PR intentionally has no NEWS.md entry

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants