Fitting API¶
Top-level fitting helpers are documented under Top-level API. This
page documents objective, optimizer, and candidate-selection configuration
classes from gmm_divergence.fitting.
gmm_divergence.fitting
¶
Public fitting API.
Objective Configuration¶
ForwardKL
dataclass
¶
ForwardKL(sampling: SampleSpec = Draw())
Forward KL fitting objective configuration.
Fits the candidate-mixture weights by minimizing
where \(p\) is the reference distribution and \(q_{\mathbf{w}}\) is the weighted combination of the candidate mixtures. Since \(\log p(X)\) does not depend on \(\mathbf{w}\), the implemented objective minimizes the negative expected log-density of \(q_{\mathbf{w}}\) under samples from \(p\).
sampling
class-attribute
instance-attribute
¶
sampling: SampleSpec = field(default_factory=Draw)
Sampling specification for the expectation under p.
Use sampling.Draw(...) to draw fresh samples, sampling.Samples(...) for
precomputed reference samples, or sampling.Stratified(...) when p is a
Gaussian-family distribution and fixed per-component counts are desired.
ReverseKL
dataclass
¶
Reverse KL fitting objective configuration.
Fits the candidate-mixture weights by minimizing
The implementation evaluates a fixed-sample estimator using samples from the reference distribution \(p\) for diagnostics and samples from each candidate mixture \(q_i\) for the reverse objective.
p_sampling
class-attribute
instance-attribute
¶
p_sampling: SampleSpec = field(default_factory=Draw)
Sampling specification for diagnostics under p.
q_sampling
class-attribute
instance-attribute
¶
q_sampling: BatchSampleSpec = field(default_factory=Draw)
Sampling specification for fixed batches from each q_i.
Use sampling.Draw(...) to draw one batch per candidate distribution,
sampling.Stratified(...) for fixed per-component counts, or
sampling.SampleBatches(...) to provide those batches directly.
BidirectionalKL
dataclass
¶
BidirectionalKL(
p_sampling: SampleSpec = Draw(),
q_sampling: BatchSampleSpec = Draw(),
alpha: float = 0.5,
)
Bidirectional KL fitting objective configuration.
Fits the candidate-mixture weights by minimizing a weighted combination of forward and reverse KL objectives:
Values of \(\alpha\) closer to one emphasize coverage of \(p\) by \(q_{\mathbf{w}}\), while values closer to zero emphasize the reverse-KL objective.
alpha
class-attribute
instance-attribute
¶
Weight assigned to the forward KL term.
p_sampling
class-attribute
instance-attribute
¶
p_sampling: SampleSpec = field(default_factory=Draw)
Sampling specification for the forward term under p.
q_sampling
class-attribute
instance-attribute
¶
q_sampling: BatchSampleSpec = field(default_factory=Draw)
Sampling specification for the reverse term under each q_i.
Use sampling.Draw(...) to draw one batch per candidate distribution,
sampling.Stratified(...) for fixed per-component counts, or
sampling.SampleBatches(...) to provide those batches directly.
JensenShannon
dataclass
¶
Jensen-Shannon fitting objective configuration.
Fits the candidate-mixture weights by minimizing
where
This objective is symmetric and bounded, while still using fixed samples
from p and each candidate mixture q_i during optimization.
p_sampling
class-attribute
instance-attribute
¶
p_sampling: SampleSpec = field(default_factory=Draw)
Sampling specification for the term under p.
q_sampling
class-attribute
instance-attribute
¶
q_sampling: BatchSampleSpec = field(default_factory=Draw)
Sampling specification for fixed batches from each q_i.
Use sampling.Draw(...) to draw one batch per candidate distribution,
sampling.Stratified(...) for fixed per-component counts, or
sampling.SampleBatches(...) to provide those batches directly.
MomentMatching
dataclass
¶
Moment-matching fitting objective configuration.
Fits weights by matching moments of \(q_{\mathbf{w}}\) to moments of the reference distribution \(p\) instead of optimizing a sampled KL estimate. The first-moment objective compares mixture means,
and can optionally include second-moment information as well.
fit_second_moments
class-attribute
instance-attribute
¶
Whether to include covariance information in the objective.
Optimizers¶
SoftmaxLBFGSB
dataclass
¶
L-BFGS-B optimizer over unconstrained softmax logits.
The optimizer represents mixture weights through logits \(\mathbf{z} \in \mathbb{R}^N\) and maps them to simplex weights with
This avoids explicit simplex constraints in the optimizer while still ensuring that all fitted weights are non-negative and sum to one.
SimplexSLSQP
dataclass
¶
SLSQP optimizer over simplex-constrained weights.
The optimizer works directly with the mixture weights \(\mathbf{w} \in \Delta_N\), where
This keeps the optimization variables interpretable but requires the optimizer to enforce the simplex equality and bound constraints.
max_iterations
class-attribute
instance-attribute
¶
Maximum number of optimizer iterations.
min_weight
class-attribute
instance-attribute
¶
Minimum weight threshold for the optimizer.
Ideally, this would be zero, but SLSQP can fail when weights are exactly zero. Setting it to small positive values avoids numerical issues while still allowing the optimizer to effectively prune components with negligible weights.
Candidate Selectors¶
score_candidates
¶
score_candidates(
p: GaussianLike,
q_i: Sequence[GaussianLike],
/,
*,
direction: Literal["forward", "reverse", "bidirectional"] = "forward",
alpha: float = 0.5,
method: KLMethod | None = None,
prefer_closed_form: bool = True,
) -> FloatArray
Return KL-based scores for candidate distributions.
Source code in src/gmm_divergence/fitting/_selector.py
rank_candidates
¶
rank_candidates(
p: GaussianLike,
q_i: Sequence[GaussianLike],
/,
*,
direction: Literal["forward", "reverse", "bidirectional"] = "forward",
alpha: float = 0.5,
method: KLMethod | None = None,
prefer_closed_form: bool = True,
limit: int | None = None,
) -> list[tuple[int, float]]
Return candidate indices and scores sorted from best to worst.
Source code in src/gmm_divergence/fitting/_selector.py
TopKSelector
dataclass
¶
TopKSelector(
k: int,
*,
direction: Literal["forward", "reverse", "bidirectional"] = "forward",
alpha: float = 0.5,
kl_method: KLMethod = MonteCarlo(sampling=Draw(rng=0)),
)
Bases: _KLSelectorBase
ThresholdSelector
dataclass
¶
ThresholdSelector(
threshold: float,
*,
direction: Literal["forward", "reverse", "bidirectional"] = "forward",
alpha: float = 0.5,
kl_method: KLMethod = MonteCarlo(sampling=Draw(rng=0)),
)
Bases: _KLSelectorBase
Select candidates whose KL score is at or below a fixed threshold.
ToleranceSelector
dataclass
¶
ToleranceSelector(
delta: float,
mode: Literal["absolute", "relative"] = "absolute",
*,
direction: Literal["forward", "reverse", "bidirectional"] = "forward",
alpha: float = 0.5,
kl_method: KLMethod = MonteCarlo(sampling=Draw(rng=0)),
)
Bases: _KLSelectorBase
Select candidates within a tolerance of the best KL score.
In absolute mode, candidates with KL <= min(KL) + delta are kept. In
relative mode, candidates with KL <= min(KL) + delta * abs(min(KL)) are
kept, so delta=0.5 means within 50% of the best score when the best score
is positive.
QuantileSelector
dataclass
¶
QuantileSelector(
quantile: float,
*,
direction: Literal["forward", "reverse", "bidirectional"] = "forward",
alpha: float = 0.5,
kl_method: KLMethod = MonteCarlo(sampling=Draw(rng=0)),
)
Bases: _KLSelectorBase