# GaussianKDE1DimV1#

class zfit.pdf.GaussianKDE1DimV1(obs, data, bandwidth=None, weights=None, truncate=False, name='GaussianKDE1DimV1')[source]#

Bases: zfit.models.kde.KDEHelper, zfit.models.dist_tfp.WrapDistribution

EXPERIMENTAL, FEEDBACK WELCOME Exact, one dimensional, (truncated) Kernel Density Estimation with a Gaussian Kernel.

This implementation features an exact implementation as is preferably used for smaller (max. ~ a few thousand points) data sets. For larger data sets, methods such as KDE1DimGrid that bin the dataset may be more efficient Kernel Density Estimation is a non-parametric method to approximate the density of given points.

$f_h(x) = \frac{1}{nh} \sum_{i=1}^n K\Big(\frac{x-x_i}{h}\Big)$

where the kernel in this case is a (truncated) Gaussian

$K = \exp \Big(\frac{(x - x_i)^2}{\sigma^2}\Big)$

The bandwidth of the kernel can be estimated in different ways. It can either be a global bandwidth, corresponding to a single value, or a local bandwidth, each corresponding to one data point.

Usage

The KDE can be instantiated by using a numpy-like data sample, preferably a zfit.Data object. This will be used as the mean of the kernels. The bandwidth can either be given as a parameter (with length 1 for a global one or length equal to the data size) - a rather advanced concept for methods such as cross validation - or determined from the data automatically, either through a simple method like scott or silverman rule of thumbs or through an iterative, adaptive method.

# generate some example kernels
size = 150
data = np.random.normal(size=size, loc=2, scale=3)

limits = (-15, 5)
obs = zfit.Space("obs1", limits=limits)
kde_silverman = zfit.pdf.GaussianKDE1DimV1(data=data, obs=obs, bandwidth='silverman')

# for a better smoothing of the kernels, use an adaptive approach

Parameters
• data (ztyping.ParamTypeInput) –

​Data sample to approximate the density from. The points represent positions of the kernel, the $$x_i$$. This is preferrably a ZfitData, but can also be an array-like object.

If the data has weights, they will be taken into account. This will change the count of the events, whereas weight $$w_i$$ of $$x_i$$ will scale the value of $$K_i( x_i)$$, resulting in a factor of :math:frac{w_i}{sum w_i} .

If no weights are given, each kernel will be scaled by the same constant $$\frac{1}{n_{data}}$$.​

• obs (ztyping.ObsTypeInput) – ​Observable space of the KDE. As with any other PDF, this will be used as the default norm, but does not define the domain of the PDF. Namely, this can be a smaller space than data, as long as the name of the observable match. Using a larger dataset is actually good practice avoiding bountary biases, see also Boundary bias and padding.​

• bandwidth (ztyping.ParamTypeInput | str) –

Valid pre-defined options are {‘silverman’, ‘scott’, ‘adaptive’}.​Bandwidth of the kernel, often also denoted as $$h$$. For a Gaussian kernel, this corresponds to sigma. This can be calculated using pre-defined options or by specifying a numerical value that is broadcastable to data – a scalar or an array-like object with the same size as data.

A scalar value is usually referred to as a global bandwidth while an array holds local bandwidths​

• parameter (The bandwidth can also be a) –

• However (which should be used with caution.) –

:param : :param it allows to use it in cross-valitadion with a likelihood method.: :type weights: None | np.ndarray | tf.Tensor :param weights:​Weights of each event

in data, can be None or Tensor-like with shape compatible with data. Instead of using this parameter, it is preferred to use a ZfitData as data that contains weights. This will change the count of the events, whereas weight $$w_i$$ of $$x_i$$ will scale the value of $$K_i( x_i)$$, resulting in a factor of :math:frac{w_i}{sum w_i} .

If no weights are given, each kernel will be scaled by the same constant $$\frac{1}{n_{data}}$$.​

Parameters
• truncate (bool) – If a truncated Gaussian kernel should be used with the limits given by the obs lower and upper limits. This can cause NaNs in case datapoints are outside of the limits.

Add dependencies that render the cache invalid if they change.

Parameters
• cache_deps (Union[zfit.core.interfaces.ZfitGraphCachable, Iterable[zfit.core.interfaces.ZfitGraphCachable]]) –

• allow_non_cachable (bool) – 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, *, norm_range=None)#

Analytical integration over function and raise Error if not possible.

Parameters
• limits (Union[Tuple[Tuple[float, ...]], Tuple[float, ...], bool, Space]) – the limits to integrate over

• norm (Union[Tuple[Tuple[float, ...]], Tuple[float, ...], bool, Space, None]) – the limits to normalize over

Return type

Union[float, 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.

apply_yield(value, norm=False, log=False)#

If a norm_range is given, the value will be multiplied by the yield.

Parameters
• value (float | tf.Tensor) –

• norm (ztyping.LimitsTypeInput) –

• log (bool) –

Return type

float | tf.Tensor

Returns

Numerical

as_func(norm=False, *, norm_range=None)#

Return a Function with the function model(x, norm=norm).

Parameters

norm (Union[Tuple[Tuple[float, ...]], Tuple[float, ...], bool, Space]) –

copy(**override_parameters)#

Creates a copy of the model.

Note: the copy model may continue to depend on the original initialization arguments.

Parameters

**override_parameters – String/value dictionary of initialization arguments to override with new value.

Return type

BasePDF

Returns

A new instance of type(self) initialized from the union

of self.parameters and override_parameters, i.e., dict(self.parameters, **override_parameters).

Return an extended version of this pdf with yield yield_. The parameters are shared.

Parameters
• yield

Return type

ZfitPDF

Returns

ZfitPDF

create_projection_pdf(limits, *, options=None, limits_to_integrate=None)#

Create a PDF projection by integrating out some of the dimensions. (deprecated arguments)

Warning: SOME ARGUMENTS ARE DEPRECATED: (limits_to_integrate). They will be removed in a future version. Instructions for updating: Use limits instead.

The new projection pdf is still fully dependent on the pdf it was created with.

Parameters
• () (options) –

• ()

• limits (Union[ZfitLimit, Tensor, ndarray, Iterable[float], float, Tuple[float], List[float], bool, None]) –

Return type

ZfitPDF

Returns

A pdf without the dimensions from limits_to_integrate.

create_sampler(n=None, limits=None, fixed_params=True)#

Create a Sampler that acts as Data but can be resampled, also with changed parameters and n.

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

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 – From which space to sample.

• fixed_params – 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 Sampler has been created with when it resamples.

Returns

Sampler

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: tensorflow.python.framework.dtypes.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 – If True, only return floating Parameter

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).

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 (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

set[ZfitParameter]

get_yield()#

Return the yield (only for extended models).

Return type

Parameter | None

Returns

The yield of the current model or None

property is_extended: bool#

Flag to tell whether the model is extended or not.

Return type

bool

Returns

A boolean.

log_pdf(x, norm=None, *, norm_range=None)#

Log probability density function normalized over norm_range.

Parameters
• x (Union[float, Tensor]) – float or double Tensor.

• norm (Union[Tuple[Tuple[float, ...]], Tuple[float, ...], bool, Space, None]) – Space to normalize over

Return type

Union[float, Tensor]

Returns

A Tensor of type self.dtype.

property name: str#

The name of the object.

Return type

str

property norm: Space | None | bool#

Return the current normalization range. If None and the obs have limits, they are returned.

Return type

Space | None | bool

Returns

The current normalization range.

property norm_range: Space | None | bool#

Return the current normalization range. If None and the obs have limits, they are returned. (deprecated)

Warning: THIS FUNCTION IS DEPRECATED. It will be removed in a future version. Instructions for updating: Use the norm attribute instead.

Return type

Space | None | bool

Returns

The current normalization range.

normalization(limits, *, options=None)#

Return the normalization of the function (usually the integral over limits).

Parameters
• () (options) –

• ()

• limits (Union[Tuple[Tuple[float, ...]], Tuple[float, ...], bool, Space]) – The limits on where to normalize over

Return type

Union[float, Tensor]

Returns

The normalization value

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

Numerical integration over the model.

Parameters
• limits (Union[Tuple[Tuple[float, ...]], Tuple[float, ...], bool, Space]) – the limits to integrate over

• norm (Union[Tuple[Tuple[float, ...]], Tuple[float, ...], bool, Space, None]) – the limits to normalize over

Return type

Union[float, Tensor]

Returns

The integral value

Parameters
• an (any keyword argument. The value has to be gettable from the instance (has to be) –

• self. (attribute or callable method of) –

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

Register an analytic integral with the class. (deprecated arguments)

Warning: SOME ARGUMENTS ARE DEPRECATED: (supports_norm_range). They will be removed in a future version. Instructions for updating: Use supports_norm instead.

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 (ztyping.LimitsType) – If a Space is given, it is used as limits. Otherwise arguments to instantiate a Range class can be given as follows.|limits_init|

• 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) – 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) – 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 (Union[zfit.core.interfaces.ZfitGraphCachable, Iterable[zfit.core.interfaces.ZfitGraphCachable]]) –

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)#

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 (ztyping.nSamplingTypeIn) –

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 (ztyping.LimitsType) – In which region to sample in

Return type

SampleData

Returns

SampleData(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.

set_norm_range(norm)#

Set the normalization range (temporarily if used with contextmanager).

Parameters

norm (Union[ZfitLimit, Tensor, ndarray, Iterable[float], float, Tuple[float], List[float], bool, None]) –

set_yield(value)#

Make the model extended by setting a yield. If possible, prefer to use create_extended.

This does not alter the general behavior of the PDF. The pdf and integrate and similar methods will continue to return the same - normalized to 1 - values. However, not only can this parameter be accessed via get_yield, the methods ext_pdf and ext_integral provide a version of pdf and integrate respecetively that is multiplied by the yield.

These can be useful for plotting and for binned likelihoods.

Parameters

() (value) –

unnormalized_pdf(x)#

PDF “unnormalized”. Use functions for unnormalized pdfs. this is only for performance in special cases. (deprecated)

Warning: THIS FUNCTION IS DEPRECATED. It will be removed in a future version. Instructions for updating: Use pdf(norm=False) instead

Parameters

x (Union[float, Tensor]) – The value, have to be convertible to a Tensor

Return type

Union[float, Tensor]

Returns

1-dimensional tf.Tensor containing the unnormalized pdf.

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'.