Laplace distribution: model sensor error and tolerance

Learn the Laplace distribution through sensor error. Explore location, scale, density, interval probabilities, and the link between absolute error and median fitting.

By 9 min read

What you will learn

  • Explain how location and positive scale change a Laplace distribution.
  • Calculate an interval probability from the cumulative distribution function.
  • Separate density height from probability and scale from standard deviation.
  • Connect a Laplace error model to absolute-error loss and median fitting.

Before you start

A sensor can be unbiased and still miss a tight tolerance often. Bias describes the center of its errors; spread describes how far the errors vary around that center.

The Laplace distribution gives us one model for both. We will use invented robot sensor errors to calculate the chance of staying within a tolerance, then connect the same model to absolute-error fitting.

Set the center and spread

The Laplace distribution is a continuous distribution on the whole real line. It is also called the double exponential distribution: its density falls exponentially on each side of its center.

Two parameters define it:

  • Location μ, pronounced “mu,” sets the center. It is the distribution's mean, median, and mode, the location of the density's peak.
  • Scale b sets its spread and must be strictly positive. Larger b spreads probability farther from μ.

For an error measured in centimeters, both μ and b have units of centimeters. The NIST double exponential reference gives the location, scale, and main properties.

Scale and standard deviation have different values:

Var(E) = 2b²
SD(E) = √2 b

With b = 1 cm, the variance is 2 cm² and the standard deviation is about 1.41 cm. Moving μ shifts the distribution while keeping that spread unchanged.

This is a probability distribution. The Laplace transform is a tool for transforming functions, and the Laplacian is a differential operator. Sharing a name does not make their formulas interchangeable.

Read probability as area

The probability density function, or PDF, is:

f(e) = (1 / 2b) exp(−|e − μ| / b), with b > 0

The absolute value measures distance from μ. Equal distances on either side have equal density. The graph has a corner at its peak and a total area of one.

Density height tells you how tightly probability concentrates near a value. An interval's area gives its probability. In a continuous model, a single exact value has probability zero, even at the peak.

Set b = 0.25 cm and the peak reaches 2 per cm. That is a density, not a 200% chance. Multiplying density by a small interval width approximates the probability in that interval; wider intervals require the full area calculation.

The Stan Laplace reference also defines this density. Check parameter meanings when reading library documentation: the symbol used for its scale can differ from b.

Explore sensor tolerance

Define signed error as reported distance minus reference distance. Positive error means the sensor reports too far; negative error means it reports too near. Our model starts with μ = 0 cm and b = 1 cm.

The tolerance band stays centered on the desired zero error. A tolerance t accepts errors from −t to +t. It does not move when μ changes.

Interactive experiment

How often is sensor error within tolerance?

Move the location and scale, then set the acceptable error around zero. The shaded area gives its probability.

Press Enter or leave a number field to apply it. Escape cancels the edit.

Density (per cm)Peak: 0.500 per cm
Laplace density and sensor toleranceLaplace density with location 0 centimeters and scale 1 centimeters. The shaded area between -1 and 1 centimeters contains 63.21% probability. The remaining probability is 18.39% below and 18.39% above the tolerance band. The dotted vertical line marks location, and the curve continues beyond the displayed range.

Signed sensor error (cm)

Shading covers errors from −1.00 to 1.00 cm. The dotted line marks μ = 0.00 cm. Both plot scales adjust to fit the settings; the distribution extends beyond the chart.
Within tolerance
63.21%
Outside tolerance
36.79%
Below −t
18.39%
Above +t
18.39%
Standard deviation
1.41 cm
Variance
2.00 cm²

The tolerance band stays centered on zero. It contains 63.21% of errors under the selected model.

These synthetic settings illustrate a probability model. The readouts use the full distribution and exact formulas; the drawn curve uses sampled points.

At the starting tolerance of 1 cm, about 63.21% of modeled errors lie inside the band. Choose Bias +1 cm: the distribution shifts right, and the accepted fraction falls to 43.23%.

Choose Wider spread to keep μ at zero and double b to 2 cm. The accepted fraction becomes 39.35%. Then try No tolerance: the band has zero width, so its probability is zero.

Both plot scales adjust with the settings. Use the axis labels and probability readouts when comparing presets. The readouts include the distribution's full tails beyond the drawing.

Calculate interval and tail probabilities

The cumulative distribution function, or CDF, gives F(e) = P(E ≤ e). It collects all probability to the left of e:

F(e) = ½ exp((e − μ) / b), when e < μ
F(e) = 1 − ½ exp(−(e − μ) / b), when e ≥ μ

Both branches meet at F(μ) = 0.5. The CDF rises from zero to one as e runs from far left to far right.

For endpoints a ≤ c, subtract the two cumulative probabilities:

P(a ≤ E ≤ c) = F(c) − F(a)

The distinction between strict and inclusive endpoints disappears here because a single point has zero probability. The upper-tail probability is P(E > c) = 1 − F(c).

For a band centered on the distribution's location, symmetry gives a shorter formula:

P(|E − μ| > t) = exp(−t / b), for t ≥ 0

Our sensor band centers on zero. Use this shortcut for that band only when μ = 0. With bias, use the CDF at −t and +t.

Work through a one-centimeter tolerance

For μ = 0, b = 1, and t = 1, the lower boundary gives F(−1) = 0.5 exp(−1) ≈ 0.18394. The upper boundary gives F(1) = 1 − 0.5 exp(−1) ≈ 0.81606.

Subtracting gives P(−1 ≤ E ≤ 1) ≈ 0.63212, or 63.21%. Each outside tail contains about 18.39%, for a total failure probability of 36.79%.

In 1,000 readings, this model predicts about 632 within tolerance on average. A particular run will vary. The per-reading probability alone does not establish that successive errors are independent.

For 95% coverage around μ, solve exp(−t / b) = 0.05. This gives t = −b ln(0.05) ≈ 3b. At b = 1 cm, the central 95% interval extends about 3 cm on each side of μ.

This interval describes possible sensor errors under fixed parameters. It does not express uncertainty about μ itself. The Bayesian inference lesson separates those two questions.

Connect the model to absolute error

Suppose observed errors e₁ through eₙ are independent draws from one Laplace distribution. Taking the negative logarithm of their likelihood gives:

−log L(μ, b) = n log(2b) + (1 / b) Σ |eᵢ − μ|

For fixed b, choosing μ to maximize likelihood therefore minimizes the sum of absolute errors. A sample median minimizes that sum. For an even sample, every point between the two middle observations is a minimizer; their midpoint is a common choice.

After choosing that median, the fitted scale is the mean absolute deviation from it. The SciPy Laplace guide gives both estimators.

Consider errors [-1, 0, 0, 1, 8] cm. Their median is 0 cm. Their absolute deviations sum to 10 cm, so the fitted b is 10 / 5 = 2 cm.

Replacing 8 with 80 leaves the median at zero, but raises the fitted scale to 16.4 cm. The stable center estimate does not make extreme observations irrelevant. If every observation is identical, the fitted scale collapses toward zero and no positive-scale maximum exists.

For regression with independent Laplace errors and a shared scale, the same likelihood leads to minimizing absolute residuals. Compare this objective with the squared-error fit in linear regression. Changing the error model changes which prediction errors receive the most weight.

Check the modeling assumptions

The example does not establish that a real sensor follows a Laplace distribution. A useful model needs evidence from the conditions where it will operate.

  • Define the error and check the accuracy of the reference measurement.
  • Examine symmetry, drift, clipped readings, and separate operating modes.
  • Estimate location and spread using representative data, then check interval coverage on fresh readings.
  • Account for dependence before combining repeated readings into a likelihood.

The Laplace distribution allows errors of any magnitude. A sensor with hard limits or a separate failure mode may need a different model. As in conditional probability, the reference population determines what a probability claim means.

Reproduce the results in Python

This standard-library example reproduces the starting probability and the median fit. It uses the CDF directly for this moderate interval.

from math import exp, sqrt
from statistics import median

def laplace_cdf(error, mu, b):
    if b <= 0:
        raise ValueError("Scale must be positive")
    z = (error - mu) / b
    return 0.5 * exp(z) if z < 0 else 1 - 0.5 * exp(-z)

mu, b, tolerance = 0.0, 1.0, 1.0
inside = (laplace_cdf(tolerance, mu, b)
          - laplace_cdf(-tolerance, mu, b))

errors = [-1, 0, 0, 1, 8]
fitted_mu = median(errors)
fitted_b = sum(abs(error - fitted_mu) for error in errors) / len(errors)

print(f"Within tolerance: {inside:.2%}")
print(f"Standard deviation: {sqrt(2) * b:.2f} cm")
print(f"Fitted location: {fitted_mu:.2f} cm")
print(f"Fitted scale: {fitted_b:.2f} cm")

Expected output:

Within tolerance: 63.21%
Standard deviation: 1.41 cm
Fitted location: 0.00 cm
Fitted scale: 2.00 cm

Check your understanding

Exercise 1. Keep μ = 0 cm and b = 1 cm. Increase tolerance to 2 cm. Find the probability inside the band and the probability in each outside tail.

Show solution: use the centered tail formula

The total outside probability is exp(−2) ≈ 13.53%. The inside probability is 1 − exp(−2) ≈ 86.47%. Symmetry splits the outside probability into about 6.77% per tail.

Exercise 2. Let μ = 1 cm, b = 1 cm, and t = 1 cm. Explain why the two tails outside the zero-centered tolerance band differ, then calculate the probability above +1 cm.

Show solution: locate the band relative to the center

The band runs from −1 to +1 cm, while the distribution centers at +1 cm. Its upper boundary is exactly the median, so P(E > 1) = 50%. The lower tail is F(−1) = 0.5 exp(−2) ≈ 6.77%. The remaining 43.23% lies inside the band.

Sources and further study