# Dynamical covariance matrix

I need to fit a model with a covariance matrix changing over time. A toy example code could look like this:

``````data {
int<lower=1> m; // Dimension of observation vector
int<lower=1> N; // Length of time series
vector[m] y[N]; // Time series
}
parameters {
cov_matrix[m] Sigma[N];
}
transformed parameters {
vector[m] mu[N];

//some modelling procedure for the processes mean can go here

}

model {
y[1:N] ~  multi_normal(mu, Sigma);
}
``````

Stan complains about the Sigma not being correctly specified. So my problem is the syntax of specifying such a dynamic covariance matrix. Is there any available examples for this problem somewhere that I am not aware of? Or could someone please help me with defining that structure?

How are you attempting to model the change in the covariance matrix? Just a new covariance matrix at every timestep? In that case, declare an array of covariance matrices, and then index into that array in the model block to pass the correct covariance matrix for each vector `y`. I’m unsure whether `multi_normal` is vectorized over the data and the `Sigma`, but you can wrap this bit in a for-loop and get what you need.

I just updated the code above. The mean vector is added. When I try a similar model like above with a fixed covariance matrix it works fine. I just want to extend that to model heteroscedasticity by letting the covariance matrix (which should be diagonal) change for every time step similar to the mean. I followed the same logic assuming `cov_matrix[m] Sigma[N];` works just fine similar to `vector[m] mu[N];` but my problem with Stan is that you can’t do such generalizations and the same syntax that works for the mean is not working for the covariance.

But you’re right, using a for loop instead of using the vectroized syntax works fine. I was wondering if I am doing something wrong when using the vectorized format but apparently that is only defined for the mean vector and not the covariance matrix.

seems like this discussion Partial-pooling of correlation (or covariance) matrices? - #4 by BenH could help, though if you really have diagonal cov matrices you’re in much simpler territory – but it might give you the idea :)

2 Likes