Skip to content

Conversation

@xImoZA
Copy link

@xImoZA xImoZA commented Nov 25, 2025

This PR standardizes the behavior of distributions to ensure consistent return types (scalars vs arrays) and improves the numerical stability of PDF calculations.

Changes

  • Numerical Stability: Refactored pdf methods in all distributions to calculate probability density via np.exp(lpdf(X)).
  • Standardized Outputs: Updated distribution methods (pdf, lpdf, ppf, log_gradients) to strictly distinguish between inputs:
    • Scalar input -> Scalar output.
    • Array input -> Array output with correct dtype.
  • Generate: - Update generate method signature in ContinuousDistribution and implementations to support size as int, tuple, or None (SciPy-like behavior).
  • Testing: Added comprehensive property-based tests (via Hypothesis) for all distributions ensuring that:
    • Scalar inputs return scalars (np.isscalar check).
    • Array inputs return arrays with correct shapes and dtypes.
    • Add unit tests covering various size shapes (scalar, 1D, ND, empty)

@xImoZA xImoZA changed the base branch from refactor/rework-arch to feat(core)/switch-to-generics November 25, 2025 11:52
@xImoZA xImoZA self-assigned this Nov 25, 2025
@xImoZA xImoZA requested a review from iraedeus November 25, 2025 11:53
@VITYANA
Copy link

VITYANA commented Dec 3, 2025

good code :shipit:

Base automatically changed from feat(core)/switch-to-generics to refactor/rework-arch December 4, 2025 21:01
@xImoZA xImoZA force-pushed the refactor(distributions)/standardize-output-types branch from 193460d to 4d50d2e Compare December 4, 2025 21:11
Copy link

@iraedeus iraedeus left a comment

Choose a reason for hiding this comment

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

  1. In some places a scalar is returned, and in others a zero-dimensional array is returned. I suggest returning a scalar everywhere (using result[()])
  2. I also propose standardizing the 'generate' method, making it scipy/numpy-like. To do this, simply pass size to the scipy generator. Size parameter should be of type int | tuple[int, ...] | None

X = np.asarray(X, dtype=self.dtype)
component_pdfs = np.array([comp.pdf(X) for comp in self.components])
return np.asarray(np.dot(self.weights, component_pdfs))
return np.dot(self.weights, component_pdfs)
Copy link

Choose a reason for hiding this comment

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

Why not just return np.exp(self.lpdf(X))?

@xImoZA xImoZA force-pushed the refactor(distributions)/standardize-output-types branch from 3f6ff15 to 4891f1d Compare December 5, 2025 13:26
@xImoZA xImoZA requested a review from iraedeus December 5, 2025 13:38
@iraedeus iraedeus merged commit 8707036 into refactor/rework-arch Dec 5, 2025
6 checks passed
@iraedeus iraedeus deleted the refactor(distributions)/standardize-output-types branch December 5, 2025 23:52
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.

REFACTOR: Standardize output types in distributions FIX: Handle scalar case in tests

4 participants