06. Wider graph: higher cointegration rank and deterministic terms#
The first four notebooks built up to a working BayesianVECM, but only for the narrowest case: coint_rank=1 and deterministic="n". This notebook shows what changes when the graph supports any rank and all five deterministic codes.
Two extensions land in this slice:
Higher cointegration rank (\(r > 1\)). When \(K\) variables share more than one long-run relationship the model needs \(r\) cointegrating vectors: a \((K, r)\) matrix \(\beta\) instead of a \((K, 1)\) column. The Johansen identification generalises: the top \(r \times r\) block of \(\beta\) is pinned to \(I_r\), and the remaining \((K - r) \times r\) entries are free.
Deterministic terms. Real data usually has a non-zero mean or a trend; ignoring that pushes everything into the residuals and biases the cointegration estimates. The design layer already supported five codes (
"n","co","ci","lo","li"); now the PyMC graph does too.
The implementation insight is simple: the graph reads shapes off the design matrices rather than reconstructing them from k_ar_diff and deterministic. Because cointegration_design already appended the right deterministic columns, the graph gets the right shapes for free: no separate code path per code.
What changes in the model:
Code |
|
|
Effect on graph |
|---|---|---|---|
|
\(K\) |
\(Kk\) |
baseline |
|
\(K\) |
\(Kk + 1\) |
\(\Gamma\) widens by one column |
|
\(K\) |
\(Kk + 1\) |
\(\Gamma\) widens by one column |
|
\(K + 1\) |
\(Kk\) |
\(\beta\) gains one free row |
|
\(K + 1\) |
\(Kk\) |
\(\beta\) gains one free row |
from __future__ import annotations
import numpy as np
from bayesian_vecm import BayesianVECM
from bayesian_vecm._design import cointegration_design
np.set_printoptions(precision=3, suppress=True)
# ---------------------------------------------------------------------------
# Sampling configuration
# ---------------------------------------------------------------------------
# Set FAST_SAMPLING = True for quick execution (CI / first read-through).
# Set FAST_SAMPLING = False for publication-quality posteriors.
# ---------------------------------------------------------------------------
FAST_SAMPLING = True
if FAST_SAMPLING:
DRAWS, TUNE, CHAINS = 200, 200, 2
else:
DRAWS, TUNE, CHAINS = 1000, 1000, 4
print(f"Sampling config: draws={DRAWS}, tune={TUNE}, chains={CHAINS}")
if FAST_SAMPLING:
print("(FAST_SAMPLING=True: posteriors are coarser; set to False for full quality)")
Sampling config: draws=200, tune=200, chains=2
(FAST_SAMPLING=True: posteriors are coarser; set to False for full quality)
1. Higher cointegration rank: a trivariate \(r = 2\) model#
The econometrics#
With \(K\) variables the cointegration rank \(r\) tells us how many independent long-run equilibria exist among them. \(r = 1\) (notebooks 03–05) is the simplest case: one cointegrating vector, one error-correction term. \(r = 2\) means two linearly independent long-run relationships.
Everything in the VECM equation generalises:
\(\beta\) is \((K, r)\): each column is one cointegrating vector.
\(\alpha\) is \((K, r)\): each column is the loadings on the corresponding relation.
Identification: pin \(\beta[:r, :] = I_r\). For \(r = 2\) the top \(2 \times 2\) block is fixed at \(I_2\).
The DGP#
We generate a trivariate series (\(K = 3\)) with two cointegrating relations:
In the Johansen normalisation these are stacked as columns of \(\beta\):
The top \(2 \times 2\) block is \(I_2\): exactly the pin the graph enforces. The free row is \((-0.5, -0.3)\) and is what the sampler estimates.
def make_trivariate_r2(
n_obs: int = 150,
seed: int = 7,
) -> np.ndarray:
"""Trivariate VECM DGP with cointegration rank 2.
Two cointegrating relations:
ec1 = y0 - 0.5 * y2
ec2 = y1 - 0.3 * y2
True beta = [[1, 0], [0, 1], [-0.5, -0.3]] (Johansen-normalised).
True alpha = [[-0.3, 0], [0, -0.3], [0.1, 0.1]].
"""
rng = np.random.default_rng(seed=seed)
y = np.zeros((n_obs, 3))
y[0] = rng.normal(size=3)
for t in range(1, n_obs):
ec1 = y[t - 1, 0] - 0.5 * y[t - 1, 2]
ec2 = y[t - 1, 1] - 0.3 * y[t - 1, 2]
y[t, 0] = y[t - 1, 0] - 0.3 * ec1 + rng.normal(scale=0.5)
y[t, 1] = y[t - 1, 1] - 0.3 * ec2 + rng.normal(scale=0.5)
y[t, 2] = y[t - 1, 2] + 0.1 * ec1 + 0.1 * ec2 + rng.normal(scale=0.5)
return y
endog_r2 = make_trivariate_r2(n_obs=150, seed=7)
print(f"Shape: {endog_r2.shape} (T=150, K=3)")
print("\nFirst 5 rows:")
print(endog_r2[:5])
Shape: (150, 3) (T=150, K=3)
First 5 rows:
[[ 0.001 0.299 -0.274]
[-0.486 -0.043 -0.718]
[-0.418 0.575 -0.96 ]
[-0.746 0.561 -0.689]
[-0.573 -0.134 -0.667]]
2. Fit the \(r = 2\) model#
The public API is identical to the \(r = 1\) case: just pass coint_rank=2. The graph builds a \((3, 2)\) \(\beta\) with the top \(2 \times 2\) block pinned to \(I_2\) and the bottom row free.
model_r2 = BayesianVECM(k_ar_diff=1, coint_rank=2, deterministic="n")
model_r2.fit(
endog_r2,
draws=DRAWS,
tune=TUNE,
chains=CHAINS,
random_seed=42,
progressbar=False,
cores=1,
)
print("Fit complete.")
print(model_r2.summary())
NUTS[nutpie]: [alpha, beta_free, Gamma, Sigma_chol]
Fit complete.
mean sd eti89_lb eti89_ub ess_bulk ess_tail r_hat \
alpha[0, 0] -0.36 0.079 -0.49 -0.24 170 285 1.02
alpha[0, 1] -0.108 0.055 -0.19 -0.016 344 277 1.01
alpha[1, 0] 0.057 0.081 -0.078 0.19 281 251 1.00
alpha[1, 1] -0.209 0.054 -0.3 -0.12 218 218 1.02
alpha[2, 0] 0.093 0.077 -0.036 0.22 379 292 1.00
alpha[2, 1] 0.098 0.051 0.018 0.18 478 275 1.00
beta[0, 0] 1 0 1 1 400 400 NaN
beta[0, 1] 0 0 0 0 400 400 NaN
beta[1, 0] 0 0 0 0 400 400 NaN
beta[1, 1] 1 0 1 1 400 400 NaN
beta[2, 0] -0.522 0.02 -0.55 -0.49 290 243 1.00
beta[2, 1] -0.336 0.031 -0.39 -0.29 332 248 1.01
Gamma[0, 0] -0.015 0.079 -0.14 0.11 316 330 1.01
Gamma[0, 1] 0.113 0.09 -0.035 0.26 367 276 1.00
Gamma[0, 2] 0.163 0.088 0.011 0.3 450 236 1.01
Gamma[1, 0] -0.187 0.085 -0.32 -0.047 340 289 1.00
Gamma[1, 1] 0.06 0.1 -0.1 0.21 332 342 1.00
Gamma[1, 2] 0.032 0.092 -0.11 0.18 360 195 1.00
Gamma[2, 0] 0.169 0.087 0.027 0.3 427 250 1.02
Gamma[2, 1] 0.099 0.088 -0.048 0.24 455 311 1.00
Gamma[2, 2] -0.092 0.082 -0.22 0.039 464 263 1.00
Sigma[0, 0] 0.222 0.026 0.18 0.26 532 227 1.00
Sigma[0, 1] 0.005 0.02 -0.031 0.036 458 294 1.00
Sigma[0, 2] 0.02 0.018 -0.0092 0.05 459 298 1.00
Sigma[1, 0] 0.005 0.02 -0.031 0.036 458 294 1.00
Sigma[1, 1] 0.23 0.028 0.19 0.28 493 341 1.00
Sigma[1, 2] -0.046 0.019 -0.079 -0.017 416 272 1.00
Sigma[2, 0] 0.02 0.018 -0.0092 0.05 459 298 1.00
Sigma[2, 1] -0.046 0.019 -0.079 -0.017 416 272 1.00
Sigma[2, 2] 0.202 0.024 0.17 0.24 505 319 1.00
mcse_mean mcse_sd
alpha[0, 0] 0.0058 0.0039
alpha[0, 1] 0.003 0.0021
alpha[1, 0] 0.0048 0.0034
alpha[1, 1] 0.0036 0.0023
alpha[2, 0] 0.004 0.0027
alpha[2, 1] 0.0023 0.0016
beta[0, 0] 0 NaN
beta[0, 1] 0 NaN
beta[1, 0] 0 NaN
beta[1, 1] 0 NaN
beta[2, 0] 0.0011 0.00081
beta[2, 1] 0.0017 0.0015
Gamma[0, 0] 0.0045 0.0028
Gamma[0, 1] 0.0046 0.0034
Gamma[0, 2] 0.0042 0.003
Gamma[1, 0] 0.0046 0.0032
Gamma[1, 1] 0.0055 0.0036
Gamma[1, 2] 0.0049 0.0032
Gamma[2, 0] 0.0042 0.0031
Gamma[2, 1] 0.0041 0.0029
Gamma[2, 2] 0.0038 0.0026
Sigma[0, 0] 0.0011 0.00074
Sigma[0, 1] 0.00097 0.00076
Sigma[0, 2] 0.00087 0.00062
Sigma[1, 0] 0.00097 0.00076
Sigma[1, 1] 0.0013 0.00082
Sigma[1, 2] 0.00095 0.00065
Sigma[2, 0] 0.00087 0.00062
Sigma[2, 1] 0.00095 0.00065
Sigma[2, 2] 0.0011 0.00084
3. Inspecting \(\beta\): shape and identification#
Three things to verify:
Shape.
betashould be \((3, 2)\): three variables, two cointegrating vectors.Pin. The top \(2 \times 2\) block should be exactly \(I_2\) in every posterior draw.
Free row. The third row should recover something close to \((-0.5, -0.3)\): the true values we put in the DGP.
beta_draws = model_r2.idata_.posterior["beta"].values # (chains, draws, K, r)
n_chains, n_draws, K, r = beta_draws.shape
print(f"beta shape: ({K}, {r}), (K, r) as expected")
print()
# 1. Pin: take one draw and check the top 2x2 block
one_draw = beta_draws[0, 0]
print("One posterior draw of beta:")
print(one_draw)
print()
print("Top 2x2 block (should be I_2 exactly):")
print(one_draw[:r, :])
print()
# 2. Free row: posterior mean
free_row_mean = beta_draws[:, :, 2, :].mean(axis=(0, 1))
print(f"Free row (beta[2, :]): posterior mean : {free_row_mean}")
print("Free row (beta[2, :]): true values : [-0.5, -0.3]")
beta shape: (3, 2), (K, r) as expected
One posterior draw of beta:
[[ 1. 0. ]
[ 0. 1. ]
[-0.495 -0.351]]
Top 2x2 block (should be I_2 exactly):
[[1. 0.]
[0. 1.]]
Free row (beta[2, :]): posterior mean : [-0.522 -0.336]
Free row (beta[2, :]): true values : [-0.5, -0.3]
4. Inspecting \(\alpha\): loading matrix#
\(\alpha\) is \((K, r) = (3, 2)\). Column \(j\) of \(\alpha\) gives the speed-of-adjustment coefficients for cointegrating relation \(j\). The true values are:
With 150 observations and fast sampling the posteriors won’t be tight, but the signs should be right and the magnitudes in the right ballpark.
alpha_draws = model_r2.idata_.posterior["alpha"].values # (chains, draws, K, r)
alpha_mean = alpha_draws.mean(axis=(0, 1))
alpha_sd = alpha_draws.std(axis=(0, 1))
print(f"alpha shape: {alpha_mean.shape}, (K, r) = (3, 2)")
print()
print("Posterior mean of alpha:")
print(alpha_mean)
print()
print("Posterior std of alpha:")
print(alpha_sd)
print()
print("True alpha:")
print(np.array([[-0.3, 0.0], [0.0, -0.3], [0.1, 0.1]]))
alpha shape: (3, 2), (K, r) = (3, 2)
Posterior mean of alpha:
[[-0.36 -0.108]
[ 0.057 -0.209]
[ 0.093 0.098]]
Posterior std of alpha:
[[0.079 0.055]
[0.081 0.054]
[0.077 0.051]]
True alpha:
[[-0.3 0. ]
[ 0. -0.3]
[ 0.1 0.1]]
5. Deterministic terms: inside vs outside the cointegrating relation#
Most real-world time series have a non-zero unconditional mean, and many have a drift. A VECM without deterministic terms forces the cointegrating relation to have mean zero and the levels to have no drift: usually wrong.
The five codes map to Johansen’s (1995) cases as follows:
Code |
Term |
Position |
Johansen case |
|---|---|---|---|
|
none |
none |
1 |
|
constant |
outside, in \(\Gamma\,\Delta x_t\) |
3 |
|
constant |
inside, in \(\beta^{\top} y_{t-1}\) |
2 |
|
linear trend |
outside, in \(\Gamma\,\Delta x_t\) |
5 without its constant |
|
linear trend |
inside, in \(\beta^{\top} y_{t-1}\) |
4 without its constant |
Inside vs outside, the intuition:
Outside (
"co","lo"): the constant or trend enters the short-run equation directly, like adding an intercept to an OLS regression. The cointegrating relation itself is still assumed to have mean zero. \(\Delta x\) gains one column, so \(\Gamma\) widens from \((K, Kk)\) to \((K, Kk+1)\).Inside (
"ci","li"): the constant or trend is restricted to the cointegrating relation: it shifts the long-run equilibrium rather than just the short-run dynamics. This is usually the economically appropriate choice when you believe the series are cointegrated around a non-zero mean. \(y_{t-1}\) gains one column, so \(\beta\) widens from \((K, r)\) to \((K+1, r)\) and the extra row is free (sampled).
How to choose: if the cointegrating relation itself has a non-zero mean (e.g. a spread that trades around 50 basis points, not zero), use "ci". If the levels drift but the cointegrating relation is mean-zero, use "co". Case 3 ("co") is the usual default when the levels drift. Case 2 ("ci") suits levels that do not drift.
6. Inside constant ("ci"): \(\beta\) gains a free row#
We use the bivariate DGP from notebook 05 (\(\beta = (1, -0.5)^{\top}\), \(\alpha = (-0.4, 0.2)^{\top}\)) but add a non-zero mean to the cointegrating relation. That is, the true long-run equilibrium is \(y_0 - 0.5 y_1 + \mu = 0\) for some intercept \(\mu\), which "ci" can estimate.
With deterministic="ci", cointegration_design appends a column of ones to y_lag1, making it \((T_{\text{eff}}, K+1)\). The graph reads that shape and builds \(\beta\) as \((K+1, r) = (3, 1)\). The first entry is still pinned to 1; the second (the \(y_1\) coefficient) and the third (the intercept) are both free.
def make_bivariate_ci(
n_obs: int = 120,
mu_ci: float = 2.0, # intercept inside the cointegrating relation
seed: int = 1,
) -> np.ndarray:
"""Bivariate VECM DGP with a constant inside the cointegrating relation.
Long-run equilibrium: y0 - 0.5 * y1 + 2.0 = 0, i.e. y0 ≈ 0.5 * y1 - 2.
True beta (Johansen) = [1, -0.5, 2.0]^T (length-3 vector for K+1=3).
alpha = [-0.4, 0.2].
"""
rng = np.random.default_rng(seed=seed)
y = np.zeros((n_obs, 2))
y[0] = rng.normal(size=2)
for t in range(1, n_obs):
# ec includes the intercept inside the cointegrating relation
ec = y[t - 1, 0] - 0.5 * y[t - 1, 1] + mu_ci
y[t, 0] = y[t - 1, 0] - 0.4 * ec + rng.normal(scale=0.5)
y[t, 1] = y[t - 1, 1] + 0.2 * ec + rng.normal(scale=0.5)
return y
endog_ci = make_bivariate_ci(n_obs=120, mu_ci=2.0, seed=1)
print(f"Shape: {endog_ci.shape}")
print("\nSample mean (both series should have non-zero mean):")
print(endog_ci.mean(axis=0))
Shape: (120, 2)
Sample mean (both series should have non-zero mean):
[-2.696 -1.27 ]
# Inspect the design to confirm y_lag1 has K+1 = 3 columns.
design_ci = cointegration_design(endog_ci, k_ar_diff=1, deterministic="ci")
print(f"delta_y shape : {design_ci.delta_y.shape} (T_eff, K)")
print(f"delta_x shape : {design_ci.delta_x.shape} (T_eff, K*k), unchanged")
print(f"y_lag1 shape : {design_ci.y_lag1.shape} (T_eff, K+1): extra column is the ones column")
print()
print("Last column of y_lag1 (should be all ones):")
print(design_ci.y_lag1[:5, -1])
delta_y shape : (118, 2) (T_eff, K)
delta_x shape : (118, 2) (T_eff, K*k), unchanged
y_lag1 shape : (118, 3) (T_eff, K+1): extra column is the ones column
Last column of y_lag1 (should be all ones):
[1. 1. 1. 1. 1.]
model_ci = BayesianVECM(k_ar_diff=1, coint_rank=1, deterministic="ci")
model_ci.fit(
endog_ci,
draws=DRAWS,
tune=TUNE,
chains=CHAINS,
random_seed=42,
progressbar=False,
cores=1,
)
print("Fit complete.")
print(model_ci.summary())
NUTS[nutpie]: [alpha, beta_free, Gamma, Sigma_chol]
Fit complete.
mean sd eti89_lb eti89_ub ess_bulk ess_tail r_hat \
alpha[0, 0] -0.416 0.087 -0.56 -0.28 439 261 1.01
alpha[1, 0] 0.347 0.085 0.21 0.48 312 223 1.01
beta[0, 0] 1 0 1 1 400 400 NaN
beta[1, 0] -0.574 0.056 -0.67 -0.49 348 200 1.01
beta[2, 0] 2 0.12 1.8 2.2 328 266 1.01
Gamma[0, 0] -0.201 0.091 -0.34 -0.056 476 302 1.00
Gamma[0, 1] -0.146 0.091 -0.3 0.0017 447 299 1.00
Gamma[1, 0] -0.108 0.085 -0.24 0.038 310 272 1.01
Gamma[1, 1] -0.063 0.094 -0.21 0.081 860 348 1.00
Sigma[0, 0] 0.214 0.028 0.18 0.26 412 290 1.00
Sigma[0, 1] -0.02 0.018 -0.052 0.0086 625 325 1.00
Sigma[1, 0] -0.02 0.018 -0.052 0.0086 625 325 1.00
Sigma[1, 1] 0.208 0.026 0.17 0.25 720 401 1.03
mcse_mean mcse_sd
alpha[0, 0] 0.0042 0.0026
alpha[1, 0] 0.0048 0.0035
beta[0, 0] 0 NaN
beta[1, 0] 0.0031 0.0025
beta[2, 0] 0.0069 0.0079
Gamma[0, 0] 0.0042 0.0026
Gamma[0, 1] 0.0044 0.0031
Gamma[1, 0] 0.0049 0.0032
Gamma[1, 1] 0.0032 0.0023
Sigma[0, 0] 0.0014 0.0011
Sigma[0, 1] 0.00073 0.00049
Sigma[1, 0] 0.00073 0.00049
Sigma[1, 1] 0.00097 0.00061
beta_ci = model_ci.idata_.posterior["beta"].values # (chains, draws, K+1, r)
print(f"beta shape: {beta_ci.shape[2:]}, (K+1, r) = (3, 1) as expected")
print()
beta_ci_mean = beta_ci.mean(axis=(0, 1)) # (3, 1)
print("Posterior mean of beta (K+1, r):")
print(beta_ci_mean)
print()
print("Interpretation:")
print(f" beta[0, 0] = {beta_ci_mean[0, 0]:.4f} (pinned to 1.0: Johansen normalisation)")
print(f" beta[1, 0] = {beta_ci_mean[1, 0]:.4f} (y1 coefficient; true = -0.5)")
print(f" beta[2, 0] = {beta_ci_mean[2, 0]:.4f} (intercept inside EC; true = 2.0)")
beta shape: (3, 1), (K+1, r) = (3, 1) as expected
Posterior mean of beta (K+1, r):
[[ 1. ]
[-0.574]
[ 1.997]]
Interpretation:
beta[0, 0] = 1.0000 (pinned to 1.0: Johansen normalisation)
beta[1, 0] = -0.5742 (y1 coefficient; true = -0.5)
beta[2, 0] = 1.9970 (intercept inside EC; true = 2.0)
7. Outside constant ("co"): \(\Gamma\) gains a column#
With deterministic="co" the constant enters the short-run equation but not the cointegrating relation: the long-run equilibrium is still assumed mean-zero. cointegration_design appends a column of ones to delta_x, making it \((T_{\text{eff}}, Kk+1)\). The graph reads that shape and builds \(\Gamma\) as \((K, Kk+1)\): one extra column for the intercept of the short-run dynamics.
\(\beta\) stays \((K, r) = (2, 1)\) because y_lag1 is unchanged.
We reuse the same DGP as notebooks 04–05 (mean-zero cointegrating relation), which is the right setting for "co".
def make_bivariate_baseline(
n_obs: int = 120,
seed: int = 2,
) -> np.ndarray:
"""Bivariate VECM DGP with mean-zero cointegrating relation.
Same as notebooks 04-05: beta=(1, -0.5), alpha=(-0.4, 0.2).
"""
rng = np.random.default_rng(seed=seed)
y = np.zeros((n_obs, 2))
y[0] = rng.normal(size=2)
for t in range(1, n_obs):
ec = y[t - 1, 0] - 0.5 * y[t - 1, 1]
y[t, 0] = y[t - 1, 0] - 0.4 * ec + rng.normal(scale=0.5)
y[t, 1] = y[t - 1, 1] + 0.2 * ec + rng.normal(scale=0.5)
return y
endog_co = make_bivariate_baseline(n_obs=120, seed=2)
# Inspect design
design_co = cointegration_design(endog_co, k_ar_diff=1, deterministic="co")
print(f"delta_y shape : {design_co.delta_y.shape} (T_eff, K)")
extra = "(extra column is the outside-term ones column)"
print(f"delta_x shape : {design_co.delta_x.shape} (T_eff, K*k+1) {extra}")
print(f"y_lag1 shape : {design_co.y_lag1.shape} (T_eff, K), unchanged")
print()
print("Last column of delta_x (should be all ones):")
print(design_co.delta_x[:5, -1])
delta_y shape : (118, 2) (T_eff, K)
delta_x shape : (118, 3) (T_eff, K*k+1) (extra column is the outside-term ones column)
y_lag1 shape : (118, 2) (T_eff, K), unchanged
Last column of delta_x (should be all ones):
[1. 1. 1. 1. 1.]
model_co = BayesianVECM(k_ar_diff=1, coint_rank=1, deterministic="co")
model_co.fit(
endog_co,
draws=DRAWS,
tune=TUNE,
chains=CHAINS,
random_seed=42,
progressbar=False,
cores=1,
)
print("Fit complete.")
print(model_co.summary())
NUTS[nutpie]: [alpha, beta_free, Gamma_centred, Sigma_chol]
Fit complete.
mean sd eti89_lb eti89_ub ess_bulk ess_tail r_hat \
alpha[0, 0] -0.364 0.083 -0.49 -0.24 291 263 1.01
alpha[1, 0] 0.09 0.082 -0.049 0.21 260 304 1.02
beta[0, 0] 1 0 1 1 400 400 NaN
beta[1, 0] -0.51 0.18 -0.81 -0.26 198 251 1.02
Gamma[0, 0] -0.069 0.088 -0.21 0.078 343 261 1.01
Gamma[0, 1] -0.076 0.089 -0.22 0.06 457 360 1.01
Gamma[0, 2] 0.03 0.086 -0.11 0.17 260 311 1.01
Gamma[1, 0] -0.03 0.085 -0.17 0.1 419 306 1.00
Gamma[1, 1] -0.088 0.1 -0.26 0.075 479 346 1.00
Gamma[1, 2] -0.023 0.057 -0.11 0.073 478 377 1.00
Sigma[0, 0] 0.253 0.033 0.21 0.31 447 341 1.00
Sigma[0, 1] -0.022 0.024 -0.06 0.014 728 365 1.00
Sigma[1, 0] -0.022 0.024 -0.06 0.014 728 365 1.00
Sigma[1, 1] 0.267 0.036 0.21 0.33 664 300 1.00
mcse_mean mcse_sd
alpha[0, 0] 0.0049 0.003
alpha[1, 0] 0.0051 0.0036
beta[0, 0] 0 NaN
beta[1, 0] 0.013 0.0094
Gamma[0, 0] 0.0049 0.0034
Gamma[0, 1] 0.0042 0.003
Gamma[0, 2] 0.0054 0.0037
Gamma[1, 0] 0.0041 0.0027
Gamma[1, 1] 0.0046 0.0032
Gamma[1, 2] 0.0026 0.0016
Sigma[0, 0] 0.0017 0.0014
Sigma[0, 1] 0.00089 0.00063
Sigma[1, 0] 0.00089 0.00063
Sigma[1, 1] 0.0014 0.00098
gamma_co = model_co.idata_.posterior["Gamma"].values # (chains, draws, K, Kk+1)
beta_co = model_co.idata_.posterior["beta"].values # (chains, draws, K, r)
print(f"Gamma shape: {gamma_co.shape[2:]}, (K, Kk+1) = (2, 3) as expected")
print(f"beta shape : {beta_co.shape[2:]} (K, r) = (2, 1), unchanged by outside term")
print()
gamma_mean = gamma_co.mean(axis=(0, 1))
print("Posterior mean of Gamma (K, Kk+1):")
print(gamma_mean)
print()
print("Interpretation:")
print(" Columns 0-1: short-run lag coefficients (Gamma_1)")
print(f" Column 2 : intercept of the short-run equation (posterior mean = {gamma_mean[:, 2]})")
print()
beta_co_mean = beta_co.mean(axis=(0, 1))
print(f"beta[1, 0] posterior mean: {beta_co_mean[1, 0]:.4f} (true = -0.5)")
Gamma shape: (2, 3), (K, Kk+1) = (2, 3) as expected
beta shape : (2, 1) (K, r) = (2, 1), unchanged by outside term
Posterior mean of Gamma (K, Kk+1):
[[-0.069 -0.076 0.031]
[-0.03 -0.088 -0.023]]
Interpretation:
Columns 0-1: short-run lag coefficients (Gamma_1)
Column 2 : intercept of the short-run equation (posterior mean = [ 0.031 -0.023])
beta[1, 0] posterior mean: -0.5105 (true = -0.5)
8. What this unlocks and what’s still to come#
Shipped this slice (feat/wider-graph):
The PyMC graph now covers every single-code case:
# Higher rank:
model = BayesianVECM(k_ar_diff=1, coint_rank=2, deterministic="n")
# Constant inside the cointegrating relation:
model = BayesianVECM(k_ar_diff=1, coint_rank=1, deterministic="ci")
# Constant outside (in short-run dynamics):
model = BayesianVECM(k_ar_diff=1, coint_rank=1, deterministic="co")
# Trend codes work the same way:
model = BayesianVECM(k_ar_diff=1, coint_rank=1, deterministic="li")
model = BayesianVECM(k_ar_diff=1, coint_rank=1, deterministic="lo")
All five codes work for any valid coint_rank. The key insight (reading shapes off the design matrices rather than branching on deterministic) kept the implementation to a handful of lines.
Since this notebook. The horseshoe prior on \(\Gamma\) shipped (notebook 10). Stochastic volatility and inferring the rank jointly were not built; the first release fixes \(r\) and uses
select_coint_rankbefore fitting.
Still to come at this stage (modelling extensions, in rough order):
Sparse priors (horseshoe). The \(\Gamma\) block has \(K^2 k\) parameters; most are near zero in practice. A regularised horseshoe would shrink irrelevant entries automatically.
Stochastic volatility. Replace constant \(\Sigma\) with a time-varying Cholesky structure: important for financial data.
Uncertain cointegration rank. Fix \(r\) is a deliberate simplification; inferring it jointly is harder and deferred until the fixed-\(r\) estimator is solid.