MICE Package API
Top-level imports
MICE: Multi-Iteration stochastiC Estimator
A gradient estimator for stochastic optimization that uses successive control variates along the optimization path to reduce variance.
Core implementation
- class mice.core_impl.MICE(grad: ~typing.Callable[[~numpy.ndarray, ~typing.Any], ~numpy.ndarray], sampler: ~typing.Callable[[int], ~typing.Any] | ~numpy.ndarray | ~typing.Sequence[~typing.Any], eps: float = 0.577, min_batch: int = 10, restart_factor: int = 10, max_cost: float = inf, stop_crit_norm: float = 0.0, stop_crit_prob: float = 0.05, convex: bool = False, policy: ~mice.policy.DropRestartClipPolicy = <factory>, recorder: ~mice.logging.Recorder | None = None, use_resampling: bool = True, re_part: int = 5, re_quantile: float = 0.05, re_tot_cost: float = 0.2, re_min_n: int = 5, re_max_samp: int = 1000, max_grad_batch_elems: int = 5000000)[source]
Bases:
objectMulti-Iteration stochastiC Estimator for stochastic gradients.
This class maintains a hierarchy of control variates and adaptively allocates samples across levels to satisfy an error tolerance while reducing gradient evaluation cost.
- policy: DropRestartClipPolicy
Core compatibility layer
Compatibility shim for core imports.
- class mice.core.MICE(grad: ~typing.Callable[[~numpy.ndarray, ~typing.Any], ~numpy.ndarray], sampler: ~typing.Callable[[int], ~typing.Any] | ~numpy.ndarray | ~typing.Sequence[~typing.Any], eps: float = 0.577, min_batch: int = 10, restart_factor: int = 10, max_cost: float = inf, stop_crit_norm: float = 0.0, stop_crit_prob: float = 0.05, convex: bool = False, policy: ~mice.policy.DropRestartClipPolicy = <factory>, recorder: ~mice.logging.Recorder | None = None, use_resampling: bool = True, re_part: int = 5, re_quantile: float = 0.05, re_tot_cost: float = 0.2, re_min_n: int = 5, re_max_samp: int = 1000, max_grad_batch_elems: int = 5000000)[source]
Bases:
objectMulti-Iteration stochastiC Estimator for stochastic gradients.
This class maintains a hierarchy of control variates and adaptively allocates samples across levels to satisfy an error tolerance while reducing gradient evaluation cost.
- evaluate(x: ndarray) ndarray[source]
Evaluate a MICE gradient estimate at
xand update internal state.Returns the aggregated gradient estimator for the current iterate.
- policy: DropRestartClipPolicy
Policy
- class mice.policy.DropRestartClipPolicy(drop_param: float = 0.5, restart_param: float = 0.0, max_hierarchy_size: int = 1000, aggr_cost: float = 0.1, clip_type: str | None = None, clip_every: int = 0)[source]
Bases:
objectPolicy parameters.
Drop is checked every iteration. Clip can be checked every clip_every iterations or fully deactivated (clip_type=None).
Norm estimators
- class mice.norms.PlainNormEstimator(convex: bool = False, _best: float = inf)[source]
Bases:
objectPlain norm estimator: uses ||g_hat||.
- class mice.norms.ResamplingNormEstimator(re_part: int = 5, re_quantile: float = 0.05, stop_quantile: float = 0.95, convex: bool = False, _best_tol: float = inf, _best_stop: float = inf)[source]
Bases:
objectResampling norm estimator: consumes a vector of resampled ||g_hat|| values and returns two quantiles: - a conservative low-quantile for error tolerance selection - a quantile for the stochastic stopping rule
Sampling
State
- class mice.state.WelfordVec(mean: ndarray, m2: ndarray, n: int = 0)[source]
Bases:
objectOnline mean / M2 accumulator for vectors.
We store M2 per-coordinate (same shape as mean) so that sum(var_i) == M2.sum()/(n-1) matches the manuscript’s use of V_{l,k} = sum_i Var(Delta^{(i)}_{l,k}).
- classmethod zeros(dim: int, dtype=<class 'numpy.float64'>) WelfordVec[source]
- class mice.state.ResamplingAcc(re_part: int, sum_total: ndarray, cnt_total: int, sum_part: ndarray, cnt_part: ndarray)[source]
Bases:
objectMaintain partitioned sums/counts to compute leave-one-partition-out means efficiently (O(re_part * d) to materialize all LOO means).
- classmethod zeros(re_part: int, dim: int, dtype=<class 'numpy.float64'>) ResamplingAcc[source]
- class mice.state.LevelState(x: ndarray, cost: int, x_prev: ndarray | None, sample_fn: Callable[[int], Any], delta_stats: WelfordVec, base_stats: WelfordVec | None, m_min: int, delta_resamp: ResamplingAcc | None = None, base_resamp: ResamplingAcc | None = None, m_prev: int = 0)[source]
Bases:
objectStatistics for a single level (either base gradient or a gradient difference).
base level: Delta = grad(x_l)
diff level: Delta = grad(x_l) - grad(x_prev)
- delta_stats: WelfordVec
- base_stats: WelfordVec | None
- delta_resamp: ResamplingAcc | None
- base_resamp: ResamplingAcc | None
Logging
- class mice.logging.Recorder(events: ~typing.List[~typing.Dict[str, ~typing.Any]] = <factory>)[source]
Bases:
objectMinimal event recorder for MICE.
Keep this extremely lightweight; convert to pandas on demand (future).
Plotting
- mice.plot_mice.plot_mice(data, ax, x, y, style='loglog', markers=True, legend=True, color='C0')[source]
Plot MICE logs on the given Matplotlib axes.
- Parameters:
data (pandas.DataFrame) – DataFrame containing optimization log data, including event markers.
ax (matplotlib.axes.Axes) – Axes object where the data will be plotted.
x (str) – Column name used for the x-axis.
y (str) – Column name used for the y-axis.
style ({'loglog', 'semilogy', 'semilogx', 'plot'}) – Plot style controlling linear/log scaling of axes.
markers (bool) – If True, adds event markers (start, add, dropped, restart, end).
legend (bool) – If True, adds a legend.
color (str) – Line color passed to Matplotlib.
- Returns:
The updated axes.
- Return type:
matplotlib.axes.Axes