Skip to content

Conversation

@xImoZA
Copy link

@xImoZA xImoZA commented Nov 4, 2025

This pull request refactors the core and distributions modules to use generic numeric types.

Implementation:

  • Switched to Generic Typing: Refactored core classes (core, distributions) to use typing.Generic with a TypeVar("DType").
  • Added tests for return values to ensure type correctness.

@xImoZA xImoZA requested a review from iraedeus November 4, 2025 22:42
@xImoZA xImoZA self-assigned this Nov 4, 2025
@xImoZA xImoZA linked an issue Nov 4, 2025 that may be closed by this pull request
@xImoZA xImoZA requested a review from iraedeus November 11, 2025 17:25
@xImoZA xImoZA marked this pull request as draft November 13, 2025 13:39
@xImoZA xImoZA marked this pull request as ready for review November 13, 2025 16:47
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.

There are many uses of np.isclose or np.assert_allclose with arbitrary atol values ​​for all dtypes. I suggest trying atol=np.finfo(dtype).eps

def test_init_does_not_recreate_components_with_correct_dtype(self, dtype):
"""Tests that components with the correct dtype are not recreated."""

comp_f32 = Exponential(loc=0.0, rate=1.0, dtype=dtype)

Choose a reason for hiding this comment

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

Invalid variable name, here the component has arbitrary precision

Suggested change
comp_f32 = Exponential(loc=0.0, rate=1.0, dtype=dtype)
comp = Exponential(loc=0.0, rate=1.0, dtype=dtype)

X = np.asarray(X, dtype=float64)
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))

Choose a reason for hiding this comment

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

I would rewrite this method for better numerical stability:

Suggested change
return np.asarray(np.dot(self.weights, component_pdfs))
return np.exp(self.lpdf(X))

Then you will need to synchronize the corresponding test like this:

изображение

"""

X = np.atleast_1d(X)
X = np.atleast_1d(X).astype(self.dtype)

Choose a reason for hiding this comment

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

Suggested change
X = np.atleast_1d(X).astype(self.dtype)
X = np.asarray(X, dtype=self.dtype)


dtype = mixture_model.dtype

expected_lpdf = np.log(mixture_model.pdf(X))

Choose a reason for hiding this comment

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

Suggested change
expected_lpdf = np.log(mixture_model.pdf(X))
expected_lpdf = np.log(w1 * c1.pdf(X) + w2 * c2.pdf(X))

assert isinstance(samples, np.ndarray)
assert samples.dtype == dtype

def test_generate_with_size_zero(self, mixture_model):

Choose a reason for hiding this comment

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

Forgot to check here dtype of the output

@xImoZA xImoZA force-pushed the feat(core)/switch-to-generics branch 2 times, most recently from 488aa59 to fa9cbae Compare November 24, 2025 15:04
@xImoZA xImoZA requested a review from iraedeus November 24, 2025 15:16
@xImoZA xImoZA force-pushed the feat(core)/switch-to-generics branch from fa9cbae to cd36859 Compare November 24, 2025 18:07
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.

FEAT: Switch from float64 to Generics

3 participants