POISSON_DIST

Overview

The POISSON_DIST function computes values for the Poisson distribution, a discrete probability distribution that models the number of events occurring within a fixed interval of time or space, given a known average rate. This distribution is widely used in applications such as queuing theory, telecommunications, insurance risk modeling, and analyzing rare events like equipment failures or website traffic.

The Poisson distribution is named after French mathematician Siméon Denis Poisson. It assumes events occur independently at a constant average rate. The distribution is characterized by a single parameter \mu (mu), which represents both the mean and variance of the distribution.

This implementation uses the scipy.stats.poisson module from SciPy, the fundamental library for scientific computing in Python. The function supports multiple calculation modes including the probability mass function (PMF), cumulative distribution function (CDF), survival function, inverse functions, and descriptive statistics.

The probability mass function for the Poisson distribution is defined as:

f(k) = \frac{e^{-\mu} \mu^k}{k!}

where k \geq 0 is a non-negative integer representing the number of events, \mu \geq 0 is the expected rate (mean), and e is Euler’s number. When \mu = 0, the PMF returns 1.0 at k = 0 since zero events is certain when the rate is zero.

The function supports the following modes: pmf calculates the probability of exactly k events; cdf gives the probability of at most k events; sf (survival function) returns the probability of more than k events; icdf and isf compute the inverse functions for determining quantiles; and mean, var, std, and median return descriptive statistics. The optional loc parameter shifts the distribution along the number line.

For additional details on discrete probability distributions in SciPy, see the rv_discrete documentation. The source code is available on GitHub.

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

Excel Usage

=POISSON_DIST(k, mu, poisson_mode, loc)
  • k (float, required): Value at which to evaluate the distribution. For icdf/isf, probability in [0, 1].
  • mu (float, required): Mean (expected value) of the Poisson distribution. Must be >= 0.
  • poisson_mode (str, optional, default: “pmf”): Calculation mode to use.
  • loc (float, optional, default: 0): Location parameter that shifts the distribution.

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

Examples

Example 1: PMF at k=3 with mu=2

Inputs:

k mu poisson_mode loc
3 2 pmf 0

Excel formula:

=POISSON_DIST(3, 2, "pmf", 0)

Expected output:

0.1804

Example 2: CDF at k=3 with mu=2

Inputs:

k mu poisson_mode loc
3 2 cdf 0

Excel formula:

=POISSON_DIST(3, 2, "cdf", 0)

Expected output:

0.8571

Example 3: Survival function at k=3 with mu=2

Inputs:

k mu poisson_mode loc
3 2 sf 0

Excel formula:

=POISSON_DIST(3, 2, "sf", 0)

Expected output:

0.1429

Example 4: Mean of distribution with mu=5

Inputs:

k mu poisson_mode loc
0 5 mean 0

Excel formula:

=POISSON_DIST(0, 5, "mean", 0)

Expected output:

5

Example 5: Inverse CDF at probability 0.5

Inputs:

k mu poisson_mode loc
0.5 4 icdf 0

Excel formula:

=POISSON_DIST(0.5, 4, "icdf", 0)

Expected output:

4

Example 6: Variance of distribution with mu=3

Inputs:

k mu poisson_mode loc
0 3 var 0

Excel formula:

=POISSON_DIST(0, 3, "var", 0)

Expected output:

3

Python Code

from scipy.stats import poisson as scipy_poisson
import math

def poisson_dist(k, mu, poisson_mode='pmf', loc=0):
    """
    Compute Poisson distribution values using scipy.stats.poisson.

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

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

    Args:
        k (float): Value at which to evaluate the distribution. For icdf/isf, probability in [0, 1].
        mu (float): Mean (expected value) of the Poisson distribution. Must be >= 0.
        poisson_mode (str, optional): Calculation mode to use. 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 poisson_mode first
    valid_modes = {"pmf", "cdf", "sf", "icdf", "isf", "mean", "var", "std", "median"}
    if not isinstance(poisson_mode, str) or poisson_mode not in valid_modes:
        return "Invalid input: poisson_mode must be one of 'pmf', 'cdf', 'sf', 'icdf', 'isf', 'mean', 'var', 'std', or 'median'."

    # Validate mu
    try:
        mu_val = float(mu)
        if mu_val < 0:
            return "Invalid input: mu must be >= 0."
    except (TypeError, ValueError):
        return "Invalid input: mu must be a number."

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

    # Helper to process k value
    def process_k(val):
        try:
            return float(val)
        except (TypeError, ValueError):
            return None

    # Handle mean/var/std/median modes (k parameter is ignored)
    if poisson_mode == "mean":
        try:
            return float(scipy_poisson.mean(mu_val, loc=loc_val))
        except Exception as e:
            return f"Error computing mean: {str(e)}"
    if poisson_mode == "var":
        try:
            return float(scipy_poisson.var(mu_val, loc=loc_val))
        except Exception as e:
            return f"Error computing variance: {str(e)}"
    if poisson_mode == "std":
        try:
            return float(scipy_poisson.std(mu_val, loc=loc_val))
        except Exception as e:
            return f"Error computing standard deviation: {str(e)}"
    if poisson_mode == "median":
        try:
            return float(scipy_poisson.median(mu_val, loc=loc_val))
        except Exception as e:
            return f"Error computing median: {str(e)}"

    # PMF, CDF, SF, ICDF, ISF modes
    def compute(val):
        kval = process_k(val)
        if kval is None:
            return "Invalid input: k must be a number."

        # Validate probability range for icdf/isf
        if poisson_mode in ["icdf", "isf"]:
            if kval < 0 or kval > 1:
                return "Invalid input: probability must be between 0 and 1 for icdf/isf."

        try:
            if poisson_mode == "pmf":
                result = scipy_poisson.pmf(kval, mu_val, loc=loc_val)
            elif poisson_mode == "cdf":
                result = scipy_poisson.cdf(kval, mu_val, loc=loc_val)
            elif poisson_mode == "sf":
                result = scipy_poisson.sf(kval, mu_val, loc=loc_val)
            elif poisson_mode == "icdf":
                result = scipy_poisson.ppf(kval, mu_val, loc=loc_val)
            elif poisson_mode == "isf":
                result = scipy_poisson.isf(kval, mu_val, loc=loc_val)

            # Handle NaN/inf
            if math.isnan(result):
                return "Result is NaN (not a number)."
            if math.isinf(result):
                return "Result is infinite."

            return float(result)
        except Exception as e:
            return f"Error in {poisson_mode} calculation: {str(e)}"

    # Handle 2D list or scalar input
    if isinstance(k, list):
        # Validate 2D list structure
        if not all(isinstance(row, list) for row in k):
            return "Invalid input: k must be a scalar or 2D list."

        result = []
        for row in k:
            result_row = []
            for val in row:
                out = compute(val)
                if isinstance(out, str):
                    return out
                result_row.append(out)
            result.append(result_row)
        return result
    else:
        return compute(k)

Online Calculator