Skip to content

Add logpdf/pdf/cdf for TruncatedExponentialFamilyDistribution (closes #290) - #310

Merged
bvdmitri merged 3 commits into
mainfrom
fix/truncated-ef-logpdf
Sep 3, 2026
Merged

Add logpdf/pdf/cdf for TruncatedExponentialFamilyDistribution (closes #290)#310
bvdmitri merged 3 commits into
mainfrom
fix/truncated-ef-logpdf

Conversation

@bvdmitri

@bvdmitri bvdmitri commented Sep 2, 2026

Copy link
Copy Markdown
Member

Closes #290. Also addresses #296 (7a).

Problem

TruncatedExponentialFamilyDistribution is exported and subtypes UnivariateDistribution, but defined no logpdf/pdf/cdf, so logpdf(dtr, x) threw a MethodError. Downstream code that truncates an EF distribution and queries its density failed at runtime.

Fix

Add the standard truncated density/cdf using the already-stored lcdf/ucdf:

  • normalization Z = ucdf - lcdf
  • logpdf(d, x) = logpdf(untruncated, x) - log(Z) (−Inf outside the truncated support)
  • pdf(d, x) = exp(logpdf(d, x))
  • cdf(d, x) = clamp((cdf(untruncated, x) - lcdf) / Z, 0, 1)

Also removes the unused import Distributions: logsubexp (issue #296, item 7a). The issue text suggested logsubexp(ucdf, lcdf), but that computes log(e^ucdf − e^lcdf), not log(ucdf − lcdf); the plain log(ucdf − lcdf) is correct and needs no extra import.

Test

New testitem compares the continuous case against Distributions.truncated (ground truth) for logpdf/pdf/cdf plus boundary behavior, and checks the discrete methods now exist and are self-consistent.

Note for reviewer

For discrete untruncated distributions the constructor stores lcdf = cdf(d, l) = P(X ≤ l), so the normalization ucdf - lcdf = P(l < X ≤ u) excludes the lower-bound mass P(X = l) even though insupport treats [l, u] as closed. That is a pre-existing lower-bound convention question in the constructor, independent of these density methods; flagging for a follow-up decision rather than changing truncation semantics here. cc @Nimrais

Reported by @docxology.

…ribution (#290)

The type is exported and subtypes UnivariateDistribution but defined no density
methods, so logpdf/pdf/cdf threw MethodError. Add them using the stored lcdf/ucdf
normalization (Z = ucdf - lcdf); cdf is clamped to [0, 1].

Also removes the unused `logsubexp` import (issue #296-7a) — the correct
normalization is log(ucdf - lcdf), which does not need it.

Tests compare the continuous case against Distributions.truncated as ground truth
and check the discrete methods exist and are self-consistent.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@codecov

codecov Bot commented Sep 2, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83.94%. Comparing base (6435377) to head (744fc16).
⚠️ Report is 10 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #310      +/-   ##
==========================================
+ Coverage   83.86%   83.94%   +0.08%     
==========================================
  Files          45       45              
  Lines        4078     4087       +9     
==========================================
+ Hits         3420     3431      +11     
+ Misses        658      656       -2     

☔ 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.

@Nimrais Nimrais left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@bvdmitri
bvdmitri merged commit 5b23041 into main Sep 3, 2026
7 checks passed
@bvdmitri
bvdmitri deleted the fix/truncated-ef-logpdf branch September 3, 2026 11:49
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.

Issue 01 — TruncatedExponentialFamilyDistribution has no logpdf/pdf/cdf

2 participants