scipy.special.nbdtrik#

scipy.special.nbdtrik(y, n, p, out=None) = <wrapped_ufunc 'nbdtrik'>#

Negative binomial percentile function.

Returns the inverse with respect to the parameter k of y = nbdtr(k, n, p), the negative binomial cumulative distribution function.

Parameters:
yarray_like

The probability of k or fewer failures before n successes (float).

narray_like

The target number of successes (positive int).

parray_like

Probability of success in a single event (float).

outndarray, optional

Optional output array for the function results

Returns:
kscalar or ndarray

The maximum number of allowed failures such that nbdtr(k, n, p) = y.

See also

nbdtr

Cumulative distribution function of the negative binomial.

nbdtrc

Survival function of the negative binomial.

nbdtri

Inverse with respect to p of nbdtr(k, n, p).

nbdtrin

Inverse with respect to n of nbdtr(k, n, p).

scipy.stats.nbinom

Negative binomial distribution

Notes

This function wraps routines from the Boost Math C++ library [1].

Array API Standard Support

nbdtrik has support for Python Array API Standard compatible backends in addition to NumPy. The following combinations of backend and device (or other capability) are supported.

Library

CPU

GPU

NumPy

✅

n/a

CuPy

n/a

⛔

PyTorch

✅

⛔

JAX

✅

⛔

Dask

✅

n/a

For the NumPy backend, this function supports all NumPy ufunc keyword arguments. Other backends may support out, but typically none of the other ufunc kwargs. out is typically supported for CuPy and PyTorch, but not currently in cases where SciPy relies on a generic Array API implementation or, for PyTorch on CPU, falls back to the NumPy backend. out is never supported for JAX because JAX arrays are immutable. nbdtrik does not currently support out for the PyTorch backend.

See Support for the array API standard for more information.

References

[1]

The Boost Developers. “Boost C++ Libraries”. https://www.boost.org/.

Examples

Compute the negative binomial cumulative distribution function for an exemplary parameter set.

>>> import numpy as np
>>> from scipy.special import nbdtr, nbdtrik
>>> k, n, p = 5, 2, 0.5
>>> cdf_value = nbdtr(k, n, p)
>>> cdf_value
0.9375

Verify that nbdtrik recovers the original value for k.

>>> nbdtrik(cdf_value, n, p)
5.0

Plot the function for different parameter sets.

>>> import matplotlib.pyplot as plt
>>> p_parameters = [0.2, 0.5, 0.7, 0.5]
>>> n_parameters = [30, 30, 30, 80]
>>> linestyles = ['solid', 'dashed', 'dotted', 'dashdot']
>>> parameters_list = list(zip(p_parameters, n_parameters, linestyles))
>>> cdf_vals = np.linspace(0, 1, 1000)
>>> fig, ax = plt.subplots(figsize=(8, 8))
>>> for parameter_set in parameters_list:
...     p, n, style = parameter_set
...     nbdtrik_vals = nbdtrik(cdf_vals, n, p)
...     ax.plot(cdf_vals, nbdtrik_vals, label=rf"$n={n},\ p={p}$",
...             ls=style)
>>> ax.legend()
>>> ax.set_ylabel("$k$")
>>> ax.set_xlabel("$CDF$")
>>> ax.set_title("Negative binomial percentile function")
>>> plt.show()
../../_images/scipy-special-nbdtrik-1_00_00.png

The negative binomial distribution is also available as scipy.stats.nbinom. The percentile function method ppf returns the result of nbdtrik rounded up to integers:

>>> from scipy.stats import nbinom
>>> q, n, p = 0.6, 5, 0.5
>>> nbinom.ppf(q, n, p), nbdtrik(q, n, p)
(5.0, 4.800428460273882)