nemos.glm.GLM.score#
- GLM.score(X, y, score_type='log-likelihood', aggregate_sample_scores=<function mean>)[source]#
Evaluate the goodness-of-fit of the model to the observed neural data.
This method computes the goodness-of-fit score, which can either be the mean log-likelihood or of two versions of the pseudo-\(R^2\). The scoring process includes validation of input compatibility with the model’s parameters, ensuring that the model has been previously fitted and the input data are appropriate for scoring. A higher score indicates a better fit of the model to the observed data.
- Parameters:
X (
Union
[Array
,FeaturePytree
, ArrayLike]) – The exogenous variables. Shape(n_time_bins, n_features)
.y (ArrayLike) – Neural activity. Shape
(n_time_bins, )
.score_type (
Literal
['log-likelihood'
,'pseudo-r2-McFadden'
,'pseudo-r2-Cohen'
]) – Type of scoring: either log-likelihood or pseudo-\(R^2\).aggregate_sample_scores (
Callable
) – Function that aggregates the score of all samples.
- Returns:
The log-likelihood or the pseudo-\(R^2\) of the current model.
- Return type:
score
- Raises:
NotFittedError – If
fit
has not been called first with this instance.ValueError – If X structure doesn’t match the params, and if X and y have different number of samples.
Examples
>>> # example input >>> import numpy as np >>> X, y = np.random.normal(size=(10, 2)), np.random.poisson(size=10) >>> import nemos as nmo >>> model = nmo.glm.GLM() >>> model = model.fit(X, y) >>> # get model score >>> log_likelihood_score = model.score(X, y) >>> # get a pseudo-R2 score >>> pseudo_r2_score = model.score(X, y, score_type='pseudo-r2-McFadden')
Notes
The log-likelihood is not on a standard scale, its value is influenced by many factors, among which the number of model parameters. The log-likelihood can assume both positive and negative values.
The Pseudo-\(R^2\) is not equivalent to the \(R^2\) value in linear regression. While both provide a measure of model fit, and assume values in the [0,1] range, the methods and interpretations can differ. The Pseudo-\(R^2\) is particularly useful for generalized linear models when the interpretation of the \(R^2\) as explained variance does not apply (i.e., when the observations are not Gaussian distributed).
Why does the traditional \(R^2\) is usually a poor measure of performance in GLMs?
In the context of GLMs the variance and the mean of the observations are related. Ignoring the relation between them can result in underestimating the model performance; for instance, when we model a Poisson variable with large mean we expect an equally large variance. In this scenario, even if our model perfectly captures the mean, the high-variance will result in large residuals and low \(R^2\). Additionally, when the mean of the observations varies, the variance will vary too. This violates the “homoschedasticity” assumption, necessary for interpreting the \(R^2\) as variance explained.
The \(R^2\) capture the variance explained when the relationship between the observations and the predictors is linear. In GLMs, the link function sets a non-linear mapping between the predictors and the mean of the observations, compromising the interpretation of the \(R^2\).
Note that it is possible to re-normalized the residuals by a mean-dependent quantity proportional to the model standard deviation (i.e. Pearson residuals). This “rescaled” residual distribution however deviates substantially from normality for counting data with low mean (common for spike counts). Therefore, even the Pearson residuals performs poorly as a measure of fit quality, especially for GLM modeling counting data.
Refer to the
nmo.observation_models.Observations
concrete subclasses for the likelihood and pseudo-\(R^2\) equations.