# Trouble with Basic Multivariate Normal-Inverse Wishart Setup

Hi, brand new Stan user here. I am trying to create a toy example of the standard MVN-Inverse Wishart setting (see these notes for a detailed description) using CmdStan. I am having trouble generating samples from my posterior and repeatedly get a “data y value beyond array dimension range”, even with seemingly tame inputs.

My Stan program, multivarnormal.stan, is as follows:

data {
int<lower=0> N;
vector[2] y[N];
vector[2] muPrior_mu;
cov_matrix[2] muPrior_sigma;
real<lower=1> sigmaPrior_nu;
cov_matrix[2] sigmaPrior_sigma;
}
parameters {
vector[2] mu;
cov_matrix[2] sigma;
}
model {
mu ~ multi_normal(muPrior_mu, muPrior_sigma);
sigma ~ inv_wishart(sigmaPrior_nu, sigmaPrior_sigma);
for (n in 1:N)
y[n] ~ multi_normal(mu, sigma);
}


My data file, multivarnormal.data, is as follows:

N <- 1
y <- structure(c(1,1),
.Dim = c(N,2))
muPrior_mu <- structure(c(0,0),
.Dim = c(1,2))
muPrior_sigma <- structure(c(1,0,0,1),
.Dim = c(2,2))
sigmaPrior_nu <- 3;
sigmaPrior_sigma <- structure(c(1,0,0,1),
.Dim = c(2,2))


My intention here is to pass in a single observation of the vector (1, 1). The prior for the multivariate mean has a mean of (0, 0) and identity covariance matrix, while the prior for the inverse wishart has a \nu of 3 and an identity matrix for \Sigma.

The Stan file compiles fine when I run make on it, but when I run:

<filepath>/multivarnormal sample data file=<filepath>/multivarnormal.data


I get this error:

data y value beyond array dimension range


My understanding of this error is that the sampler has run past the integer arithmetic upper bound which might happen when the choice of prior puts non-trivial density at enormous values. But I’ve tried to pick priors that would prevent this behavior, and there is only one data point so the likelihood shouldn’t be pulling the posterior anywhere near huge numbers. Perhaps I’m formatting my input data wrong? I’m confused about what’s happening here and would dearly appreciate any help.

muPrior_mu should just be a vector rather than a 1\times 2 matrix, i.e.

muPrior_mu <-  c(0, 0)

1 Like

Thanks for the quick response! I updated my .data file to this:

N <- 1
y <- structure(c(1,1),
.Dim = c(N,2))
muPrior_mu <- c(0, 0)
muPrior_sigma <- structure(c(1,0,0,1),
.Dim = c(2,2))
sigmaPrior_nu <- 3
sigmaPrior_sigma <- structure(c(1,0,0,1),
.Dim = c(2,2))


Unfortunately I’m getting the same issue. Maybe one of the structures needs to be reformatted?

Not sure. With muPrior_mu as a vector it works in rstan.

1 Like

Was able to get this working. Turned out that passing N into the .Dim field of y was the problem. Simply passing 1 makes it work. I’m curious if this would be considered a bug or if it is expected behavior, but either way thanks for the help @bgoodri!