solve_simplex#

mlquantify.solvers.solve_simplex(objective, n_classes, x0=None, bounds=None, tol=1e-10, random_state=None)[source]#

Minimize a function over the probability simplex using SLSQP.

Applies scipy.optimize.minimize with the SLSQP method, an equality constraint ensuring the prevalence vector sums to 1, and box constraints bounding each component to [0, 1]. The result is clipped to non-negative values and re-normalized.

Parameters:
objectivecallable

Function f(p) -> float where p is an array of shape (n_classes,).

n_classesint

Number of classes (determines the dimensionality of x0).

x0array-like of shape (n_classes,) or None, default=None

Initial prevalence guess. Defaults to the uniform distribution when random_state is None, or to a random simplex point otherwise.

boundslist of (float, float) or None, default=None

Per-component box constraints. Defaults to [(0, 1)] * n_classes.

tolfloat, default=1e-10

Convergence tolerance passed to SLSQP.

random_stateint, RandomState instance, or None, default=None

Seed for generating a random starting point when x0 is None.

Returns:
prevalencendarray of shape (n_classes,)

Estimated prevalence vector summing to 1.

lossfloat

Objective value at the optimum.

Examples

>>> import numpy as np
>>> from mlquantify.solvers._simplex import solve_simplex
>>> target = np.array([0.2, 0.5, 0.3])
>>> objective = lambda p: np.sum((np.asarray(p) - target) ** 2)
>>> prevalence, loss = solve_simplex(objective, n_classes=3)
>>> np.round(prevalence, 2)
array([0.2, 0.5, 0.3])