A Negative Binomial model for overdispersed count data using mean-dispersion parameterization.
This model represents a Negative Binomial distribution [4] commonly used to model overdispersed
count data [5][6] (i.e., data where the variance exceeds the mean), which cannot be captured by a
standard Poisson model. The distribution is parameterized by the predicted mean rate
(\(\mu\)) and a fixed dispersion parameter (\(\phi\)) or scale of the model.
Important: the scale parameter must be estimated from the data for accurately capturing the
dispersion. In the context of NeMoS GLM, estimation can be achieved by cross-validating the
scale parameter. One may use scikit-learn GridSearchCV for example.
The variance of the Negative Binomial distribution under this parameterization is:
\[\mathrm{Var}(Y) = \mu + \phi \mu^2\]
where \(\mu\) is the predicted mean, and \(\phi\) is the dispersion parameter. This
formulation corresponds to the Negative Binomial as a Gamma–Poisson mixture.
The scale parameter \(\phi\) is related to the canonical Negative Binomial
shape parameter r as:
\[r = \frac{1}{\phi}\]
As \(\phi \to 0\) (equivalently, \(r \to \infty\)), the distribution approaches a
Poisson distribution. This makes the model flexible for handling both equidispersed
(Poisson-like) and overdispersed data.
Parameters:
scale – The dispersion parameter \(\phi\). Lower values correspond to lower overdispersion, and as
\(\phi \to 0\), the model behaves like a Poisson. The shape parameter of
the Negative Binomial is given by r = 1 / scale.
This uses PEP-487 [1] to set the set_{method}_request methods. It
looks for the information available in the set default values which are
set using __metadata_request__* class attributes, or inferred
from method signatures.
The __metadata_request__* class attributes are used when a method
does not explicitly accept a metadata through its arguments or if the
developer would like to specify a request value for those metadata
which are different from the default None.
Compute the residual deviance for a Negative Binomial model.
The deviance measures how well a statistical model fits the data by
quantifying the difference between the observed values and the values
predicted by the model. Lower values of deviance indicate a better fit.
Parameters:
observations (Array) – Observed count data. Shape (n_time_bins,) or (n_time_bins,n_observations).
predicted_rate (Array) – Predicted mean count of the Negative Binomial distribution. Shape matches observations.
scale (Union[float, Array, None]) – Dispersion parameter of the distribution.
The scale parameter of the Negative Binomial distribution is set
at initialization and affect the likelihood landscape. This implies
that the scale parameter cannot be estimated post-hoc without
re-fitting a model.
Note that the arguments of this method are not used but are kept for
API consistency—i.e., all Observations.estimate_scale methods
have the same signature.
NeMoS currently does not support joint estimation of scale and mean for the negative binomial.
For alternatives, see the R package MASS
glm.nb
for more details.
This computes the Negative Binomial log-likelihood of the predicted mean
rate for the observed counts.
Parameters:
y (Array) – Observed count data. Shape (n_time_bins,) or (n_time_bins,n_observations).
predicted_rate (Array) – The predicted mean of the Negative Binomial distribution. Shape (n_time_bins,) or
(n_time_bins,n_observations).
scale (Union[float, Array, None]) – The scale (dispersion) parameter of the distribution. It is related to the shape r as r=1/scale.
Default is the scale provided at initialization self.scale.
aggregate_sample_scores (Callable) – Function that aggregates the log-likelihood across samples (e.g., jnp.mean or jnp.sum).
Returns:
The log-likelihood of the Negative Binomial model. Shape (1,).
Compute the pseudo-\(R^2\) metric for the GLM, as defined by McFadden et al. [2]
or by Cohen et al. [3].
This metric evaluates the goodness-of-fit of the model relative to a null (baseline) model that assumes a
constant mean for the observations. While the pseudo-\(R^2\) is bounded between 0 and 1 for the
training set, it can yield negative values on out-of-sample data, indicating potential over-fitting.
Parameters:
y (Array) – The neural activity. Expected shape: (n_time_bins,)
predicted_rate (Array) – The mean neural activity. Expected shape: (n_time_bins,)
score_type (Literal['pseudo-r2-McFadden', 'pseudo-r2-Cohen']) – The pseudo-\(R^2\) type.
The pseudo-\(R^2\) of the model. A value closer to 1 indicates a better model fit,
whereas a value closer to 0 suggests that the model doesn’t improve much over the null model.
where \(L_M\), \(L_0\) and \(L_s\) are the likelihood of the fitted model, the null model (a
model with only the intercept term), and the saturated model (a model with one parameter per
sample, i.e. the maximum value that the likelihood could possibly achieve). \(D_M\) and \(D_0\) are
the model and the null deviance, \(D_i = -2 \left[ \log(L_s) - \log(L_i) \right]\) for \(i=M,0\).
The method works on simple estimators as well as on nested objects
(such as Pipeline). The latter have
parameters of the form <component>__<parameter> so that it’s
possible to update each component of a nested object.