Need help with Mixed Frequency Vector Autoregression (MF_VAR ECONOMETRIC MODEL)

Hi all I am working with MF VAR
It is note to mention about how the estimates of the MFVAR model is produced:

The model is simple VAR

The method is typically done (based on literature) using MCMC where the z_t is linear kalman filter estimates.

The point of doing this is that after generate z_t for all t = 1 ,2,…T.
we need to update vector x_t for low frequency (cause x_t contains both monthly and quarterly data observation). Suppose we have 10 of mothly and 1 quarter stack in a vector x_t. After generate z_t the algorithm suggests we need to replace x_t ( quarterly obs) with z_t that has just been sampled.

It goes on and on interatively in MCMC but can we do this in Stan?

In my work however i wish to do it with Black Box Variational inferences.

My question is:
Is there any way to update x_t which appearently it is Y_t with sampled z_t in Stan?

Best and Happy second lock down in UK maybe?


1 Like

Sorry for not getting to you earlier.

The model you show seems to be (at quick glance) completely within the capabilities of Stan - I believe there are multiple discussion of both Kalman filters and vector autoregressive models in Stan both on the forums and in the wider literature.

I am not sure I understand what you mean. This seems to me as a description of something like Gibbs sampling. This should be unnecessary for Stan - in Stan you just need to compute the density given the paramters and Stan does the rest for you (assuming the density is well-behaved, which it might not be).

On the last StanCon Sarah Heaps had a talk about some misbehaving VAR models, although I don’t understand the topic well enough to be able to tell if this is relevant for you.

Does that make sense?

1 Like

Thank you @martinmodrak for reaching out.

My apologies for mislead question.

My question was only suppose our likelihood PDF is

y ~ normal(X * beta , sigma)

where y = vector [N], beta = vector[M] , X = matrix[N,M]

during the sampling in PyStan is there a way to “alter” y variable?

That present is a very good one i learn the stationary stuff from there which no econometricians has rarely mentioned about.



I am not sure I follow. If y is defined in the parameters block than no, you can’t directly alter it, it is fully controlled by the sampler. You can obviously always define a new variable z that is some function of y, but I guess that’s for some reason not enough for you.

So why would you need to change y directly? Clarifying this would probably help in getting to the root of the problem.

I think i should have said earlier that it is some kind of like MIDAS or NOWCAST thing.

data {
int<lower=1> T; // Number of data
int<lower=1> M; // Number of covariates
int<lower=1> N; // Dim of Observations
vector[M] X[T];
vector[N] Y[T];
parameters {
matrix[N,M] beta_tilde; // 15
matrix<lower=0>[N,M] lambda; //15
real<lower=0> tau_tilde; // 1
cholesky_factor_corr[N] L_Omega; // 31: 56
vector<lower=0>[N] L_sigma;
matrix[T,M] state_parameters;
transformed parameters{
matrix[N,M] beta;
matrix[N, N] L_Sigma;
matrix[N,N] Big_Sigma;
matrix[M,M] state_sigma;
beta = lambda .* beta_tilde * tau_tilde;
L_Sigma = diag_pre_multiply(L_sigma, L_Omega);
Big_Sigma = tcrossprod(L_Sigma);
state_sigma[1:N,1:N] = Big_Sigma;
model {
// State - Space Part.
state_parameters[1] ~ multi_normal(rep_vector(0,M), state_sigma);
for (t in 2:T)
state_parameters[t] ~ multi_normal(state_parameters[t-1], state_sigma);
// Hierarchical Horseshoe Parameters
tau_tilde ~ cauchy(0,4);
to_vector(lambda) ~ cauchy(0,4);
to_vector(beta_tilde) ~ std_normal();
// Cholesky Factor - Covariance for VAR
L_Omega ~ lkj_corr_cholesky(4);
L_sigma ~ cauchy(0, 2.5);
// Mean to mimic observed data Y
vector[N] mu[T];
for (t in 1:T)
mu[t] = beta*X[t];
Y ~ multi_normal_cholesky(mu, L_Sigma);

So basically this is just typical Vector Autoregression (VAR) with State Space Model. Now Observed data Y in stan code contains both Monthly and Quarterly frequency data.

Y_t = [y_{m,t},y_{q,t}]
y_{m,t} is monthly observed data, and y_{q,t} is quarterly data.

The reason why we do Mixed-Frequency VAR is to estimate Quarterly data as Monthly data. Its kind a like “NOWCASTING” or “REAL TIME FORECAST” as mentioned above.

In order to do so we need to replace Y by State Parameters ie (state_parameters in stan code). We only replace the “low frequency data” ie only Quarterly data.

Sorry for bad explanation but do you think it is possible in stan?

1 Like

I tried to get back to this a few times, but I admit I can’t really wrap my head around this.

The only thing I have is that for forecasting you would usually not alter the model block, but instead create your forecasts in the generated quantities block - here you can compute anything you need from the parameters and this will be stored with the rest of the fit. (if I understand it correctly, you don’t need to alter the parameters to influence sampling, but only to create predictions)

1 Like


A silly question, have you check this? They talk about your model and gives a package for implementing it.

I will say is a VAR model looks like a linear state-space model or DLM (you know a var model can always be written as a SSM)

I agree with Martin, I still don’t understand what you need? can you say more?

If what you need is a forecast (1-step ahead prediction) you can easily do it in the generated quantities.

Can you explain more? Also in your Stan code, you sample the state’s parameters, but you don’t put how these parameters interact with the data (this is really important in DLM , SSM, and Stancode). As you see below you are sample does states practically independently from the data. Please help me check if that part of your code is right.

Hope this helps for now!


@martinmodrak Thanks for reaching out !
I have got some interesting complete stan code as an example from Michael DeWittjr

Here is the code

data {
int N1; // length of low frequency series
int N2; // length of high frequency series
int freq; // every freq-th observation of the high frequency series we get an observation of the low frequency one
vector[N1] y;
vector[N2] z1;
vector[N2] z2;
parameters {
real<lower = 0> sigma_epsilon;
real<lower = 0> sigma_eta;
vector[3] gamma;
//real g_s1;
vector[N2] x;
model {
int count;
sigma_epsilon ~ cauchy(0,1);
sigma_eta ~ cauchy(0,1);
gamma ~ normal(0,1);
count = 0;
for(i in 2:N2){
target += normal_lpdf(x[i]| x[i-1]*gamma[1] + z1[i]*gamma[2] + z2[i]*gamma[3], sigma_eta);
count = count + 1;
target += normal_lpdf(y[count]| x[i], sigma_epsilon); } }

It is very interesting for doing nowcast in stan. Rather than using a replacement of low-frequency variable as high-frequency variable from state-space model. They just change the likelihood using count as an indicator when the low-frequency is observed and thus NOWCAST IS DONE !

I have extended some stochastic volatility and some hierarchical shrinkage on high-frequency coefficient in state eq as well.

Hopefully this topic helps many econometrician out there.

@asael_am thanks for sending some R package for me but im doing Variational inference and need to access the log_prob from Stan. In fact i have coded this model successfully on Matlab but unable to do so in Variational inference Fashion.



Ok great! Glad you figure it out!


1 Like