FitResult#
- class zfit.result.FitResult(loss, params, minimizer, valid, edm, fmin, criterion, status=None, converged=None, message=None, info=None, approx=None, niter=None, evaluator=None)[source]#
Bases:
ZfitResult
Create a
FitResult
from a minimization. Store parameter values, minimization infos and calculate errors.Any errors calculated are saved under
self.params
dictionary with:{parameter: {error_name1: {'low': value, 'high': value or similar}}
- Parameters
loss – The loss function that was minimized. Usually, but not necessary, contains also the pdf, data and constraints.
params – Result of the fit where each
Parameter
key has the value from the minimum found by the minimizer.minimizer – Minimizer that was used to obtain this
FitResult
and will be used to calculate certain errors. If the minimizer is state-based (like “iminuit”), then this is a copy and the state of otherFitResults
or of the actual minimizer that performed the minimization won’t be altered.valid –
Indicating whether the result is valid or not. This is the strongest indication and serves as the global flag. The reasons why a result may be invalid can be arbitrary, including but not exclusive:
parameter(s) at the limit
maxiter reached without proper convergence
the minimizer maybe even converged but it is known that this is only a local minimum
To indicate the reason for the invalidity, pass a message.
edm – The estimated distance to minimum which is the criterion value at the minimum.
fmin – Value of the function at the minimum.
criterion – Criterion that was used during the minimization. This determines the estimated distance to the minimum (edm)
status – A status code (if available) that describes the minimization termination. 0 means a valid termination.
converged – Whether the fit has successfully converged or not. The result itself can still be an invalid minimum such as if the parameters are at or close to the limits or in case another minimum is found.
message – Human-readable message to indicate the reason if the fitresult is not valid. If the fit is valid, the message (should) be an empty string (or None), otherwise, it should denote the reason for the invalidity.
info –
Additional information (if available) such as number of gradient function calls or the original minimizer return message. This is a relatively free field and _no single field_ in it is guaranteed to be stable. Some recommended fields:
original: contains the original returned object by the minimizer used internally.
optimizer: the actual instance of the wrapped optimizer (if available)
approx – Collection of approximations found during the minimization process such as gradient and hessian.
niter – Approximate number of iterations ~= number of function evaluations ~= number of gradient evaluations. This is an approximated value and the exact meaning can differ between different minimizers.
evaluator – Loss evaluator that was used during the minimization and that may contain information about the last evaluations of the gradient etc. which can serve as approximations.
- classmethod from_ipopt(loss, params, problem, minimizer, valid, values, message, converged, edm, niter, fmin, status, criterion, evaluator)[source]#
Create a
FitResult
from an ipopt minimization.- Parameters
loss – The loss function that was minimized. Usually, but not necessary, contains also the pdf, data and constraints.
params – Result of the fit where each
Parameter
key has the value from the minimum found by the minimizer.problem – |@doc:result.init.problem||@docend:result.init.problem|
minimizer – Minimizer that was used to obtain this
FitResult
and will be used to calculate certain errors. If the minimizer is state-based (like “iminuit”), then this is a copy and the state of otherFitResults
or of the actual minimizer that performed the minimization won’t be altered.valid –
Indicating whether the result is valid or not. This is the strongest indication and serves as the global flag. The reasons why a result may be invalid can be arbitrary, including but not exclusive:
parameter(s) at the limit
maxiter reached without proper convergence
the minimizer maybe even converged but it is known that this is only a local minimum
To indicate the reason for the invalidity, pass a message.
values – Values of the parameters at the found minimum.
message – Human-readable message to indicate the reason if the fitresult is not valid. If the fit is valid, the message (should) be an empty string (or None), otherwise, it should denote the reason for the invalidity.
converged – Whether the fit has successfully converged or not. The result itself can still be an invalid minimum such as if the parameters are at or close to the limits or in case another minimum is found.
edm – The estimated distance to minimum which is the criterion value at the minimum.
niter – Approximate number of iterations ~= number of function evaluations ~= number of gradient evaluations. This is an approximated value and the exact meaning can differ between different minimizers.
fmin – Value of the function at the minimum.
status – A status code (if available) that describes the minimization termination. 0 means a valid termination.
criterion – Criterion that was used during the minimization. This determines the estimated distance to the minimum (edm)
evaluator – Loss evaluator that was used during the minimization and that may contain information about the last evaluations of the gradient etc. which can serve as approximations.
- Return type
zfit.minimize.FitResult
- classmethod from_minuit(loss, params, minuit, minimizer, valid, values=None, message=None, converged=None, edm=None, niter=None, fmin=None, status=None, criterion=None, evaluator=None)[source]#
Create a
FitResult
from aMigradResult
returned byiminuit.Minuit.migrad()
and a iminuitMinuit
instance with the corresponding zfit objects.- Parameters
loss – zfit Loss that was minimized.
params – Iterable of the zfit parameters that were floating during the minimization.
minuit – Return value of the iminuit migrad command, the instance of
iminuit.Minuit
minimizer – Instance of the zfit Minuit minimizer that was used to minimize the loss.
valid –
Indicating whether the result is valid or not. This is the strongest indication and serves as the global flag. The reasons why a result may be invalid can be arbitrary, including but not exclusive:
parameter(s) at the limit
maxiter reached without proper convergence
the minimizer maybe even converged but it is known that this is only a local minimum
To indicate the reason for the invalidity, pass a message.
values – Values of the parameters at the found minimum.
message – Human-readable message to indicate the reason if the fitresult is not valid. If the fit is valid, the message (should) be an empty string (or None), otherwise, it should denote the reason for the invalidity.
converged – Whether the fit has successfully converged or not. The result itself can still be an invalid minimum such as if the parameters are at or close to the limits or in case another minimum is found.
edm – The estimated distance to minimum which is the criterion value at the minimum.
niter – Approximate number of iterations ~= number of function evaluations ~= number of gradient evaluations. This is an approximated value and the exact meaning can differ between different minimizers.
fmin – Value of the function at the minimum.
status – A status code (if available) that describes the minimization termination. 0 means a valid termination.
criterion – Criterion that was used during the minimization. This determines the estimated distance to the minimum (edm)
evaluator – Loss evaluator that was used during the minimization and that may contain information about the last evaluations of the gradient etc. which can serve as approximations.
- Returns
A
FitResult
as if zfit Minuit was used.- Return type
zfit.minimize.FitResult
- classmethod from_scipy(loss, params, result, minimizer, message, valid, criterion, edm=None, niter=None, evaluator=None)[source]#
Create a ``FitResult from a SciPy
OptimizeResult
.- Parameters
loss – The loss function that was minimized. Usually, but not necessary, contains also the pdf, data and constraints.
params – Result of the fit where each
Parameter
key has the value from the minimum found by the minimizer.result – Result of the SciPy optimization.
minimizer – Minimizer that was used to obtain this
FitResult
and will be used to calculate certain errors. If the minimizer is state-based (like “iminuit”), then this is a copy and the state of otherFitResults
or of the actual minimizer that performed the minimization won’t be altered.message – Human-readable message to indicate the reason if the fitresult is not valid. If the fit is valid, the message (should) be an empty string (or None), otherwise, it should denote the reason for the invalidity.
edm – The estimated distance to minimum which is the criterion value at the minimum.
niter – Approximate number of iterations ~= number of function evaluations ~= number of gradient evaluations. This is an approximated value and the exact meaning can differ between different minimizers.
valid –
Indicating whether the result is valid or not. This is the strongest indication and serves as the global flag. The reasons why a result may be invalid can be arbitrary, including but not exclusive:
parameter(s) at the limit
maxiter reached without proper convergence
the minimizer maybe even converged but it is known that this is only a local minimum
To indicate the reason for the invalidity, pass a message.
criterion – Criterion that was used during the minimization. This determines the estimated distance to the minimum (edm)
evaluator – Loss evaluator that was used during the minimization and that may contain information about the last evaluations of the gradient etc. which can serve as approximations.
- Return type
zfit.minimize.FitResult
- classmethod from_nlopt(loss, opt, params, minimizer, valid, values=None, message=None, converged=None, edm=None, niter=None, fmin=None, status=None, criterion=None, evaluator=None, inv_hessian=None, hessian=None)[source]#
Create a
FitResult
from an NLopt optimizer.- Parameters
loss – The loss function that was minimized. Usually, but not necessary, contains also the pdf, data and constraints.
opt – Optimizer instance of NLopt
params – Result of the fit where each
Parameter
key has the value from the minimum found by the minimizer.minimizer – Minimizer that was used to obtain this
FitResult
and will be used to calculate certain errors. If the minimizer is state-based (like “iminuit”), then this is a copy and the state of otherFitResults
or of the actual minimizer that performed the minimization won’t be altered.valid –
Indicating whether the result is valid or not. This is the strongest indication and serves as the global flag. The reasons why a result may be invalid can be arbitrary, including but not exclusive:
parameter(s) at the limit
maxiter reached without proper convergence
the minimizer maybe even converged but it is known that this is only a local minimum
To indicate the reason for the invalidity, pass a message.
values – Values of the parameters at the found minimum.
message – Human-readable message to indicate the reason if the fitresult is not valid. If the fit is valid, the message (should) be an empty string (or None), otherwise, it should denote the reason for the invalidity.
converged – Whether the fit has successfully converged or not. The result itself can still be an invalid minimum such as if the parameters are at or close to the limits or in case another minimum is found.
edm – The estimated distance to minimum which is the criterion value at the minimum.
niter – Approximate number of iterations ~= number of function evaluations ~= number of gradient evaluations. This is an approximated value and the exact meaning can differ between different minimizers.
fmin – Value of the function at the minimum.
status – A status code (if available) that describes the minimization termination. 0 means a valid termination.
criterion – Criterion that was used during the minimization. This determines the estimated distance to the minimum (edm)
evaluator – Loss evaluator that was used during the minimization and that may contain information about the last evaluations of the gradient etc. which can serve as approximations.
inv_hessian – The (approximated) inverse hessian matrix.
hessian – The (approximated) hessian matrix.
- Return type
- property edm: float#
The estimated distance to the minimum.
- Return type
float
- Returns
Numeric
- property fmin: float#
Function value at the minimum.
- Return type
float
- Returns
Numeric
- hesse(params=None, method=None, cl=None, name=None, error_name=None)[source]#
Calculate for
params
the symmetric error using the Hessian/covariance matrix. (deprecated arguments)Deprecated: SOME ARGUMENTS ARE DEPRECATED:
(error_name)
. They will be removed in a future version. Instructions for updating: Usename
insteadThis method estimates the covariance matric using the inverse of the Hessian matrix. The assumption is that the loss profile - usually a likelihood or a :math:chi^2 - is hyperbolic. This is usually the case for fits with many observations, i.e. it is exact in the asymptotic limit. If the loss profile is not hyperbolic, another method, “zfit_error” or “minuit_minos” should be used.
Weights Weighted likelihoods are a special class of likelihoods as they are not an actual likelihood. However, the minimum is still valid, however the profile is not a proper likelihood. Therefore, corrections will be automatically applied to the Hessian uncertainty estimation in order to correct for the effects in the weights. The corrections used are “asymptotically correct” and are described in
Parameter uncertainties in weighted unbinned maximum likelihood fits`
by Christoph Langenbruch. Since this method uses the jacobian matrix, it takes significantly longer to calculate than witout weights.- Parameters
params (ParamsTypeOpt) – The parameters to calculate the Hessian symmetric error. If None, use all parameters.
method (str | Callable) – the method to calculate the covariance matrix. Can be {‘minuit_hesse’, ‘hesse_np’, ‘approx’} or a callable.
cl (float | None) – Confidence level for the error. If None, use the default value of 0.68.
name (str | bool | None) – The name for the error in the dictionary. This will be added to the information collected in params under
params[p][name]
where p is a Parameter. If the name isFalse
, it won’t be added and only returned. Defaulst to'hesse'
.
- Return type
dict[ZfitIndependentParameter, dict]
- Returns
- Result of the hessian (symmetric) error as dict with each parameter holding
the error dict {‘error’: sym_error}.
So given param_a (from zfit.Parameter(.))
error_a = result.hesse(params=param_a)[param_a]['error']
error_a is the hessian error.
- error(params=None, method=None, error_name=None, sigma=1.0)[source]#
Deprecated since version unknown: Use
errors()
instead.- Parameters
params (ParamsTypeOpt) – The parameters or their names to calculate the errors. If
params
isNone
, use all floating parameters.method (str | Callable) – The method to use to calculate the errors. Valid choices are {‘minuit_minos’} or a Callable.
sigma (float) –
Errors are calculated with respect to
sigma
std deviations. The definition of 1 sigma depends on the loss function and is defined there.For example, the negative log-likelihood (without the factor of 2) has a correspondents of \(\Delta\) NLL of 1 corresponds to 1 std deviation.
error_name (str) – The name for the error in the dictionary.
- Return type
OrderedDict
- Returns
- A
OrderedDict
containing as keys the parameter names and as value adict
which contains (next to probably more things) two keys ‘lower’ and ‘upper’, holding the calculated errors. Example: result[‘par1’][‘upper’] -> the asymmetric upper error of ‘par1’
- A
- errors(params=None, method=None, name=None, error_name=None, cl=None, sigma=None)[source]#
Calculate and set for
params
the asymmetric error using the set error method. (deprecated arguments) (deprecated arguments)Deprecated: SOME ARGUMENTS ARE DEPRECATED:
(error_name)
. They will be removed in a future version. Instructions for updating: Use name instead.Deprecated: SOME ARGUMENTS ARE DEPRECATED:
(sigma)
. They will be removed in a future version. Instructions for updating: Use cl for confidence level instead.- Parameters
params (ParamsTypeOpt) – The parameters or their names to calculate the errors. If
params
isNone
, use all floating parameters.method (str | Callable) – The method to use to calculate the errors. Valid choices are {‘minuit_minos’, ‘zfit_errors’} or a Callable.
cl (float | None) – Uncertainties are calculated with respect to the confidence level cl. The default is 68.3%. For example, the negative log-likelihood (without the factor of 2) has a correspondents of \(\Delta\) NLL of 1 corresponds to 1 std deviation.
sigma – Errors are calculated with respect to
sigma
std deviations. The definition of 1 sigma depends on the loss function and is defined there.name (str) – The name for the error in the dictionary. Defaults to
errors
- Return type
tuple[OrderedDict, None | FitResult]
- Returns
- A
OrderedDict
containing as keys the parameter and as value adict
which contains (next to often more things) two keys ‘lower’ and ‘upper’, holding the calculated errors. Furthermore, it has
cl
to indicate the convidence level the uncertainty was calculated with. Example: result[par1][‘upper’] -> the asymmetric upper error of ‘par1’
- A
- covariance(params=None, method=None, as_dict=False)[source]#
Calculate the covariance matrix for
params
.- Parameters
params (ParamsTypeOpt) – The parameters to calculate the covariance matrix. If
params
isNone
, use all floating parameters.method (str | Callable) – The method to use to calculate the covariance matrix. Valid choices are {‘minuit_hesse’, ‘hesse_np’} or a Callable.
as_dict (bool) – Default
False
. IfTrue
then returns a dictionnary.
- Returns
2D
numpy.array
of shape (N, N);dict`(param1, param2) -> covariance if `as_dict == True
.
- correlation(params=None, method=None, as_dict=False)[source]#
Calculate the correlation matrix for
params
.- Parameters
params (ParamsTypeOpt) – The parameters to calculate the correlation matrix. If
params
isNone
, use all floating parameters.method (str | Callable) – The method to use to calculate the correlation matrix. Valid choices are {‘minuit_hesse’, ‘hesse_np’} or a Callable.
as_dict (bool) – Default
False
. IfTrue
then returns a dictionnary.
- Returns
2D
numpy.array
of shape (N, N);dict`(param1, param2) -> correlation if `as_dict == True
.