bayesianbandits.LaplaceApproximator#

class bayesianbandits.LaplaceApproximator(n_iter: int = 5, tol: float = 0.0001)#

Bases: PosteriorApproximator

Laplace approximation via iteratively reweighted least squares (IRLS).

Approximates the posterior of a Bayesian GLM by finding the MAP estimate \(\hat{w}\) and fitting a Gaussian centered at the mode with precision equal to the negative Hessian:

\[p(w \mid \mathcal{D}) \approx \mathcal{N}\bigl(\hat{w},\; (\alpha I + X^T W X)^{-1}\bigr)\]

where \(W\) is the diagonal matrix of IRLS weights (Fisher information).

Parameters:
  • n_iter (int, default 5) –

    Maximum number of Newton (IRLS) iterations per update.

    • 1: Single-step update from the current posterior. Fast and usually sufficient for online/streaming use where the posterior from the previous step is a good initialization.

    • 3–5: Good default for mini-batch updates.

    • >10: Use for batch fitting when full convergence is needed (pair with a tight tol).

  • tol (float, default 1e-4) – Convergence tolerance on the coefficient change. Iteration stops when \(\|w_{\text{new}} - w_{\text{old}}\|_\infty < \text{tol}\). Only effective when n_iter > 1.

See also

BayesianGLM

Estimator that uses this approximation strategy.

Examples

Fast single-step updates for online learning:

>>> from bayesianbandits import LaplaceApproximator, BayesianGLM
>>> fast = LaplaceApproximator(n_iter=1)
>>> model = BayesianGLM(approximator=fast)

Tight convergence for batch fitting:

>>> batch = LaplaceApproximator(n_iter=500, tol=1e-8)
>>> model = BayesianGLM(approximator=batch)
__init__(n_iter: int = 5, tol: float = 0.0001) None#
n_iter: int = 5#
tol: float = 0.0001#
update_posterior(X: ndarray[tuple[int, ...], dtype[float64]] | csc_array, y: ndarray[tuple[int, ...], dtype[float64]], prior_mean: ndarray[tuple[int, ...], dtype[float64]] | csc_array, prior_precision: ndarray[tuple[int, ...], dtype[float64]] | csc_array, link: Literal['logit', 'log'], sample_weight: ndarray[tuple[int, ...], dtype[float64]] | None, learning_rate: float, sparse: bool) GaussianPosterior#