BINOM

Overview

The BINOM function computes values from the binomial distribution, a fundamental discrete probability distribution that models the number of successes in a fixed number of independent Bernoulli trials. Each trial has only two possible outcomes (success or failure) with a constant probability of success. This distribution is widely used in quality control, clinical trials, A/B testing, and any scenario involving binary outcomes.

The binomial distribution is characterized by two parameters: n (the number of trials) and p (the probability of success on each trial). The probability mass function (PMF) gives the probability of observing exactly k successes:

P(X = k) = \binom{n}{k} p^k (1-p)^{n-k}

where \binom{n}{k} = \frac{n!}{k!(n-k)!} is the binomial coefficient, and k \in \{0, 1, 2, \ldots, n\}.

This function supports multiple computation modes:

  • pmf: Probability mass function — probability of exactly k successes
  • cdf: Cumulative distribution function — probability of at most k successes
  • sf: Survival function — probability of more than k successes (equivalent to 1 - \text{CDF})
  • icdf: Inverse CDF (percent point function) — returns the smallest k such that CDF(k) ≥ q
  • isf: Inverse survival function — returns the smallest k such that SF(k) ≤ q
  • mean, var, std, median: Distribution statistics where mean = np and variance = np(1-p)

The optional loc parameter shifts the distribution along the number line, which can be useful for modeling distributions that don’t start at zero.

Relationship to Bernoulli and Negative Binomial

The Binomial distribution represents the sum of n independent Bernoulli trials. While the Binomial distribution models the number of successes in a fixed number of trials, the Negative Binomial distribution models the number of trials required to reach a fixed number of successes. Both assume a constant probability of success p across all trials.

This implementation uses SciPy’s scipy.stats.binom module, which leverages the Boost Math C++ library for high-performance numerical computations.

This example function is provided as-is without any representation of accuracy.

Excel Usage

=BINOM(k, n, p, binom_mode, loc)
  • k (list[list], required): Value(s) at which to evaluate the distribution. For statistics modes (mean, var, std, median), this is ignored.
  • n (int, required): Number of trials (must be >= 0).
  • p (float, required): Probability of success (0 <= p <= 1).
  • binom_mode (str, optional, default: “pmf”): Output type for the binomial distribution calculation.
  • loc (float, optional, default: 0): Location parameter that shifts the distribution.

Returns (float): Distribution result (float), or error message string.

Example 1: PMF with required arguments only

Inputs:

k n p
3 10 0.5

Excel formula:

=BINOM(3, 10, 0.5)

Expected output:

0.117188

Example 2: CDF with some optional arguments

Inputs:

k n p binom_mode
3 10 0.5 cdf

Excel formula:

=BINOM(3, 10, 0.5, "cdf")

Expected output:

0.171875

Example 3: Survival function with all arguments

Inputs:

k n p binom_mode loc
3 10 0.5 sf 0

Excel formula:

=BINOM(3, 10, 0.5, "sf", 0)

Expected output:

0.828125

Example 4: Mean of the distribution

Inputs:

k n p binom_mode loc
0 10 0.5 mean 0

Excel formula:

=BINOM(0, 10, 0.5, "mean", 0)

Expected output:

5

Python Code

Show Code
from scipy.stats import binom as scipy_binom

def binom(k, n, p, binom_mode='pmf', loc=0):
    """
    Compute Binomial distribution values: PMF, CDF, SF, ICDF, ISF, mean, variance, std, or median.

    See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.binom.html

    This example function is provided as-is without any representation of accuracy.

    Args:
        k (list[list]): Value(s) at which to evaluate the distribution. For statistics modes (mean, var, std, median), this is ignored.
        n (int): Number of trials (must be >= 0).
        p (float): Probability of success (0 <= p <= 1).
        binom_mode (str, optional): Output type for the binomial distribution calculation. Valid options: pmf, cdf, sf, icdf, isf, mean, var, std, median. Default is 'pmf'.
        loc (float, optional): Location parameter that shifts the distribution. Default is 0.

    Returns:
        float: Distribution result (float), or error message string.
    """
    try:
      def to2d(x):
        return [[x]] if not isinstance(x, list) else x

      # Validate n
      try:
        n_val = int(n)
        if n_val < 0:
          return "Error: Invalid input: n must be >= 0."
      except Exception:
        return "Error: Invalid input: n must be an integer."
      # Validate p
      try:
        p_val = float(p)
        if not (0 <= p_val <= 1):
          return "Error: Invalid input: p must be between 0 and 1."
      except Exception:
        return "Error: Invalid input: p must be a number."
      # Validate loc
      try:
        loc_val = float(loc)
      except Exception:
        return "Error: Invalid input: loc must be a number."
      # Validate binom_mode
      valid_modes = {"pmf", "cdf", "sf", "icdf", "isf", "mean", "var", "std", "median"}
      if not isinstance(binom_mode, str) or binom_mode not in valid_modes:
        return f"Error: Invalid input: binom_mode must be one of {sorted(valid_modes)}."

      # Handle statistics
      if binom_mode == "mean":
        return float(scipy_binom.mean(n_val, p_val, loc=loc_val))
      if binom_mode == "var":
        return float(scipy_binom.var(n_val, p_val, loc=loc_val))
      if binom_mode == "std":
        return float(scipy_binom.std(n_val, p_val, loc=loc_val))
      if binom_mode == "median":
        return float(scipy_binom.median(n_val, p_val, loc=loc_val))

      # PMF, CDF, SF, ICDF, ISF
      k_2d = to2d(k)

      def compute(val):
        try:
          kval = float(val)
        except Exception:
          return None

        if binom_mode == "pmf":
          return float(scipy_binom.pmf(kval, n_val, p_val, loc=loc_val))
        elif binom_mode == "cdf":
          return float(scipy_binom.cdf(kval, n_val, p_val, loc=loc_val))
        elif binom_mode == "sf":
          return float(scipy_binom.sf(kval, n_val, p_val, loc=loc_val))
        elif binom_mode == "icdf":
          return float(scipy_binom.ppf(kval, n_val, p_val, loc=loc_val))
        elif binom_mode == "isf":
          return float(scipy_binom.isf(kval, n_val, p_val, loc=loc_val))

      result = []
      for row in k_2d:
        if not isinstance(row, list):
          return "Error: Invalid input: k must be a scalar or 2D list."
        result_row = []
        for val in row:
          out = compute(val)
          if out is None:
            return "Error: Invalid input: k must be a number."
          result_row.append(out)
        result.append(result_row)
      if not isinstance(k, list) and len(result) == 1 and len(result[0]) == 1:
        return result[0][0]
      return result
    except Exception as e:
      return f"Error: {str(e)}"

Online Calculator

Value(s) at which to evaluate the distribution. For statistics modes (mean, var, std, median), this is ignored.
Number of trials (must be >= 0).
Probability of success (0 <= p <= 1).
Output type for the binomial distribution calculation.
Location parameter that shifts the distribution.