Skip to content

Instantly share code, notes, and snippets.

@ckrapu
Created February 12, 2022 11:18
Show Gist options
  • Save ckrapu/274ebb30e8eb9f7499e7f8826c73f455 to your computer and use it in GitHub Desktop.
Save ckrapu/274ebb30e8eb9f7499e7f8826c73f455 to your computer and use it in GitHub Desktop.
VAR(1) with Frobenius penalty on weight matrix
import numpy as np
import pymc3 as pm
import theano.tensor as tt
def frobenius_norm(X):
return tt.sum(tt.nlinalg.trace(A@A.T))**0.5
# Create simulated data via forward evolution of system
K = 3
T = 10
error_sd_true = 0.5
A_true = np.random.randn(K,K)
x = np.zeros([T, K])
x[0] = np.random.randn(K)
for t in range(1, T):
jump = np.random.randn(K)
x[t] = A_true@x[t-1] + jump*error_sd_true
penalty_weight = 0.1
with pm.Model():
A_scale = pm.InverseGamma('A_scale', alpha=1, beta=1)
A = pm.Normal('A', sd=A_scale, shape=[K,K])
sd = pm.HalfCauchy('error_sd', beta=1)
pm.Normal('x_end', mu=x[0:-1]@A, observed=x[1:], sd=sd)
pm.Potential('A_penalty', frobenius_norm(A) * penalty_weight)
trace = pm.sample()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment