nstat.extras.validation.nemos_bridge

Cross-validate nstat.fit_poisson_glm() against NeMoS.

NeMoS (https://github.com/flatironinstitute/nemos) is a JAX-backed Poisson/Gamma GLM toolbox from the Flatiron Institute Center for Computational Neuroscience. It uses the same raised-cosine / B-spline basis families that nstat does (and that the 2012 paper assumes), with an MIT license that is GPL-2 compatible.

This bridge fits the same Poisson GLM in both libraries on a user-supplied design matrix and response vector, then returns a diff-comparison object so callers can assert agreement to a chosen tolerance. It is the strongest available Python-side cross-check on nstat.fit_poisson_glm() short of running MATLAB-engine itself.

Install:

pip install nstat-toolbox[test-parity] # or just: pip install nemos

Functions

cross_validate_poisson_glm(X, y, *[, ...])

Fit a Poisson GLM with both nstat and NeMoS; return the comparison.

Classes

GLMComparison(nstat_coef, nemos_coef, ...)

Side-by-side Poisson-GLM fit comparison.

class nstat.extras.validation.nemos_bridge.GLMComparison(nstat_coef, nemos_coef, coef_inf_norm, coef_rel_inf_norm)[source]

Bases: object

Side-by-side Poisson-GLM fit comparison.

Variables:
  • nstat_coef (numpy.ndarray) – Coefficient vector from nstat.fit_poisson_glm(). Includes the intercept as the first element if the nstat fit included one.

  • nemos_coef (numpy.ndarray) – Coefficient vector from nemos.glm.GLM(...).fit(...). Layout matches nstat_coef (intercept-first if present).

  • coef_inf_norm (float) – \(\|\beta_{nstat} - \beta_{nemos}\|_\infty\). Use this as the assertion target in parity tests.

  • coef_rel_inf_norm (float) – Same but normalized by |nstat_coef|_inf for unitless reports.

Parameters:
nstat_coef: ndarray
nemos_coef: ndarray
coef_inf_norm: float
coef_rel_inf_norm: float
assert_agree(atol=0.001, rtol=0.001)[source]

Assert nstat and NeMoS coefficients agree within tolerance.

Raises:

AssertionError – If neither the absolute (atol) nor relative (rtol) tolerance is met.

Parameters:
Return type:

None

nstat.extras.validation.nemos_bridge.cross_validate_poisson_glm(X, y, *, include_intercept=True)[source]

Fit a Poisson GLM with both nstat and NeMoS; return the comparison.

Both fits use canonical log-link, no L2 regularization on the NeMoS side, and a tiny ridge on the nstat side (its default l2=1e-6); the agreement target should be loose enough to absorb that ridge.

Parameters:
  • X (ndarray) – Design matrix, shape (n_samples, n_features).

  • y (ndarray) – Spike-count vector, shape (n_samples,).

  • include_intercept (bool) – If True (default), both fits include an intercept and the returned coefficient vectors are intercept-first.

Return type:

GLMComparison

Examples

>>> import numpy as np
>>> rng = np.random.default_rng(0)
>>> X = rng.standard_normal((500, 3))
>>> beta_true = np.array([0.2, -0.4, 0.1])
>>> rates = np.exp(0.5 + X @ beta_true)
>>> y = rng.poisson(rates)
>>> cmp = cross_validate_poisson_glm(X, y)
>>> cmp.assert_agree(atol=1e-2)