# Variable "simplex" does not exist

Consider this Stan model taken verbatim from the Stan documentation

``````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 - Phi(eta - c);
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 - Phi(eta - c);
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?

My versions are:

``````> packageVersion('rstan')
 ‘2.21.2’
> rstan::stan_version()
 "2.21.0"
``````

You can’t declare the constrained variable types (e.g., `simplex`, `ordered`) in the model block, only in the `parameter` and `transformed parameter` blocks.

Additionally, it looks like you’re attempting to use an ordered probit model, have you considered the `ordered_probit` distribution?

``````data {
int<lower=2> K;
int<lower=0> N;
int<lower=1> D;
array[N] int<lower=1, upper=K> y;
matrix[N,D]  x;
}
parameters {
vector[D] beta;
ordered[K - 1] c;
}
model {
y ~ ordered_probit(x * beta, c);
}
``````

Note that I’ve also changed the type of `x` to a `matrix`

1 Like

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:

2 Likes