Covariance matrices in an array

280 views
Skip to first unread message

FUKAYA Keiichi

unread,
Sep 18, 2012, 2:22:22 AM9/18/12
to stan-...@googlegroups.com
Hi all,

I am trying to analyze vector time series data in RStan. Assuming that a vector time series was taken at multiple sites, I intended to specify different covariance matrix for each site. This was done by embedding covariance matrices in an array with a dimension I (i.e., number of the sites) like:

parameters {
    ...
    cov_matrix[N] Sigma[I];
    ...
}
model {
    ...
    for (i in 1:I) {
        for (t in 2:T) {
            y[t, i] ~ multi_normal(mu_y_t, Sigma[i]);
    ...
}

where both y[t, i] and mu_y_t were specified as vectors of length N. When I run this model, however, I got the following error messages:

LOCATION:  file=input; line=19, column=17

                y[t, i] ~ multi_normal(mu_y_t, Sigma[i]);
                ^-- here

DIAGNOSTIC(S) FROM PARSER:

no matches for function name="multi_normal_log"
    arg 0 type=vector
    arg 1 type=vector
    arg 2 type=row vector
unknown distribution=multi_normal
Parser expecting: "}"


which seems that here Sigma might be recognized in Stan as a matrix, rather than an array.

Was this a wrong approach to model multiple covariance matrices in Stan? Follwing is an R code which would reconstruct this error.

library(rstan)
 
model_code <- '
    data {
        int<lower = 0> T;
        int<lower = 0> I;
        int<lower = 0> N;
        vector[N] y[T, I];
        int<lower = 0> df;
        cov_matrix[N] sm;
    }
    parameters {
        cov_matrix[N] Sigma[I];
    }
    model {
        for (t in 2:T) {
            for (i in 1:I) {
                vector[N] mu_y_t;
                for (n in 1:N)
                    mu_y_t[n] <- y[n, t-1, i];
                y[t, i] ~ multi_normal(mu_y_t, Sigma[i]);
            }
        }
        for (i in 1:I)
            Sigma[i] ~ wishart(df, sm);
    }
'

T <- 20; I <- 10; N <- 3
y <- array(rnorm(T * I * N), dim = c(T, I, N))

data <- list(T = T, I = I, N = N, y = y,
             df = N, sm = diag(N))

fit <- stan(model_code = model_code, data = data)


Regards,

Keiichi

----
Keiichi FUKAYA, Ph.D.
Faculty of Environmental Science
Hokkaido University

Bob Carpenter

unread,
Sep 18, 2012, 2:43:32 PM9/18/12
to stan-...@googlegroups.com
Thanks for the clear bug report.

This is a bug in type inference that I'll fix
right away.

Here's the simplest model that tickles the bug:

data {
vector[5] y[10];
vector[5] mu[10];
}
parameters {
cov_matrix[5] Sigma[10];
}
model {
for (n in 1:10)
y[n] ~ multi_normal(mu[n],Sigma[n]);
}

which produces the following error:

ERROR PARSING
LOCATION: file=input; line=10, column=7

y[i] ~ multi_normal(mu[i],Sigma[i]);
^-- here

DIAGNOSTIC(S) FROM PARSER:

no matches for function name="multi_normal_log"
arg 0 type=vector
arg 1 type=vector
arg 2 type=row vector
unknown distribution=multi_normal
Parser expecting: <statement>

We should be releasing 1.0.2 (or 1.1.0) later
this week or next week, and the fix will go in it.
I'll post back to this list when it's fixed, so
if you're using the command line version and
want to grab the latest version from version control,
that would work until we release 1.0.2.

Until then, there's not a good workaround due
to the problem with type inference.

- Bob

FUKAYA Keiichi

unread,
Sep 18, 2012, 11:58:52 PM9/18/12
to stan-...@googlegroups.com
Hi Bob,

Thanks for the fixing the bug very promptly. I also look forward the
next release.

Cheers,
Keiichi


2012/9/19 Bob Carpenter <ca...@alias-i.com>:

Guy Freeman

unread,
Oct 17, 2012, 3:09:41 AM10/17/12
to stan-...@googlegroups.com
Has this bug been fixed? I downloaded and installed rstan version
1.0.2 but I came across the same error.

Guy

eric.c.brown

unread,
Oct 29, 2012, 5:16:44 PM10/29/12
to stan-...@googlegroups.com
Me too! (I haven't seen the patch in version control)

Ben Goodrich

unread,
Oct 30, 2012, 2:33:00 PM10/30/12
to stan-...@googlegroups.com
I think it was fixed in v1.0.2 by commits

5179786a67fd53a1f995bd2131eeb50e69b1fd9d
0840a1d64e7f9a5ce8cc9a764945d95d73531bf4

Is there still a problem?

Ben

eric.c.brown

unread,
Oct 30, 2012, 3:55:06 PM10/30/12
to
Yes, I'm still getting the same error (rstan 1.0.3), but it could also be a mistake in my code. I wonder if someone else (Guy or Keiichi) can verify?

Ben Goodrich

unread,
Oct 30, 2012, 4:20:10 PM10/30/12
to stan-...@googlegroups.com
On Tuesday, October 30, 2012 3:45:50 PM UTC-4, eric.c.brown wrote:
Yes, I'm getting the same error, but it could also be a mistake in my code. I wonder if someone else can verify?

Bob's simple example is verified to work


   data {
     vector
[5] y[10];
     vector
[5] mu[10];
   
}

   parameters
{
     cov_matrix
[5] Sigma[10];
   
}

   model
{
     
for (n in 1:10)
         y
[n] ~ multi_normal(mu[n],Sigma[n]);
   
}

Ben

eric.c.brown

unread,
Oct 31, 2012, 10:57:07 AM10/31/12
to stan-...@googlegroups.com
Thanks for confirming, Ben.  I must have another mistake, and I will make a new post to see if I can get help to figure out my problem.

Reply all
Reply to author
Forward
0 new messages