Skip to content

pyLGM

General-purpose latent Gaussian models for Python — the model class behind INLA. Declare fixed effects, random effects, temporal (RW/AR1/seasonal), mixed-frequency (MIDAS), spatial (CAR), directed-network (SAR) and dynamic-panel (SDPD) structure; fit them under Gaussian, Poisson, Bernoulli, Binomial, negative-binomial, Gamma, Beta or survival likelihoods, from a Pandas or Spark DataFrame, with exact-Gaussian and Laplace engines, empirical Bayes / MAP-II hyperparameter estimation, INLA-style posterior integration, and a sparse solver that carries the full uncertainty surface at network scale.

Deterministic, no MCMC, no new dependencies.

pip install pylgm

Start here

A Poisson model with a per-region random intercept:

import pandas as pd
from pylgm import Fixed, IID, LGM, Poisson

frame = pd.DataFrame({
    "region": ["north", "north", "north", "south", "south", "south"],
    "time":   [1, 2, 3, 1, 2, 3],
    "x":      [0.0, 0.5, 1.0, 0.0, 0.5, 1.0],
    "count":  [3, 5, 8, 2, 3, 5],
})

model = LGM(
    response="count",
    likelihood=Poisson(),
    predictor=Fixed("1 + x") + IID("region", index="region", precision=2.0),
    panel=("region",),
    time="time",
)

result = model.fit(frame, engine="laplace")
print(result.fitted_mean.round(3).tolist())

Guide

Page Covers
How it works One fit() call end to end — compile, engine, solve, hyperparameters — then prediction and forecasting
Likelihoods Gaussian (exact), Poisson, Bernoulli, Binomial, negative-binomial, Gamma, Beta, Weibull/exponential survival
Effects Fixed, IID, RW1/RW2, AR1 (optionally replicated), Seasonal, MIDAS, SpaceTime
Effects → Modifiers R-INLA's f() arguments as wrappers: Weighted (weights), Copy (copy), Replicated (replicate), Grouped (group + control.group)
Spatial effects Besag, ProperCAR, BYM2, weighted graphs, directed SAR, dynamic DynamicSpatialPanel
Effects → Constraints Arbitrary linear constraints A x = e (R-INLA extraconstr)
Empirical Bayes and priors Type-II ML, MAP-II priors, bounded hyperparameters
INLA integration Grid quadrature, simplified/full-Laplace marginals, DIC/WAIC/CPO/PIT
Prediction Fit-row and out-of-sample prediction, and forecasting new levels
Model comparison Rolling-origin backtests, candidate selection, calibration metrics, artifacts
Spark Spark / Databricks data boundary
Comparison Measured against a GLM, XGBoost and a Metropolis sampler — including where pyLGM loses
Theory The LGM class, Laplace/INLA inference, every structured effect, with references
Internals & release policy Scope, compiled-IR policy, artifact publication, release gate
Roadmap What's shipped and what's next
Development Working on pyLGM itself

Worked examples

Example Shows
Disease mapping Besag spatial smoothing on Scotland lip cancer — fit to observed counts jumps 0.63 → 0.96 vs a non-spatial GLM
Count regression Horseshoe-crab Poisson GLM matching statsmodels, plus estimated overdispersion for honest uncertainty
Anselin (1988) reproduction The reference Columbus crime result, reproduced on real contiguity data
Dynamic networks One network per year across 48 US states, filling panel gaps 5× better than the baselines
Method comparison pyLGM vs GLM vs XGBoost vs Metropolis, on problems chosen so each one wins somewhere

The examples gallery indexes all 21 runnable scripts.