shapiq.utils.log_binomΒΆ

shapiq.utils.log_binom(n, k)[source]ΒΆ

Natural logarithm of the binomial coefficient log(binom(n, k)).

Computed via scipy.special.gammaln() as gammaln(n + 1) - gammaln(k + 1) - gammaln(n - k + 1). This stays finite for large n where scipy.special.binom() overflows to inf (the central coefficient binom(n, n/2) exceeds the float64 range already at n of roughly 1029). Keeping the binomial in log-space lets the Shapley/SII weights and the sampling probabilities – whose ratios stay well-scaled even when the individual coefficients are astronomically large – be combined without intermediate overflow/underflow.

Parameters:
  • n (int) – The number of elements (a non-negative integer).

  • k (int | ndarray) – The number of chosen elements. May be a scalar or a numpy array. Entries outside [0, n] yield -inf (i.e. binom(n, k) == 0).

Return type:

float | ndarray

Returns:

log(binom(n, k)) as a float (scalar k) or a numpy array (array k).