zfit.loss module

class zfit.loss.ExtendedUnbinnedNLL(model, data, fit_range=None, constraints=None)[source]

Bases: zfit.core.loss.UnbinnedNLL

An Unbinned Negative Log Likelihood with an additional poisson term for the

property is_extended
add_cache_deps(cache_deps, allow_non_cachable=True)

Add dependencies that render the cache invalid if they change.

Parameters
  • cache_deps (Union[ForwardRef, Iterable[ForwardRef]]) –

  • allow_non_cachable (bool) – If True, allow cache_dependents to be non-cachables. If False, any cache_dependents that is not a ZfitCachable will raise an error.

Raises

TypeError – if one of the cache_dependents is not a ZfitCachable _and_ allow_non_cachable if False.

add_constraints(constraints)
property constraints
copy(deep=False, name=None, **overwrite_params)
Return type

ZfitObject

property data
property dtype

The dtype of the object

Return type

DType

property errordef
Return type

Union[float, int]

property fit_range
get_cache_deps(only_floating=True)

Return a set of all independent Parameter that this object depends on.

Parameters

only_floating (bool) – If True, only return floating Parameter

Return type

OrderedSet

get_dependencies(only_floating=True)

DEPRECATED FUNCTION

Warning: THIS FUNCTION IS DEPRECATED. It will be removed in a future version. Instructions for updating: Use get_params instead if you want to retrieve the independent parameters or get_cache_deps in case you need the numerical cache dependents (advanced).

Return type

OrderedSet

get_params(floating=True, is_yield=None, extract_independent=True, only_floating=<class 'zfit.util.checks.NotSpecified'>)

Recursively collect parameters that this object depends on according to the filter criteria.

Which parameters should be included can be steered using the arguments as a filter.
  • None: do not filter on this. E.g. floating=None will return parameters that are floating as well as

    parameters that are fixed.

  • True: only return parameters that fulfil this criterion

  • False: only return parameters that do not fulfil this criterion. E.g. floating=False will return

    only parameters that are not floating.

Parameters
  • floating (Optional[bool]) – if a parameter is floating, e.g. if floating() returns True

  • is_yield (Optional[bool]) – if a parameter is a yield of the _current_ model. This won’t be applied recursively, but may include yields if they do also represent a parameter parametrizing the shape. So if the yield of the current model depends on other yields (or also non-yields), this will be included. If, however, just submodels depend on a yield (as their yield) and it is not correlated to the output of our model, they won’t be included.

  • extract_independent (Optional[bool]) – If the parameter is an independent parameter, i.e. if it is a ZfitIndependentParameter.

Return type

Set[ZfitParameter]

gradients(params=None)
Return type

List[Tensor]

graph_caching_methods = [<function FunctionWrapperRegistry.__call__.<locals>.concrete_func>, <function FunctionWrapperRegistry.__call__.<locals>.concrete_func>, <function FunctionWrapperRegistry.__call__.<locals>.concrete_func>]
instances = <_weakrefset.WeakSet object>
property model
property name
property params
Return type

~ParametersType

register_cacher(cacher)

Register a cacher that caches values produces by this instance; a dependent.

Parameters

cacher (Union[ForwardRef, Iterable[ForwardRef]]) –

reset_cache(reseter)
reset_cache_self()

Clear the cache of self and all dependent cachers.

value()
value_gradients(params)
Return type

Tuple[Tensor, Tensor]

value_gradients_hessian(params, hessian=None)
Return type

Tuple[Tensor, Tensor, Tensor]

class zfit.loss.UnbinnedNLL(model, data, fit_range=None, constraints=None)[source]

Bases: zfit.core.loss.BaseLoss

The Unbinned Negative Log Likelihood.

property is_extended
add_cache_deps(cache_deps, allow_non_cachable=True)

Add dependencies that render the cache invalid if they change.

Parameters
  • cache_deps (Union[ForwardRef, Iterable[ForwardRef]]) –

  • allow_non_cachable (bool) – If True, allow cache_dependents to be non-cachables. If False, any cache_dependents that is not a ZfitCachable will raise an error.

Raises

TypeError – if one of the cache_dependents is not a ZfitCachable _and_ allow_non_cachable if False.

add_constraints(constraints)
property constraints
copy(deep=False, name=None, **overwrite_params)
Return type

ZfitObject

property data
property dtype

The dtype of the object

Return type

DType

property errordef
Return type

Union[float, int]

property fit_range
get_cache_deps(only_floating=True)

Return a set of all independent Parameter that this object depends on.

Parameters

only_floating (bool) – If True, only return floating Parameter

Return type

OrderedSet

get_dependencies(only_floating=True)

DEPRECATED FUNCTION

Warning: THIS FUNCTION IS DEPRECATED. It will be removed in a future version. Instructions for updating: Use get_params instead if you want to retrieve the independent parameters or get_cache_deps in case you need the numerical cache dependents (advanced).

Return type

OrderedSet

get_params(floating=True, is_yield=None, extract_independent=True, only_floating=<class 'zfit.util.checks.NotSpecified'>)

Recursively collect parameters that this object depends on according to the filter criteria.

Which parameters should be included can be steered using the arguments as a filter.
  • None: do not filter on this. E.g. floating=None will return parameters that are floating as well as

    parameters that are fixed.

  • True: only return parameters that fulfil this criterion

  • False: only return parameters that do not fulfil this criterion. E.g. floating=False will return

    only parameters that are not floating.

Parameters
  • floating (Optional[bool]) – if a parameter is floating, e.g. if floating() returns True

  • is_yield (Optional[bool]) – if a parameter is a yield of the _current_ model. This won’t be applied recursively, but may include yields if they do also represent a parameter parametrizing the shape. So if the yield of the current model depends on other yields (or also non-yields), this will be included. If, however, just submodels depend on a yield (as their yield) and it is not correlated to the output of our model, they won’t be included.

  • extract_independent (Optional[bool]) – If the parameter is an independent parameter, i.e. if it is a ZfitIndependentParameter.

Return type

Set[ZfitParameter]

gradients(params=None)
Return type

List[Tensor]

graph_caching_methods = [<function FunctionWrapperRegistry.__call__.<locals>.concrete_func>, <function FunctionWrapperRegistry.__call__.<locals>.concrete_func>, <function FunctionWrapperRegistry.__call__.<locals>.concrete_func>]
instances = <_weakrefset.WeakSet object>
property model
property name
property params
Return type

~ParametersType

register_cacher(cacher)

Register a cacher that caches values produces by this instance; a dependent.

Parameters

cacher (Union[ForwardRef, Iterable[ForwardRef]]) –

reset_cache(reseter)
reset_cache_self()

Clear the cache of self and all dependent cachers.

value()
value_gradients(params)
Return type

Tuple[Tensor, Tensor]

value_gradients_hessian(params, hessian=None)
Return type

Tuple[Tensor, Tensor, Tensor]

class zfit.loss.BaseLoss(model, data, fit_range=None, constraints=None)[source]

Bases: zfit.core.interfaces.ZfitLoss, zfit.core.baseobject.BaseNumeric

A “simultaneous fit” can be performed by giving one or more model, data, fit_range to the loss. The length of each has to match the length of the others.

Parameters
gradients(params=None)[source]
Return type

List[Tensor]

add_constraints(constraints)[source]
property name
property model
property data
property fit_range
property constraints
value()[source]
property errordef
Return type

Union[float, int]

value_gradients(params)[source]
Return type

Tuple[Tensor, Tensor]

value_gradients_hessian(params, hessian=None)[source]
Return type

Tuple[Tensor, Tensor, Tensor]

add_cache_deps(cache_deps, allow_non_cachable=True)

Add dependencies that render the cache invalid if they change.

Parameters
  • cache_deps (Union[ForwardRef, Iterable[ForwardRef]]) –

  • allow_non_cachable (bool) – If True, allow cache_dependents to be non-cachables. If False, any cache_dependents that is not a ZfitCachable will raise an error.

Raises

TypeError – if one of the cache_dependents is not a ZfitCachable _and_ allow_non_cachable if False.

copy(deep=False, name=None, **overwrite_params)
Return type

ZfitObject

property dtype

The dtype of the object

Return type

DType

get_cache_deps(only_floating=True)

Return a set of all independent Parameter that this object depends on.

Parameters

only_floating (bool) – If True, only return floating Parameter

Return type

OrderedSet

get_dependencies(only_floating=True)

DEPRECATED FUNCTION

Warning: THIS FUNCTION IS DEPRECATED. It will be removed in a future version. Instructions for updating: Use get_params instead if you want to retrieve the independent parameters or get_cache_deps in case you need the numerical cache dependents (advanced).

Return type

OrderedSet

get_params(floating=True, is_yield=None, extract_independent=True, only_floating=<class 'zfit.util.checks.NotSpecified'>)

Recursively collect parameters that this object depends on according to the filter criteria.

Which parameters should be included can be steered using the arguments as a filter.
  • None: do not filter on this. E.g. floating=None will return parameters that are floating as well as

    parameters that are fixed.

  • True: only return parameters that fulfil this criterion

  • False: only return parameters that do not fulfil this criterion. E.g. floating=False will return

    only parameters that are not floating.

Parameters
  • floating (Optional[bool]) – if a parameter is floating, e.g. if floating() returns True

  • is_yield (Optional[bool]) – if a parameter is a yield of the _current_ model. This won’t be applied recursively, but may include yields if they do also represent a parameter parametrizing the shape. So if the yield of the current model depends on other yields (or also non-yields), this will be included. If, however, just submodels depend on a yield (as their yield) and it is not correlated to the output of our model, they won’t be included.

  • extract_independent (Optional[bool]) – If the parameter is an independent parameter, i.e. if it is a ZfitIndependentParameter.

Return type

Set[ZfitParameter]

graph_caching_methods = [<function FunctionWrapperRegistry.__call__.<locals>.concrete_func>]
instances = <_weakrefset.WeakSet object>
property params
Return type

~ParametersType

register_cacher(cacher)

Register a cacher that caches values produces by this instance; a dependent.

Parameters

cacher (Union[ForwardRef, Iterable[ForwardRef]]) –

reset_cache(reseter)
reset_cache_self()

Clear the cache of self and all dependent cachers.

class zfit.loss.SimpleLoss(func, deps=<zfit.util.checks.NotSpecified object>, dependents=<zfit.util.checks.NotSpecified object>, errordef=None)[source]

Bases: zfit.core.loss.BaseLoss

Loss from a (function returning a) Tensor.

Parameters
  • func (Callable) – Callable that constructs the loss and returns a tensor.

  • deps (Iterable[ForwardRef]) – The dependents (independent zfit.Parameter) of the loss. If not given, the dependents are figured out automatically.

  • errordef (Optional[float]) – Definition of which change in the loss corresponds to a change of 1 sigma. For example, 1 for Chi squared, 0.5 for negative log-likelihood.

property errordef
add_cache_deps(cache_deps, allow_non_cachable=True)

Add dependencies that render the cache invalid if they change.

Parameters
  • cache_deps (Union[ForwardRef, Iterable[ForwardRef]]) –

  • allow_non_cachable (bool) – If True, allow cache_dependents to be non-cachables. If False, any cache_dependents that is not a ZfitCachable will raise an error.

Raises

TypeError – if one of the cache_dependents is not a ZfitCachable _and_ allow_non_cachable if False.

add_constraints(constraints)
property constraints
copy(deep=False, name=None, **overwrite_params)
Return type

ZfitObject

property data
property dtype

The dtype of the object

Return type

DType

property fit_range
get_cache_deps(only_floating=True)

Return a set of all independent Parameter that this object depends on.

Parameters

only_floating (bool) – If True, only return floating Parameter

Return type

OrderedSet

get_dependencies(only_floating=True)

DEPRECATED FUNCTION

Warning: THIS FUNCTION IS DEPRECATED. It will be removed in a future version. Instructions for updating: Use get_params instead if you want to retrieve the independent parameters or get_cache_deps in case you need the numerical cache dependents (advanced).

Return type

OrderedSet

get_params(floating=True, is_yield=None, extract_independent=True, only_floating=<class 'zfit.util.checks.NotSpecified'>)

Recursively collect parameters that this object depends on according to the filter criteria.

Which parameters should be included can be steered using the arguments as a filter.
  • None: do not filter on this. E.g. floating=None will return parameters that are floating as well as

    parameters that are fixed.

  • True: only return parameters that fulfil this criterion

  • False: only return parameters that do not fulfil this criterion. E.g. floating=False will return

    only parameters that are not floating.

Parameters
  • floating (Optional[bool]) – if a parameter is floating, e.g. if floating() returns True

  • is_yield (Optional[bool]) – if a parameter is a yield of the _current_ model. This won’t be applied recursively, but may include yields if they do also represent a parameter parametrizing the shape. So if the yield of the current model depends on other yields (or also non-yields), this will be included. If, however, just submodels depend on a yield (as their yield) and it is not correlated to the output of our model, they won’t be included.

  • extract_independent (Optional[bool]) – If the parameter is an independent parameter, i.e. if it is a ZfitIndependentParameter.

Return type

Set[ZfitParameter]

gradients(params=None)
Return type

List[Tensor]

graph_caching_methods = [<function FunctionWrapperRegistry.__call__.<locals>.concrete_func>]
instances = <_weakrefset.WeakSet object>
property model
property name
property params
Return type

~ParametersType

register_cacher(cacher)

Register a cacher that caches values produces by this instance; a dependent.

Parameters

cacher (Union[ForwardRef, Iterable[ForwardRef]]) –

reset_cache(reseter)
reset_cache_self()

Clear the cache of self and all dependent cachers.

value()
value_gradients(params)
Return type

Tuple[Tensor, Tensor]

value_gradients_hessian(params, hessian=None)
Return type

Tuple[Tensor, Tensor, Tensor]

zfit.loss.experimental_enable_loss_penalty(enable=True)[source]

EXPERIMENTAL! Enable a loss penalty if the loss is NaN, which can push back the minimizer.

Won’t work with every minimizer

Parameters

enable – If True, enable this feature.