On 1/21/14, 3:33 PM, Jason Engle wrote:
> I'm trying to translate a Nominal Response model (Bock 1972) from a working BUGS code that runs interminably slowly.
> I've tried STAN with the same code, but the performance fares equally as poorly, if not worse,
Is it mixing slowly or just running slowly per iteration?
Speeding up the implementation won't help with the mixing.
Often poor mixing arises because a model's not well identified.
What you want to compare is time per effective sample, not
time per iteration. The number of effective samples you get out
in the end is all that matters in terms of posterior inference.
The residual error in estimating posterior means is reported
as MCMC std error in Stan output, and that's just (estimated)
posterior variance divided by the sqrt of the number of effective
samples.
> which leads me to suspect
> I'm doing something suboptimal in the world of STAN, but have had no luck in figuring it out over the course of a day of
> putzing.
> My primary thought is that (from forums and such):
>
> resp[k,j]'~categorical(p[j,k]);
>
> should be done all at once as vectors. That's fine, but I run across an error when I form it as resp'<-categorical(p):
>
> no matches for function name="transpose"
> arg 0 type=int[2]
> available function signatures for transpose:
> 0. transpose(vector) : row vector
> 1. transpose(row vector) : vector
> 2. transpose(matrix) : matrix
> expression is ill formed
Stan's strongly typed and each function is very picky about
the type of arguments it works on. Your variable "resp"
is a 2D integer array (i.e., int[2]), whereas the transpose
operator (') only applies to vectors, row vectors, or matrices.
I rewrote the model for you more efficiently. To understand
the changes, note that if x is a matrix then x[n] is a row
vector, and look up the definition of categorical_logit, which
will lead you to softmax. You were implementing softmax yourself
in code. But using the correct parameterization of categorical
will be much faster. Also note that I vectorized the prior for z.
And please check my work on the transforms
to make sure that both I did it right and you understand what I did.
(We should have a version of categorical_logit that takes a row vector
as a parameter, and then you wouldn't need that final transposition.)
You could definite temporaries for the argument to categorical_logit,
but it's not necessary in Stan.
- Bob
data {
int N;
int T;
int NC;
int resp[T,N];
matrix[T,NC] lam;
}
parameters {
real theta[N];
real thetaN[T];
matrix[T,NC] z;
}
model {
theta ~ normal(0,1);
thetaN ~ normal(0,1);
for (t in 1:T)
z[t] ~ normal(0,1);
for (n in 1:N)
for (t in 1:T)
resp[t,n] ~ categorical_logit((z[t]
+ lam[t] * fabs(theta[n] - thetaN[t]))');
}