MathalyseResearch

Learn AI · Chapter 3 · Probability and Information Theory

Technical Details of Continuous Variables

Three technical ideas that make continuous probability safe to use: sets of measure zero, properties that hold almost everywhere, and the Jacobian factor that corrects a density when a variable is transformed.

Book §3.12, pages 71–73Equations 3.43–3.47About 40 minutes
Section 1

Densities, sets and integration

How to use this page

Click any colored symbol, in an equation or in a sentence, and it explains itself. Then make the guesses and move the sliders before you read on.

the density of x, the one you start from the density of y after the transformation the stretch factor, which corrects the density a set of values you integrate over a small length budget

The book builds probability for continuous variables on one rule from Section 3.3.2. The probability that a continuous vector lands in a set is the integral of its probability density function, written , over that set.

Click any coloured symbol to see what it means.

Two facts follow at once. A density is not a probability: it is a probability per unit length, so it can be larger than 1. As the set shrinks to a point, its probability shrinks to zero, while the density there does not.

Predict first A normal density with spread has a peak at its center. About how high is that peak?

Pick one guess. The answer appears here.

Probability of a set from a density

density the shaded area is the probability of the intervalisolated points

Set the spread to 0.2 and the peak density rises to about 2, a perfectly legal value, because the bell is narrow and its area is still 1. Now drag the width to 0. The probability of an interval around the peak falls to exactly 0 although the curve is still at its highest there. In a continuous world, any single outcome has probability zero, and positive probability belongs to stretches of the line. Tick the box and 50 extra isolated points join the set. The probability does not move, since each point adds zero.

Challenge +8 XP

Set the center to 0 and the spread to 0.2 so that the density at the center is above 1, then drag the width to 0.

Going deeper: the normal density has total area 1 (added here, not printed in the book)

The bell in the demo is the normal density with mean 0 and spread :

For every value of the area under this curve over the whole line is exactly 1, so the total probability is 1 even when the peak is far above 1. The readout integrates the density numerically over the chosen interval with Simpson’s rule.

Sets that cannot be integrated over

The book flags a warning. Some choices of produce paradoxes. One can build two sets and with no point in common for which . Probabilities of disjoint sets can add to at most 1, so these sets break the basic rules.

Predict first Which of these sets can be assigned a probability safely, without paradox?

Pick one guess. The answer appears here.

Going deeper: where the paradoxes come from (measure theory, outside this module)

The paradoxes are built by using the infinite precision of real numbers to the full, for example with fractal-shaped sets or sets defined by transforming the rational numbers. The book points to the Banach-Tarski theorem as an entertaining example. The cure belongs to measure theory, one of whose main results describes exactly which sets can be assigned a probability without paradox. The book does not develop measure theory, and it states that in this textbook integration is only done over sets with simple descriptions. The practical rule is the same: intervals, boxes, balls and their finite unions are all safe.

The book keeps two other pieces of measure theory, because they state theorems about most points while allowing a few exceptions. They are the next two sections.

Section 2

Measure zero

Measure theory gives a way to say that a set of points is negligibly small. Such a set is said to have measure zero. The book does not define it formally and gives the intuition: a set of measure zero occupies no volume in the space where it lives. Length, area and volume are the three everyday cases. The book’s examples are:

Predict first Which has measure zero in the plane: a line segment, or a filled triangle?

Pick one guess. The answer appears here.

  • In the plane, a line has measure zero, because it has no area. A filled polygon has positive measure.
  • An individual point has measure zero.

Thickening a set

Here is a hands-on way to see why a line is negligible and a polygon is not. Thicken the shape by a distance on every side, and ask what fraction of the square its thickened version covers. The demo scatters 20000 random points in the unit square and counts how many fall within of the shape.

Predict first A filled triangle and a line segment are each thickened by the same tiny . Which covers the larger fraction of the square?

Pick one guess. The answer appears here.

Fraction of the square within of a shape

the shaperandom points within of itother random points (2500 of 20000 drawn)

Drag towards 0.001 for the point and the line. The covered fraction collapses: for the line it is about proportional to , for the point to . Halving halves the line’s fraction and quarters the point’s.

Challenge +8 XP

Select the line segment and make the covered fraction fall below 1%. Which thickness does that need?

Then switch to the triangle. Its fraction stops shrinking at the triangle’s own area, because a filled shape has an inside that thickness cannot take away. That inside is what positive measure means.

Countably many points still add up to nothing

Countably many means the points can be listed one after another. A union of countably many sets of measure zero also has measure zero, so the rationals, which can be listed this way, are negligible. This is the surprising case, since they are packed densely into every stretch of the line.

Predict first The -th rational gets an interval of length . What is the total length after all of them?

Pick one guess. The answer appears here.

List the rationals in as a first, a second, and so on. Pick any tiny number . Cover the first with an interval of length , the second with one of length , and the -th with length . Every rational is covered, and the intervals together have total length at most

The demo shows the first rationals from a list ordered by denominator.

Covering a countable list of points

a rational numberits covering interval (drawn at least 2 px wide)total length so far

Since can be as small as you like, the rationals fit inside a set of total length as small as you like, so their measure is zero. The argument is added here and is not printed in the book.

The tick marks are the rationals. The violet bars are the intervals. They shrink so fast that after a handful of them they are thinner than a pixel, and the total length climbs towards but never passes it. That is the whole reason a countable set can be ignored.

Challenge +8 XP

Cover at least 30 rationals while the total budget stays at 0.001 or less.

Going deeper: the geometric sum behind the covering (added here)

The lengths form a geometric series. Its partial sum after terms is , which is always below and tends to it as grows. That is the formula in the readout of the demo. The rest of the infinite list uses at most the difference, which is why the budget is never exceeded.

Section 3

Almost everywhere

The second term the book borrows from measure theory is almost everywhere. A property holds almost everywhere if it holds throughout the whole space except on a set of measure zero. Since the exceptions occupy a negligible amount of space, they can be ignored for many purposes.

The function used as a neuron activation has a kink at 0 and no derivative there, but it is differentiable everywhere else. This example is added here and is not drawn from the book.

Predict first A sample is drawn from a normal distribution and passed through . What is the chance it lands exactly on the kink at 0?

Pick one guess. The answer appears here.

Where does the derivative fail to exist?

, a sum of ramps, the slopekinks, where does not existsamples from a normal distribution

With one kink the function is exactly the ramp . Each added kink makes the slope jump at a new place. The readout counts how many of the 5000 random samples hit a kink exactly, and the answer is 0 every time. Samples do come close: the fraction within of a kink shrinks in proportion to , which you can watch by moving the slider.

Challenge +8 XP

Add kinks with the slider, set the distance to 0.06 or less, and make more than 10% of the samples land within that distance of a kink.

The exceptions exist, they form a countable set, and a continuous random draw never finds them. Away from the kinks the slope agrees with a numerical central difference to many digits, which is also reported.

This is the sense in which results hold “almost everywhere”: statements about nearly every point, with a negligible exception list.

Going deeper: discrete and continuous results (the book’s remark, with an added fact)

The book adds that some important results hold for all values of a discrete variable but only almost everywhere for a continuous one. One such result returns in Section 3.13: the KL divergence between two continuous distributions is zero exactly when they are equal almost everywhere. A related fact, added here, is that the probability of an event does not change when you alter a density at a few points.

Section 4

Transforming a random variable

The third technical detail concerns variables that are deterministic functions of one another. Take , with invertible, continuous and differentiable. If has density , it is tempting to plug the inverse into :

Predict first Take the naive rule for with uniform on (0, 1). What is its total area?

Pick one guess. The answer appears here.

The book says that this is not the case, and that it is a common mistake. Its example is with , the uniform distribution on the interval from 0 to 1, whose density is 1 on that interval and 0 outside. For this map the inverse is , so the naive rule reads:

Predict first Under , does a length in x become a length or in y?

Pick one guess. The answer appears here.

This equals 1 on the interval from 0 to one half and 0 elsewhere. The book then writes

(3.43)
Click any coloured symbol to see what it means.

and a probability density must integrate to 1. The error is that the naive rule ignores how distorts space. A small region of length around carries probability , and if stretches or squeezes that region, the density must change by the opposite factor.

The corrected rule

The book asks for the two tiny regions to carry equal probability:

(3.44)
Click any coloured symbol to see what it means.

Write . Divide out the -region length for the density of , and the -region length for :

(3.45)
Click any coloured symbol to see what it means.
(3.46)
Click any coloured symbol to see what it means.

Predict first For with uniform on (0, 1), what is the correct density of on the interval from 0 to one half?

Pick one guess. The answer appears here.

The only new piece beyond the naive rule is the stretch factor, shown in orange. It is the absolute value of the derivative, because lengths are positive. Where squeezes space the density of rises, and where stretches space it falls.

Going deeper: why the stretch factor comes from the derivative (added here, not printed in the book)

Begin with the equality of tiny probabilities in Equation 3.44. Write the length of a tiny region in -space as , and the matching region in -space as . Along the map, and are linked by the derivative, . The substitution turns Equation 3.44 into the corrected density, and the absolute value keeps every quantity positive. Nothing new is assumed: the rule is the change of variables for a single integral, applied to probability.

For the factor is , so on the interval from 0 to one half, which integrates to 1. The next demo compares the naive and corrected rules for four transformations against histograms of samples.

Naive rule against the corrected rule

histogram of samples of naive rule corrected rule, with the stretch factor

In every tab the histogram follows the teal curve and not the orange one. In the first tab the naive curve has the right shape but half the height, so its area is 1/2, the book’s Equation 3.43.

The areas shown in the box are computed by integrating over the values of , which is the naive density after a change of integration variable. They are exact up to numerical error, and they equal the areas of the orange curves.

Challenge +8 XP

Open the y = x² tab, show the histogram and the naive rule, and find the case where the naive curve has area 1 yet misses the histogram.

In the second tab the naive rule integrates to 1 and still fails to match the histogram: the true density is large near 0, where squaring squeezes the interval from 0 to 0.1 down to the interval from 0 to 0.01. Normalization would not have caught this error, which is a reason to prefer the corrected rule over checking the area. For the log-normal tab and the tanh tab the naive curve has the wrong area as well.

Predict first A linear map has determinant 0.5. Compared with the density of , the density of at the image point is...

Pick one guess. The answer appears here.

Section 5

The Jacobian in higher dimensions

With vectors, the stretch factor becomes a matrix. The book calls it the Jacobian matrix, the matrix of first partial derivatives of the transformation. For real-valued vectors the book’s result is Equation 3.47:

(3.47)
Click any coloured symbol to see what it means.

The matrix inside is , the derivatives of itself.

Going deeper: the book’s entries and the reciprocal relation (notation from the book, with an added remark)

In the book’s text the entries are , which describes the inverse map from to . Because the determinants of a map and of its inverse are reciprocals, one can equally use the inverse Jacobian with the density of , as in Equation 3.45. The reciprocal relation is a standard fact and is mentioned here to connect the two forms. It is not spelled out in the book.

The picture matches one dimension, with areas in place of lengths. The densities differ by the ratio of cell areas, which is the absolute value of the determinant. Pick a cell on the left to see its image on the right.

Predict first In the polar map, a cell of the same size in is taken at radius , and another at . Which has the larger image area?

Pick one guess. The answer appears here.

A tiny cell, its image, and the area ratio

x-space
y-space

the chosen cell and its imagethe whole source region and its imagerandom points, mapped to

In the linear tab the cell and its image always have the same area ratio, , wherever you place the cell, because a linear map stretches space the same way everywhere. Make the determinant small and the parallelogram thins out, and the density on it rises to so that the total stays 1.

Challenge +8 XP

In the linear tab, make |det A| below 0.1 but not zero, so the density on the image rises above 10.

Try the matrix that sets and so that the determinant is 0: the square collapses onto a line, its area is 0, and no density in the plane can describe the result. This is why the map must be invertible.

The polar tab is the case where the stretch factor changes from place to place. Points are drawn uniformly in and converted with . A cell at radius is stretched by exactly , so near the center the same probability is squeezed into very little area: the dots crowd together there.

Predict first At radius compared with radius , is the density of larger or smaller?

Pick one guess. The answer appears here.

The density in the plane is , which grows without limit as . A uniform distribution in one coordinate system is not uniform in another, and the Jacobian is the bookkeeping that tells you how.

Going deeper: the polar Jacobian determinant (added here, not printed in the book)

The derivatives of the map are the entries of the matrix below. Its determinant is , so the area of a cell in the image is times the area of the cell in -space:

What to take away

  • Probabilities come from integrating a density over a set. A density is a probability per unit volume, so it can exceed 1, and a single point has probability 0.
  • Pathological sets can break the rules, and measure theory says which sets are safe. Everyday sets such as intervals and boxes are safe.
  • A set of measure zero occupies no volume. Points, lines in the plane and countable unions of them have measure zero, and so do the rational numbers.
  • A property that holds almost everywhere fails only on a set of measure zero, which a continuous random draw never reaches.
  • If with invertible and differentiable, then . Plugging the inverse in without the stretch factor is wrong.
  • For vectors the stretch factor is the absolute value of the Jacobian determinant, and it can vary from place to place.
Final quiz

Check yourself

Questions that mix the whole module. Each one is scored, and the score feeds your mastery of this module.

Source: Ian Goodfellow, Yoshua Bengio and Aaron Courville, Deep Learning, MIT Press, 2016, Section 3.12 (pages 71–73), free to read at deeplearningbook.org. Where the book prints equation numbers, the numbers here follow the book. The order of ideas follows the book. The explanations and all demos were written for this page.