Statistical Model#
This module defines StatisticalModel, the central user-facing object in
spey. It wraps any backend that inherits BackendBase and provides a
unified API for likelihood evaluation, hypothesis testing, upper-limit extraction, and
model combination. The module also exposes statistical_model_wrapper(), the
decorator that backends are registered with, and the PoiTest type alias used
throughout.
Decorator that promotes a |
- class spey.StatisticalModel(backend: BackendBase, analysis: str, xsection: float = nan, ntoys: int = 1000)[source]#
Bases:
HypothesisTestingBaseUnified interface to any
speystatistical model backend.StatisticalModelis the central user-facing object inspey. It wraps any backend that inheritsBackendBase, giving every backend a consistent API for:evaluating the (negative) log-likelihood \(-\log\mathcal{L}(\mu,\hat{\theta}_\mu)\);
maximising the likelihood to obtain \(\hat\mu\) and \(\hat{\theta}\);
computing p-values, CLs values, and upper limits via asymptotic, toy-based, or \(\chi^2\) calculators;
generating Asimov data;
combining two models with the
@operator.
Instances are normally obtained through
spey.get_backend()rather than constructed directly:import spey # Obtain a backend constructor wrapped as StatisticalModel pdf_wrapper = spey.get_backend("default.poisson") # Build the statistical model model = pdf_wrapper( signal_yields=[12.0, 15.0], background_yields=[50.0, 60.0], data=[48, 63], analysis="my_analysis", xsection=0.05, # pb )
Likelihood evaluation
# Negative log-likelihood at mu = 1 nll = model.likelihood(poi_test=1.0) # Profile likelihood ratio test statistic nll_free, _ = model.maximize_likelihood() # Asimov (expected) likelihood nll_asimov = model.asimov_likelihood(poi_test=1.0)
Hypothesis testing
# Observed CLs value cls_obs = model.exclusion_confidence_level(poi_test=1.0) # Expected (apriori) CLs value cls_exp = model.exclusion_confidence_level( poi_test=1.0, expected=spey.ExpectationType.apriori ) # One-sided 95 % CL upper limit on the signal strength mu_ul = model.poi_upper_limit(confidence_level=0.95) # Upper limit on the cross section (requires xsection to be set) xsec_ul = model.s95obs
Multi-parameter fits
When a backend exposes more than one parameter of interest,
poi_testandpoi_indicesaccept either anintindex, astrparameter name, or adictmapping indices / names to values:# Fix mu_0 = 1.0, mu_1 = 0.5 nll = model.likelihood(poi_test={0: 1.0, 1: 0.5}) # Retrieve fitted values of two named POIs muhat_dict, nll = model.maximize_likelihood(poi_indices=[0, 1])
Model combination
combined = model_a @ model_b # uses the @ operator # or equivalently: combined = model_a.combine(model_b)
- Parameters:
backend (
BackendBase) – Statistical model backend. Must be an instance of a class that inheritsBackendBase.analysis (
str) – Unique identifier of the statistical model used for book-keeping purposes.xsection (
float, defaultnp.nan) – Signal cross section in units chosen by the user. Only required forexcluded_cross_section(),s95obs, ands95exp.ntoys (
int, default1000) – Number of pseudo-experiments (toys) used by the toy-based hypothesis-testing calculator. Ignored when the asymptotic or \(\chi^2\) calculator is used.
- Raises:
AssertionError – If
backenddoes not inheritBackendBase.- Returns:
A statistical model object wrapping the given backend with a unified hypothesis- testing interface.
- Return type:
- analysis: str#
Unique identifier as analysis name
- asimov_likelihood(poi_test: float | Dict[int | str, float] = 1.0, expected: ExpectationType = observed, return_nll: bool = True, test_statistics: str = 'qtilde', init_pars: List[float] | None = None, par_bounds: List[Tuple[float, float]] | None = None, **kwargs) float[source]#
Compute likelihood of the statistical model generated with the Asimov data.
- Parameters:
poi_test (
PoiTest, default1.0) – Parameter of interest, \(\mu\). Accepts the same formats aslikelihood(): a plainfloatfixes the primary POI, while adictof{index_or_name: value}fixes multiple parameters simultaneously.expected (ExpectationType) –
Sets which values the fitting algorithm should focus and p-values to be computed.
observed: Computes the p-values with via post-fit prescription which means that the experimental data will be assumed to be the truth (default).aposteriori: Computes the expected p-values with via post-fit prescription which means that the experimental data will be assumed to be the truth.apriori: Computes the expected p-values with via pre-fit prescription which means that the SM will be assumed to be the truth.
return_nll (
bool, defaultTrue) – IfTrue, returns negative log-likelihood value. ifFalsereturns likelihood value.test_statistics (
Text, default"qtilde") –test statistics.
'qtilde': (default) performs the calculation using the alternative test statistic, \(\tilde{q}_{\mu}\), see eq. (62) of [arXiv:1007.1727] (qmu_tilde()).Warning
Note that this assumes that \(\hat\mu\geq0\), hence
allow_negative_signalassumed to beFalse. If this function has been executed by user,speyassumes that this is taken care of throughout the external code consistently. Whilst computing p-values or upper limit on \(\mu\) throughspeythis is taken care of automatically in the backend.'q': performs the calculation using the test statistic \(q_{\mu}\), see eq. (54) of [arXiv:1007.1727] (qmu()).'q0': performs the calculation using the discovery test statistic, see eq. (47) of [arXiv:1007.1727] \(q_{0}\) (q0()).
The choice of
test_statisticswill effect the generation of the Asimov data where the fit is performed via \(\mu=1\) iftest_statistics="q0"and \(\mu=0\) for others. Note that this \(\mu\) does not correspond to thepoi_testinput of this function but it determines how Asimov data is generated.init_pars (
List[float], defaultNone) – initial parameters for the optimiserpar_bounds (
List[Tuple[float, float]], defaultNone) – parameter bounds for the optimiser.kwargs –
Keyword arguments forwarded to both the Asimov data generation fit (via
generate_asimov_data()) and the subsequent likelihood evaluation (vialikelihood()). Both calls receive an independent copy ofkwargs.Consumed by
prepare_for_fit:do_grad(bool, defaultTrue): Whether to request the gradient of the objective function from the backend. Falls back toFalseautomatically if the backend raisesNotImplementedError.constraints(List[Dict], default[]): Additional scipy-style constraint dicts to pass to the optimiser. Any constraints defined on the backend itself are always appended.
Consumed by
fit(the core optimisation loop):minimizer(str, default"scipy"or the value of theSPEY_OPTIMISERenvironment variable): Selects the numerical minimiser. Accepted values are"scipy"and"minuit"(requiresiminuit).hessian(Callable[[np.ndarray], np.ndarray], defaultNone): Hessian of the objective function. Passed to scipy ashess; ignored by minuit.
Scipy-minimiser options (used when
minimizer="scipy"):method(str, default"SLSQP"): Scipy optimisation method.maxiter(int, default10000): Maximum number of iterations.tol(float, default1e-6): Convergence tolerance.disp(bool, defaultFalse): IfTrue, print convergence messages.ntrials(int, default1): Number of re-tries with progressively expanded parameter bounds when the minimiser does not converge.
Minuit-minimiser options (used when
minimizer="minuit"):method(str, default"migrad"): Minuit algorithm ("migrad"or"simplex").maxiter(int, default10000): Maximum number of function calls.tol(float, default1e-6): Convergence tolerance.disp(int, default0): Minuit print level (0= silent).strategy(int, default0): Minuit strategy (0= fast,1= default,2= slow but more accurate).errordef(float, defaultMinuit.LIKELIHOOD): Value by which Minuit defines a one-sigma interval (0.5for NLL,1.0for \(\chi^2\)).
Note
fixed_poi_valueis not an accepted kwarg here. The POI used for Asimov data generation is determined bytest_statistics(1.0for"q0",0.0otherwise), andpoi_testfixes the POI for the likelihood evaluation. Passingfixed_poi_valuewould cause aTypeErrorin both inner calls and is therefore intercepted and discarded with a warning.Unknown keys are logged as a warning and silently discarded by the minimiser.
- Returns:
likelihood computed for asimov data
- Return type:
float
- property available_calculators: List[str]#
Returns available calculator names.
Possible entries are
'toy','asymptotic', and'chi_square', depending on what the underlying backend supports.- Returns:
Subset of
['toy', 'asymptotic', 'chi_square']listing the calculators that are available for this model.- Return type:
List[str]
- property backend: BackendBase#
The underlying backend instance.
- Returns:
The backend object that was supplied at construction time. All likelihood and sampling calls are delegated to this object.
- Return type:
BackendBase
- property backend_type: str#
Human-readable name of the backend.
Returns the value of the backend’s
nameattribute when present, and falls back to the class name otherwise.- Returns:
Backend identifier string (e.g.
"default.poisson").- Return type:
str
- chi2(poi_test: float | Dict[int | str, float] = 1.0, poi_test_denominator: float | Dict[int | str, float] | None = None, expected: ExpectationType = observed, allow_negative_signal: bool = False, init_pars: List[float] | None = None, par_bounds: List[Tuple[float, float]] | None = None, mle_kwargs=None, likelihood_kwargs=None) float#
Compute the profile likelihood ratio \(\chi^2\) test statistic.
When
poi_test_denominator=None, evaluates the profile likelihood ratio against the unconditional maximum:\[\chi^2 = -2\log\left(\frac{\mathcal{L}(\mu,\hat\theta_\mu)}{\mathcal{L}(\hat\mu,\hat\theta)}\right)\]When
poi_test_denominatoris set, it replaces the denominator with a second fixed-\(\mu\) likelihood:\[\chi^2 = -2\log\left(\frac{\mathcal{L}(\mu,\theta_\mu)}{\mathcal{L}(\mu_{\rm denom},\theta_{\mu_{\rm denom}})}\right)\]where \(\mu_{\rm denom}\) is
poi_test_denominatorwhich is typically zero to compare signal model with the background only model.- Parameters:
poi_test (
PoiTestorlist[float], default1.0) – Parameter of interest, \(\mu\). A plainfloat(or iterable of floats) fixes the primary POI — when iterable, \(\chi^2\) is computed for each element. Alternatively, adictof{index_or_name: value}fixes multiple parameters simultaneously (iterating over dicts is not supported).poi_test_denominator (
PoiTest, defaultNone) – Parameter of interest for the denominator. Accepts the same formats aspoi_test. IfNonethe maximum likelihood is computed instead.expected (ExpectationType) –
Sets which values the fitting algorithm should focus and p-values to be computed.
observed: Computes the p-values with via post-fit prescription which means that the experimental data will be assumed to be the truth (default).aposteriori: Computes the expected p-values with via post-fit prescription which means that the experimental data will be assumed to be the truth.apriori: Computes the expected p-values with via pre-fit prescription which means that the SM will be assumed to be the truth.
allow_negative_signal (
bool, defaultTrue) – IfTrue\(\hat\mu\) value will be allowed to be negative. Only valid whenpoi_test_denominator=None.init_pars (
List[float], defaultNone) – initial parameters for the optimiserpar_bounds (
List[Tuple[float, float]], defaultNone) – parameter bounds for the optimiser.mle_kwargs (
dict, defaultNone) –Keyword arguments forwarded to the denominator evaluation. When
poi_test_denominator=Nonethey are passed tomaximize_likelihood()(free fit of \(\hat\mu,\hat\theta\)); otherwise they are passed tolikelihood()at the fixed \(\mu_{\rm denom}\). IfNone, an empty dict is used. Accepted keys:Consumed by
prepare_for_fit:do_grad(bool, defaultTrue): Request the gradient of the objective function from the backend. Falls back toFalseautomatically if the backend raisesNotImplementedError.constraints(List[Dict], default[]): Additional scipy-style constraint dicts appended to any backend-defined constraints.fixed_poi_value(Union[float, Dict[int, float]], defaultNone): Fix one or more POIs during the maximisation while the remaining parameters are profiled freely. Only effective in themaximize_likelihoodbranch (i.e. whenpoi_test_denominator=None); ignored (with a warning) bylikelihood, which takes the fixed POIs frompoi_test_denominator.
Consumed by
fit(the core optimisation loop):minimizer(str, default"scipy"or the value of theSPEY_OPTIMISERenvironment variable): Selects the numerical minimiser. Accepted values are"scipy"and"minuit"(requiresiminuit).hessian(Callable[[np.ndarray], np.ndarray], defaultNone): Hessian of the objective function. Passed to scipy as thehessargument; ignored by minuit.
Scipy-minimiser options (used when
minimizer="scipy"):method(str, default"SLSQP"): Scipy optimisation method (e.g."SLSQP","L-BFGS-B","trust-constr").maxiter(int, default10000): Maximum number of iterations.tol(float, default1e-6): Convergence tolerance.disp(bool, defaultFalse): Print convergence messages ifTrue.ntrials(int, default1): Number of re-tries with progressively expanded parameter bounds when the minimiser does not converge.
Minuit-minimiser options (used when
minimizer="minuit"):method(str, default"migrad"): Minuit algorithm ("migrad"or"simplex").maxiter(int, default10000): Maximum number of function calls.tol(float, default1e-6): Convergence tolerance.disp(int, default0): Minuit print level (0= silent).strategy(int, default0): Minuit strategy (0= fast,1= default,2= slow but more accurate).errordef(float, defaultMinuit.LIKELIHOOD): Value by which Minuit defines a one-sigma interval (0.5for NLL,1.0for \(\chi^2\)).
Unknown keys are logged as a warning and silently discarded by the minimiser.
likelihood_kwargs (
dict, defaultNone) –Keyword arguments forwarded to
likelihood()when evaluating the numerator at each requestedpoi_testvalue (including every element of an iterable or scan). IfNone, an empty dict is used. Accepts the same keys asmle_kwargsabove, with one caveat:fixed_poi_valueis not supported here — the numerator’s fixed POIs are controlled entirely bypoi_test. Passingfixed_poi_valueis logged as a warning and discarded.
- Returns:
value of the \(\chi^2\).
- Return type:
float
- chi2_test(expected: ExpectationType = observed, confidence_level: float = 0.95, limit_type: Literal['right', 'left', 'two-sided'] = 'two-sided', allow_negative_signal: bool = None, parameter: int | str | None = None, poi_value: float = 1.0, n_scan: int = 3, n_multistart: int = 2, **kwargs) List[float]#
Determine parameter value(s) that constrain the \(\chi^2\) distribution at a specified confidence level via 1D profiling.
When
parameter=None(default), the method profiles the primary POI and finds the POI values where the profile \(\chi^2\) equals the thresholdchi2.isf(alpha, df=1).When
parameteris set to a nuisance parameter index or name, the POI is fixed topoi_value(default1.0) and the method profiles the chosen nuisance parameter instead, locating the nuisance value(s) at the same \(\chi^2\) threshold. This is useful for setting 1D confidence intervals on any model parameter.Added in version 0.2.0.
Changed in version 0.2.7: The ability to profile any given nuisance parameter has been implemented. The 1D profile is now enumerated by coarse scan plus bracketed root refinement, so non-convex likelihoods with disjoint confidence regions return every crossing in ascending order. A small multi-start is performed before the root search to harden the NLL minimum used as the anchor of the \(\chi^2\) threshold.
Attention
The degrees of freedom are set to one, referring to the single profiled parameter (either the POI or the selected nuisance parameter).
- Parameters:
expected (ExpectationType) –
Specifies the type of expectation for the fitting algorithm and p-value computation.
observed: Computes p-values using post-fit prescription, assuming experimental data as the truth.apriori: Computes expected p-values using pre-fit prescription, assuming the Standard Model (SM) as the truth.
confidence_level (
float, default0.95) – The confidence level for the interval. Must be between 0 and 1. This refers to the total inner area under the bell curve, noted as \(CL\) below.limit_type (
'right','left'or'two-sided', default'two-sided') – Specifies which side of the \(\chi^2\) distribution should be constrained.allow_negative_signal (
bool, defaultNone) – Controls whether the POI can be negative during the global unconstrained maximisation. IfNone, it is set toTruefor two-sided and left limits, andFalsefor right limits. Ignored whenparameteris notNone(the global fit is always unconstrained in that case).parameter (
intorstr, defaultNone) – Index or name of the nuisance parameter to profile. WhenNone(default) the primary POI is profiled (existing behaviour). When set, the POI is fixed topoi_valueand the selected nuisance parameter is scanned instead. String values are resolved viaparameter_names.poi_value (
float, default1.0) – Fixed value of the primary POI when profiling a nuisance parameter (i.e. whenparameteris notNone). Has no effect whenparameter=None. If poi_value=None, primary POI will also be minimised during optimisation.n_scan (
int, default121) – Number of uniformly-spaced grid points used by the coarse scan that enumerates sign changes of the profile \(\chi^2 - \text{threshold}\) function. Each sign-change interval is then refined to full precision withtoms748(). Increasing this value improves detection of narrow features in non-convex profiles at the cost of additional NLL evaluations; values below 3 are clamped to 3.n_multistart (
int, default9) – Number of evenly-spaced evaluations used by the internal multi-start scan that re-anchors the NLL minimum before the root search. A bounded scalar minimisation is then run around the best point found by the scan. Increasing this value reduces the risk of the anchor being trapped in a local minimum at the cost of additional NLL evaluations; values below 2 are clamped to 2.
- Keyword Arguments:
xtol (
float, default2e-12) – Absolute tolerance passed totoms748(). The root-finder stops when the bracket width falls below this value.rtol (
float, default1e-4) – Relative tolerance passed totoms748(). The root-finder stops when the bracket width is smaller thanrtol * |root|.maxiter (
int, default10000) – Maximum number of function evaluations allowed insidetoms748().
- Returns:
Parameter value(s) at which the profile \(\chi^2\) equals the threshold, in ascending order. For a convex profile this is one value for one-sided limits and two values for two-sided limits (backwards compatible); for a non-convex profile with disjoint confidence regions every crossing is returned, so users that rely on a fixed list length should check
len(result)before unpacking.- Return type:
List[float]- Raises:
ValueError – If
parameterrefers to the POI index, if the parameter name is not found in the model config, if the parameter index is out of range, or if the model has only one parameter (no nuisance parameters to profile).
- combine(other, **kwargs)[source]#
Combination routine between two statistical models.
Note
This function’s availability is backend dependent.
- Parameters:
other (
StatisticalModel) – Statistical model to be combined with this modelkwargs – backend specific arguments.
- Raises:
CombinerNotAvailable – If this statistical model does not have a combination routine implementation.
AssertionError – If the combination routine in the backend does not return a
BackendBaseobject.
- Returns:
Returns a new combined statistical model.
- Return type:
- property config: ModelConfig#
Retreive model configuration
- excluded_cross_section(expected: ExpectationType = observed) float[source]#
Compute excluded cross section value at 95% CL
- Parameters:
expected (ExpectationType) –
Sets which values the fitting algorithm should focus and p-values to be computed.
observed: Computes the p-values with via post-fit prescription which means that the experimental data will be assumed to be the truth (default).aposteriori: Computes the expected p-values with via post-fit prescription which means that the experimental data will be assumed to be the truth.apriori: Computes the expected p-values with via pre-fit prescription which means that the SM will be assumed to be the truth.
- Raises:
UnknownCrossSection – If the cross-section is
nan.- Returns:
Returns the upper limit at 95% CL on cross section value where the unit is defined by the user.
- Return type:
float
- exclusion_confidence_level(poi_test: float | Dict[int | str, float] = 1.0, expected: ExpectationType = observed, allow_negative_signal: bool = False, calculator: Literal['asymptotic', 'toy', 'chi_square'] = 'asymptotic', **kwargs) List[float]#
Compute the exclusion confidence level \(CL_s\) at a given \(\mu\).
\(CL_s\) is defined as
\[CL_s = \frac{p_{s+b}}{1 - p_b}\]and is returned as \(1 - p\text{-value}\). The number of returned values depends on the
expectedmode:observed→ one value (fitted to observed data).aposteriori/apriori→ five values representing \(-2\sigma,\,-1\sigma,\,\text{central},\,+1\sigma,\,+2\sigma\) fluctuations from the background.
- Parameters:
poi_test (
PoiTest, default1.0) – Parameter of interest \(\mu\) at which to evaluate \(CL_s\).expected (ExpectationType) –
Selects the expectation mode.
observed: Post-fit, returns one value (default).aposteriori: Post-fit nuisance treatment, returns five expected values.apriori: Pre-fit / SM hypothesis, returns five expected values.
Setting
expected="all"returns both the observed and the five expected values simultaneously.allow_negative_signal (
bool, defaultFalse) – WhenTrue, \(\hat\mu\) is unconstrained, switching the test statistic from \(\tilde{q}_\mu\) to \(q_\mu\).calculator (
'asymptotic','toy'or'chi_square', default'asymptotic') –"asymptotic": Asymptotic formulae from [arXiv:1007.1727]."toy": Pseudo-experiment-based p-values (requiresis_toy_calculator_available)."chi_square": \(\chi^2\)-based p-values; uses \(\chi^2 = -2\log[\mathcal{L}(\mu,\hat\theta_\mu)/\mathcal{L}(0,\hat\theta_0)]\).
kwargs –
Additional keyword arguments forwarded to the optimiser, including:
init_pars (
List[float], defaultNone): Initial parameter values for the optimiser.par_bounds (
List[Tuple[float, float]], defaultNone): Parameter bounds for the optimiser.
- Raises:
CalculatorNotAvailable – If the requested
calculatoris not available.- Returns:
\(CL_s\) value(s). One value for
observed; five values ordered \((-2\sigma,\,-1\sigma,\,\text{central},\,+1\sigma,\,+2\sigma)\) for expected modes.- Return type:
List[float]
- fixed_poi_sampler(poi_test: float | Dict[int | str, float], size: int | None = None, expected: ExpectationType = observed, init_pars: List[float] | None = None, par_bounds: List[Tuple[float, float]] | None = None, **kwargs) ndarray | Callable[[int], ndarray][source]#
Sample data from the statistical model with fixed parameter of interest.
- Parameters:
poi_test (
PoiTest) – Parameter of interest, \(\mu\). A plainfloatfixes the primary POI; adictof{index_or_name: value}fixes multiple parameters at once.size (
int, defaultNone) – sample size. IfNonea callable function will be returned which takes sample size as input.expected (ExpectationType) –
Sets which values the fitting algorithm should focus and p-values to be computed.
observed: Computes the p-values with via post-fit prescription which means that the experimental data will be assumed to be the truth (default).aposteriori: Computes the expected p-values with via post-fit prescription which means that the experimental data will be assumed to be the truth.apriori: Computes the expected p-values with via pre-fit prescription which means that the SM will be assumed to be the truth.
init_pars (
List[float], defaultNone) – initial parameters for the optimiserpar_bounds (
List[Tuple[float, float]], defaultNone) – parameter bounds for the optimiser.kwargs – keyword arguments for the optimiser.
- Raises:
MethodNotAvailable – If backend does not have sampler implementation.
- Returns:
Sampled data with shape of
(size, number of bins)or callable function to sample from directly.- Return type:
Union[np.ndarray, Callable[[int], np.ndarray]]
- generate_asimov_data(expected: ExpectationType = observed, test_statistic: str = 'qtilde', init_pars: List[float] | None = None, par_bounds: List[Tuple[float, float]] | None = None, **kwargs) List[float][source]#
Generate Asimov data for the statistical model. This function generates a set of parameters (nuisance and poi i.e. \(\theta\) and \(\mu\)) with respect to
test_statisticinput which determines the value of \(\mu\) i.e. iftest_statistic="q0"\(\mu=1\) and 0 for anything else. The objective function is used to optimize the statistical model to find the fit parameters for fixed poi optimisation. Then fit parameters are used to retrieve the expected data throughexpected_data()function.- Parameters:
expected (ExpectationType) –
Sets which values the fitting algorithm should focus and p-values to be computed.
observed: Computes the p-values with via post-fit prescription which means that the experimental data will be assumed to be the truth (default).aposteriori: Computes the expected p-values with via post-fit prescription which means that the experimental data will be assumed to be the truth.apriori: Computes the expected p-values with via pre-fit prescription which means that the SM will be assumed to be the truth.
test_statistic (
Text, default"qtilde") –test statistics.
'qtilde': (default) performs the calculation using the alternative test statistic, \(\tilde{q}_{\mu}\), see eq. (62) of [arXiv:1007.1727] (qmu_tilde()).Warning
Note that this assumes that \(\hat\mu\geq0\), hence
allow_negative_signalassumed to beFalse. If this function has been executed by user,speyassumes that this is taken care of throughout the external code consistently. Whilst computing p-values or upper limit on \(\mu\) throughspeythis is taken care of automatically in the backend.'q': performs the calculation using the test statistic \(q_{\mu}\), see eq. (54) of [arXiv:1007.1727] (qmu()).'q0': performs the calculation using the discovery test statistic, see eq. (47) of [arXiv:1007.1727] \(q_{0}\) (q0()).
init_pars (
List[float], defaultNone) – initial parameters for the optimiserpar_bounds (
List[Tuple[float, float]], defaultNone) – parameter bounds for the optimiser.kwargs – keyword arguments for the optimiser.
- Returns:
Asimov data
- Return type:
List[float]
- property is_alive: bool#
Returns True if at least one bin has non-zero signal yield.
- property is_asymptotic_calculator_available: bool#
Whether the asymptotic calculator can be used with this backend.
The asymptotic calculator requires either:
a working
expected_data()implementation, orboth
asimov_negative_loglikelihood()andminimize_asimov_negative_loglikelihood()to be overridden by the backend.
- Returns:
Trueif the asymptotic calculator is available.- Return type:
bool
- property is_chi_square_calculator_available: bool#
Whether the \(\chi^2\) calculator can be used with this backend.
The \(\chi^2\) calculator only requires the negative log-likelihood, which every backend must implement, so this property always returns
True.- Returns:
Always
True.- Return type:
bool
- property is_toy_calculator_available: bool#
Whether the toy (pseudo-experiment) calculator can be used with this backend.
Requires the backend to override
get_sampler().- Returns:
Trueif the toy calculator is available.- Return type:
bool
- likelihood(poi_test: float | Dict[int | str, float] = 1.0, expected: ExpectationType = observed, return_nll: bool = True, data: List[float] | ndarray | None = None, return_parameters: bool = False, init_pars: List[float] | None = None, par_bounds: List[Tuple[float, float]] | None = None, **kwargs) float | Tuple[float, ndarray][source]#
Compute the likelihood of the statistical model at a fixed parameter of interest.
- Parameters:
poi_test (
PoiTest, default1.0) – Parameter of interest, \(\mu\). Can be a singlefloat(fixes the primary POI identified bypoi_index) or adictmapping POI indices (int) or names (str) to their fixed values. String keys are resolved viaparameter_names. When afloatis given, behaviour is identical to previous versions.expected (ExpectationType) –
Sets which values the fitting algorithm should focus and p-values to be computed.
observed: Computes the p-values with via post-fit prescription which means that the experimental data will be assumed to be the truth (default).aposteriori: Computes the expected p-values with via post-fit prescription which means that the experimental data will be assumed to be the truth.apriori: Computes the expected p-values with via pre-fit prescription which means that the SM will be assumed to be the truth.
return_nll (
bool, defaultTrue) – IfTrue, returns negative log-likelihood value. ifFalsereturns likelihood value.data (
Union[List[float], np.ndarray], defaultNone) – input data that to fit. IfNonedata will be set according toexpectedinput.return_parameters (
bool, defaultFalse) – Return fit parameters.init_pars (
List[float], defaultNone) – initial parameters for the optimiserpar_bounds (
List[Tuple[float, float]], defaultNone) – parameter bounds for the optimiser.kwargs –
Keyword arguments forwarded through
prepare_for_fitto the optimiser. The following keys are recognised:Consumed by
prepare_for_fit:do_grad(bool, defaultTrue): Whether to request the gradient of the objective function from the backend. Falls back toFalseautomatically if the backend raisesNotImplementedError.constraints(List[Dict], default[]): Additional scipy-style constraint dicts to pass to the optimiser. Any constraints defined on the backend itself are always appended.
Consumed by
fit(the core optimisation loop):minimizer(str, default"scipy"or the value of theSPEY_OPTIMISERenvironment variable): Selects the numerical minimiser. Accepted values are"scipy"and"minuit"(requiresiminuit).hessian(Callable[[np.ndarray], np.ndarray], defaultNone): Hessian of the objective function with respect to the variational parameters. Passed to scipy as thehessargument; ignored by the minuit minimiser.
Scipy-minimiser options (used when
minimizer="scipy"):method(str, default"SLSQP"): Scipy optimisation method (e.g."SLSQP","L-BFGS-B","trust-constr").maxiter(int, default10000): Maximum number of iterations.tol(float, default1e-6): Convergence tolerance.disp(bool, defaultFalse): IfTrue, print convergence messages.ntrials(int, default1): Number of re-tries with progressively expanded parameter bounds when the minimiser does not converge.
Minuit-minimiser options (used when
minimizer="minuit"):method(str, default"migrad"): Minuit algorithm. Accepted values are"migrad"and"simplex".maxiter(int, default10000): Maximum number of function calls.tol(float, default1e-6): Convergence tolerance.disp(int, default0): Minuit print level (0= silent).strategy(int, default0): Minuit strategy (0= fast,1= default,2= slow but more accurate).errordef(float, defaultMinuit.LIKELIHOOD): Value by which Minuit defines a one-sigma interval (0.5for NLL,1.0for \(\chi^2\)).
Unknown keys are logged as a warning and silently discarded by the minimiser.
- Returns:
Likelihood of the statistical model at a fixed signal strength.
- Return type:
float
- maximize_asimov_likelihood(return_nll: bool = True, expected: ExpectationType = observed, test_statistics: str = 'qtilde', init_pars: List[float] | None = None, par_bounds: List[Tuple[float, float]] | None = None, poi_indices: List[int | str] | None = None, **kwargs) Tuple[float | Dict[int | str, float], float][source]#
Find the maximum of the likelihood which computed with respect to Asimov data.
- Parameters:
return_nll (
bool, defaultTrue) – IfTrue, returns negative log-likelihood value. ifFalsereturns likelihood value.expected (ExpectationType) –
Sets which values the fitting algorithm should focus and p-values to be computed.
observed: Computes the p-values with via post-fit prescription which means that the experimental data will be assumed to be the truth (default).aposteriori: Computes the expected p-values with via post-fit prescription which means that the experimental data will be assumed to be the truth.apriori: Computes the expected p-values with via pre-fit prescription which means that the SM will be assumed to be the truth.
test_statistics (
Text, default"qtilde") –test statistic.
'qtilde': (default) performs the calculation using the alternative test statistic, \(\tilde{q}_{\mu}\), see eq. (62) of [arXiv:1007.1727] (qmu_tilde()).Warning
Note that this assumes that \(\hat\mu\geq0\), hence
allow_negative_signalassumed to beFalse. If this function has been executed by user,speyassumes that this is taken care of throughout the external code consistently. Whilst computing p-values or upper limit on \(\mu\) throughspeythis is taken care of automatically in the backend.'q': performs the calculation using the test statistic \(q_{\mu}\), see eq. (54) of [arXiv:1007.1727] (qmu()).'q0': performs the calculation using the discovery test statistic, see eq. (47) of [arXiv:1007.1727] \(q_{0}\) (q0()).
init_pars (
List[float], defaultNone) – initial parameters for the optimiserpar_bounds (
List[Tuple[float, float]], defaultNone) – parameter bounds for the optimiser.poi_indices (
List[Union[int, str]], defaultNone) – IfNone, returns the primary POI value as a singlefloat. If a list of parameter indices (int) or names (str) is provided, returns adictmapping each requested key to its fitted value. Passed directly tomaximize_likelihood().kwargs –
Keyword arguments forwarded to both the Asimov data generation fit (via
generate_asimov_data()) and the subsequent maximisation (viamaximize_likelihood()). Both calls receive an independent copy ofkwargs.Consumed by
prepare_for_fit:do_grad(bool, defaultTrue): Whether to request the gradient of the objective function from the backend. Falls back toFalseautomatically if the backend raisesNotImplementedError.constraints(List[Dict], default[]): Additional scipy-style constraint dicts to pass to the optimiser. Any constraints defined on the backend itself are always appended.
Consumed by
fit(the core optimisation loop):minimizer(str, default"scipy"or the value of theSPEY_OPTIMISERenvironment variable): Selects the numerical minimiser. Accepted values are"scipy"and"minuit"(requiresiminuit).hessian(Callable[[np.ndarray], np.ndarray], defaultNone): Hessian of the objective function. Passed to scipy ashess; ignored by minuit.fixed_poi_value(Union[float, Dict[int, float]], defaultNone): Fix one or more POIs during the maximisation step while allowing the remaining parameters to be profiled freely. A plainfloatfixes the primary POI; adictof{index: value}fixes multiple POIs simultaneously. This kwarg is intercepted and discarded (with a warning) in the Asimov data generation step, where the POI is already determined bytest_statistics.
Scipy-minimiser options (used when
minimizer="scipy"):method(str, default"SLSQP"): Scipy optimisation method.maxiter(int, default10000): Maximum number of iterations.tol(float, default1e-6): Convergence tolerance.disp(bool, defaultFalse): IfTrue, print convergence messages.ntrials(int, default1): Number of re-tries with progressively expanded parameter bounds when the minimiser does not converge.
Minuit-minimiser options (used when
minimizer="minuit"):method(str, default"migrad"): Minuit algorithm ("migrad"or"simplex").maxiter(int, default10000): Maximum number of function calls.tol(float, default1e-6): Convergence tolerance.disp(int, default0): Minuit print level (0= silent).strategy(int, default0): Minuit strategy (0= fast,1= default,2= slow but more accurate).errordef(float, defaultMinuit.LIKELIHOOD): Value by which Minuit defines a one-sigma interval (0.5for NLL,1.0for \(\chi^2\)).
Unknown keys are logged as a warning and silently discarded by the minimiser.
- Returns:
When
poi_indices=None: \(\hat\mu\) (float) and the (negative) log-likelihood. Whenpoi_indicesis provided: adictof{index_or_name: fitted_value}and the (negative) log-likelihood.- Return type:
Tuple[Union[float, Dict[Union[int, str], float]], float]
- maximize_likelihood(return_nll: bool | None = True, expected: ExpectationType | None = observed, allow_negative_signal: bool | None = True, data: List[float] | ndarray | None = None, init_pars: List[float] | None = None, par_bounds: List[Tuple[float, float]] | None = None, poi_indices: List[int | str] | None = None, **kwargs) Tuple[float | Dict[int | str, float], float][source]#
Find the maximum of the likelihood.
- Parameters:
return_nll (
bool, defaultTrue) – IfTrue, returns negative log-likelihood value. ifFalsereturns likelihood value.expected (ExpectationType) –
Sets which values the fitting algorithm should focus and p-values to be computed.
observed: Computes the p-values with via post-fit prescription which means that the experimental data will be assumed to be the truth (default).aposteriori: Computes the expected p-values with via post-fit prescription which means that the experimental data will be assumed to be the truth.apriori: Computes the expected p-values with via pre-fit prescription which means that the SM will be assumed to be the truth.
allow_negative_signal (
bool, defaultTrue) – IfTrue\(\hat\mu\) value will be allowed to be negative.data (
Union[List[float], np.ndarray], defaultNone) – input data that to fit. IfNonedata will be set according toexpectedinput.init_pars (
List[float], defaultNone) – initial parameters for the optimiserpar_bounds (
List[Tuple[float, float]], defaultNone) – parameter bounds for the optimiser.poi_indices (
List[Union[int, str]], defaultNone) – IfNone, returns a singlefloatfor the primary POI (identified bypoi_index). If a list of parameter indices (int) or names (str) is provided, returns adictmapping each requested key to its fitted value. String keys are resolved viaparameter_names.kwargs –
Keyword arguments forwarded through
prepare_for_fitto the optimiser. Accepts the same keys aslikelihood():Consumed by
prepare_for_fit:do_grad(bool, defaultTrue): Whether to request the gradient of the objective function from the backend. Falls back toFalseautomatically if the backend raisesNotImplementedError.constraints(List[Dict], default[]): Additional scipy-style constraint dicts to pass to the optimiser. Any constraints defined on the backend itself are always appended.fixed_poi_value(Union[float, Dict[int, float]], defaultNone): Fix one or more parameters of interest during the optimisation while allowing the remaining parameters (nuisance and other POIs) to be minimised freely. A plainfloatfixes the primary POI (identified bypoi_index); adictof{index: value}fixes multiple POIs simultaneously. This is particularly useful in multi-POI fits where, for example, one signal strength is held fixed while others are profiled out.
Consumed by
fit(the core optimisation loop):minimizer(str, default"scipy"or the value of theSPEY_OPTIMISERenvironment variable): Selects the numerical minimiser. Accepted values are"scipy"and"minuit"(requiresiminuit).hessian(Callable[[np.ndarray], np.ndarray], defaultNone): Hessian of the objective function with respect to the variational parameters. Passed to scipy as thehessargument; ignored by the minuit minimiser.
Scipy-minimiser options (used when
minimizer="scipy"):method(str, default"SLSQP"): Scipy optimisation method.maxiter(int, default10000): Maximum number of iterations.tol(float, default1e-6): Convergence tolerance.disp(bool, defaultFalse): IfTrue, print convergence messages.ntrials(int, default1): Number of re-tries with progressively expanded parameter bounds when the minimiser does not converge.
Minuit-minimiser options (used when
minimizer="minuit"):method(str, default"migrad"): Minuit algorithm ("migrad"or"simplex").maxiter(int, default10000): Maximum number of function calls.tol(float, default1e-6): Convergence tolerance.disp(int, default0): Minuit print level.strategy(int, default0): Minuit strategy (0= fast,1= default,2= slow but more accurate).errordef(float, defaultMinuit.LIKELIHOOD): Value by which Minuit defines a one-sigma interval.
Unknown keys are logged as a warning and silently discarded by the minimiser.
- Returns:
When
poi_indices=None: \(\hat\mu\) (float) and the (negative) log-likelihood. Whenpoi_indicesis provided: adictof{index_or_name: fitted_value}and the (negative) log-likelihood.- Return type:
Tuple[Union[float, Dict[Union[int, str], float]], float]
- ntoys#
Number of toy pseudo-experiments used by the toy-based calculator.
- poi_upper_limit(expected: ExpectationType = observed, confidence_level: float = 0.95, low_init: float = 1.0, hig_init: float = 1.0, expected_pvalue: Literal['nominal', '1sigma', '2sigma'] = 'nominal', maxiter: int = 10000, optimiser_arguments: Dict[str, Any] | None = None) float | List[float]#
Compute the upper limit for the parameter of interest (POI), denoted as \(\mu\).
- Parameters:
expected (
ExpectationType, defaultobserved) –Specifies the type of expectation for the fitting algorithm and p-value computation.
observed: Computes p-values using post-fit prescription, assuming experimental data as the truth (default).aposteriori: Computes expected p-values using post-fit prescription, assuming experimental data as the truth.apriori: Computes expected p-values using pre-fit prescription, assuming the Standard Model (SM) as the truth.
confidence_level (
float, default0.95) – Confidence level for the upper limit, representing \(1 - CL_s\). Must be between 0 and 1. Default is 0.95.low_init (
Optional[float], default1.0) –Initial lower limit for the search algorithm. If None, it is determined by \(\hat\mu + 1.5\sigma_{\hat\mu}\). Default is 1.0.
Note
\(\sigma_{\hat\mu}\) is determined via
sigma_mu()function.hig_init (
Optional[float], default1.0) –Initial upper limit for the search algorithm. If None, it is determined by \(\hat\mu + 2.5\sigma_{\hat\mu}\). Default is 1.0.
Note
\(\sigma_{\hat\mu}\) is determined via
sigma_mu()function.expected_pvalue (
Literal["nominal", "1sigma", "2sigma"], default"nominal") –In case of
aposterioriandaprioriexpectation, specifies the type of expected p-value for upper limit calculation."nominal": Computes the upper limit for the central p-value. Returns a single value."1sigma": Computes the upper limit for the central p-value and \(1\sigma\) fluctuation from background. Returns 3 values."2sigma": Computes the upper limit for the central p-value and \(1\sigma\) and \(2\sigma\) fluctuation from background. Returns 5 values.
Note
For
expected=spey.ExpectationType.observed,expected_pvalueargument will be overwritten to"nominal".allow_negative_signal (
bool, defaultTrue) – Allows for negative signal values, changing the computation of the test statistic. Default is False.maxiter (
int, default10000) – Maximum number of iterations for the optimiser. Default is 10000.optimiser_arguments (
Dict, defaultNone) – Additional arguments for the optimiser used to compute the likelihood and its maximum. Default is None.
- Returns:
A single value representing the upper limit for the nominal case.
A list of values representing the upper limits for the central value and statistical deviations (for “1sigma” and “2sigma” cases). The order is: \(-2\sigma\), \(-1\sigma\), central value, \(1\sigma\), \(2\sigma\).
- Return type:
Union[float, List[float]]- Raises:
AssertionError – If the confidence level is not between 0 and 1.
- prepare_for_fit(data: List[float] | ndarray | None = None, expected: ExpectationType = observed, allow_negative_signal: bool | None = True, **kwargs) Dict[source]#
Prepare backend for the optimiser.
- Parameters:
data (
Union[List[float], np.ndarray], defaultNone) – input data that to fitexpected (ExpectationType) –
Sets which values the fitting algorithm should focus and p-values to be computed.
observed: Computes the p-values with via post-fit prescription which means that the experimental data will be assumed to be the truth (default).aposteriori: Computes the expected p-values with via post-fit prescription which means that the experimental data will be assumed to be the truth.apriori: Computes the expected p-values with via pre-fit prescription which means that the SM will be assumed to be the truth.
allow_negative_signal (
bool, defaultTrue) – IfTrue\(\hat\mu\) value will be allowed to be negative.
- Returns:
Dictionary of necessary toolset for the fit. objective function,
"func", use gradient boolean,"do_grad"and function to compute negative log-likelihood with given fit parameters,"nll".- Return type:
Dict
- pull(poi_test: float | Dict[int | str, float] = 1.0, expected: ExpectationType = observed, allow_negative_signal: bool = True, **kwargs) float#
Pull: measures how many standard deviations the observation is away from the expectation.
\[\text{pull}(\mu) = \operatorname{sign}(\hat{\mu}-\mu) \sqrt{-2\log\frac{L(\mu,\hat{\hat{\theta}}(\mu))}{L(\hat{\mu},\hat{\theta})}}\]the square of the pull is the likelihood-ratio test statistic.
- Parameters:
poi_test (
PoiTestorlist[float], default1.0) – Parameter of interest, \(\mu\). A plainfloat(or iterable of floats) fixes the primary POI — when iterable, \(\chi^2\) is computed for each element. Alternatively, adictof{index_or_name: value}fixes multiple parameters simultaneously (iterating over dicts is not supported).expected (ExpectationType) –
Sets which values the fitting algorithm should focus and p-values to be computed.
observed: Computes the p-values with via post-fit prescription which means that the experimental data will be assumed to be the truth (default).aposteriori: Computes the expected p-values with via post-fit prescription which means that the experimental data will be assumed to be the truth.apriori: Computes the expected p-values with via pre-fit prescription which means that the SM will be assumed to be the truth.
allow_negative_signal (
bool, defaultTrue) – IfTrue\(\hat\mu\) value will be allowed to be negative. Only valid whenpoi_test_denominator=None.kwargs – keyword arguments for the optimiser.
- Returns:
value of pull.
- Return type:
float
- property s95exp: float#
Expected excluded cross section at 95% CL (pre-fit / a-priori expectation).
Shorthand for
excluded_cross_section(ExpectationType.apriori). The result represents the cross-section value that would be excluded at the 95% confidence level if no signal were present (SM hypothesis), expressed in the same units asxsection.- Raises:
UnknownCrossSection – If
xsectionhas not been set (i.e. isnan).- Returns:
Expected 95% CL excluded cross section value in user-defined units.
- Return type:
float
- property s95obs: float#
Observed excluded cross section at 95% CL (post-fit / observed expectation).
Shorthand for
excluded_cross_section(ExpectationType.observed). The result represents the cross-section value excluded at the 95% confidence level using the actual observed data, expressed in the same units asxsection.- Raises:
UnknownCrossSection – If
xsectionhas not been set (i.e. isnan).- Returns:
Observed 95% CL excluded cross section value in user-defined units.
- Return type:
float
- sigma_mu(poi_test: float | Dict[int | str, float], expected: ExpectationType = observed, test_statistics: Literal['qtilde', 'q', 'q0'] = 'qtilde', **kwargs) float#
Estimate the standard deviation of \(\hat\mu\) at a fixed \(\mu\).
Attempts the Hessian-based estimate first (via
sigma_mu_from_hessian()) if that method exists on the subclass. When the Hessian is not available, falls back to the Asimov approximation from eq. (31) of [arXiv:1007.1727]:\[\sigma_A = \frac{|\mu - \mu^\prime|}{\sqrt{q_{\mu,A}}}, \qquad q_{\mu,A} = -2\ln\lambda_A(\mu)\]where \(\mu^\prime\) is the best-fit value on the Asimov dataset.
- Parameters:
poi_test (
PoiTest) – Parameter of interest value \(\mu\) at which to evaluate \(\sigma_\mu\).expected (ExpectationType) –
Selects which dataset to condition on.
observed: Use observed data (post-fit, default).aposteriori: Use observed data with post-fit nuisance treatment.apriori: Use background-only prediction (pre-fit / SM hypothesis).
test_statistics (
str, default"qtilde") –Test statistic used for the Asimov approximation (ignored when the Hessian path is taken).
'qtilde': \(\tilde{q}_\mu\), eq. (62) of [arXiv:1007.1727].Warning
This assumes \(\hat\mu \geq 0\).
spey’s public API enforces this automatically.'q': \(q_\mu\), eq. (54) of [arXiv:1007.1727].'q0': Discovery statistic \(q_0\), eq. (47) of [arXiv:1007.1727].
kwargs –
Additional keyword arguments forwarded to the optimiser, including:
init_pars (
List[float], defaultNone): Initial parameter values for the optimiser.par_bounds (
List[Tuple[float, float]], defaultNone): Parameter bounds for the optimiser.
- Returns:
Estimated standard deviation \(\sigma_\mu\) of the parameter of interest at the given \(\mu\).
- Return type:
float
- sigma_mu_from_hessian(poi_test: float | Dict[int | str, float], expected: ExpectationType = observed, init_pars: List[float] | None = None, par_bounds: List[Tuple[float, float]] | None = None, **kwargs) float[source]#
Compute variance of \(\mu\) from inverse Hessian. See eq. (27-28) in [arXiv:1007.1727].
- Parameters:
poi_test (
PoiTest) – Parameter of interest, \(\mu\). A plainfloatfixes the primary POI; adictof{index_or_name: value}fixes multiple parameters simultaneously.expected (ExpectationType) –
Sets which values the fitting algorithm should focus and p-values to be computed.
observed: Computes the p-values with via post-fit prescription which means that the experimental data will be assumed to be the truth (default).aposteriori: Computes the expected p-values with via post-fit prescription which means that the experimental data will be assumed to be the truth.apriori: Computes the expected p-values with via pre-fit prescription which means that the SM will be assumed to be the truth.
init_pars (
List[float], defaultNone) – initial parameters for the optimiserpar_bounds (
List[Tuple[float, float]], defaultNone) – parameter bounds for the optimiser.kwargs – keyword arguments for the optimiser.
- Raises:
MethodNotAvailable – If backend does not have Hessian implementation.
- Returns:
variance on parameter of interest.
- Return type:
float
- significance(expected: ExpectationType = observed, **kwargs) Tuple[float, float, List[float], List[float]]#
Compute the discovery significance of a positive signal.
Uses the discovery test statistic \(q_0\) (eq. 47 of [arXiv:1007.1727]) to quantify the evidence for a signal above the background-only hypothesis. The Asimov significance \(\sqrt{q_{0,A}}\) gives the median expected sensitivity, while \(\sqrt{q_0}\) is computed from the observed data. See sec. 5.1 of [arXiv:1007.1727] for details.
Note
aposterioriandobservedboth perform a post-fit computation and therefore return identical results. The only meaningful distinction is between post-fit (observed) and pre-fit (apriori) computations.- Parameters:
expected (ExpectationType) –
Selects which dataset to condition on.
observed: Post-fit (default).apriori: Pre-fit / SM hypothesis.
kwargs –
Additional keyword arguments forwarded to the optimiser, including:
init_pars (
List[float], defaultNone): Initial parameter values for the optimiser.par_bounds (
List[Tuple[float, float]], defaultNone): Parameter bounds for the optimiser.
- Returns:
A 4-tuple
(sqrt_q0A, sqrt_q0, pvalues, expected_pvalues)where:sqrt_q0A— Asimov discovery significance \(\sqrt{q_{0,A}}\).sqrt_q0— Observed discovery significance \(\sqrt{q_0}\).pvalues— Observed p-value(s) for the \(q_0\) test.expected_pvalues— Expected p-value(s) at \(-2\sigma,\,-1\sigma,\,\text{central},\,+1\sigma,\,+2\sigma\).
- Return type:
Tuple[float, float, List[float], List[float]]
- xsection: float#
Value of the cross section, unit is defined by the user.