data {
int<lower=2> K;
int<lower=0> N;
int<lower=1> D;
array[N] int<lower=1, upper=K> y;
array[N] row_vector[D] x;
}
parameters {
vector[D] beta;
ordered[K - 1] c;
}
model {
vector[K] theta;
for (n in 1:N) {
real eta;
eta = x[n] * beta;
theta[1] = 1 - Phi(eta - c[1]);
for (k in 2:(K - 1)) {
theta[k] = Phi(eta - c[k - 1]) - Phi(eta - c[k]);
}
theta[K] = Phi(eta - c[K - 1]);
y[n] ~ categorical(theta);
}
}
In the first line of the model block, we declare the vector theta. Given that theta must in fact sum to one, I would assumed that simplex[K] theta would be at least as good as vector[K] theta.
However, the following raises a Variable "simplex" does not exist parser error.
data {
int<lower=2> K;
int<lower=0> N;
int<lower=1> D;
array[N] int<lower=1, upper=K> y;
array[N] row_vector[D] x;
}
parameters {
vector[D] beta;
ordered[K - 1] c;
}
model {
simplex[K] theta;
for (n in 1:N) {
real eta;
eta = x[n] * beta;
theta[1] = 1 - Phi(eta - c[1]);
for (k in 2:(K - 1)) {
theta[k] = Phi(eta - c[k - 1]) - Phi(eta - c[k]);
}
theta[K] = Phi(eta - c[K - 1]);
y[n] ~ categorical(theta);
}
}
Is there some reason why simplex[K] can not be used?
If that’s the way it is, then that’s the way it is, but is there some reason for that? I am not sure why something like
model {
vector[K] foobar
...
is possible, but simplex[K] in place of vector[K] is not. Perhaps it is something to do with how the simplex constraint must be checked and that can only be done in a parameter block?
I am doing something related, but not identical so doing something as in the documentation example, where the probability vector is re-created in a loop over observations is the more direct way to implement it, or at least it seems like it is the most direct way to do so right now. But thank you.
Yeah, that’s the gist. Formally, the constraints get checked in all blocks except parameters and model. In parameters the constraints are enforced by constructing the variables using constraining transforms applied to unconstrained variables. And in model, variables defined locally don’t get saved and don’t get checked. See: