solve_binary#

mlquantify.solvers.solve_binary(objective, solver='auto', grid_size=101, tol=1e-06)[source]#

Minimize a scalar objective over the binary prevalence space [0, 1].

Supports three strategies: exhaustive grid search, ternary search (for unimodal objectives), and scipy’s bounded scalar minimizer.

Parameters:
objectivecallable

Function f(alpha) -> float where alpha is the prevalence of the positive class in [0, 1].

solver{‘auto’, ‘grid’, ‘ternary’, ‘bounded’}, default=’auto’

Optimization strategy.

  • 'auto' selects 'bounded'.

  • 'grid' evaluates objective at grid_size equally spaced points and returns the best.

  • 'ternary' applies ternary search (assumes unimodal objective).

  • 'bounded' uses scipy.optimize.minimize_scalar with method='bounded'.

grid_sizeint, default=101

Number of candidate prevalence values for the 'grid' solver.

tolfloat, default=1e-6

Convergence tolerance for the 'ternary' solver.

Returns:
prevalencendarray of shape (2,)

Estimated binary prevalence vector [1 - alpha, alpha].

lossfloat

Objective value at the optimum.

Raises:
ValueError

If solver is not one of the recognised identifiers.

Examples

>>> from mlquantify.solvers._binary import solve_binary
>>> prevalence, loss = solve_binary(lambda a: (a - 0.3) ** 2,
...                                 solver="bounded")
>>> round(prevalence[1], 2)
0.3