BaseFuncV1#

class zfit.func.BaseFuncV1(obs=None, dtype=tf.float64, name='BaseFunc', params=None)[source]#

Bases: BaseModel, ZfitFunc

TODO(docs): explain subclassing.

func(x, name='value', *, params=None)[source]#

The function evaluated at x.

Parameters:
Returns:

or dataset? Update: rather not, what would obs be?

Return type:

# TODO(Mayou36)

as_pdf()[source]#

Create a PDF out of the function.

Returns:

A PDF with the current function as the unnormalized probability.

add_cache_deps(cache_deps, allow_non_cachable=True)#

Add dependencies that render the cache invalid if they change.

Parameters:
  • cache_deps

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

Raises:

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

analytic_integrate(limits, norm=None, *, params=None)#

Analytical integration over function and raise Error if not possible.

Parameters:
  • limits (Union[tuple[tuple[float, ...]], tuple[float, ...], bool, Space]) – ​Limits of the integration.​

  • norm (Union[tuple[tuple[float, ...]], tuple[float, ...], bool, Space]) – ​Normalization of the integration. By default, this is the same as the default space of the PDF. False means no normalization and returns the unnormed integral.​

  • params (Mapping[Union[str, ZfitParameter], TypeVar(ParamTypeInput, zfit.interface.ZfitParameter, Union[int, float, complex, tf.Tensor, zfit.interface.ZfitParameter])] | None) – ​Mapping of the parameter names to the actual values. The parameter names refer to the names of the parameters, typically Parameter, that the model was _initialized_ with, not the name of the models parametrization.​

Return type:

float | tf.Tensor

Returns:

The integral value

Raises:
  • AnalyticIntegralNotImplementedError – If no analytical integral is available (for this limits).

  • NormRangeNotImplementedError – if the norm argument is not supported. This means that no analytical normalization is available, explicitly: the analytical integral over the limits = norm is not available.

create_sampler(n=None, limits=None, *, fixed_params=None, params=None)#

Create a SamplerData that acts as Data but can be resampled, also with changed parameters and (deprecated arguments)

Deprecated: SOME ARGUMENTS ARE DEPRECATED: (fixed_params). They will be removed in a future version. Instructions for updating: Use params instead.

  1. If limits is not specified, space is used (if the space contains limits). If n is None and the model is an extended pdf, ‘extended’ is used by default.

Parameters:
  • n (int | tf.Tensor | str) –

    The number of samples to be generated. Can be a Tensor that will be or a valid string. Currently implemented:

    • ’extended’: samples poisson(yield) from each pdf that is extended.

  • limits (Union[tuple[tuple[float, ...]], tuple[float, ...], bool, Space]) – From which space to sample.

  • fixed_params (bool | list[ZfitParameter] | tuple[ZfitParameter] | None) – A list of Parameters that will be fixed during several resample calls. If True, all are fixed, if False, all are floating. If a Parameter is not fixed and its value gets updated (e.g. by a Parameter.set_value() call), this will be reflected in resample. If fixed, the Parameter will still have the same value as the SamplerData has been created with when it resamples.

  • params (TypeVar(ParamTypeInput, zfit.interface.ZfitParameter, Union[int, float, complex, tf.Tensor, zfit.interface.ZfitParameter])) – ​Mapping of the parameter names to the actual values. The parameter names refer to the names of the parameters, typically Parameter, that the model was _initialized_ with, not the name of the models parametrization.​

Return type:

SamplerData

Returns:

SamplerData

Raises:
  • NotExtendedPDFError – if ‘extended’ is chosen (implicitly by default or explicitly) as an option for n but the pdf itself is not extended.

  • ValueError – if n is an invalid string option.

  • InvalidArgumentError – if n is not specified and pdf is not extended.

property dtype: DType#

The dtype of the object.

get_params(floating=True, is_yield=None, extract_independent=True, *, autograd=None)#

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 (bool | None) – if a parameter is floating, e.g. if floating() returns True

  • is_yield (bool | None) – 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 (bool | None) – If the parameter is an independent parameter, i.e. if it is a ZfitIndependentParameter.

Return type:

OrderedSet[ZfitParameter]

property has_analytic_integral#

Return whether the PDF has an analytic integral over its full dimension.

This does not imply that all different integrals, i.e. over different ranges or just partial variable are available.

Returns:

integrate(limits, norm=None, *, options=None, params=None, var=None)#

Integrate the function over limits (normalized over norm_range if not False).

If an analytic integration function is available, it is used, otherwise numerical methods will be invoked. If the integration is in more than one dimension and no analytical integration method is provided, this can be unstable and the PDF will warn that the accuracy target was not reached.

Parameters:
  • limits (Union[tuple[tuple[float, ...]], tuple[float, ...], bool, Space]) – ​Limits of the integration.​

  • norm (Union[tuple[tuple[float, ...]], tuple[float, ...], bool, Space]) – ​Normalization of the integration. By default, this is the same as the default space of the PDF. False means no normalization and returns the unnormed integral.​

  • options

    ​Options for the integration. Additional options for the integration. Currently supported options are:

    • type: one of (bins) This hints that bins are integrated. A method that is vectorizable, non-dynamic and therefore less suitable for complicated functions is chosen.

    Other options may be available in the future.​

  • params (TypeVar(ParamTypeInput, zfit.interface.ZfitParameter, Union[int, float, complex, tf.Tensor, zfit.interface.ZfitParameter])) – ​Mapping of the parameter names to the actual values. The parameter names refer to the names of the parameters, typically Parameter, that the model was _initialized_ with, not the name of the models parametrization.​

Return type:

float | tf.Tensor

Returns:

The integral value as a scalar with shape ()

property name: str#

The name of the object.

numeric_integrate(limits, norm=None, *, options=None, params=None)#

Numerical integration over the model.

Parameters:
  • limits (Union[tuple[tuple[float, ...]], tuple[float, ...], bool, Space]) – ​Limits of the integration.​

  • norm (Union[tuple[tuple[float, ...]], tuple[float, ...], bool, Space]) – ​Normalization of the integration. By default, this is the same as the default space of the PDF. False means no normalization and returns the unnormed integral.​

  • options

    ​Options for the integration. Additional options for the integration. Currently supported options are:

    • type: one of (bins) This hints that bins are integrated. A method that is vectorizable, non-dynamic and therefore less suitable for complicated functions is chosen.

    Other options may be available in the future.​

  • params (Mapping[Union[str, ZfitParameter], TypeVar(ParamTypeInput, zfit.interface.ZfitParameter, Union[int, float, complex, tf.Tensor, zfit.interface.ZfitParameter])]) – ​Mapping of the parameter names to the actual values. The parameter names refer to the names of the parameters, typically Parameter, that the model was _initialized_ with, not the name of the models parametrization.​

Return type:

float | tf.Tensor

Returns:

The integral value

partial_analytic_integrate(x, limits, norm=None, *, params=None)#

Do analytical partial integration of the function over the limits and evaluate it at x.

Dimension of limits and x have to add up to the full dimension and be therefore equal to the dimensions of norm (if not False)

Parameters:
  • x (Union[ndarray, tf.Tensor, Data]) – The value at which the partially integrated function will be evaluated

  • limits (Union[tuple[tuple[float, ...]], tuple[float, ...], bool, Space]) – ​Limits of the integration that will be integrated out. Has to be a subset of the PDFs observables.​

  • norm (Union[tuple[tuple[float, ...]], tuple[float, ...], bool, Space]) – ​Normalization of the integration. By default, this is the same as the default space of the PDF. False means no normalization and returns the unnormed integral.​

  • params (TypeVar(ParamTypeInput, zfit.interface.ZfitParameter, Union[int, float, complex, tf.Tensor, zfit.interface.ZfitParameter])) – ​Mapping of the parameter names to the actual values. The parameter names refer to the names of the parameters, typically Parameter, that the model was _initialized_ with, not the name of the models parametrization.​

Return type:

Union[ndarray, tf.Tensor, Data]

Returns:

The value of the partially integrated function evaluated at x.

Raises:
  • AnalyticIntegralNotImplementedError – if the analytic integral (over these limits) is not implemented

  • NormRangeNotImplementedError – if the norm argument is not supported. This means that no analytical normalization is available, explicitly: the analytical integral over the limits = norm is not available.

partial_integrate(x, limits, *, norm=None, options=None, params=None)#

Partially integrate the function over the limits and evaluate it at x.

Dimension of limits and x have to add up to the full dimension and be therefore equal to the dimensions of norm (if not False)

Parameters:
  • x (Union[ndarray, tf.Tensor, Data]) – The value at which the partially integrated function will be evaluated

  • limits (Union[tuple[tuple[float, ...]], tuple[float, ...], bool, Space]) – ​Limits of the integration that will be integrated out. Has to be a subset of the PDFs observables.​

  • norm – ​Normalization of the integration. By default, this is the same as the default space of the PDF. False means no normalization and returns the unnormed integral.​

  • options

    ​Options for the integration. Additional options for the integration. Currently supported options are:

    • type: one of (bins) This hints that bins are integrated. A method that is vectorizable, non-dynamic and therefore less suitable for complicated functions is chosen.

    Other options may be available in the future.​

  • params (Mapping[Union[str, ZfitParameter], TypeVar(ParamTypeInput, zfit.interface.ZfitParameter, Union[int, float, complex, tf.Tensor, zfit.interface.ZfitParameter])]) – ​Mapping of the parameter names to the actual values. The parameter names refer to the names of the parameters, typically Parameter, that the model was _initialized_ with, not the name of the models parametrization.​

Return type:

Union[ndarray, tf.Tensor, Data]

Returns:

The value of the partially integrated function evaluated at x.

partial_numeric_integrate(x, limits, norm=None, *, params=None)#

Force numerical partial integration of the function over the limits and evaluate it at x.

Dimension of limits and x have to add up to the full dimension and be therefore equal to the dimensions of norm (if not False)

Parameters:
  • x (float | tf.Tensor) – The value at which the partially integrated function will be evaluated

  • limits (Union[tuple[tuple[float, ...]], tuple[float, ...], bool, Space]) – ​Limits of the integration that will be integrated out. Has to be a subset of the PDFs observables.​

  • norm (Union[tuple[tuple[float, ...]], tuple[float, ...], bool, Space]) – ​Normalization of the integration. By default, this is the same as the default space of the PDF. False means no normalization and returns the unnormed integral.​

  • params (TypeVar(ParamTypeInput, zfit.interface.ZfitParameter, Union[int, float, complex, tf.Tensor, zfit.interface.ZfitParameter])) – ​Mapping of the parameter names to the actual values. The parameter names refer to the names of the parameters, typically Parameter, that the model was _initialized_ with, not the name of the models parametrization.​

Return type:

float | tf.Tensor

Returns:

The value of the partially integrated function evaluated at x.

classmethod register_analytic_integral(func, limits=None, priority=50, *, supports_norm=None, supports_norm_range=None, supports_multiple_limits=None)#

Register an analytic integral with the class.

Parameters:
  • func (Callable) –

    A function that calculates the (partial) integral over the axes limits. The signature has to be the following:

    • x (ZfitData, None): the data for the remaining axes in a partial

      integral. If it is not a partial integral, this will be None.

    • limits (ZfitSpace): the limits to integrate over.

    • norm_range (ZfitSpace, None): Normalization range of the integral.

      If not supports_supports_norm_range, this will be None.

    • params (Dict[param_name, zfit.Parameters]): The parameters of the model.

    • model (ZfitModel):The model that is being integrated.

  • limits (Union[tuple[tuple[float, ...]], tuple[float, ...], bool, Space]) – ​Limits of the integration.​

  • priority (int | float) – Priority of the function. If multiple functions cover the same space, the one with the highest priority will be used.

  • supports_multiple_limits (bool | None) – If True, the ``limits` given to the integration function can have multiple limits. If False, only simple limits will pass through and multiple limits will be auto-handled.

  • supports_norm (bool | None) – If True, norm argument to the function may not be None. If False, norm will always be None and care is taken of the normalization automatically.

Return type:

None

register_cacher(cacher)#

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

Parameters:

cacher

classmethod register_inverse_analytic_integral(func)#

Register an inverse analytical integral, the inverse (unnormalized) cdf.

Parameters:

func (Callable) – A function with the signature func(x, params), where x is a Data object and params is a dict.

Return type:

None

reset_cache_self()#

Clear the cache of self and all dependent cachers.

sample(n=None, limits=None, *, x=None, params=None)#

Sample n points within limits from the model.

If limits is not specified, space is used (if the space contains limits). If n is None and the model is an extended pdf, ‘extended’ is used by default.

Parameters:
  • n (int | tf.Tensor | str) –

    The number of samples to be generated. Can be a Tensor that will be or a valid string. Currently implemented:

    • ’extended’: samples poisson(yield) from each pdf that is extended.

  • limits (Union[tuple[tuple[float, ...]], tuple[float, ...], bool, Space]) – In which region to sample in

  • params (TypeVar(ParamTypeInput, zfit.interface.ZfitParameter, Union[int, float, complex, tf.Tensor, zfit.interface.ZfitParameter])) – ​Mapping of the parameter names to the actual values. The parameter names refer to the names of the parameters, typically Parameter, that the model was _initialized_ with, not the name of the models parametrization.​

Returns:

The observables are the limits

Return type:

Data(n_obs, n_samples)

Raises:
  • NotExtendedPDFError – if ‘extended’ is (implicitly by default or explicitly) chosen as an option for n but the pdf itself is not extended.

  • ValueError – if n is an invalid string option.

  • InvalidArgumentError – if n is not specified and pdf is not extended.

update_integration_options(draws_per_dim=None, mc_sampler=None, tol=None, max_draws=None, draws_simpson=None)#

Set the integration options.

Parameters:
  • max_draws (default ~1'000'000) – Maximum number of draws when integrating . Typically 500’000 - 5’000’000.

  • tol – Tolerance on the error of the integral. typically 1e-4 to 1e-8

  • draws_per_dim – The draws for MC integration to do per iteration. Can be set to 'auto’.

  • draws_simpson – Number of points in one dimensional Simpson integration. Can be set to 'auto'.