Ragged Arrays with reduce_sum?

Has anyone figured out a way to use reduce_sum with ragged arrays?

I guess if/when there are “true” ragged arrays, this would be seamless, but is there a hack for until then?

The implementation details will depend on the structure of your data and the computation you need to do.

Here’s one example:
I needed to do a row-wise computation on an array with varying numbers of columns, so I filled the trailing elements of each row with nonsense, and then passed a one-dimensional integer array as data to tell Stan how many elements of each row to grab. It’s embedded in a gargantuan model, but take a look at lines 151, 219, 317, 321, 349, and 742 to see the relevant parts.

ah okay, sure. I originally moved to the ragged array idea because I didn’t want to increase the memory footprint (originally I had filled everything in with zeros to make things rectangular).

Do you think the memory increase vs the reduce_sum parallelization is worth the trade off?

I would think so, right?

If the array is data then the memory footprint shouldn’t matter much (as long as you aren’t running up against the memory limitations of your system). If the array is a parameter then it’s less clear, and probably depends on “how ragged” the array is (how many more elements are in the rectangular array compared to the ragged).

2 Likes