mixle.analysis.kde module¶
Kernel density, mode, and point-process intensity estimation.
Nonparametric estimates of where the mass is without assuming a parametric family:
KDE/kde()– kernel density estimation in 1-D (and product-kernel in higher dimensions), with automatic bandwidth (Silverman / Scott), boundary correction by reflection for densities on a half-line or interval (a plain KDE leaks mass across a hard boundary and biases the edge down), and adaptive (variable) bandwidths that widen in the sparse tails (Abramson).
kde_mode()– the location of the density’s peak (“where is the mode, and how sure am I?”) with a bootstrap confidence interval.
intensity()– the intensitylambda(t)of an inhomogeneous Poisson / point process by kernel smoothing of event locations, with optional edge correction (ties to the Cox-process machinery elsewhere in the library).
Bandwidths are in data units; "silverman" and "scott" are the rule-of-thumb selectors.
- class KDE(data, *, bandwidth='silverman', bounds=None, adaptive=False)[source]
Bases:
objectA fitted kernel density estimate.
Use
kde()to construct. Evaluate withevaluate()(or call the instance). Supports a Gaussian kernel, reflection boundary correction (bounds), and adaptive bandwidths.
- kde(data, *, bandwidth='silverman', bounds=None, adaptive=False)[source]
Construct a kernel density estimate (Gaussian kernel).
- Parameters:
data (ndarray) –
(n,)sample.bandwidth –
"silverman","scott", or a positive float.bounds –
(lo, hi)support limits for reflection boundary correction; either may beNonefor an unbounded side (e.g.(0.0, None)for a positive variable).adaptive (bool) – use Abramson variable bandwidths (wider where the pilot density is low).
- Returns:
A
KDE.- Return type:
KDE
- silverman_bandwidth(data)[source]
Silverman’s rule-of-thumb bandwidth
0.9 min(sd, IQR/1.34) n^{-1/5}(1-D).
- scott_bandwidth(data)[source]
Scott’s rule-of-thumb bandwidth
sd * n^{-1/(d+4)}.
- kde_mode(data, *, bandwidth='silverman', bounds=None, grid=None, ci=False, n_boot=500, ci_level=0.95, seed=0)[source]
Estimate the mode (peak location) of a density, optionally with a bootstrap CI.
- Parameters:
data (ndarray) –
(n,)sample.bandwidth – passed to
kde().bounds – passed to
kde().grid (ndarray | None) – evaluation grid; defaults to 512 points spanning the data range.
ci (bool) – if True return a percentile bootstrap interval for the mode.
n_boot (int) – bootstrap controls.
ci_level (float) – bootstrap controls.
seed (int | RandomState | None) – bootstrap controls.
- Returns:
The mode (float), or
{'mode', 'ci_low', 'ci_high'}whenciis True.- Return type:
- intensity(events, grid, *, bandwidth='silverman', domain=None, edge_correct=True)[source]
Kernel intensity
lambda(t)of an inhomogeneous Poisson / point process.Unlike a density (which integrates to 1), the intensity integrates to the expected number of events:
lambda_hat(t) = sum_i K_h(t - t_i). Withedge_correctthe estimate is divided by the fraction of the kernel falling insidedomain, removing the downward bias near the boundary.- Parameters:
events (ndarray) –
(m,)event locations.grid (ndarray) – points
tat which to evaluate the intensity.bandwidth –
"silverman","scott", or a float.domain (tuple[float, float] | None) –
(lo, hi)observation window (defaults to the event range); used for edge correction.edge_correct (bool) – divide by the in-window kernel mass at each
t.
- Returns:
The intensity evaluated on
grid.- Return type: