Validate transformed params: y is not symmetric. y[1,2] = nan, but y[2,1] = nan

Dear Stan community,

I have adjusted the bmgarch DCC garch model to include mixing parameters (as option distribution = 2). The mixing parameters are estimated separately and hence used as data input for the model.

However, when I run the model I get the following error:
validate transformed params: y is not symmetric. y[1,2] = nan, but y[2,1] = nan

I am a bit confused as there is no ‘y’ parameter. Does someone maybe know what goes wrong?

I have attached trial data and trial mixing parameters.

B_trial.RData (6.3 KB)
mu_trial.RData (2.2 KB)
trial_data_stan.csv (152.3 KB)
DCCMGARCH.stan (6.3 KB)

In your Stan program, you have two transformed parameters that require symmetry:

the only transformed parameter you have that requires symmetry is this one:

transformed parameters {
  cov_matrix[nt] H[T];
  cov_matrix[nt] Qr[T];

So that means that one of the entries for H[t] has not been defined (nan is the default initialization precisely to catch these kinds of errors).

The definitions are:

Qr[1,] = Qr1_init;
H[1] = Qr[1,];

Even better would be to not have three copies of Qr1_init in the program.

This should be fine, because Qr1_init is defined to be of type cov_matrix.

So the problem is probably this:

  for (t in 2:T) {
    Qr[t,] = (1 - a_q - b_q) * S + a_q * (u[t-1,] * u[t-1,]') + b_q * Qr[t-1,];
    H[t,] = quad_form_diag(R[t, ], D[t, ]);

I would recommend writing a little test for symmetry here and reporting an error using reject("t = ", t) if it’s not symmetric to let you know which iteration failed. I’d do it with a function (we’ve been meaning to add these to the language for years, but haven’t yet):

functions {
  void test_symmetry(matrix x) {
    if (rows(x) != cols(x)) reject("columns different sizes");
    for (m in 1:rows(x)) {
      for (n in m + 1:cols(x)) {
        if (x[m, n] != x[n, m]) reject("x", {m, n}, " != x", {n, m});

and then insert

print('testing symmetry for H", t)`

after each H[t] is defined and the same for the other one.

Style comment: Everywhere (left and right sides of equations) you have A[t, ] , it’s cleaner and probably more efficient to just use A[t]. If you wanted to use BUGS style or R style, that’d have to be A[t, , ] in all the cases you used it. Again, that has the same meaning as just A[t] if A has at least three indexes.