Skip to the lesson
FixTheRobot Explore Robotics / Probability for Robotics / M2 Random Variables and Probability Distributions / M2-T2

Probability for Robotics · Theory · M2

M2-T2 — Continuous Random Variables, Densities and the CDF

A distance can take any value in a range, so no single value has any probability. Probability lives in intervals, and the density says how thickly it is spread.

  • Lesson 2 of 4
  • Beginner
  • Module 2: Random Variables and Probability Distributions
  • Theory
  • About 18 minutes to read

Distances, angles, times and voltages are not counted: they vary smoothly, and the probability that one of them equals a particular number exactly is zero. That sounds like the end of probability for sensors, but it is the start of the density, the tool that every Gaussian filter in this course is built on. This lesson explains what a density value means (and why it can be larger than 1), how areas under it become probabilities, and how the cumulative distribution turns those areas into subtractions.

Learning objectives

After this lesson, you should be able to:

  • Explain why a continuous random variable has no mass at a single value, and what a density value means instead.
  • Compute the probability of an interval from a density (as an area) and from a cumulative distribution (as a difference).
  • Derive the density as the limit of a histogram, and explain how it changes with the unit of measurement.
  • Sample from a distribution by inverting its cumulative distribution.

The intuition

Rover stops after a 2 m drive, and the error of the stop position, measured in millimetres, can be any real number around zero. Ask for the probability that the error is exactly 3.000000 mm and the answer is zero: among infinitely many possible values no single one can carry a share of the unit of probability. Ask for the probability that the error lies between 2 and 4 mm and you get a sensible, non-zero answer. Probability for a continuous quantity lives in intervals.

The picture to keep in mind is clay again, but now spread continuously along a rod instead of lumped on points. A single point of the rod has no mass; a piece of the rod does. The thickness of the clay at a place is its density: mass per unit length, so it is measured in grams per centimetre. A thick patch means that a short piece there is heavy. A density curve for probability does the same thing: the height of the curve at xx says how much probability sits in a short interval around xx, per unit length.

You can watch the idea appear by drawing a histogram. Take 2000 simulated stop errors and sort them into bins. With wide bins you see a blocky shape. Narrow the bins and the shape follows the curve more closely, until there are so few samples per bin that the bars become ragged. The bar heights are scaled by the bin width so that their total area is one, and that scaling is what lets the bars settle onto one curve instead of shrinking away.

The same stop errors drawn with narrower and narrower binsThree histograms of the same 2000 simulated stop errors, each with an orange bell curve laid over it. With bins 10 millimetres wide the bars are few and blocky. With 4 millimetre bins they follow the curve more closely. With 1 millimetre bins they are ragged but still scatter around the same curve. In each panel the bar heights are rescaled so that the total area is one, so the heights stay on the same scale as the bins shrink.-40-2002040stop error (mm)bins 10 mm wide-40-2002040stop error (mm)bins 4 mm wide-40-2002040stop error (mm)bins 1 mm wide
The same 2000 stop errors drawn with three bin widths. Bar heights are the fraction of samples in the bin divided by the bin width, so the total area is 1 in every panel. The orange curve is the density that the bars approach.

Three consequences follow, and the rest of the lesson makes each one precise.

  • The height of a density is not a probability. It is probability per unit length. The curve above peaks at about 40 per metre. Nothing is wrong: a tiny interval of width 1 mm around the peak holds about 0.04.
  • Probability is area. The probability that the error lies in an interval is the area under the curve over that interval.
  • Heights depend on units, areas do not. Measure in centimetres and the same curve peaks at 0.4 per centimetre; the areas, and therefore the probabilities, are unchanged.

For a continuous quantity, never ask “what is the probability of this value?”. Ask “what is the probability of this interval?”, and answer it with an area.

Definitions

Continuous random variable XX
A random variable that can take any value in an interval of numbers: a distance, an angle, a time, a voltage.
Density fX(x)f_X(x)
A function with fX(x)≥0f_X(x) \ge 0 whose area over an interval is the probability of that interval. Its unit is the inverse of the unit of xx: if xx is in metres, ff is in 1/m. It is also called the probability density function.
Cumulative distribution FX(x)F_X(x)
The probability that XX is at most xx: the area under the density up to xx. It has no unit; it rises from 0 to 1.
Quantile function FX−1(q)F_X^{-1}(q)
The value below which XX falls with probability qq. The median is F−1(0.5)F^{-1}(0.5). Also called the inverse CDF.
Support
The set of values where the density is non-zero.
Density
P(a≤X≤b)=∫abfX(x) dx,∫−∞∞fX(x) dx=1,fX(x)≥0P(a \le X \le b) = \int_a^b f_X(x)\,dx, \qquad \int_{-\infty}^{\infty} f_X(x)\,dx = 1, \qquad f_X(x) \ge 0
The integral ∫ab\int_a^b is the area under the curve from aa to bb. The three conditions are the axioms of probability for continuous values.
Cumulative distribution
FX(x)=P(X≤x)=∫−∞xfX(t) dt,P(a<X≤b)=FX(b)−FX(a)F_X(x) = P(X \le x) = \int_{-\infty}^{x} f_X(t)\,dt, \qquad P(a < X \le b) = F_X(b) - F_X(a)
The cumulative distribution turns an area into a subtraction.
Density from the cumulative distribution
fX(x)=dFXdxf_X(x) = \frac{dF_X}{dx}
The density is the slope of the cumulative distribution: it climbs fastest where the density is large.
A density, a probability as an area, and the cumulative distributionLeft: a bell-shaped density of the stop error with standard deviation 10 millimetres and a peak of about 39.9 per metre. The area under the curve between minus 5 and plus 5 millimetres is shaded and labelled probability 0.383. Right: the cumulative distribution rising from 0 to 1. At minus 5 millimetres it equals 0.309 and at plus 5 millimetres 0.691, and the vertical distance between those two values is the same probability, 0.383.-40-20020400153045stop error (mm)density f (per metre)shaded area = 0.383peak 39.9 per metre -40-200204000.250.50.751stop error (mm)cumulative F F(5) − F(−5) = 0.383 0.3090.691
Left: the density of a stop error with a 10 mm standard deviation. The shaded area between −5 and +5 mm is the probability of docking within 5 mm. Right: the cumulative distribution of the same variable. The probability is the vertical distance between its values at −5 and +5 mm.

Three densities to know

Three continuous distributions
DistributionDensityDescribesParameters
Uniform on [a,b][a, b]f(x)=1b−af(x) = \dfrac{1}{b-a} for a≤x≤ba \le x \le b, else 0A quantity known only to lie in a range: rounding error, an unknown start offset, “somewhere in this corridor”.a,ba, b: the ends of the range
Exponentialf(t)=λe−λtf(t) = \lambda e^{-\lambda t} for t≥0t \ge 0The waiting time until a random event that has no memory: a network dropout, a sensor fault, a radioactive count.λ\lambda: rate (events per second); mean 1/λ1/\lambda
Gaussian (normal)f(x)=1σ2π e−(x−μ)22σ2f(x) = \dfrac{1}{\sigma\sqrt{2\pi}}\, e^{-\frac{(x-\mu)^2}{2\sigma^2}}The sum of many small independent errors: nearly every sensor-noise model in robotics. The next lesson studies it.μ\mu: centre; σ\sigma: spread

For the exponential, the area to the right of tt has a simple closed form, P(T>t)=e−λtP(T > t) = e^{-\lambda t}, so F(t)=1−e−λtF(t) = 1 - e^{-\lambda t}. The uniform distribution has the flat density 1/(b−a)1/(b-a): its area over the whole range b−ab - a long is exactly 1.

Derivation: from a histogram to a density

Density, probability and the cumulative distribution

  1. pi=P(xi≤X<xi+Δ)  ≈  ninp_i = P(x_i \le X < x_i + \Delta) \;\approx\; \dfrac{n_i}{n}

    Sort nn samples into bins of width Δ\Delta. The fraction ni/nn_i / n that land in bin ii estimates its probability (the Monte Carlo lesson).

  2. hi=nin Δh_i = \dfrac{n_i}{n\,\Delta}

    Divide by the bin width. The bar of height hih_i and width Δ\Delta has area pip_i, and all the bars together have area 1. This scaling is why the bars in the figure above settle onto one curve as Δ\Delta shrinks.

  3. f(x)=lim⁡Δ→0P(x≤X<x+Δ)Δf(x) = \lim_{\Delta \to 0} \dfrac{P(x \le X < x + \Delta)}{\Delta}

    Define the density as the limit: probability per unit length at xx. For a short interval, probability ≈f(x) Δ\approx f(x)\,\Delta.

  4. P(a≤X≤b)=∑ipi  ≈  ∑if(xi) Δ  ⟶  ∫abf(x) dxP(a \le X \le b) = \sum_i p_i \;\approx\; \sum_i f(x_i)\,\Delta \;\longrightarrow\; \int_a^b f(x)\,dx

    Add the masses of the bins between aa and bb (axiom 3, the bins are disjoint). As Δ→0\Delta \to 0 the sum of thin rectangles becomes the area under the curve.

  5. ∫−∞∞f(x) dx=1,f(x)≥0\int_{-\infty}^{\infty} f(x)\,dx = 1, \qquad f(x) \ge 0

    Axiom 2 (the whole line has probability 1) and axiom 1 (no negative probability).

  6. P(X=c)=∫ccf(x) dx=0P(X = c) = \int_c^c f(x)\,dx = 0

    An interval of zero width has zero area. A density can be positive at cc while P(X=c)P(X = c) is zero: probability is spread over intervals, never stacked on a point.

  7. F(x)=∫−∞xf(t) dt    ⇒    f(x)=dFdxF(x) = \int_{-\infty}^{x} f(t)\,dt \;\;\Rightarrow\;\; f(x) = \dfrac{dF}{dx}

    The cumulative distribution is the running area. By the fundamental theorem of calculus its slope is the density: it climbs fastest where the density is large.

  8. P(a<X≤b)=F(b)−F(a)P(a < X \le b) = F(b) - F(a)

    The area between aa and bb is the area up to bb minus the area up to aa. Because single points have no probability, it does not matter whether the ends are included.

What happens to a density when the unit changes

Let XX be a length in metres and Y=cXY = cX the same length in a smaller unit (c=100c = 100 for centimetres, c=1000c = 1000 for millimetres).

  1. FY(y)=P(cX≤y)=P ⁣(X≤yc)=FX ⁣(yc)F_Y(y) = P(cX \le y) = P\!\left(X \le \dfrac{y}{c}\right) = F_X\!\left(\dfrac{y}{c}\right)

    Rewrite the event cX≤ycX \le y as an event about XX. This holds for c>0c > 0.

  2. fY(y)=ddy FX ⁣(yc)=1c fX ⁣(yc)f_Y(y) = \dfrac{d}{dy}\,F_X\!\left(\dfrac{y}{c}\right) = \dfrac{1}{c}\, f_X\!\left(\dfrac{y}{c}\right)

    Differentiate with the chain rule. The density value in the new unit is divided by cc: the same probability is spread over an axis that is cc times longer.

  3. ∫fY(y) dy=∫fX(x) dx=1\int f_Y(y)\,dy = \int f_X(x)\,dx = 1

    The total area is unchanged. Heights depend on the unit, areas do not.

Sampling a density: the inverse transform

To simulate a random variable with cumulative distribution FF you need only a generator of uniform numbers.

  1. U∼Uniform(0,1),X=F−1(U)U \sim \mathrm{Uniform}(0, 1), \qquad X = F^{-1}(U)

    Draw a uniform number and push it through the quantile function.

  2. P(X≤x)=P(F−1(U)≤x)=P(U≤F(x))=F(x)P(X \le x) = P\bigl(F^{-1}(U) \le x\bigr) = P\bigl(U \le F(x)\bigr) = F(x)

    Apply the increasing function FF to both sides of the inequality; for a uniform variable P(U≤u)=uP(U \le u) = u. So XX has exactly the cumulative distribution FF.

  3. F(t)=1−e−λt    ⇒    F−1(u)=−ln⁡(1−u)λF(t) = 1 - e^{-\lambda t} \;\;\Rightarrow\;\; F^{-1}(u) = -\dfrac{\ln(1 - u)}{\lambda}

    For the exponential distribution the inverse is a closed formula, so a waiting time is one logarithm of a uniform number.

A density value is probability per unit length. Probabilities are areas, areas are differences of the cumulative distribution, and a uniform random number turns into any other distribution through the inverse cumulative distribution.

A worked example

Three sensors, three densities, one program that prints every number below.

  1. A rounded reading (uniform). A depth sensor rounds to the nearest centimetre, so its rounding error is uniform on [−0.5,0.5][-0.5, 0.5] cm, which is [−0.005,0.005][-0.005, 0.005] m. The density is 1/(0.5−(−0.5))=11/(0.5 - (-0.5)) = 1 per cm, or 1/0.01=1001/0.01 = 100 per metre: the same quantity, two different density values. The probability of an error within ±2 mm is the area 0.4 cm×1 per cm=0.40.4 \text{ cm} \times 1 \text{ per cm} = 0.4, or 0.004 m×100 per m=0.40.004 \text{ m} \times 100 \text{ per m} = 0.4. Areas agree, heights do not.
  2. A stop error (Gaussian). The stop error has standard deviation σ=10\sigma = 10 mm =0.01= 0.01 m. At the centre the density is 1/(0.012π)=39.891/(0.01\sqrt{2\pi}) = 39.89 per metre, far above 1. That is fine because the curve is narrow. The probability of stopping within ±5 mm is F(0.005)−F(−0.005)=0.6915−0.3085=0.3829F(0.005) - F(-0.005) = 0.6915 - 0.3085 = 0.3829.
  3. Check the area. Adding up 1000 thin rectangles of the density between −5 mm and +5 mm also gives 0.3829.
  4. A time between dropouts (exponential). Radio dropouts occur on average every 2 s, so λ=0.5\lambda = 0.5 per second. The probability that the next one comes within 2 s is F(2)=1−e−1=0.6321F(2) = 1 - e^{-1} = 0.6321. Note that the mean waiting time is not a safe guarantee: more than half the gaps are shorter than the mean, and a good share are much longer.
  5. Simulate it. Draw uu uniform, compute t=−ln⁡(1−u)/0.5t = -\ln(1-u)/0.5. Out of 20 000 draws the fraction below 2 s is 0.631, against the exact 0.6321.
import math
from robotprob import Rng, uniform_pdf, uniform_cdf, gaussian_pdf, gaussian_cdf, exponential_cdf

# 1. rounding error, uniform on [-0.5, 0.5] cm = [-0.005, 0.005] m
print('density in 1/cm :', uniform_pdf(0.0, -0.5, 0.5))
print('density in 1/m  :', uniform_pdf(0.0, -0.005, 0.005))
p_cm = uniform_cdf(0.2, -0.5, 0.5) - uniform_cdf(-0.2, -0.5, 0.5)
p_m = uniform_cdf(0.002, -0.005, 0.005) - uniform_cdf(-0.002, -0.005, 0.005)
print('P(|e| <= 2 mm)  :', round(p_cm, 4), round(p_m, 4))

# 2. stop error, Gaussian with standard deviation 1 cm: a density above 1
sigma = 0.01
print('peak density    :', round(gaussian_pdf(0.0, 0.0, sigma), 2), 'per metre')
p = gaussian_cdf(0.005, 0.0, sigma) - gaussian_cdf(-0.005, 0.0, sigma)
print('P(|e| <= 5 mm)  :', round(p, 4))
n = 1000
w = 0.01 / n
area = sum(gaussian_pdf(-0.005 + (i + 0.5) * w, 0.0, sigma) * w for i in range(n))
print('area under curve:', round(area, 4))

# 3. time between dropouts, exponential with mean 2 s, sampled with the inverse CDF
rng = Rng(3)
times = [-math.log(1.0 - rng.random()) / 0.5 for _ in range(20000)]
print('P(T < 2 s) exact    :', round(exponential_cdf(2.0, 0.5), 4))
print('P(T < 2 s) simulated:', round(sum(1 for t in times if t < 2.0) / len(times), 4))
density in 1/cm : 1.0
density in 1/m  : 100.0
P(|e| <= 2 mm)  : 0.4 0.4
peak density    : 39.89 per metre
P(|e| <= 5 mm)  : 0.3829
area under curve: 0.3829
P(T < 2 s) exact    : 0.6321
P(T < 2 s) simulated: 0.631
The density of the rounding error is 1 in one unit and 100 in another, and the probability is 0.4 in both: the density has a unit, the probability does not.

In robotics

Sensor models are densities

When a range sensor returns 2.013 m, the sensor model of module 5 does not say “the probability that the reading is 2.013 m” (zero). It gives the density of the reading at that value, given the robot's pose. A particle filter multiplies such density values to weight its particles, and a Kalman filter evaluates them implicitly inside its update. These values are called likelihoods (M3-T2: Bayes' Rule: Prior, Likelihood and Posterior), and they are not probabilities: they can exceed 1, they change with the unit of the measurement, and only their ratios between hypotheses mean anything.

That last point is exactly why filters normalize. A change from metres to millimetres multiplies every likelihood by the same constant, and the normalization step removes it. A fixed threshold on a raw density value, such as “reject a measurement whose likelihood is below 0.01”, does not survive a change of unit. Gating on a distance measured in standard deviations (the Mahalanobis distance of M4-T3: The Multivariate Gaussian and Covariance Ellipses) does.

Probabilities of intervals as safety numbers

“What is the probability that the position error exceeds the clearance to the wall?” is the area of a density to the right of a number: one minus a cumulative distribution value. The navigation course sizes safety margins this way in Navigation Under Pose Uncertainty.

Waiting times and faults

Time to the next communication dropout, packet arrival or sensor fault is modelled with the exponential distribution when the event has no memory. A failure rate λ\lambda of 10-4 per hour gives a mean time of 10 000 hours, yet the probability of a failure in the first hour is about 10-4 and in the first 1000 hours about 9.5%. The UAV course uses this in fault tree analysis.

Uniform for “somewhere in a range”

A robot that has no idea where it is in a corridor 20 m long starts a localizer with a uniform belief: density 0.05 per metre everywhere. Quantization of encoder ticks and analogue-to-digital converters also gives uniform errors, with standard deviation Δ/12\Delta/\sqrt{12} for a step Δ\Delta.

Sampling in simulators

Every noisy simulator turns uniform random numbers into the distribution it needs. The inverse transform is the general method and the Gaussian sampler of M2-T3: The Gaussian Distribution is a clever special case. When a simulated robot behaves implausibly, a sampler that quietly produces the wrong distribution (a standard deviation used as a variance, degrees used as radians) is a frequent cause.

Common misconceptions

“A density value is a probability.”

A density is probability per unit length. It can be 40 per metre, as above, and it changes with the unit. Only areas are probabilities. The height of the curve multiplied by a small width gives an approximate probability for that small interval.

“If P(X = x) is zero, the value x is impossible.”

Some value must occur, and each one has probability zero. Probability zero means “no share of the unit is stacked here”, not “cannot happen”. In practice nobody asks for an exact value; a dock, a tolerance or a gate is always an interval.

“A mean waiting time of 2 seconds means an event every 2 seconds.”

The exponential distribution is memoryless: the chance of a dropout in the next second is the same however long it has been quiet. Gaps vary widely, and a long quiet spell does not make a dropout “due”. Designing a system on the assumption that the mean is a guaranteed gap is a common source of timeouts that are too short.

“The bars of a histogram are probabilities.”

Bars show counts or fractions per bin. To compare a histogram with a density you divide the fraction by the bin width. With the same data and wider bins the fractions grow, while the density heights stay put.

Key takeaways

  1. A continuous random variable has no probability at a single value. Probability is attached to intervals.
  2. A density f(x) is probability per unit length. Its height can exceed 1 and depends on the unit; the area under it over an interval is the probability of that interval, and the total area is 1.
  3. The cumulative distribution F(x) is the area up to x. P(a < X ≤ b) = F(b) − F(a), and the density is the slope of F.
  4. Changing units divides a density by the scale factor and leaves probabilities untouched. Raw density values (likelihoods) are therefore compared by ratio, never against a fixed threshold.
  5. A uniform random number becomes any distribution through the inverse cumulative distribution: X = F⁻¹(U). For the exponential, T = −ln(1 − U)/λ.

References and further reading

Each source is marked free or paid. Free to read does not mean free to reuse: the course resources page lists every source with its licence.

Knowledge check

Three questions. Write an answer, then reveal the explanation. Your answers stay in your browser.

Question 1

A laser rangefinder’s error has a density of 25 per metre at zero and is nearly flat within a few millimetres of it. About how probable is an error within ±1 mm? Is a density of 25 a problem?

Explanation

For a short interval, probability ≈ density × width = 25 per m × 0.002 m = 0.05, about 5%. A density of 25 is not a problem: it is probability per metre and only its area has to be at most 1. The distribution is simply narrow (a Gaussian with that peak has a standard deviation of about 16 mm). If the same density were stated per millimetre it would be 0.025 per mm, and 0.025 × 2 mm gives the same 0.05.

Question 2

For a Gaussian stop error with a standard deviation of 1 cm, the cumulative distribution takes the values F(−1 cm) = 0.1587 and F(+2 cm) = 0.9772. What is the probability that the error lies between −1 cm and +2 cm, and the probability that it exceeds +2 cm? Why do these answers not depend on whether the end points are included?

Explanation

P(−1 < X ≤ 2) = F(2) − F(−1) = 0.9772 − 0.1587 = 0.8185. P(X > 2) = 1 − F(2) = 0.0228. Including or excluding an end point changes nothing because a single value has probability zero (the area of an interval of zero width), so P(X ≤ 2) = P(X < 2) for a continuous variable. This is different from a discrete variable such as a detection count, where P(X ≤ 2) and P(X < 2) differ by the mass at 2.

Question 3

A teammate re-logs a particle filter’s sensor data in millimetres instead of metres, with the sensor model rewritten accordingly. The particle weights before normalization shrink by a factor of 1000. Will the localization change? What kind of code in the filter might break?

Explanation

The localization should not change. Every particle’s weight is multiplied by the same constant, and normalizing the weights divides it out, so the posterior is identical. Code that compares a raw weight or likelihood with a fixed number would break: for example “reset the filter if the best likelihood is below 0.01”, or “skip the measurement if the likelihood is below a threshold”. Such tests should be written in terms of unit-free quantities, such as the Mahalanobis distance or the ratio of the likelihood to its maximum possible value.

What’s next

The most important density in robotics is the Gaussian. The next lesson explains what its two parameters mean physically, what the 68–95–99.7 rule says about a sensor, and why the bell curve appears so often.

Practice this lesson

Sensor Noise Explorer

A range sensor reads a wall at a fixed distance and the readings scatter. Build the histogram, estimate bias and spread, and find the outliers a Gaussian cannot explain.

Next lesson

M2-T3 — The Gaussian Distribution

Read the mean and standard deviation of a Gaussian, use the 68-95-99.7 rule and the standard normal, and say when the Gaussian is a poor model.

Nothing below is required to finish this lesson.

Other courses on this site treat neighbouring topics from their own angle: