DLAPLACE

Overview

The DLAPLACE function computes values for the Discrete Laplace distribution, also known as the double geometric distribution. This distribution is the discrete analog of the continuous Laplace distribution and is defined over all integers (positive, negative, and zero), making it suitable for modeling symmetric discrete phenomena with heavier tails than the normal distribution.

The probability mass function (PMF) for the discrete Laplace distribution is:

f(k) = \tanh\left(\frac{a}{2}\right) \exp(-a|k|)

where k is any integer and a > 0 is the shape parameter that controls the distribution’s spread. Smaller values of a produce wider distributions with heavier tails, while larger values concentrate probability mass near zero.

This implementation uses SciPy’s dlaplace from the scipy.stats module. The function supports multiple output modes including the PMF, cumulative distribution function (CDF), survival function (SF), inverse CDF (ICDF/quantile function), inverse survival function (ISF), and statistical properties such as mean, variance, standard deviation, and median. The optional loc parameter shifts the distribution along the integer axis.

The discrete Laplace distribution arises naturally as the difference of two independent geometric random variables and has applications in signal processing, image analysis, and modeling count data with symmetric but heavy-tailed behavior. Its relationship to the continuous Laplace distribution—sometimes called the “double exponential distribution”—makes it useful in contexts where discrete approximations of exponentially-decaying symmetric processes are needed.

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

Excel Usage

=DLAPLACE(k, a, dlaplace_mode, loc)
  • k (list[list], required): Value(s) at which to evaluate the distribution (integer for PMF/CDF/SF, probability for ICDF/ISF).
  • a (float, required): Shape parameter (must be greater than 0).
  • dlaplace_mode (str, optional, default: “pmf”): Output type to compute.
  • 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 default mode

Inputs:

k a
2 1

Excel formula:

=DLAPLACE(2, 1)

Expected output:

0.0625

Example 2: CDF at k=2

Inputs:

k a dlaplace_mode
2 1 cdf

Excel formula:

=DLAPLACE(2, 1, "cdf")

Expected output:

0.9636

Example 3: Survival function with 2D array input

Inputs:

k a dlaplace_mode loc
1 1 sf 0
2

Excel formula:

=DLAPLACE({1;2}, 1, "sf", 0)

Expected output:

Result
0.0989
0.0364

Example 4: Inverse CDF (quantile) at probability 0.5

Inputs:

k a dlaplace_mode loc
0.5 1 icdf 0

Excel formula:

=DLAPLACE(0.5, 1, "icdf", 0)

Expected output:

0

Python Code

from scipy.stats import dlaplace as scipy_dlaplace

def dlaplace(k, a, dlaplace_mode='pmf', loc=0):
    """
    Compute Discrete Laplace distribution values: PMF, CDF, SF, ICDF, ISF, mean, variance, std, or median.

    See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.dlaplace.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 (integer for PMF/CDF/SF, probability for ICDF/ISF).
        a (float): Shape parameter (must be greater than 0).
        dlaplace_mode (str, optional): Output type to compute. Valid options: PMF, CDF, SF, ICDF, ISF, Mean, Variance, Std Dev, 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:
        a = float(a)
        if a <= 0:
            return "Invalid input: a must be > 0."
    except (ValueError, TypeError):
        return "Invalid input: a must be a number."

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

    valid_modes = {"pmf", "cdf", "sf", "icdf", "isf", "mean", "var", "std", "median"}
    if dlaplace_mode not in valid_modes:
        return f"Invalid input: dlaplace_mode must be one of {', '.join(sorted(valid_modes))}."

    # Create frozen distribution
    dist = scipy_dlaplace(a, loc=loc)

    if dlaplace_mode in ["mean", "var", "std", "median"]:
        if dlaplace_mode == "mean":
            return float(dist.mean())
        elif dlaplace_mode == "var":
            return float(dist.var())
        elif dlaplace_mode == "std":
            return float(dist.std())
        elif dlaplace_mode == "median":
            return float(dist.median())

    def compute(val):
        try:
            val = float(val)
        except (ValueError, TypeError):
            return "Invalid input: k must be a number."

        if dlaplace_mode == "pmf":
            return float(dist.pmf(val))
        elif dlaplace_mode == "cdf":
            return float(dist.cdf(val))
        elif dlaplace_mode == "sf":
            return float(dist.sf(val))
        elif dlaplace_mode == "icdf":
            return float(dist.ppf(val))
        elif dlaplace_mode == "isf":
            return float(dist.isf(val))
        return "Unknown error."

    if isinstance(k, list):
        result = []
        for row in k:
            if not isinstance(row, list):
                return "Invalid input: k must be a 2D list."
            result_row = []
            for val in row:
                res = compute(val)
                if isinstance(res, str):
                    return res
                result_row.append(res)
            result.append(result_row)
        return result
    else:
        return compute(k)

Online Calculator