Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 20 additions & 77 deletions mlquantify/likelihood/_generalized.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.

Expand All @@ -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).
Expand All @@ -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: ...}

Expand All @@ -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``.

Expand Down
11 changes: 7 additions & 4 deletions tests/test_aggregate_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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."""
Expand Down
52 changes: 26 additions & 26 deletions tests/test_likelihood.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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):
Expand Down
Loading