spey.optimizer.core.fit#
- spey.optimizer.core.fit(func: Callable[[ndarray], ndarray], model_configuration: ModelConfig, do_grad: bool = False, hessian: Callable[[ndarray], ndarray] | None = None, initial_parameters: ndarray | None = None, bounds: List[Tuple[float, float]] | None = None, fixed_poi_value: float | Dict[int, float] | None = None, logpdf: Callable[[List[float]], float] | None = None, constraints: List[Dict] | None = None, **options) Tuple[float, ndarray][source]#
Dispatch a fit to either the scipy or minuit minimiser.
The minimiser is selected by the
minimizerkey inoptions(defaults to theSPEY_OPTIMISERenvironment variable, or"scipy").fixed_poi_valuemay be afloat(pins the primary POI) or adictof{index: value}(pins multiple parameters); pinned parameters are appended to the optimiser’s fixed-mask via the underlying minimiser.- Parameters:
func (
Callable[[np.ndarray], np.ndarray]) – Function to be optimised. Ifdo_grad=True, the function must returnTuple[float, np.ndarray]containing the value and its gradient with respect to the variational parameters.model_configuration (
ModelConfig) – Model configuration providing POI index, suggested initialisation, and suggested bounds.do_grad (
bool, defaultFalse) – Whetherfuncreturns its gradient alongside the value.hessian (
Optional[Callable[[np.ndarray], np.ndarray]], defaultNone) – Hessian forwarded to scipy as thehesskeyword. Ignored by minuit, which estimates the Hessian internally.initial_parameters (
Optional[np.ndarray], defaultNone) – Initial guess. WhenNone, falls back tosuggested_init.bounds (
Optional[List[Tuple[float, float]]], defaultNone) – Per- parameter(lower, upper)bounds (useNonefor an open side). WhenNone, bounds are derived from the model configuration plus the optional POI-fixing logic.fixed_poi_value (
Optional[Union[float, Dict[int, float]]], defaultNone) – If afloat, pins the model’s primary POI. If adict, pins every listed{index: value}.logpdf (
Optional[Callable[[List[float]], float]], defaultNone) – If provided, the function value returned is replaced bylogpdf(fit_parameters)(typical use: return the log-likelihood rather than the twice-NLL passed in asfunc).constraints (
Optional[List[Dict]], defaultNone) – Extra scipy-style constraint dicts forwarded to the underlying minimiser.**options – Forwarded to the selected minimiser; see
spey.optimizer.scipy_tools.minimize()andspey.optimizer.minuit_tools.minimize()for accepted keys.minimizerselects the backend.
- Returns:
The objective value (or
logpdf(fit_parameters)whenlogpdfis provided) and the corresponding fit parameter vector.- Return type:
Tuple[float, np.ndarray]