Let's say I have an N-side dice with non-uniform probabilities for each side and I throw it M times. Now instead of observing the individual outcomes, we only observe the sum.
I have to code the likelihood where I have to sum over the multinomial likelihood components restricted to having that observed sum.
If N=3, M = 2 and the sum is 4, than it is clear that I have to sum over the two cases where one of the throws is 1 and the other 3 plus the case where both are 2.
I could also enumerate all possibilities, calculate the sum and restrict the calculation to the combinations with the sum I'm interested in, but clearly that becomes intractable quite quickly with increasing N and M.
So I am looking for an efficient approach to select the constant-sum combinations in R.
One option is to use
RcppAlgos::compositionsGeneral()which employs 'efficient algorithms for partitioning numbers under various constraints'.As @ThomasIsCoding has pointed out, this approach can fail with the message:
So to deal with this, we can catch errors and fall back on
permuteGeneral()with constraints in this event:Note that the documentation includes the following about calculating permutations with contraints: