build_lag_design_matrix#
- impulso.build_lag_design_matrix(endog, n_lags, exog=None)[source]#
Build the lag-major VAR design matrix and trimmed response.
Stacks n_lags lagged blocks of endog side by side in lag-major order — every variable’s lag 1, then every variable’s lag 2, and so on — matching the coeff coordinate VAR._build_pymc_model labels posterior draws with (“L<lag>.<variable>”). The leading n_lags rows of endog (and of exog, when given) are consumed as initial conditions, so both the response and the exogenous block are trimmed to the same estimation sample the regressors cover.
Works on a plain numpy.ndarray or on a symbolic 2-D PyTensor tensor: the lag blocks are built with the same slicing either way, and only the final concatenation branches — numpy.hstack for an array, pytensor.tensor.concatenate for a tensor. This lets endog be a latent block declared elsewhere in a PyMC model rather than observed data. PyTensor is imported lazily, inside the symbolic branch, so importing this module never pulls it in.
- Parameters:
endog (np.ndarray | pt.TensorVariable) – Endogenous data of shape (T, n_vars) — a numpy array of observed values, or a symbolic 2-D tensor (e.g. a latent block of another PyMC model).
n_lags (int) – Number of lags to stack. Must be >= 1.
exog (ndarray | None) – Optional exogenous regressors of shape (T, n_exog), trimmed alongside endog. None (the default) if the model has no exogenous block.
- Returns:
Y: trimmed response, endog[n_lags:], shape (T - n_lags, n_vars).
X_lag: lag-major stacked regressors, shape (T - n_lags, n_vars * n_lags), same array/tensor kind as endog.
X_exog: exog[n_lags:] if exog was given, else None.
- Return type:
Tuple (Y, X_lag, X_exog)
- Raises:
ValueError – If n_lags is not positive.