Author: | Pierre Barbier de Reuille <pierre.barbierdereuille@gmail.com> |
---|
This module contains a set of methods to compute univariate KDEs. See the objects in the pyqt_fit.kde module for more details on these methods.
These methods provide various variations on \(\hat{K}(x;X,h,L,U)\), the modified kernel evaluated on the point \(x\) based on the estimation points \(X\), a bandwidth \(h\) and on the domain \([L,U]\).
The definitions of the methods rely on the following definitions:
These definitions correspond to:
[1] | (1, 2) Jones, M. C. 1993. Simple boundary correction for kernel density estimation. Statistics and Computing 3: 135–146. |
The exact definition of such a method is found in pyqt_fit.kde.KDE1D.method
Helper method returning a regular grid on the domain of the KDE.
Parameters: |
|
---|---|
Returns: | A vector of N regularly spaced points |
Compute the bandwidth and covariance for the model, based of its xdata attribute
Base class providing a default grid method and a default method for unbounded evaluation of the PDF and CDF. It also provides default methods for the other metrics, based on PDF and CDF calculations.
Note: |
|
---|
The following methods are interface methods that should be overriden with ones specific to the implemented method.
Method called by the KDE1D object right after fitting to allow for one-time calculation.
Parameters: | kde (pyqt_fit.kde.KDE1D) – KDE object being fitted |
---|---|
Default: | Compute the bandwidth and covariance if specified as functions |
Compute the PDF of the estimated distribution.
Parameters: |
|
---|---|
Return type: | ndarray |
Returns: | Returns the out variable, updated with the PDF. |
Default: | Direct implementation of the formula for unbounded pdf computation. |
Evaluate the PDF of the distribution on a regular grid with at least N elements.
Parameters: |
|
---|---|
Return type: | (ndarray, ndarray) |
Returns: | The array of positions the PDF has been estimated on, and the estimations. |
Default: | Evaluate \(pdf(x)\) on a grid generated using generate_grid() |
Compute the CDF of the estimated distribution, defined as:
where \(l\) is the lower bound of the distribution domain and \(p\) the density of probability
Parameters: |
|
---|---|
Return type: | ndarray |
Returns: | Returns the out variable, updated with the CDF. |
Default: | Direct implementation of the formula for unbounded CDF computation. |
Evaluate the CDF of the distribution on a regular grid with at least N elements.
Parameters: |
|
---|---|
Return type: | (ndarray, ndarray) |
Returns: | The array of positions the CDF has been estimated on, and the estimations. |
Default: | Evaluate \(cdf(x)\) on a grid generated using generate_grid() |
Compute the inverse cumulative distribution (quantile) function, defined as:
Parameters: |
|
---|---|
Return type: | ndarray |
Returns: | Returns the out variable, updated with the iCDF. |
Default: | First approximate the result using linear interpolation on the CDF and refine the result numerically using the Newton method. |
Compute the inverse cumulative distribution (quantile) function on a grid.
Note: | The default implementation is not as good an approximation as the plain icdf default method. |
---|---|
Parameters: |
|
Return type: | (ndarray, ndarray) |
Returns: | The array of positions the CDF has been estimated on, and the estimations. |
Default: | Linear interpolation of the inverse CDF on a grid |
Compute the survival function, defined as:
Parameters: |
|
---|---|
Return type: | ndarray |
Returns: | Returns the out variable, updated with the survival function. |
Default: | Compute explicitly \(1 - cdf(x)\) |
Compute the survival function on a grid.
Parameters: |
|
---|---|
Return type: | (ndarray, ndarray) |
Returns: | The array of positions the survival function has been estimated on, and the estimations. |
Default: | Compute explicitly \(1 - cdf(x)\) |
Compute the inverse survival function, defined as:
Parameters: |
|
---|---|
Return type: | ndarray |
Returns: | Returns the out variable, updated with the inverse survival function. |
Default: | Compute \(icdf(1-p)\) |
Compute the inverse survival function on a grid.
Note: | The default implementation is not as good an approximation as the plain isf default method. |
---|---|
Parameters: |
|
Return type: | (ndarray, ndarray) |
Returns: | The array of positions the CDF has been estimated on, and the estimations. |
Default: | Linear interpolation of the inverse survival function on a grid |
Compute the hazard function evaluated on the points.
The hazard function is defined as:
where \(p(x)\) is the probability density function and \(sf(x)\) is the survival function.
Parameters: |
|
---|---|
Return type: | ndarray |
Returns: | Returns the out variable, updated with the hazard function |
Default: | Compute explicitly \(pdf(x) / sf(x)\) |
Compute the hazard function on a grid.
Parameters: |
|
---|---|
Return type: | (ndarray, ndarray) |
Returns: | The array of positions the hazard function has been estimated on, and the estimations. |
Default: | Compute explicitly \(pdf(x) / sf(x)\) |
Compute the cumulative hazard function evaluated on the points.
The hazard function is defined as:
where \(l\) is the lower bound of the domain, \(h\) the hazard function and \(sf\) the survival function.
Parameters: |
|
---|---|
Return type: | ndarray |
Returns: | Returns the out variable, updated with the cumulative hazard function |
Default: | Compute explicitly \(-\ln sf(x)\) |
Compute the hazard function on a grid.
Parameters: |
|
---|---|
Return type: | (ndarray, ndarray) |
Returns: | The array of positions the hazard function has been estimated on, and the estimations. |
Default: | Compute explicitly \(-\ln sf(x)\) |
Type: | str |
---|
Specify a human-readable name for the method, for presentation purposes.
But the class also provide a number of utility methods to help implementing you own:
Here are the methods implemented in pyqt_fit. To access these methods, the simplest is to use the instances provided.
Instance of the KDE1DMethod class.
Instance of the RenormalizationMethod class.
Instance of the ReflectionMethod class.
Instance of the LinearCombinationMethod class.
Instance of the CyclicMethod class.
Creates an instance of TransformKDE1DMethod
Method used by pyqt_fit.kde.KDE1D by default. :Default: reflection
This method consists in using the normal kernel method, but renormalize to only take into account the part of the kernel within the domain of the density [1].
The kernel is then replaced with:
where:
This method consist in simulating the reflection of the data left and right of the boundaries. If one of the boundary is infinite, then the data is not reflected in that direction. To this purpose, the kernel is replaced with:
where:
See the pyqt_fit.kde_methods for a description of the various symbols.
When computing grids, if the bandwidth is constant, the result is computing using CDT.
This method uses the linear combination correction published in [1].
The estimation is done with a modified kernel given by:
where:
This method assumes cyclic boundary conditions and works only for closed boundaries.
The estimation is done with a modified kernel given by:
where:
When computing grids, if the bandwidth is constant, the result is computing using FFT.
Create a transform object.
Parameters: |
|
---|---|
Return type: | Transform |
Returns: | A transform object with function, inverse and derivative of the inverse |
The inverse function must be provided, either as argument or as attribute to the object. The derivative of the inverse will be estimated numerically if not provided.
Note: | All the functions should accept an out argument to store the result. |
---|
Compute the Kernel Density Estimate of a dataset, transforming it first to a domain where distances are “more meaningful”.
Often, KDE is best estimated in a different domain. This object takes a KDE1D object (or one compatible), and a transformation function.
Given a random variable \(X\) of distribution \(f_X\), the random variable \(Y = g(X)\) has a distribution \(f_Y\) given by:
In our term, \(Y\) is the random variable the user is interested in, and \(X\) the random variable we can estimate using the KDE. In this case, \(g\) is the transform from \(Y\) to \(X\).
So to estimate the distribution on a set of points given in \(x\), we need a total of three functions:
- Direct function: transform from the original space to the one in which the KDE will be perform (i.e. \(g^{-1}: y \mapsto x\))
- Invert function: transform from the KDE space to the original one (i.e. \(g: x \mapsto y\))
- Derivative of the invert function
If the derivative is not provided, it will be estimated numerically.
Parameters: |
|
---|---|
Note: | all given functions should accept an optional out argument to get a pre-allocated array to store its result. Also the out parameter may be one of the input argument. |