Add logpdf/pdf/cdf for TruncatedExponentialFamilyDistribution (closes #290) - #310
Merged
Conversation
…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 Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #290. Also addresses #296 (7a).
Problem
TruncatedExponentialFamilyDistributionis exported and subtypesUnivariateDistribution, but defined nologpdf/pdf/cdf, sologpdf(dtr, x)threw aMethodError. 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:Z = ucdf - lcdflogpdf(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 suggestedlogsubexp(ucdf, lcdf), but that computeslog(e^ucdf − e^lcdf), notlog(ucdf − lcdf); the plainlog(ucdf − lcdf)is correct and needs no extra import.Test
New testitem compares the continuous case against
Distributions.truncated(ground truth) forlogpdf/pdf/cdfplus 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 normalizationucdf - lcdf = P(l < X ≤ u)excludes the lower-bound massP(X = l)even thoughinsupporttreats[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 @NimraisReported by @docxology.