nemos.glm.PopulationGLM.fit#

PopulationGLM.fit(X, y, init_params=None)[source]#

Fit GLM to the activity of a population of neurons.

Fit and store the model parameters as attributes coef_ and intercept_. Each neuron can have different predictors. The feature_mask will determine which feature will be used for which neurons. See the note below for more information on the feature_mask.

Parameters:
  • X (Union[Array, FeaturePytree, ArrayLike]) – Predictors, array of shape (n_timebins, n_features) or pytree of the same shape.

  • y (ArrayLike) – Target neural activity arranged in a matrix, shape (n_timebins, n_neurons).

  • init_params (Optional[Tuple[Union[dict, ArrayLike], ArrayLike]]) – 2-tuple of initial parameter values: (coefficients, intercepts). If None, we initialize coefficients with zeros, intercepts with the log of the mean neural activity. coefficients is an array of shape (n_features, n_neurons) or pytree of the same shape, intercepts is an array of shape (n_neurons, )

Raises:
  • ValueError – If init_params is not of length two.

  • ValueError – If dimensionality of init_params are not correct.

  • ValueError – If X is not two-dimensional.

  • ValueError – If y is not two-dimensional.

  • ValueError – If the feature_mask is not of the right shape.

  • ValueError – If solver returns at least one NaN parameter, which means it found an invalid solution. Try tuning optimization hyperparameters.

  • TypeError – If init_params are not array-like

  • TypeError – If init_params[i] cannot be converted to jnp.ndarray for all i

Notes

The feature_mask is used to select features for each neuron, and it is an NDArray or a nemos.pytrees.FeaturePytree of 0s and 1s. In particular,

  • If the mask is in array format, feature i is a predictor for neuron j if feature_mask[i, j] == 1.

  • If the mask is a :class:nemos.pytrees.FeaturePytree, then "feature_name" is a predictor of neuron j if feature_mask["feature_name"][j] == 1.

Examples

>>> # Generate sample data
>>> import jax.numpy as jnp
>>> import numpy as np
>>> from nemos.glm import PopulationGLM
>>> # Define predictors (X), weights, and neural activity (y)
>>> num_samples, num_features, num_neurons = 100, 3, 2
>>> X = np.random.normal(size=(num_samples, num_features))
>>> # Weights is defined by how each feature influences the output, shape (num_features, num_neurons)
>>> weights = np.array([[ 0.5,  0. ], [-0.5, -0.5], [ 0. ,  1. ]])
>>> # Output y simulates a Poisson distribution based on a linear model between features X and wegihts
>>> y = np.random.poisson(np.exp(X.dot(weights)))
>>> # Define a feature mask, shape (num_features, num_neurons)
>>> feature_mask = jnp.array([[1, 0], [1, 1], [0, 1]])
>>> # Create and fit the model
>>> model = PopulationGLM(feature_mask=feature_mask).fit(X, y)
>>> print(model.coef_.shape)
(3, 2)