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
|
Fit a Poisson GLM with both nstat and NeMoS; return the comparison. |
Classes
|
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:
objectSide-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 matchesnstat_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|_inffor unitless reports.
- Parameters:
- 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:
- Return type:
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)