spey.optimizer.core.fit

Contents

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 minimizer key in options (defaults to the SPEY_OPTIMISER environment variable, or "scipy"). fixed_poi_value may be a float (pins the primary POI) or a dict of {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. If do_grad=True, the function must return Tuple[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, default False) – Whether func returns its gradient alongside the value.

  • hessian (Optional[Callable[[np.ndarray], np.ndarray]], default None) – Hessian forwarded to scipy as the hess keyword. Ignored by minuit, which estimates the Hessian internally.

  • initial_parameters (Optional[np.ndarray], default None) – Initial guess. When None, falls back to suggested_init.

  • bounds (Optional[List[Tuple[float, float]]], default None) – Per- parameter (lower, upper) bounds (use None for an open side). When None, bounds are derived from the model configuration plus the optional POI-fixing logic.

  • fixed_poi_value (Optional[Union[float, Dict[int, float]]], default None) – If a float, pins the model’s primary POI. If a dict, pins every listed {index: value}.

  • logpdf (Optional[Callable[[List[float]], float]], default None) – If provided, the function value returned is replaced by logpdf(fit_parameters) (typical use: return the log-likelihood rather than the twice-NLL passed in as func).

  • constraints (Optional[List[Dict]], default None) – Extra scipy-style constraint dicts forwarded to the underlying minimiser.

  • **options – Forwarded to the selected minimiser; see spey.optimizer.scipy_tools.minimize() and spey.optimizer.minuit_tools.minimize() for accepted keys. minimizer selects the backend.

Returns:

The objective value (or logpdf(fit_parameters) when logpdf is provided) and the corresponding fit parameter vector.

Return type:

Tuple[float, np.ndarray]