GEOM

Overview

The GEOM function computes values for the geometric distribution, a discrete probability distribution that models the number of Bernoulli trials needed to achieve the first success. This distribution is fundamental in probability theory and arises naturally in scenarios involving repeated independent trials with a constant success probability.

The geometric distribution exists in two common parameterizations. The implementation used here via SciPy counts the number of trials until the first success (supported on k = 1, 2, 3, \ldots). The probability mass function (PMF) is defined as:

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

where p is the probability of success on each trial (0 < p \leq 1) and k is the trial number on which the first success occurs. For more details, see the SciPy geom documentation.

A key property of the geometric distribution is memorylessness—the only discrete distribution with this property. This means the probability of future success is independent of past failures: P(X > m + n \mid X > n) = P(X > m). The continuous analogue of the geometric distribution is the exponential distribution, which shares the same memoryless property.

The distribution’s moments are well-defined:

  • Mean: E(X) = \dfrac{1}{p}
  • Variance: \text{Var}(X) = \dfrac{1-p}{p^2}

Common applications include modeling waiting times for the first success in quality control, the number of attempts until winning a game of chance, reliability analysis in engineering, and queueing theory. For a comprehensive treatment of the geometric distribution and its properties, see the Wikipedia article on geometric distribution.

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

Excel Usage

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

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

Examples

Example 1: PMF with required arguments only

Inputs:

k p
3 0.5

Excel formula:

=GEOM(3, 0.5)

Expected output:

0.125

Example 2: CDF with mode optional argument

Inputs:

k p geom_mode
3 0.5 cdf

Excel formula:

=GEOM(3, 0.5, "cdf")

Expected output:

0.875

Example 3: Inverse CDF (ICDF) with mode optional argument

Inputs:

k p geom_mode
0.5 0.5 icdf

Excel formula:

=GEOM(0.5, 0.5, "icdf")

Expected output:

1

Example 4: Survival function with all arguments specified

Inputs:

k p geom_mode loc
3 0.5 sf 0

Excel formula:

=GEOM(3, 0.5, "sf", 0)

Expected output:

0.125

Python Code

from scipy.stats import geom as scipy_geom

def geom(k, p, geom_mode='pmf', loc=0):
    """
    Compute Geometric distribution values using scipy.stats.geom.

    See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.geom.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.
        p (float): Probability of success (0 < p <= 1).
        geom_mode (str, optional): Output type for the geometric 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.
    """
    # Validate p
    try:
        p_val = float(p)
        if not (0 < p_val <= 1):
            return "Invalid input: p must be between 0 (exclusive) and 1 (inclusive)."
    except Exception:
        return "Invalid input: p must be a number."

    # Validate loc
    try:
        loc_val = float(loc)
    except Exception:
        return "Invalid input: loc must be a number."

    # Validate geom_mode
    valid_modes = {"pmf", "cdf", "sf", "icdf", "isf", "mean", "var", "std", "median"}
    if not isinstance(geom_mode, str) or geom_mode not in valid_modes:
        return f"Invalid input: geom_mode must be one of {sorted(valid_modes)}."

    # Handle statistics (k is ignored)
    if geom_mode in ["mean", "var", "std", "median"]:
        if geom_mode == "mean":
            return float(scipy_geom.mean(p_val, loc=loc_val))
        elif geom_mode == "var":
            return float(scipy_geom.var(p_val, loc=loc_val))
        elif geom_mode == "std":
            return float(scipy_geom.std(p_val, loc=loc_val))
        elif geom_mode == "median":
            return float(scipy_geom.median(p_val, loc=loc_val))

    # Helper to normalize input to 2D list
    def to2d(x):
        return [[x]] if not isinstance(x, list) else x

    k_2d = to2d(k)

    result = []
    for row in k_2d:
        if not isinstance(row, list):
             return "Invalid input: k must be a scalar or 2D list."

        result_row = []
        for val in row:
            try:
                val_float = float(val)
            except Exception:
                return "Invalid input: k must be a number."

            if geom_mode == "pmf":
                res = scipy_geom.pmf(val_float, p_val, loc=loc_val)
            elif geom_mode == "cdf":
                res = scipy_geom.cdf(val_float, p_val, loc=loc_val)
            elif geom_mode == "sf":
                res = scipy_geom.sf(val_float, p_val, loc=loc_val)
            elif geom_mode == "icdf":
                res = scipy_geom.ppf(val_float, p_val, loc=loc_val)
            elif geom_mode == "isf":
                res = scipy_geom.isf(val_float, p_val, loc=loc_val)

            result_row.append(float(res))
        result.append(result_row)

    # If input was scalar, return scalar
    if not isinstance(k, list):
        return result[0][0]

    return result

Online Calculator