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.
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 says how much probability sits in a short interval around , 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.
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
- A random variable that can take any value in an interval of numbers: a distance, an angle, a time, a voltage.
- Density
- A function with whose area over an interval is the probability of that interval. Its unit is the inverse of the unit of : if is in metres, is in 1/m. It is also called the probability density function.
- Cumulative distribution
- The probability that is at most : the area under the density up to . It has no unit; it rises from 0 to 1.
- Quantile function
- The value below which falls with probability . The median is . Also called the inverse CDF.
- Support
- The set of values where the density is non-zero.
Three densities to know
| Distribution | Density | Describes | Parameters |
|---|---|---|---|
| Uniform on | for , else 0 | A quantity known only to lie in a range: rounding error, an unknown start offset, “somewhere in this corridor”. | : the ends of the range |
| Exponential | for | The waiting time until a random event that has no memory: a network dropout, a sensor fault, a radioactive count. | : rate (events per second); mean |
| Gaussian (normal) | The sum of many small independent errors: nearly every sensor-noise model in robotics. The next lesson studies it. | : centre; : spread |
For the exponential, the area to the right of has a simple closed form, , so . The uniform distribution has the flat density : its area over the whole range long is exactly 1.
Derivation: from a histogram to a density
Density, probability and the cumulative distribution
Sort samples into bins of width . The fraction that land in bin estimates its probability (the Monte Carlo lesson).
Divide by the bin width. The bar of height and width has area , and all the bars together have area 1. This scaling is why the bars in the figure above settle onto one curve as shrinks.
Define the density as the limit: probability per unit length at . For a short interval, probability .
Add the masses of the bins between and (axiom 3, the bins are disjoint). As the sum of thin rectangles becomes the area under the curve.
Axiom 2 (the whole line has probability 1) and axiom 1 (no negative probability).
An interval of zero width has zero area. A density can be positive at while is zero: probability is spread over intervals, never stacked on a point.
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.
The area between and is the area up to minus the area up to . 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 be a length in metres and the same length in a smaller unit ( for centimetres, for millimetres).
Rewrite the event as an event about . This holds for .
Differentiate with the chain rule. The density value in the new unit is divided by : the same probability is spread over an axis that is times longer.
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 you need only a generator of uniform numbers.
Draw a uniform number and push it through the quantile function.
Apply the increasing function to both sides of the inequality; for a uniform variable . So has exactly the cumulative distribution .
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.
- A rounded reading (uniform). A depth sensor rounds to the nearest centimetre, so its rounding error is uniform on cm, which is m. The density is per cm, or per metre: the same quantity, two different density values. The probability of an error within ±2 mm is the area , or . Areas agree, heights do not.
- A stop error (Gaussian). The stop error has standard deviation mm m. At the centre the density is per metre, far above 1. That is fine because the curve is narrow. The probability of stopping within ±5 mm is .
- Check the area. Adding up 1000 thin rectangles of the density between −5 mm and +5 mm also gives 0.3829.
- A time between dropouts (exponential). Radio dropouts occur on average every 2 s, so per second. The probability that the next one comes within 2 s is . 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.
- Simulate it. Draw uniform, compute . 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
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 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 for a step .
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
- A continuous random variable has no probability at a single value. Probability is attached to intervals.
- 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.
- 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.
- 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.
- 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.
- J. Tsitsiklis and P. Jaillet, MIT RES.6-012 Introduction to Probability , lectures 8 to 10 (continuous random variables). Free videos and notes, licence CC BY-NC-SA 4.0.
- J. Blitzstein and J. Hwang, Introduction to Probability, 2nd edition, CRC Press, 2019 , chapters 5 (continuous random variables) and 10. Free to read online; the book is copyrighted.
- C. Piech, Probability for Computer Scientists (Stanford CS109 course reader) , part 2: continuous random variables. Free to read online.
- S. Thrun, W. Burgard and D. Fox, Probabilistic Robotics, MIT Press, 2005, section 2.2 (density functions) and the sensor models of chapter 6. Paid book.
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.
Related content
Nothing below is required to finish this lesson.
- BeforeM2-T1: Discrete Random Variables and Probability Mass Functions
Define a random variable, write its probability mass function and use the Bernoulli, binomial and categorical distributions.
- LabM2-L1: 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.
Other courses on this site treat neighbouring topics from their own angle:
Get notified when the next lesson and new labs are released.
One email per new lesson or lab.