KDE#
- class zfit.prior.KDE(samples, bandwidth=None, name=None)[source]#
Bases:
ZfitPriorKernel Density Estimate prior from samples.
The KDE prior is a non-parametric way to construct a prior distribution from empirical samples. It estimates the probability density by placing kernels (typically Gaussian) at each sample point. This is particularly useful for hierarchical Bayesian models where posterior samples from one analysis become priors for another.
This prior is suitable for: - Using posterior samples from previous analyses as priors
Implementation details: - Uses exact KDE for < 1000 samples (more accurate but slower) - Switches to grid-based KDE for larger samples (faster approximation) - Automatically adds margin to bounds for numerical stability - Adapts to parameter limits while preserving sample structure
Example
>>> # Create prior from previous posterior samples >>> posterior_samples = previous_result.samples['param_name'] >>> prior = KDE(posterior_samples) >>> >>> # KDE with custom bandwidth >>> prior = KDE(samples, bandwidth=0.1) >>> >>> # Using KDE for hierarchical modeling >>> group1_posterior = fit1.samples['effect'] >>> group2_prior = KDE(group1_posterior)
Initialize a KDE prior.
- Parameters:
Note
The KDE is constructed with a 10% margin beyond the sample range to ensure numerical stability at the boundaries.
- __eq__(other)#
Compare two priors for equality.
- Parameters:
other – Another ZfitPrior instance to compare with
- Returns:
True if the priors are equal
- Return type:
- __hash__()#
Return hash of the prior based on pdf and name.
- Returns:
Hash value for the prior
- Return type:
- log_pdf(value=None)#
Return the log probability of the prior at the given value(s).
- Parameters:
value – The parameter value(s) to evaluate the log probability at
- Returns:
The log probability
- sample(n)#
Sample n values from the prior distribution.
- Parameters:
n – Number of samples to draw
- Returns:
An array of samples