I’m parallelising some existing programmes but am struggling to code up a simple ordinal probit model using partial_sum
and reduce_sum
.
The non-reduce_sum
likelihood statement is (for a 3-level ordinal response):
// linear predictor
mu = alpha + beta * x;
for(n in 1:N){// loop over cases
vector[K] theta; // probability of ordinal categories
theta[1] = Phi( (thresh[1] - mu[n])/sigma);
theta[2] = Phi( (thresh[2] - mu[n])/sigma) - Phi( (thresh[1] - mu[n])/sigma);
theta[3] = 1 - Phi( (thresh[2] - mu[n])/sigma);
target += categorical_lpmf(y[n] | theta);
}
I wrote a partial_sum
function:
real partial_sum(
int[] slice_y,
int start, int end,
matrix theta_slice
)
{
return categorical_lpmf(slice_y[start:end] | theta_slice[start:end,:]');
}
and a reduce_sum
likelihood:
matrix[N, K] theta_slice;
theta_slice[, 1] = Phi( (thresh[1] - mu)/sigma);
theta_slice[, 2] = Phi( (thresh[2] - mu)/sigma) - Phi( (thresh[1] - mu)/sigma);
theta_slice[, 3] = 1 - Phi( (thresh[2] - mu)/sigma);
target += reduce_sum(partial_sum, y, 1, theta_slice);
but I’m running into problems that the theta_slice
parameter cannot be anything but a column-vector in categorical_lpmf
, but I’m passing in theta_slice
as a matrix.
If I try to make a for
-loop in partial_sum
, it also doesn’t work:
real partial_sum(
int[] slice_y,
int start, int end,
matrix theta_slice
)
{
for(i in start:end)
return categorical_lpmf(slice_y[i] | theta_slice[i,:]');
}
which causes the error:
Chain 3 TBB Warning: Exact exception propagation is requested by application but the linked library is built without support for it
Chain 4 Exception: Exception: categorical_lpmf: Number of categories is 0, but must be in the interval [1, 3] (in '/tmp/Rtmp5Xc0sm/model-24e17999fc70.stan', line 10, column 6 to column 62) (in '/tmp/Rtmp5Xc0sm/model-24e17999fc70.stan', line 10, column 6 to column 62) [origin: unknown original type]
Any good ideas?
Thanks!
Here is the full R script: ordinal-reduce_sum-script.R (1.4 KB)
and Stan code ordinal-reduce_sum.stan (1.1 KB)