
Enumerate All Multinomial Count Vectors for a Bivariate Binary Outcome
Source:R/allmultinom.R
allmultinom.RdGenerates the complete set of non-negative integer vectors
\((x_{00}, x_{01}, x_{10}, x_{11})\) satisfying
\(x_{00} + x_{01} + x_{10} + x_{11} = n_j\), where \(n_j\) is the
sample size of group \(j \in \{t, c\}\). Each row of the returned matrix
corresponds to one possible realisation of the aggregated bivariate binary
counts. This enumeration is a prerequisite for exact operating
characteristic assessment in pbayesdecisionprob2bin.
Value
An integer matrix with \(\binom{n_j + 3}{3}\) rows and 4 columns
named x00, x01, x10, x11.
Each row is a distinct non-negative integer solution to
\(x_{00} + x_{01} + x_{10} + x_{11} = n_j\).
Rows are ordered by x00 (ascending), then x10
(ascending), then x01 (ascending).
Details
The number of non-negative integer solutions to \(x_{00} + x_{01} + x_{10} + x_{11} = n_j\) is the stars-and-bars count $$\binom{n_j + 3}{3} = \frac{(n_j + 1)(n_j + 2)(n_j + 3)}{6}.$$ For example, \(n_j = 10\) yields 286 rows and \(n_j = 20\) yields 1771 rows. The matrix is pre-allocated to avoid repeated memory reallocation, making the function efficient for the sample sizes typical in rare-disease proof-of-concept studies.
This function is an internal computational building block used by
pbayesdecisionprob2bin to enumerate the sample space over
which multinomial probabilities and decision indicators are summed when
computing exact operating characteristics.
Examples
# Example 1: n = 2 (smallest non-trivial case)
allmultinom(2)
#> x00 x01 x10 x11
#> [1,] 0 0 0 2
#> [2,] 0 1 0 1
#> [3,] 0 2 0 0
#> [4,] 0 0 1 1
#> [5,] 0 1 1 0
#> [6,] 0 0 2 0
#> [7,] 1 0 0 1
#> [8,] 1 1 0 0
#> [9,] 1 0 1 0
#> [10,] 2 0 0 0
# Example 2: n = 10 (typical rare-disease PoC group size)
mat <- allmultinom(10)
nrow(mat) # Should be choose(13, 3) = 286
#> [1] 286
all(rowSums(mat) == 10) # Every row must sum to n
#> [1] TRUE
# Example 3: n = 0 (edge case - only the all-zero row)
allmultinom(0)
#> x00 x01 x10 x11
#> [1,] 0 0 0 0
# Example 4: Verify column names
colnames(allmultinom(5))
#> [1] "x00" "x01" "x10" "x11"
# Example 5: Row counts match the stars-and-bars formula
n <- 15L
mat <- allmultinom(n)
nrow(mat) == choose(n + 3L, 3L) # Should be TRUE
#> [1] TRUE