On 3/26/13 3:25 PM, Devin Caughey wrote:
>
> I am fitting a binomial count model, where the counts are stored in R as a 3D array (group by question by time). The
> model runs fine in non-vectorized form, with the array of counts declared as follows:
>
> int<lower=0> RRY[G, Q, T]; // count of correct answers in group, by year
> int<lower=0> NNY[G, Q, T]; // count of responses in group, by year
>
> and the response model written as follows:
>
> RRY[g, q, t] ~ binomial(NNY[g, q, t], prob[q, t, g]); // group counts
>
> To speed up computation, I would like to vectorize binomial(), but this requires that I declare RRY and NNY as arrays of
> vectors, as in:
>
> vector[T] RRY[G, Q];
Not quite. Here's the signature for binomial (from
the index):
binomial_log
(ints n, ints N, reals theta):real
An "ints" type means it can be an int or an int *array*,
and "reals" means it can be a real, real array, or real vector.
You're right that there are no vectors of int types.
There is a simple and then an extreme form of vectorization
you could do for this statement:
RRY[g, q, t] ~ binomial(NNY[g, q, t], prob[q, t, g]); // group counts
If you reorganize prob to be indexed prob[g,q,t], then you can
vectorize on t:
RRY[g,q] ~ binomial(NNY[g,q], prob[g,q]);
Or you could reorganize and vectorize on the longest dimension.
At a more extreme level, you can create one large G * Q * T array
and fill it in with your own indexing and vectorize that way.
It'll be more efficient, despite all the copying, but less readable.
RRY_vec int[G * Q * T];
NNY_vec int[G * Q * T];
prob_vec real[G * Q * T];
int pos;
pos <- 1;
for (g in 1:G) {
for (q in 1:Q) {
for (t in 1:T) {
RRY_vec[pos] <- RRY[g,q,t];
NNY_vec[pos] <- NNY[g,q,t];
prob_vec[pos] <- prob[q,t,g];
pos <- pos + 1;
}
}
}
RRY_vec ~ binomail(NNY_vec,prob_vec);
- Bob