Skip to content

refactor: Categorical now stores normalized values#396

Merged
YeungOnion merged 6 commits into
statrs-dev:mainfrom
YeungOnion:cherry-pick-37f8fdf
Jul 20, 2026
Merged

refactor: Categorical now stores normalized values#396
YeungOnion merged 6 commits into
statrs-dev:mainfrom
YeungOnion:cherry-pick-37f8fdf

Conversation

@YeungOnion

Copy link
Copy Markdown
Contributor

Cherry-pick of 37f8fdf (FreezyLemon) onto current main. The original commit predates the rand 0.9/0.10 migration and the std/core split, so it conflicted on:

  • ::rand::distr::Distribution (vs. old rand::distributions::Distribution alias)
  • RngExt::random (vs. old rng.gen)

Categorical now stores norm_cdf/norm_sf directly instead of normalizing on every cdf/sf/inverse_cdf call, and inverse_cdf uses binary_search_by instead of a hand-rolled binary_index. The now-unused sample_unchecked/prob_mass_to_cdf/cdf_to_sf/binary_index helpers are removed; Multinomial::sample, which depended on categorical::prob_mass_to_cdf/sample_unchecked, now computes its cdf inline instead.

Test plan

  • cargo build --all-features
  • cargo test --all-features (192 passed)
  • cargo clippy --all-features

FreezyLemon and others added 3 commits July 19, 2026 19:19
norm_pmf (probabilities) was already normalized before
storing, but cdf and sf weren't. Instead, they were normalized
on every API call.

The refactor also reduces the amount of vec/slice iterations in `new`
from 4 to 2.
@YeungOnion
YeungOnion force-pushed the cherry-pick-37f8fdf branch from e90e861 to 9030bd7 Compare July 20, 2026 01:31
@codecov

codecov Bot commented Jul 20, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.09804% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 94.89%. Comparing base (9d33d0e) to head (07a05b7).

Files with missing lines Patch % Lines
src/distribution/categorical.rs 91.83% 4 Missing ⚠️
src/distribution/multinomial.rs 98.11% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #396      +/-   ##
==========================================
+ Coverage   94.67%   94.89%   +0.21%     
==========================================
  Files          61       61              
  Lines       13322    13344      +22     
==========================================
+ Hits        12613    12663      +50     
+ Misses        709      681      -28     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@YeungOnion
YeungOnion marked this pull request as ready for review July 20, 2026 03:03
@YeungOnion

Copy link
Copy Markdown
Contributor Author

GoF tests for sampling will still be a good idea

@YeungOnion
YeungOnion merged commit a50ede5 into statrs-dev:main Jul 20, 2026
12 checks passed
@YeungOnion
YeungOnion deleted the cherry-pick-37f8fdf branch July 20, 2026 03:06
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.

2 participants