13.1.3. Solvers#

Solvers are the optimisation backends shared by the quantifier families. They turn a distribution-matching or likelihood objective into a prevalence vector, either over the binary interval \([0, 1]\) or over the full probability simplex \(\Delta^{k-1} = \{p : p_c \ge 0,\; \sum_c p_c = 1\}\). Compose and matching quantifiers delegate their final optimisation step to one of these functions, so the same solver code is reused across many methods.

13.1.3.1. Role and mechanism#

Every optimisation-based quantifier reduces prevalence estimation to

\[\hat{p} = \arg\min_{p \in \Delta^{k-1}} \mathcal{L}(p),\]

where \(\mathcal{L}\) is a loss comparing the test representation with the prevalence-mixed training representations. The solvers differ only in how they search that feasible set.

Solver

Search space

Mechanism / when used

solve_binary

\([0, 1]\)

Scalar search over the positive prevalence (grid, ternary, or bounded). Backbone of the binary matching methods.

ternary_search

interval

Derivative-free trisection for a unimodal objective; the engine behind solve_binary(solver="ternary").

solve_simplex

simplex

SLSQP with the equality constraint \(\sum_c p_c = 1\) and box bounds; the multiclass workhorse.

minimize_prevalence

both

Dispatcher: routes binary problems to solve_binary and multiclass problems to solve_simplex.

minimize_prevalence_blocks

simplex (per block)

Minimises one estimate per representation block (e.g. a histogram bin group) and aggregates them by the median; the recipe behind HDy/HDx.

13.1.3.2. Choosing a solver#

  • Leave solver="auto" for most cases: it selects a bounded scalar search for binary problems and SLSQP for multiclass problems.

  • Use "grid" when the objective may be multi-modal and you want a global search; use "ternary" when it is unimodal and you want speed.

  • minimize_prevalence is the entry point a quantifier should call, so the same code runs unchanged in both the binary and multiclass settings.

13.1.3.3. Used by#

Binary matching (DyS, HDy, SORD) goes through solve_binary; the constrained-regression and density methods (GACC, MMD_RKHS, KDEyML, EDy) go through solve_simplex; the histogram sweep (HDy / HDx) uses minimize_prevalence_blocks.

13.1.3.4. Example#

import numpy as np
from mlquantify.solvers import minimize_prevalence

def objective(p):
    target = np.array([0.4, 0.6])
    return np.sum((p - target) ** 2)

prevalences, value = minimize_prevalence(
    objective=objective,
    n_classes=2,
    solver="slsqp",
    random_state=0,
)

See also

Distribution Matching and Counting-Based Quantifiers for the quantifiers that delegate their optimisation to these solvers.