Skip to content

Bayesian var

polars_ts.bayesian_var

Bayesian Vector Autoregression (BVAR) for multivariate time series.

BayesianVAR

Bayesian Vector Autoregression forecaster.

Parameters

target_cols Column names to model jointly (>= 2). p Number of lags. prior Prior type: "minnesota" or "normal_wishart". inference Inference method: "analytical" (conjugate posterior) or "gibbs" (Gibbs sampling). minnesota_prior Minnesota prior settings. Used when prior="minnesota". nw_prior Normal-Wishart prior settings. Used when prior="normal_wishart". coverage Credible interval coverage level (default 0.9). n_samples Number of Gibbs samples (after burn-in). burn_in Number of Gibbs burn-in samples. seed Random seed. time_col Column with timestamps.

fit(df, id_col=None)

Fit the Bayesian VAR model.

_fit_single(df, gid)

Fit BVAR on a single group/series.

predict(horizon, id_col=None)

Generate multi-step forecasts with credible intervals.

_forecast_single(result, horizon, alpha_half)

Forecast from a single fitted result.

irf(steps=20, shock_size=1.0, gid=None)

Compute impulse response functions with credible bands.

_compute_irf(B, k, p, steps, shock_size) staticmethod

Compute orthogonalized IRF from coefficient matrix.

MinnesotaPrior dataclass

Minnesota (Litterman) prior for BVAR.

Shrinks VAR coefficients toward a random walk: own first lag receives prior mean 1, all others 0. Tightness parameters control how strongly the prior pulls toward this structure.

Parameters

lambda1 Overall tightness. Smaller values shrink more aggressively. lambda2 Cross-variable tightness (relative to own-lag). Typically < 1 so cross-variable lags are shrunk harder. lambda3 Lag decay. Higher values shrink distant lags more aggressively. Prior variance for lag l is scaled by l^{-lambda3}. sigma_scale Per-variable residual variance estimates. If None, estimated from univariate AR(p) regressions.

NormalWishartPrior dataclass

Normal-Wishart conjugate prior for BVAR.

Places a matrix-normal prior on the coefficient matrix B and a Wishart prior on the precision matrix Sigma^{-1}.

Parameters

B0 Prior mean for the coefficient matrix, shape (k, k*p+1). If None, defaults to random walk (identity on first own-lag). V0 Prior precision (inverse covariance) for vec(B), shape (k*p+1, k*p+1). If None, uses Minnesota-style diagonal with the given tightness. S0 Prior scale matrix for Wishart, shape (k, k). If None, uses identity scaled by data variance. nu0 Degrees of freedom for Wishart. Must be >= k. If None, defaults to k + 2. tightness Diagonal tightness for automatic V0 construction.

BayesianVARResult dataclass

Fitted Bayesian VAR result.

Attributes

B_post Posterior mean coefficient matrix, shape (k, k*p+1). Sigma_post Posterior mean covariance matrix, shape (k, k). B_samples MCMC posterior samples for B, shape (n_samples, k, k*p+1). None for analytical inference. Sigma_samples MCMC posterior samples for Sigma, shape (n_samples, k, k). None for analytical inference. target_cols Names of the modeled variables. p Number of lags.

_build_var_matrices(data, p)

Build design matrix X and response Y for VAR(p).

Parameters

data Array of shape (n, k) with the multivariate time series. p Number of lags.

Returns

X Design matrix, shape (n-p, k*p+1) (includes intercept). Y Response matrix, shape (n-p, k).

bayesian_var(df, target_cols, horizon, p=1, prior='minnesota', inference='analytical', minnesota_prior=None, nw_prior=None, coverage=0.9, n_samples=1000, burn_in=500, seed=42, time_col='ds', id_col=None)

Bayesian VAR convenience function.

_estimate_sigma_from_ar(data, p)

Estimate per-variable residual variance from univariate AR(p).

_minnesota_prior_precision(k, p, prior, sigma_scale)

Build Minnesota prior mean B0 and precision V0_inv.

Returns

B0 Prior mean, shape (k, k*p+1). V0_inv Prior precision diagonal, shape (k*p+1,).