# Ill-typed argument in reduce sum fuction in ordered logistic model

I write a partial sum function for my ordered logistic model. But some errors make me confused. If I use the examples written by others. It still had some mistakes

My partial function like this

``````real partial_sum(
int[] slice_y,
int start, int end,
matrix x, matrix thresh,
vector beta, int[] g,
real sigma,int K
)
{
real lp = 0;
int N = (end - start + 1);

for(i in 1:N)
lp += ordered_logistic_lpmf(slice_y[i] |x[i]*beta  , thresh[g[i]]');
return lp;
}
``````

Error:

``````Error in stanc(filename, allow_undefined = TRUE) : 0

Semantic error in 'string', line 63, column 12 to column 64:

Ill-typed arguments supplied to function 'reduce_sum'. Expected arguments:
(int[], int, int, matrix, matrix, vector, int[], real, int) => real, int[], int, matrix, matrix, vector, int[], real, int

Instead supplied arguments of incompatible type: (int[], int, int, matrix, matrix, vector, int[], real, int) => real, int[], int, matrix, vector[], vector, int[], int
``````

Full model:

``````functions {

real partial_sum(
int[] slice_y,
int start, int end,
matrix x, matrix thresh,
vector beta, int[] g,
real sigma,int K
)
{
real lp = 0;
int N = (end - start + 1);

for(i in 1:N)
lp += ordered_logistic_lpmf(slice_y[i] |x[i]*beta  , thresh[g[i]]');
return lp;
}

real induced_dirichlet_lpdf(vector c, vector alpha, real phi) {
int K = num_elements(c) + 1;
vector[K - 1] sigma = inv_logit(phi - c);
vector[K] p;
matrix[K, K] J = rep_matrix(0, K, K);

// Induced ordinal probabilities
p[1] = 1 - sigma[1];
for (k in 2:(K - 1))
p[k] = sigma[k - 1] - sigma[k];
p[K] = sigma[K - 1];

// Baseline column of Jacobian
for (k in 1:K) J[k, 1] = 1;

// Diagonal entries of Jacobian
for (k in 2:K) {
real rho = sigma[k - 1] * (1 - sigma[k - 1]);
J[k, k] = - rho;
J[k - 1, k] = rho;
}

return   dirichlet_lpdf(p | alpha)
+ log_determinant(J);
}
}

data {
int<lower=1> N;             // Number of observations
int<lower=1> K;             // Number of ordinal categories
int<lower=1> D;
int<lower=1, upper=K> y[N]; // Observed ordinals
matrix[N,D] x;
int g[N];
int<lower=1> P;        //P different threshold
}

parameters {
vector[D] beta;
ordered[K - 1] thresh[P]; // (Internal) cut points
}

model {

for (i in 1:P)
thresh[i] ~ induced_dirichlet(rep_vector(1, K), 0); //P different threshold

target += reduce_sum(partial_sum, y, 1, x, thresh, beta, g, K);
}

``````

`reduce_sum` is tricky in that the call and the function being called need to line up exactly in terms of their arguments. Your code has

``````real partial_sum(
int[] slice_y,
int start, int end,
matrix x, matrix thresh,
vector beta, int[] g,
real sigma,int K) { ... }
``````

with the later call

``````   reduce_sum(partial_sum, y, 1, x, thresh, beta, g, K);
``````

and variable declrations

``````int<lower=1, upper=K> y[N];
matrix[N,D] x;
ordered[K - 1] thresh[P];
vector[D] beta;
int g[N];
int<lower=1> K;
``````

Now that I’ve written it all out, I see two problems:

• `thresh` is declared as an array of vectors in the `reduce_sum` call but as a `matrix` in `partial_sum`
• `sigma` is missing in the call

Just as a heads up, we’re going to eliminate the old array declaration syntax soon, and require arrays to be written like this:

``````array[N] int<lower=1, upper=K> y;
``````

The motivation was to have the type in a contiguous string so that we could use it in other contexts.

Thanks~Bob

I have used this type in my model

``````data {
int<lower=1> N;             // Number of observations
int<lower=1> K;             // Number of ordinal categories
int<lower=1> D;
array[N] int<lower=1, upper=K> y; // Observed ordinals
matrix[N,D] x;
int g[N];
int<lower=1> P;        //P different threshold
}
``````

And I delete the sigma.

But the result had the same warning:

``````Semantic error in 'string', line 63, column 12 to column 64:

Ill-typed arguments supplied to function 'reduce_sum'. Expected arguments:
(int[], int, int, matrix, matrix, vector, int[], int) => real, int[], int, matrix, matrix, vector, int[], int

Instead supplied arguments of incompatible type: (int[], int, int, matrix, matrix, vector, int[], int) => real, int[], int, matrix, vector[], vector, int[], int

``````

The error message says that the `partial_sum` function expects input types:

``````... => real, int[], int, matrix, matrix, vector, int[], int
``````

But has instead been passed input types:

``````... =>  real, int[], int, matrix, vector[], vector, int[], int
``````

You can see that it’s expecting the fifth argument to be a `matrix`, but you’re instead passing an array of vectors (`vector[]`). This corresponds to the `thresh` argument, so you need to change your `partial_sum` declaration to:

``````  real partial_sum(
int[] slice_y,
int start, int end,
matrix x, vector[] thresh,
vector beta, int[] g,
int K
)
``````

Thanks~ the code working efficiently.
But I meet the problems that the model have bad performance:

``````Warning messages:
1: There were 3994 divergent transitions after warmup. See
https://mc-stan.org/misc/warnings.html#divergent-transitions-after-warmup
to find out why this is a problem and how to eliminate them.
2: There were 6 transitions after warmup that exceeded the maximum treedepth. Increase max_treedepth above 10. See
https://mc-stan.org/misc/warnings.html#maximum-treedepth-exceeded
3: There were 4 chains where the estimated Bayesian Fraction of Missing Information was low. See
https://mc-stan.org/misc/warnings.html#bfmi-low
4: Examine the pairs() plot to diagnose sampling problems

5: The largest R-hat is Inf, indicating chains have not mixed.
Running the chains for more iterations may help. See
https://mc-stan.org/misc/warnings.html#r-hat
6: Bulk Effective Samples Size (ESS) is too low, indicating posterior means and medians may be unreliable.
Running the chains for more iterations may help. See
https://mc-stan.org/misc/warnings.html#bulk-ess
7: Tail Effective Samples Size (ESS) is too low, indicating posterior variances and tail quantiles may be unreliable.
Running the chains for more iterations may help. See
https://mc-stan.org/misc/warnings.html#tail-ess
> print(fit1)
Inference for Stan model: anon_model.
4 chains, each with iter=2000; warmup=1000; thin=1;
post-warmup draws per chain=1000, total post-warmup draws=4000.

mean    se_mean         sd         2.5%          25%         50%        75%      97.5% n_eff         Rhat
beta[1]            -0.62       0.68       0.97        -1.74        -1.47       -0.66       0.19       0.58     2 6.735374e+14
beta[2]             0.61       0.48       0.68        -0.44         0.26        0.80       1.15       1.30     2 3.842006e+14
beta[3]            -0.17       1.06       1.50        -1.81        -1.54       -0.29       1.08       1.72     2 5.252754e+14
beta[4]            -0.16       1.05       1.49        -1.85        -1.47       -0.27       1.03       1.75     2 4.373768e+14
beta[5]             0.09       0.64       0.90        -0.57        -0.56       -0.35       0.31       1.62     2 7.726654e+14
beta[6]             0.79       0.79       1.12        -1.07         0.47        1.15       1.47       1.92     2 5.547014e+14
beta[7]            -0.36       0.79       1.11        -1.61        -1.36       -0.42       0.58       1.03     2 7.667509e+14
beta[8]             0.33       0.52       0.74        -0.51        -0.13        0.17       0.63       1.49     2 3.992911e+14
beta[9]             0.97       1.05       1.48        -1.59         0.92        1.80       1.85       1.89     2 5.314624e+14
beta[10]            1.09       0.59       0.83        -0.31         0.92        1.39       1.56       1.87     2 3.767565e+14
beta[11]           -0.45       0.68       0.96        -1.92        -1.01       -0.16       0.40       0.44     2 3.217270e+14
beta[12]           -0.17       0.89       1.26        -1.94        -0.79       -0.15       0.47       1.58     2 6.381607e+14
beta[13]           -0.66       0.65       0.93        -1.80        -1.44       -0.63       0.15       0.43     2 1.047842e+15
beta[14]           -0.36       0.62       0.87        -1.66        -0.68       -0.27       0.05       0.78     2 6.756425e+14
beta[15]            0.58       0.70       0.99        -0.46        -0.34        0.46       1.39       1.86     2 5.129229e+14
beta[16]           -0.14       0.66       0.93        -1.17        -0.93       -0.29       0.49       1.18     2 1.504894e+15
beta[17]           -0.67       0.49       0.69        -1.63        -0.98       -0.69      -0.39       0.32     2 3.300841e+14
beta[18]            0.30       0.83       1.17        -1.60        -0.21        0.73       1.24       1.31     2 5.665061e+14
beta[19]            0.98       0.49       0.70        -0.05         0.54        1.12       1.56       1.73     2 3.081490e+14
beta[20]            0.24       0.66       0.94        -1.15        -0.30        0.39       0.94       1.33     2 4.372566e+14
beta[21]            0.43       0.35       0.49         0.00         0.05        0.24       0.62       1.24     2 2.798352e+15
beta[22]           -0.56       0.72       1.02        -1.21        -1.20       -1.12      -0.49       1.20     2 6.301830e+14
beta[23]            0.79       0.58       0.81        -0.57         0.62        1.06       1.23       1.60     2 2.151653e+15
beta[24]            0.27       0.70       0.99        -1.05        -0.34        0.26       0.87       1.64     2 7.444500e+14
beta[25]            0.38       0.69       0.98        -0.93        -0.08        0.32       0.78       1.82     2 6.886883e+14
beta[26]            0.30        NaN       1.35        -1.97         0.04        0.82       1.09       1.53   NaN 1.481444e+15
beta[27]           -0.78       0.73       1.03        -1.98        -1.69       -0.79       0.13       0.45     2 3.210543e+14
beta[28]           -1.02       0.55       0.78        -1.94        -1.67       -1.07      -0.42       0.02     2 4.750601e+14
beta[29]           -0.45       1.00       1.42        -1.99        -1.30       -0.82       0.03       1.84     2 5.156302e+14
beta[30]            0.00       0.78       1.10        -0.93        -0.61       -0.47       0.14       1.88     2 4.018415e+14
beta[31]           -0.66       0.75       1.06        -1.75        -1.38       -0.98      -0.27       1.05     2 7.989980e+14
beta[32]            0.00       0.24       0.34        -0.51        -0.12        0.02       0.14       0.45     2 6.078667e+14
beta[33]           -0.83       0.40       0.56        -1.79        -0.96       -0.57      -0.45      -0.39     2 2.577212e+14
beta[34]           -0.87       0.46       0.66        -1.81        -1.28       -0.81      -0.40      -0.07     2 3.491714e+14
beta[35]            1.20       0.43       0.61         0.26         0.94        1.30       1.56       1.95     2 2.086925e+14
beta[36]           -0.72        NaN       1.23        -1.87        -1.62       -1.16      -0.26       1.30   NaN 4.478478e+14
beta[37]            0.36       0.90       1.27        -1.45        -0.50        0.53       1.39       1.82     2 5.140457e+14
thresh[1,1]        -1.08       0.48       0.69        -1.87        -1.41       -1.24      -0.92       0.02     2 2.821368e+14
thresh[1,2]         1.10       1.10       1.56        -0.64         0.05        0.73       1.78       3.55     2 6.387606e+14
thresh[1,3]         1.88        NaN       1.94        -0.39         0.23        1.72       3.37       4.45   NaN 4.660157e+14
thresh[1,4]         4.58       2.97       4.20         0.16         0.53        4.39       8.44       9.39     2 2.626377e+14
thresh[2,1]         0.28       0.88       1.25        -1.71        -0.14        0.58       1.00       1.69     2 7.783062e+14
thresh[2,2]         1.34       1.25       1.77        -1.33         0.51        1.56       2.39       3.55     2 3.264148e+14
thresh[2,3]         2.58       1.57       2.22        -0.94         1.49        3.28       4.36       4.69     2 8.217555e+14
thresh[2,4]         4.62       2.24       3.18        -0.09         3.49        4.86       5.99       8.86     2 8.046332e+14
thresh[3,1]         0.91       0.43       0.61        -0.05         0.63        1.05       1.33       1.57     2 2.626504e+14
thresh[3,2]         3.35       1.62       2.30         1.51         1.90        2.31       3.77       7.27     2 2.165728e+14
thresh[3,3]         5.61       0.96       1.35         3.65         5.06        5.67       6.23       7.46     2 1.219751e+14
thresh[3,4]         8.34       1.26       1.78         5.69         7.24        8.78       9.88      10.12     2 1.320316e+14
thresh[4,1]         0.27        NaN       1.42        -1.47        -0.95        0.35       1.57       1.83   NaN 6.900667e+14
thresh[4,2]         2.82       1.77       2.50        -0.82         1.97        2.93       3.78       6.26     2 2.959827e+14
thresh[4,3]         3.21       1.91       2.70        -0.63         2.27        3.24       4.19       7.00     2 3.576797e+14
thresh[4,4]         4.54       1.37       1.93         2.10         3.68        4.26       5.11       7.52     2 2.316415e+14
thresh[5,1]        -0.26       0.89       1.26        -1.90        -1.28       -0.06       0.96       0.98     2 8.146487e+14
thresh[5,2]         4.13       2.28       3.23        -0.82         2.89        4.57       5.82       8.19     2 3.749206e+14
thresh[5,3]         5.25       2.45       3.46        -0.34         3.91        6.24       7.58       8.87     2 4.214789e+14
thresh[5,4]         6.91       3.74       5.29        -0.14         4.29        6.54       9.16      14.69     2 2.876267e+14
thresh[6,1]         0.03       0.76       1.07        -1.66        -0.40        0.24       0.67       1.28     2 1.063544e+15
thresh[6,2]         0.65       0.84       1.19        -1.28         0.28        1.00       1.37       1.91     2 4.219147e+14
thresh[6,3]         3.09       1.95       2.76         0.08         1.77        2.34       3.66       7.61     2 5.005839e+14
thresh[6,4]         7.03       3.48       4.92         1.29         2.96        6.52      10.60      13.77     2 2.314377e+14
thresh[7,1]         0.52       0.77       1.09        -0.98        -0.15        0.55       1.22       1.97     2 5.662206e+14
thresh[7,2]         1.94       0.80       1.13         0.08         1.61        2.31       2.64       3.07     2 4.763260e+14
thresh[7,3]         3.54       0.46       0.65         2.71         3.04        3.53       4.04       4.38     2 1.065936e+14
thresh[7,4]         5.16       1.46       2.06         2.94         4.16        4.60       5.60       8.53     2 2.121586e+14
thresh[8,1]         0.07       0.74       1.05        -1.58        -0.42        0.32       0.80       1.22     2 7.058837e+14
thresh[8,2]         2.22       1.83       2.59        -0.94         0.79        1.81       3.24       6.22     2 5.818631e+14
thresh[8,3]         3.97       1.56       2.21         0.77         2.56        4.32       5.73       6.50     2 2.445987e+14
thresh[8,4]         7.38        NaN       3.60         4.42         5.00        5.81       8.19      13.49   NaN 2.511863e+14
thresh[9,1]         0.05       0.68       0.96        -1.26        -0.66        0.16       0.86       1.13     2 8.294960e+14
thresh[9,2]         0.60       0.89       1.25        -1.07        -0.33        0.67       1.60       2.13     2 5.778260e+14
thresh[9,3]         0.90       0.93       1.31        -0.93        -0.02        1.05       1.97       2.41     2 5.444402e+14
thresh[9,4]         2.53       2.50       3.53        -0.78         0.14        1.26       3.65       8.39     2 5.747789e+14
thresh[10,1]        0.29       0.62       0.87        -1.04        -0.17        0.45       0.91       1.28     2 6.656584e+14
thresh[10,2]        1.20       0.88       1.24        -0.73         0.82        1.40       1.78       2.72     2 4.158529e+14
thresh[10,3]        5.07        NaN       2.92         2.68         3.05        3.79       5.82      10.01   NaN 8.100323e+14
thresh[10,4]        6.45       2.61       3.69         3.52         3.75        4.82       7.52      12.65     2 3.769528e+14
thresh[11,1]       -0.17       0.72       1.02        -1.83        -0.47        0.09       0.38       0.95     2 9.505231e+14
thresh[11,2]        3.28       1.17       1.65         1.63         1.71        3.02       4.58       5.45     2 2.758636e+14
thresh[11,3]        5.04       0.98       1.39         3.41         3.79        5.14       6.39       6.46     2 1.129577e+14
thresh[11,4]        7.86       1.77       2.50         4.04         6.85        8.20       9.20      10.98     2 1.995960e+14
thresh[12,1]       -0.36       1.10       1.56        -1.86        -1.84       -0.68       0.81       1.79     2 5.256462e+14
thresh[12,2]        2.47       2.19       3.09        -1.60         0.13        2.63       4.98       6.24     2 5.915609e+14
thresh[12,3]        3.92       2.35       3.33        -0.85         2.09        4.10       5.93       8.33     2 3.290808e+14
thresh[12,4]        5.37       2.59       3.67        -0.08         3.06        6.34       8.66       8.86     2 5.933454e+14
thresh[13,1]        1.63       0.26       0.36         1.02         1.52        1.77       1.87       1.96     2 1.495664e+14
thresh[13,2]        4.19       1.71       2.41         2.17         2.22        3.23       5.20       8.11     2 4.406933e+14
thresh[13,3]        6.29       3.09       4.37         2.61         3.61        4.42       7.10      13.74     2 4.297720e+14
thresh[13,4]        7.98       3.68       5.20         3.97         4.91        5.52       8.58      16.91     2 4.622998e+14
lp__         -7114967.99 5128900.69 7255181.42 -17745276.60 -11873323.76 -5310121.23 -520916.95 -116512.25     2 7.526400e+02

Samples were drawn using NUTS(diag_e) at Mon May  8 10:00:10 2023.
For each parameter, n_eff is a crude measure of effective sample size,
and Rhat is the potential scale reduction factor on split chains (at
convergence, Rhat=1).
``````

Are some problems in my settings?

I present the original code so that it can get a good result:

``````functions {
real induced_dirichlet_lpdf(vector c, vector alpha, real phi) {
int K = num_elements(c) + 1;
vector[K - 1] sigma = inv_logit(phi - c);
vector[K] p;
matrix[K, K] J = rep_matrix(0, K, K);

// Induced ordinal probabilities
p[1] = 1 - sigma[1];
for (k in 2:(K - 1))
p[k] = sigma[k - 1] - sigma[k];
p[K] = sigma[K - 1];

// Baseline column of Jacobian
for (k in 1:K) J[k, 1] = 1;

// Diagonal entries of Jacobian
for (k in 2:K) {
real rho = sigma[k - 1] * (1 - sigma[k - 1]);
J[k, k] = - rho;
J[k - 1, k] = rho;
}

return   dirichlet_lpdf(p | alpha)
+ log_determinant(J);
}
}
data {
int<lower=1> N;             // Number of observations
int<lower=1> K;             // Number of ordinal categories
int<lower=1> D;
array[N] int<lower=1, upper=K> y; // Observed ordinals
matrix[N,D] x;
int g[N];
int<lower=1> P;        //P different threshold
}

parameters {
vector[D] beta;
ordered[K - 1] thresh[P]; // (Internal) cut points

}

model {
beta~ normal(0,10);

for (i in 1:P)
thresh[i] ~ induced_dirichlet(rep_vector(1, K), 0); //P different threshold
for (i in 1:N)
y[i]~ ordered_logistic(x[i]*beta,thresh[g[i]]);
}

``````

I change the slice_y index like this:

`````` real partial_sum(
int[] slice_y,
int start, int end,
matrix x, vector[] thresh,
vector beta, int[] g,
int K
)
{
real lp = 0;
for(i in start:end)
lp += ordered_logistic_lpmf(slice_y[i-start+1] |x[i]*beta  , thresh[g[i]]);
return lp;
}
``````

it works, I use a fractioin of my data find that it runs 1468 seconds (original code use 2516 seconds). At the same time I use vectorize in partial sum function:

`````` real partial_sum(
int[] slice_y,
int start, int end,
matrix x, vector[] thresh,
vector beta, int[] g
)
{
//real lp = 0;
return ordered_logistic_lpmf(slice_y |x[start:end]*beta  , thresh[g[start:end]]);
}
``````

But it works slowly than loop. (runs 1656 seconds).