From b5bdf518d6bd0198a7209cefa740807b4e202781 Mon Sep 17 00:00:00 2001 From: luizfernandolj Date: Mon, 9 Nov 2026 15:57:42 -0300 Subject: [PATCH] MLPE: drop the estimator and stop being aggregative The estimate is the training prevalence, read from y. There is no posterior to compute, so there is no estimator to fit -- and the one fit that PR #6 kept was the entire remaining cost of the method. quapy's MLPE was always non-aggregative, takes no estimator, and its fit is two lines that count labels; this makes mlquantify's the same kind of object. The class drops SoftPredictionMixin and AggregativeMixin, passes representation=None and aggregative=False, and loses estimator, cv, stratified and shuffle from the signature. With aggregative=False, _is_configured_aggregative returns False, so _check_aggregation_configuration returns early (no mixin required) and _uses_estimator_predictions is False -- fit takes the path that reads train_priors_ from the labels and nothing else runs. The _fit_estimator_predictions override from PR #6 is removed: there is no aggregative path left to override. Measured: fit goes from about 57 ms to 0.55 ms, there is no estimator_ after fit, the estimate equals the training prevalence exactly (0.000e+00), and it is the same for a sample of 10 and of 2000. The remaining parameters are keyword-only on purpose. Positional, a caller written for the old signature -- MLPE(LogisticRegression()) -- would bind the classifier to solver and run on silently misconfigured. A TypeError is the right answer to asking for an estimator this method no longer has. My own test caught that: it is the reason for the asterisk. Breaking change for any caller that passed an estimator. Tests: MLPE leaves the aggregative parametrizations that read estimator_ and joins the no-estimator branch of _make; PR #6's two tests are replaced by three on the new contract -- no estimator and nothing fitted, an estimator is rejected, and any test sample gives the same answer. Co-Authored-By: Claude Opus 5 --- mlquantify/likelihood/_generalized.py | 97 ++++++--------------------- tests/test_aggregate_classes.py | 11 +-- tests/test_likelihood.py | 52 +++++++------- 3 files changed, 53 insertions(+), 107 deletions(-) diff --git a/mlquantify/likelihood/_generalized.py b/mlquantify/likelihood/_generalized.py index 15ec04dc..99cb54a5 100644 --- a/mlquantify/likelihood/_generalized.py +++ b/mlquantify/likelihood/_generalized.py @@ -2,18 +2,11 @@ import numpy as np -from mlquantify.base_aggregative import ( - AggregativeMixin, - SoftPredictionMixin, - _get_estimator, - _get_estimator_function, -) from mlquantify.compose import LikelihoodComposeQuantifier -from mlquantify.representations import PredictionRepresentation from mlquantify.utils._validation import validate_prevalences -class MLPE(SoftPredictionMixin, AggregativeMixin, LikelihoodComposeQuantifier): +class MLPE(LikelihoodComposeQuantifier): r"""Maximum Likelihood Prevalence Estimation (MLPE) quantifier. The trivial quantification baseline. Under the assumption of *no* shift, the @@ -23,21 +16,17 @@ class MLPE(SoftPredictionMixin, AggregativeMixin, LikelihoodComposeQuantifier): any genuine quantifier should beat; if a method cannot improve on MLPE, it is not exploiting the test data. + It is **not** aggregative and takes no estimator. ``fit`` reads the class + proportions from ``y``; nothing is trained, and no posterior is ever + computed. Taking a classifier would be taking one in order to never use + it: the estimate does not depend on it, and fitting it was the whole cost + of the method. + Parameters ---------- - estimator : estimator, optional - Kept for API compatibility with the aggregative quantifiers; it does - **not** influence the estimate, since MLPE returns the training - prevalence regardless of the test input. solver, tau_0, tau_1 : optional Retained for interface compatibility with the likelihood-composition framework; unused by this trivial baseline. - cv, stratified, shuffle : optional - Accepted for signature compatibility with the aggregative - quantifiers, and ignored: there is no cross-validation to - configure. The estimator is fitted once, because the estimate is - the training prevalence and never reads the estimator's - predictions. random_state : int or None, default=None Random seed for reproducibility. @@ -54,6 +43,10 @@ class MLPE(SoftPredictionMixin, AggregativeMixin, LikelihoodComposeQuantifier): baseline. Contrast with :class:`EMQ`, the non-trivial maximum-likelihood quantifier that re-weights the posteriors to the test set. + Being non-aggregative, it has no ``estimator_`` after ``fit``, and + ``cv``, ``stratified`` and ``shuffle`` are gone from the signature: there + is no out-of-sample prediction to configure. + See Also -------- EMQ : Non-trivial maximum-likelihood quantifier (EM re-weighting). @@ -62,12 +55,11 @@ class MLPE(SoftPredictionMixin, AggregativeMixin, LikelihoodComposeQuantifier): Examples -------- >>> from mlquantify.likelihood import MLPE - >>> from sklearn.linear_model import LogisticRegression >>> import numpy as np >>> rng = np.random.default_rng(0) >>> X = rng.standard_normal((200, 4)) >>> y = (rng.random(200) < 0.3).astype(int) - >>> q = MLPE(LogisticRegression()).fit(X, y) + >>> q = MLPE().fit(X, y) >>> q.predict(rng.standard_normal((50, 4))) # returns the training prevalence {0: ..., 1: ...} @@ -81,78 +73,29 @@ class MLPE(SoftPredictionMixin, AggregativeMixin, LikelihoodComposeQuantifier): """ + # Keyword-only on purpose. With positional parameters, a caller written + # for the old signature -- ``MLPE(LogisticRegression())`` -- would bind the + # classifier to ``solver`` and run on silently misconfigured. A TypeError + # is the right answer to asking for an estimator this method no longer has. def __init__( self, - estimator=None, + *, solver="slsqp", tau_0=0.0, tau_1=0.0, - cv=None, - stratified=True, - shuffle=False, random_state=None, ): super().__init__( - estimator=estimator, - representation=PredictionRepresentation( - method="soft", - average=False, - ), + representation=None, + estimator=None, solver=solver, - aggregative=True, + aggregative=False, tau_0=tau_0, tau_1=tau_1, random_state=random_state, ) - self.cv = cv - self.stratified = stratified - self.shuffle = shuffle self.random_state = random_state - def _fit_estimator_predictions( - self, - X, - y, - estimator_fitted=False, - cv=None, - stratified=None, - shuffle=None, - random_state=None, - cv_prediction="refit", - ): - """Fit the estimator once, without cross-validation. - - The aggregative path cross-validates the estimator to obtain - out-of-sample predictions, because an aggregation function trained on - in-sample posteriors would be optimistically biased. MLPE has no - aggregation function to train: the estimate is the training - prevalence, read from ``y``. The cross-validation was therefore pure - cost -- at the default ``cv`` it fit the estimator six times (five - folds plus the refit) and called ``predict_proba`` five times, and - then the estimate ignored all of it. - - A single fit is kept so that ``estimator_`` is still there after - ``fit``, as it is for every other aggregative quantifier. - - ``y`` is returned unchanged. The cross-validated path returned it - in fold order -- a permutation of the same labels -- and the caller - uses it only to compute ``train_priors_``, which a permutation does - not change. That is why the estimate is unaffected. - - ``cv``, ``stratified``, ``shuffle`` and the local ``random_state`` - are accepted and ignored, which is what the class docstring already - said of the estimator itself. - """ - estimator = _get_estimator(self) - estimator_function = _get_estimator_function(self) - - if not estimator_fitted: - # Fit in place, as the refit of the cross-validated path does. - estimator = estimator.fit(X, y) - self.estimator_ = estimator - - return getattr(estimator, estimator_function)(X), y - def predict(self, X): """Return the training prevalence, ignoring ``X``. diff --git a/tests/test_aggregate_classes.py b/tests/test_aggregate_classes.py index b235caff..65bfd520 100644 --- a/tests/test_aggregate_classes.py +++ b/tests/test_aggregate_classes.py @@ -26,12 +26,13 @@ def _estimator(): def _make(Quantifier): """Construct a quantifier, supplying an estimator only when it takes one.""" - if Quantifier is PWK: # k-NN based, no wrapped estimator - return Quantifier() + if Quantifier in (PWK, MLPE): # no wrapped estimator: k-NN based, and + return Quantifier() # MLPE, which is non-aggregative return Quantifier(_estimator()) -# Natively-multiclass aggregative quantifiers (counting, likelihood, neighbors). +# Natively-multiclass quantifiers (counting, likelihood, neighbors). All +# aggregative except MLPE, which takes no estimator. MULTICLASS_QUANTIFIERS = [ CC, PCC, ACC, FM, GACC, GPACC, MS, MS2, T50, TAC, TMAX, TX, EMQ, CDE, MLPE, PWK, @@ -98,7 +99,9 @@ def test_aggregate_classes_none_uses_fitted(Quantifier): _assert_full_prevalence(out, n_classes=3) -@pytest.mark.parametrize("Quantifier", [CC, PCC, EMQ, MLPE]) +# MLPE is out: being non-aggregative it has no ``estimator_`` to read +# posteriors from, and its ``aggregate`` does not depend on them. +@pytest.mark.parametrize("Quantifier", [CC, PCC, EMQ]) def test_aggregate_classes_direct(Quantifier): """Quantifiers whose ``aggregate`` is self-contained honour ``classes`` and return a prevalence spanning exactly those classes.""" diff --git a/tests/test_likelihood.py b/tests/test_likelihood.py index d43c1354..bc3d05f8 100644 --- a/tests/test_likelihood.py +++ b/tests/test_likelihood.py @@ -14,7 +14,7 @@ def _assert_valid_prevalence(prevalence, n_classes): assert prevalence.sum() == pytest.approx(1.0) -@pytest.mark.parametrize("quantifier_class", [EMQ, CDE, MLPE]) +@pytest.mark.parametrize("quantifier_class", [EMQ, CDE]) def test_likelihood_methods_fit_predict_binary(quantifier_class, binary_dataset): X, y = binary_dataset q = quantifier_class(estimator=LogisticRegression(max_iter=1000, random_state=42)) @@ -27,47 +27,47 @@ def test_likelihood_methods_fit_predict_binary(quantifier_class, binary_dataset) _assert_valid_prevalence(prevalence, n_classes=2) -def test_mlpe_fits_the_estimator_once(multiclass_dataset): - """MLPE must not cross-validate: its estimate never uses the estimator. +def test_mlpe_takes_no_estimator_and_trains_nothing(multiclass_dataset): + """MLPE is non-aggregative: it takes no estimator and fits nothing. - The estimate is the training prevalence, so out-of-sample predictions add - nothing to it. One fit is kept, so that ``estimator_`` is there as for any - other aggregative quantifier. + The estimate is the training prevalence, read from ``y``. There is no + posterior to compute, so there is no estimator to fit -- and none to + keep in ``estimator_``. """ X, y = multiclass_dataset + q = MLPE() + q.fit(X, y) - class CountingLogisticRegression(LogisticRegression): - fits = 0 - - def fit(self, X, y, sample_weight=None): - type(self).fits += 1 - return super().fit(X, y, sample_weight=sample_weight) - - estimator = CountingLogisticRegression(max_iter=1000, random_state=42) - q = MLPE(estimator=estimator).fit(X, y) - - assert CountingLogisticRegression.fits == 1 - assert hasattr(q, "estimator_") + assert not hasattr(q, "estimator_") + assert q._uses_estimator_predictions() is False with config_context(prevalence_return_type="array"): prevalence = np.asarray(q.predict(X), dtype=float) + _assert_valid_prevalence(prevalence, n_classes=len(np.unique(y))) expected = np.asarray([np.mean(y == cls) for cls in np.unique(y)], dtype=float) np.testing.assert_allclose(prevalence, expected) -@pytest.mark.parametrize("cv", [None, 2, 5]) -def test_mlpe_ignores_cv(multiclass_dataset, cv): - """``cv`` is accepted for signature compatibility and changes nothing.""" +def test_mlpe_rejects_an_estimator(): + """The estimator is gone from the signature, not accepted and ignored. + + Accepting one would advertise work that never happens. + """ + with pytest.raises(TypeError): + MLPE(LogisticRegression(max_iter=1000, random_state=42)) + + +def test_mlpe_ignores_the_test_sample(multiclass_dataset): + """Any test sample yields the same answer: the training prevalence.""" X, y = multiclass_dataset - q = MLPE(estimator=LogisticRegression(max_iter=1000, random_state=42), cv=cv) - q.fit(X, y) + q = MLPE().fit(X, y) with config_context(prevalence_return_type="array"): - prevalence = np.asarray(q.predict(X), dtype=float) + dez = np.asarray(q.predict(X[:10]), dtype=float) + todas = np.asarray(q.predict(X), dtype=float) - expected = np.asarray([np.mean(y == cls) for cls in np.unique(y)], dtype=float) - np.testing.assert_allclose(prevalence, expected) + np.testing.assert_allclose(dez, todas) def test_emq_multiclass(multiclass_dataset):