scipy.special.binom#
- scipy.special.binom(x, y, out=None) = <ufunc 'binom'>#
Binomial coefficient considered as a function of two real variables.
For real arguments, the binomial coefficient is defined as
\[\binom{x}{y} = \frac{\Gamma(x + 1)}{\Gamma(y + 1)\Gamma(x - y + 1)} = \frac{1}{(x + 1)\mathrm{B}(x - y + 1, y + 1)}\]Where \(\Gamma\) is the Gamma function (
gamma) and \(\mathrm{B}\) is the Beta function (beta) [1].- Parameters:
- x, y: array_like
Real arguments to \(\binom{x}{y}\).
- outndarray, optional
Optional output array for the function values
- Returns:
- scalar or ndarray
Value of binomial coefficient.
See also
combThe number of combinations of N things taken k at a time.
Notes
The Gamma function has poles at non-positive integers and tends to either positive or negative infinity depending on the direction on the real line from which a pole is approached. When considered as a function of two real variables, \(\binom{x}{y}\) is thus undefined when x is a negative integer.
binomreturnsnanwhenxis a negative integer. This is the case even whenxis a negative integer andyan integer, contrary to the usual convention for defining \(\binom{n}{k}\) when it is considered as a function of two integer variables.Array API Standard Support
binomhas experimental support for Python Array API Standard compatible backends in addition to NumPy. Please consider testing these features by setting an environment variableSCIPY_ARRAY_API=1and providing CuPy, PyTorch, JAX, or Dask arrays as array arguments. The following combinations of backend and device (or other capability) are supported.Library
CPU
GPU
NumPy
✅
n/a
CuPy
n/a
✅
PyTorch
✅
⛔
JAX
⚠️ no JIT
⛔
Dask
✅
n/a
See Support for the array API standard for more information.
References
Examples
The following examples illustrate the ways in which
binomdiffers from the functioncomb.>>> from scipy.special import binom, comb
When
exact=Falseandxandyare both positive,combcallsbinominternally.>>> x, y = 3, 2 >>> (binom(x, y), comb(x, y), comb(x, y, exact=True)) (3.0, 3.0, 3)
For larger values,
combwithexact=Trueno longer agrees withbinom.>>> x, y = 43, 23 >>> (binom(x, y), comb(x, y), comb(x, y, exact=True)) (960566918219.9999, 960566918219.9999, 960566918220)
binomreturnsnanwhenxis a negative integer, but is otherwise defined for negative arguments.combreturns 0 whenever one ofxoryis negative orxis less thany.>>> x, y = -3, 2 >>> (binom(x, y), comb(x, y)) (nan, 0.0)
>>> x, y = -3.1, 2.2 >>> (binom(x, y), comb(x, y)) (18.714147876804432, 0.0)
>>> x, y = 2.2, 3.1 >>> (binom(x, y), comb(x, y)) (0.037399983365134115, 0.0)