To argue in favor of returning empty vectors instead of throwing exceptions, I wanted to give another example that I’ve used, to demonstrate how this kind of indexing can be useful and natural. This probably relates to ragged data structures that @andrjohns mentioned (but I have no experience with rstanarm
).
Suppose that we have N subjects and for each subject we have a number of observations, some of which may be missing. The number of missing observations K_n differs between subjects and crucially can be zero for some subjects. We want to model these missing observations explicitly using a vector vector[sum(K)] x
. The missing observations specific for subject n can be selected from x using
vector[K[n]] y = x[sum(K[:n-1])+1:sum(K[:n])];
To put this in a working Stan model (pre v2.26, thanks @jsocolar):
data {
int N; // number of subjects
int<lower=0> K[N]; // number of missing observations for each subject
}
parameters {
vector[sum(K)] x; // concatenated missing observations
//real x[sum(K)];
}
model {
for ( n in 1:N ) {
// get the part of x corresponding to subject n
vector[K[n]] y = x[sum(K[:n-1])+1:sum(K[:n])];
//real y[K[n]] = x[sum(K[:n-1])+1:sum(K[:n])];
// do something with y...
}
x ~ normal(0,1);
}
If we now choose N=5 and K = [1,2,0,4,3], everything works fine, also with the newer versions. Notice that subject 3 has no missing observations. However, if we organize our data such that subject 1 or subject n has no missing observations, we get an exception: (try K = [0,1,2,4,3] or K = [1,2,4,3,0]).
By the way: while doing this I realized that the case K[:0]
should also throw an exception under the new rules, but it doesn’t. In turns out that this is only an issue for vectors. The code that is commented out using arrays instead of vectors does work.
In any case, I do think that the user should have been warned before this feature was removed from the language. This could potentially lead to a lot of problems when other users update their Stan version. If you agree I can report an issue on github…