Arrays of K-Simplex

Hello Stan users,

I’m using PyStan to model how people weight different aspects of an object. I have N observations and K aspects. I want the weighting to be a unit K-simplex parameter to be estimated. The following is part of my Stan model code:

data {
    int N; // number of participants
    int K; // number of aspects

parameters {
    simplex[K] aspect_weight[N]; // this is an array with unit simplexes for K aspects
    vector<lower=0>[N] alpha; // hyper-prior for Dirichlet prior

model {
    alpha ~ lognormal(0, 5);
    for (i in 1:N) {
        aspect_weight[i] ~ dirichlet(alpha);
// ...

When trying to fit the model (K = 103, N = 18542), I get the following error:

RuntimeError: Exception: dirichlet_lpmf: probabilities has dimension = 103, expecting dimension = 18542; a function was called with arguments of different scalar, array, vector, or matrix types, and they were not consistently sized; all arguments must be scalars or multidimensional values of the same shape.

To me, this sounds like the line assigning the prior does not reference i-th element of the array, but tries to access the i-th element of the simplex. Or am I missing something here?


Jeez, trying to work this out over two days and right after posting I notice that alpha needs to be a vector of size K not N. :)

1 Like

Sorry for the pain of two days of debugging.

What you’ve experienced is called the rubber ducky effect, though it really works much better with a co-developer in place of the rubber duck—it helps if the person you’re explaining things to has the capacity to understand.