bayesianbandits.GammaRegressor#
- class bayesianbandits.GammaRegressor(alpha: float, beta: float, *, learning_rate: float = 1.0, random_state: int | None | Generator = None)#
Bases:
BaseEstimator,RegressorMixinIntercept-only Gamma-Poisson conjugate regression model.
Maintains a separate Gamma posterior over the rate parameter \(\lambda\) for each unique value of the first feature. Designed for modeling count data where the rate may differ across groups. Supports sample weights (for importance-weighted updates in adversarial bandit algorithms) and online learning via
partial_fit.- Parameters:
alpha (
float) – Prior shape parameter of the Gamma distribution. The prior is \(\lambda \sim \text{Gamma}(\alpha, \beta)\). Larger values concentrate the prior more tightly around the prior mean \(\alpha / \beta\).beta (
float) – Prior rate (inverse scale) parameter. Together withalpha, determines the prior mean \(\mathbb{E}[\lambda] = \alpha / \beta\) and prior variance \(\text{Var}[\lambda] = \alpha / \beta^2\).learning_rate (
float, default1.0) – Decay rate for the posterior parameters. Values less than 1 geometrically shrink both \(\alpha\) and \(\beta\) on each call todecay, increasing posterior variance while preserving the mean. This converts the model into a forgetting estimator suitable for restless bandit problems.random_state (
int,np.random.Generator, orNone, defaultNone) – Controls the random number generator forsample. Pass an int for reproducible results across calls.
- coef_#
Posterior parameters
[alpha, beta]for each observed feature value. Unseen feature values default to the prior.- Type:
dictof{int or float: ndarray of shape (2,)}
- n_features_#
Number of features seen during
fit. Must be 1.- Type:
int
- prior_#
Prior parameters
[alpha, beta].- Type:
ndarrayofshape (2,)
See also
DirichletClassifierBayesian classification for categorical outcomes.
NormalRegressorBayesian linear regression for real-valued outcomes.
Notes
This model implements the Gamma-Poisson conjugate model. Given observations \(y_i\) with sample weights \(w_i\), the posterior update is [1]:
\[\alpha^{\text{post}} = \alpha^{\text{prior}} + \sum_{i=1}^{N} w_i \, y_i, \qquad \beta^{\text{post}} = \beta^{\text{prior}} + \sum_{i=1}^{N} w_i\]The posterior mean rate is \(\mathbb{E}[\lambda] = \alpha / \beta\) and the posterior variance is \(\text{Var}[\lambda] = \alpha / \beta^2\).
When
learning_rate < 1, callingdecayscales both \(\alpha\) and \(\beta\) equally, so the posterior mean is preserved but the variance increases.References
Examples
Basic rate estimation:
>>> import numpy as np >>> X = np.array([[1], [2], [3], [4], [5]]) >>> y = np.array([1, 2, 3, 4, 5]) >>> model = GammaRegressor(alpha=1, beta=1, random_state=0) >>> model.fit(X, y) GammaRegressor(alpha=1, beta=1, random_state=0)
Using sample weights for importance-weighted updates:
>>> weights = np.array([1.0, 2.0, 1.0, 0.5, 1.5]) >>> model.fit(X, y, sample_weight=weights) GammaRegressor(alpha=1, beta=1, random_state=0)
Online learning with
partial_fit:>>> model.partial_fit(X, y, sample_weight=weights) GammaRegressor(alpha=1, beta=1, random_state=0)
- __init__(alpha: float, beta: float, *, learning_rate: float = 1.0, random_state: int | None | Generator = None) None#
- decay(X: NDArray[Any], *, decay_rate: float | None = None) None#
Decay posterior parameters to increase uncertainty.
Scales both shape and rate by the decay factor: \(\alpha \leftarrow \gamma \alpha,\; \beta \leftarrow \gamma \beta\). Because both parameters are scaled equally, the posterior mean \(\alpha / \beta\) is preserved but the variance \(\alpha / \beta^2\) increases.
Has no effect if the model has not been fitted.
- Parameters:
X (
array-likeofshape (n_samples,1)) – Input features. Each unique value ofX[:, 0]identifies a group whose parameters are decayed.decay_rate (
float, defaultNone) – Multiplicative decay factor \(\gamma\) in (0, 1]. If None, usesself.learning_rate.
See also
partial_fitUpdate the model with new observations.
- fit(X: NDArray[Any], y: NDArray[Any], sample_weight: NDArray[Any] | None = None) Self#
Fit the model from scratch, resetting the prior.
Initializes the Gamma prior from
alphaandbetaand updates the posterior parameters using the observed counts (optionally weighted).- Parameters:
X (
array-likeofshape (n_samples,1)) – Training data. Only the first column is used; each unique value indexes a separate Gamma posterior.y (
array-likeofshape (n_samples,)) – Target values (non-negative integer counts).sample_weight (
array-likeofshape (n_samples,), defaultNone) – Individual weights for each sample. If None, all samples are given weight 1.0.
- Returns:
self – Fitted estimator.
- Return type:
See also
partial_fitIncremental update without resetting the prior.
- partial_fit(X: NDArray[Any], y: NDArray[Any], sample_weight: NDArray[Any] | None = None)#
Incrementally update the model with new observations.
Uses the current posterior as the prior for the new update. If the model has not been fitted yet, this is equivalent to calling
fit.- Parameters:
X (
array-likeofshape (n_samples,1)) – Training data. Only the first column is used; each unique value indexes a separate Gamma posterior.y (
array-likeofshape (n_samples,)) – Target values (non-negative integer counts).sample_weight (
array-likeofshape (n_samples,), defaultNone) – Individual weights for each sample. If None, all samples are given weight 1.0.
- Returns:
self – Updated estimator.
- Return type:
See also
fitFit from scratch, resetting the prior.
- predict(X: NDArray[Any]) NDArray[Any]#
Predict the posterior mean rate for each sample.
Returns the mean of the Gamma posterior \(\mathbb{E}[\lambda] = \alpha / \beta\).
If the model has not been fitted, returns the prior mean.
- Parameters:
X (
array-likeofshape (n_samples,1)) – Input features. Each unique value ofX[:, 0]indexes a separate Gamma posterior.- Returns:
y_pred – Predicted rates (posterior means).
- Return type:
ndarrayofshape (n_samples,)
See also
sampleDraw from the Gamma posterior.
- sample(X: NDArray[Any], size: int = 1) NDArray[np.float64]#
Sample rates from the Gamma posterior.
For each input, draws from \(\text{Gamma}(\alpha, \beta)\) where \(\alpha\) and \(\beta\) are the posterior shape and rate parameters for that input’s group. If the model has not been fitted, samples from the prior.
- Parameters:
X (
array-likeofshape (n_samples,1)) – Input features. Each unique value ofX[:, 0]indexes a separate Gamma posterior.size (
int, default1) – Number of independent draws from each posterior.
- Returns:
samples – Sampled rates from the Gamma posterior.
- Return type:
ndarrayofshape (size,n_samples)
See also
predictPoint estimate using the posterior mean.