# Is possible for vectorised sampling to return vector of of lp rather than their sum?

I would like to know if in principle it is possible from this vectorised statement

`````` lp = neg_binomial_2_lpmf(int_size_100  | mu_size_100, sigma_size_100);
``````

to return a vector size 100 of log probabilities rather than a real?

In other words a real is returned by design, or it’s the mathematics than demands a single number to be returned.

Thank a lot.

It’s not possible, but that’s by design rather than any mathematical restriction. The vectorised functions are designed to more efficiently compute the gradients and total likelihood for their inputs, so they’ll sample faster than a loop. The returned value is identical (within floating-point trickery) to taking the sum of a loop over each element.

So just to understand, do a vector of log-probabilities exist within the vectorized likelihood function at any point (but it’s returned as sum to the user), or does not exist?

If does exist, I would be interested in createing a C++ alternative where that vector of log-probabilities is returned.

do a vector of log-probabilities exist within the vectorized likelihood function at any point (but it’s returned as sum to the user), or does not exist

Generally no, but behaviour isn’t identical across all likelihoods. I believe the main issue is that the gradients are constructed in reference to returning the sum, and so both the return type and handling of gradients would need to change for all distributions which would be a pretty non-trivial change.

@bbbales2 Have I got that right? That for the vectorised distributions to return a vector of log-likelihoods rather than the single sum the current `operands_and_partials` approach would need a decent refactor?

2 Likes

The issue here is that returning each element instead of the sum creates a huge overhead internally. You would shoot yourself in the foot big time if you return things by element instead of the sum. It‘s probably even not such a performance difference to just do the loop in Stan as compared to working this out in C++.

2 Likes

Yeah the sum let’s us do operands and partials which is faster than having the terms separate.

1 Like