| Title: | Bayesian Bandwidth Selection for Multivariate KDE via Expectation Propagation |
|---|---|
| Description: | Implements the approximate Bayesian method for bandwidth selection in multivariate kernel density estimation (KDE) proposed in Filippone & Sanguinetti (2011) <doi:10.1016/j.csda.2011.05.023>. The method uses the Expectation Propagation (EP) algorithm to approximate the posterior distribution of the inverse bandwidth (precision matrix) under a leave-one-out cross-validated likelihood. Three covariance structures are supported: isotropic (scalar precision), diagonal, and full precision matrix. Online Bayesian updating is supported for the isotropic case. The approximate posterior can be used for bandwidth selection, model comparison (via the model evidence / Bayes factor), and online learning. |
| Authors: | Maurizio Filippone [aut, cre] |
| Maintainer: | Maurizio Filippone <[email protected]> |
| License: | GPL-3 |
| Version: | 0.1.0 |
| Built: | 2026-10-11 10:08:24 UTC |
| Source: | https://github.com/mauriziofilippone/epkde |
Runs Expectation Propagation to approximate the posterior distribution of
the diagonal kernel precision in multivariate Gaussian KDE. Each dimension is
assigned an independent Gamma prior.
ep_kde_diagonal(x, prior_shape, prior_rate, max_iter = 100, tol = 1e-06)ep_kde_diagonal(x, prior_shape, prior_rate, max_iter = 100, tol = 1e-06)
x |
Numeric matrix of training data ( |
prior_shape |
Numeric vector of length |
prior_rate |
Numeric vector of length |
max_iter |
Maximum number of EP outer iterations. Default 100. |
tol |
Convergence tolerance. Default |
A named list with elements:
post_shapeLength- vector of posterior Gamma shapes.
post_rateLength- vector of posterior Gamma rates.
log_evidenceApproximate log model evidence.
log_norm_constLog normalization constant of the posterior.
shape_factors matrix of EP factor shapes.
rate_factors matrix of EP factor rates.
log_norm_factorsLength- vector of EP factor log
normalization constants.
Filippone, M. & Sanguinetti, G. (2011). Approximate inference of the bandwidth in multivariate kernel density estimation. Computational Statistics & Data Analysis, 55(12), 3104-3122.
set.seed(1) x <- matrix(rnorm(100 * 2), ncol = 2) fit <- ep_kde_diagonal(x, prior_shape = rep(1, 2), prior_rate = rep(1, 2)) cat("Posterior mean precision:", fit$post_shape / fit$post_rate, "\n")set.seed(1) x <- matrix(rnorm(100 * 2), ncol = 2) fit <- ep_kde_diagonal(x, prior_shape = rep(1, 2), prior_rate = rep(1, 2)) cat("Posterior mean precision:", fit$post_shape / fit$post_rate, "\n")
Runs Expectation Propagation to approximate the posterior distribution of
the full kernel precision matrix in
multivariate Gaussian KDE. The approximate posterior is a Wishart
distribution .
ep_kde_full(x, prior_cov = NULL, prior_nu = 0, max_iter = 100, tol = 1e-06)ep_kde_full(x, prior_cov = NULL, prior_nu = 0, max_iter = 100, tol = 1e-06)
x |
Numeric matrix of training data ( |
prior_cov |
|
prior_nu |
Wishart degrees-of-freedom parameter |
max_iter |
Maximum number of EP outer iterations. Default 100. |
tol |
Convergence tolerance on |
A named list with elements:
post_precPosterior Wishart precision parameter
(i.e., ).
post_nuPosterior Wishart degrees-of-freedom .
post_meanPosterior mean precision matrix
.
bandwidth_matrixPosterior mean bandwidth matrix
(inverse of post_mean).
log_evidenceApproximate log model evidence.
log_norm_constLog normalization constant of the posterior.
Returns NULL with a warning if EP fails to converge.
Filippone, M. & Sanguinetti, G. (2011). Approximate inference of the bandwidth in multivariate kernel density estimation. Computational Statistics & Data Analysis, 55(12), 3104-3122.
set.seed(1) x <- matrix(rnorm(50 * 2), ncol = 2) fit <- ep_kde_full(x, prior_cov = NULL, prior_nu = 0) cat("Posterior nu:", fit$post_nu, "\n") print(fit$bandwidth_matrix)set.seed(1) x <- matrix(rnorm(50 * 2), ncol = 2) fit <- ep_kde_full(x, prior_cov = NULL, prior_nu = 0) cat("Posterior nu:", fit$post_nu, "\n") print(fit$bandwidth_matrix)
Runs Expectation Propagation to approximate the posterior distribution of
the scalar kernel precision (inverse bandwidth squared) in
multivariate Gaussian KDE with an isotropic kernel. The approximate
posterior is and the log model evidence is
also returned.
ep_kde_isotropic( x, prior_shape = 0, prior_rate = 0, max_iter = 100, tol = 1e-09 )ep_kde_isotropic( x, prior_shape = 0, prior_rate = 0, max_iter = 100, tol = 1e-09 )
x |
Numeric matrix of training data ( |
prior_shape |
Non-negative scalar; shape parameter |
prior_rate |
Non-negative scalar; rate parameter |
max_iter |
Maximum number of EP outer iterations. Default 100. |
tol |
Convergence tolerance on the change in posterior parameters.
Default |
A named list with elements:
post_shapePosterior Gamma shape .
post_ratePosterior Gamma rate .
log_evidenceApproximate log model evidence.
log_norm_constLog normalization constant of the approximate posterior (for expert use).
shape_factorsLength- vector of EP factor shape
parameters (one per data point).
rate_factorsLength- vector of EP factor rate
parameters (one per data point).
log_norm_factorsLength- vector of EP factor log
normalization constants (one per data point).
Filippone, M. & Sanguinetti, G. (2011). Approximate inference of the bandwidth in multivariate kernel density estimation. Computational Statistics & Data Analysis, 55(12), 3104-3122.
set.seed(1) x <- matrix(rnorm(100 * 2), ncol = 2) # 100 bivariate observations fit <- ep_kde_isotropic(x, prior_shape = 1, prior_rate = 1) cat("Posterior mean precision:", fit$post_shape / fit$post_rate, "\n") cat("Log model evidence:", fit$log_evidence, "\n")set.seed(1) x <- matrix(rnorm(100 * 2), ncol = 2) # 100 bivariate observations fit <- ep_kde_isotropic(x, prior_shape = 1, prior_rate = 1) cat("Posterior mean precision:", fit$post_shape / fit$post_rate, "\n") cat("Log model evidence:", fit$log_evidence, "\n")
Updates a previously computed EP approximation (from ep_kde_isotropic)
to incorporate a new batch of observations, without re-processing
the original data points from scratch.
ep_kde_online( x_old, x_new, prior_shape, prior_rate, fit_old, max_iter = 100, tol = 1e-09 )ep_kde_online( x_old, x_new, prior_shape, prior_rate, fit_old, max_iter = 100, tol = 1e-09 )
x_old |
Numeric matrix ( |
x_new |
Numeric matrix ( |
prior_shape, prior_rate
|
Prior parameters (same values used in the
original |
fit_old |
The list returned by |
max_iter, tol
|
Convergence control (see |
A named list with the same structure as ep_kde_isotropic,
but with shape_factors, rate_factors, and
log_norm_factors of length .
Filippone, M. & Sanguinetti, G. (2011). Approximate inference of the bandwidth in multivariate kernel density estimation. Computational Statistics & Data Analysis, 55(12), 3104-3122.
Given training data and a fitted precision parameter (from one of the
ep_kde_* functions), evaluates the KDE
at a set of test points.
kde_predict(x_test, x_train, precision_matrix, log_scale = FALSE)kde_predict(x_test, x_train, precision_matrix, log_scale = FALSE)
x_test |
Numeric matrix ( |
x_train |
Numeric matrix ( |
precision_matrix |
|
log_scale |
Logical; if |
Numeric vector of length with the (log-)density values at
x_test.
set.seed(1) x_train <- matrix(rnorm(100 * 2), ncol = 2) fit <- ep_kde_isotropic(x_train, prior_shape = 1, prior_rate = 1) Lambda <- fit$post_shape / fit$post_rate * diag(2) x_test <- matrix(c(0, 0, 1, 1), ncol = 2) p_hat <- kde_predict(x_test, x_train, Lambda)set.seed(1) x_train <- matrix(rnorm(100 * 2), ncol = 2) fit <- ep_kde_isotropic(x_train, prior_shape = 1, prior_rate = 1) Lambda <- fit$post_shape / fit$post_rate * diag(2) x_test <- matrix(c(0, 0, 1, 1), ncol = 2) p_hat <- kde_predict(x_test, x_train, Lambda)
A convenience wrapper that returns the log model evidence from a fitted
ep_kde_* object. The evidence can be used to compute Bayes factors
comparing, e.g., isotropic vs. diagonal vs. full precision structures.
model_evidence(fit)model_evidence(fit)
fit |
A list returned by |
A single numeric value: the approximate log model evidence.
set.seed(1) x <- matrix(rnorm(60 * 2), ncol = 2) fit_iso <- ep_kde_isotropic(x, prior_shape = 1, prior_rate = 1) fit_diag <- ep_kde_diagonal(x, prior_shape = rep(1, 2), prior_rate = rep(1, 2)) fit_full <- ep_kde_full(x, prior_cov = NULL, prior_nu = 0) cat("Log evidence isotropic:", model_evidence(fit_iso), "\n") cat("Log evidence diagonal: ", model_evidence(fit_diag), "\n") cat("Log evidence full: ", model_evidence(fit_full), "\n") # Bayes factor: full vs. isotropic (on log scale) cat("Log BF (full vs. iso):", model_evidence(fit_full) - model_evidence(fit_iso), "\n")set.seed(1) x <- matrix(rnorm(60 * 2), ncol = 2) fit_iso <- ep_kde_isotropic(x, prior_shape = 1, prior_rate = 1) fit_diag <- ep_kde_diagonal(x, prior_shape = rep(1, 2), prior_rate = rep(1, 2)) fit_full <- ep_kde_full(x, prior_cov = NULL, prior_nu = 0) cat("Log evidence isotropic:", model_evidence(fit_iso), "\n") cat("Log evidence diagonal: ", model_evidence(fit_diag), "\n") cat("Log evidence full: ", model_evidence(fit_full), "\n") # Bayes factor: full vs. isotropic (on log scale) cat("Log BF (full vs. iso):", model_evidence(fit_full) - model_evidence(fit_iso), "\n")