Исходный код pysatl_mpest.initializers.multi_start_refining
"""Facade that combines multistart with the refinement of every start."""
__author__ = "Danil Totmyanin"
__copyright__ = "Copyright (c) 2026 PySATL project"
__license__ = "SPDX-License-Identifier: MIT"
from collections.abc import Callable, Mapping, Sequence
from typing import Any
import numpy as np
from pysatl_core.families import ParametricFamily
from ..core import MixtureModel
from ..estimators import BaseEstimator
from ..typings import UnivariateFloatArray
from .initializer import Initializer
from .multi_start import MultiStartInitializer
from .refining import RefiningInitializer
[документация]
class MultiStartRefiningInitializer(Initializer):
"""Runs several starts refined by an estimator and returns the refinement with the largest log-likelihood.
Each of the ``n_init`` runs builds its start with the nested initializer and refines it with a short run of the
estimator; the result is the run with the largest log-likelihood after the refinement. This is the emEM scheme
with a short ``EM``: the length of the run is controlled by the breakpointers of the estimator.
The facade is equivalent to ``MultiStartInitializer(RefiningInitializer(initializer, estimator,
fit_kwargs=fit_kwargs), n_init)``. Note the difference from the reverse nesting
``RefiningInitializer(MultiStartInitializer(...), estimator)``, where only the best start is refined.
Parameters
----------
initializer : Initializer
The initializer that builds the starting mixture of every run.
estimator : BaseEstimator
The estimator that refines every start.
n_init : int | numpy.integer
The positive number of independent runs.
fit_kwargs : Mapping[str, Any] | Callable[[Sequence[ParametricFamily]], Mapping[str, Any]] | None, optional
Keyword arguments of every ``fit`` call after the sample and the mixture, or a function that computes them
from the families of each call.
Attributes
----------
initializer : Initializer
The nested initializer, the same object that was passed to the constructor (read-only).
estimator : BaseEstimator
The estimator, the same object that was passed to the constructor (read-only).
n_init : int
The number of runs as ``int`` (read-only).
fit_kwargs : dict[str, Any] | Callable[[Sequence[ParametricFamily]], Mapping[str, Any]]
A copy of the passed mapping; the values are shared. A passed function is stored as the same object
(read-only).
Raises
------
TypeError
If a role has the wrong type, or ``fit_kwargs`` is not a mapping, a callable or None, or a mapping does not
fit the signature of ``fit`` (the error of the signature is the ``__cause__``).
ValueError
If ``n_init`` is not a positive integer.
Methods
-------
.. autosummary::
:toctree: generated/
initialize
"""
def __init__(
self,
initializer: Initializer,
estimator: BaseEstimator,
n_init: int | np.integer,
*,
fit_kwargs: Mapping[str, Any] | Callable[[Sequence[ParametricFamily]], Mapping[str, Any]] | None = None,
) -> None:
self._refining = RefiningInitializer(initializer, estimator, fit_kwargs=fit_kwargs)
self._multi_start = MultiStartInitializer(self._refining, n_init)
@property
def initializer(self) -> Initializer:
"""Initializer: The nested initializer, the same object that was passed to the constructor."""
return self._refining.initializer
@property
def estimator(self) -> BaseEstimator:
"""BaseEstimator: The estimator, the same object that was passed to the constructor."""
return self._refining.estimator
@property
def n_init(self) -> int:
"""int: The number of runs."""
return self._multi_start.n_init
@property
def fit_kwargs(self) -> dict[str, Any] | Callable[[Sequence[ParametricFamily]], Mapping[str, Any]]:
"""The keyword arguments of every ``fit`` call: a copy of the passed mapping or the same function."""
return self._refining.fit_kwargs
def _initialize(
self, X: UnivariateFloatArray, families: Sequence[ParametricFamily], rng: np.random.Generator
) -> MixtureModel:
"""Delegate to the nested ``MultiStartInitializer`` over ``RefiningInitializer``.
The results, warnings and errors are theirs.
"""
return self._multi_start._initialize(X, families, rng)