liveBook · Manning

Sampling

As explained in Fabulous Adventures in Data Structures and Algorithms

Sampling is the process of generating a random outcome according to a probability distribution.

Sampling generates a random outcome according to a probability distribution. The distribution determines how likely each possible outcome is, whether outcomes are discrete items or real numbers. section 12.2 section 15.1

Sampling takes several forms depending on the distribution and the required result:

  • Sample discrete outcomes from a probability mass function, such as selecting colored marbles with unequal probabilities. section 12.2
  • Sample real numbers from a continuous distribution described by a probability density function; interval probabilities correspond to areas under its curve. section 15.1
  • Generate sequences by sampling an initial distribution and then sampling each transition distribution in a Markov process. section 16.6
  • Use the Metropolis algorithm to construct samples from continuous posterior distributions when a target density and relevant area information are available. section 16.0 section 16.6

Sampling a continuous distribution

For a density supported between 0 and 1, imagine throwing a dart uniformly into the shaded area beneath the PDF. The dart's x-coordinate is the sample; the chance of landing below 0.5 equals that region's area divided by the total area. section 15.1

double P(double x) => 
    0.0 <= x && x <= 1.0 ?
        Math.Pow(x, 0.5) * Math.Pow(1.0 - x, 1.5) :
        0.0;

Inspect a compact PDF implementation that returns zero outside its support and evaluates a beta-shaped density within it. section 15.1

How Fabulous Adventures in Data Structures and Algorithms covers it

How deep it goes
Develops PMFs, PDFs, Markov transitions, and Metropolis sampling, while deferring detailed algorithmic implementations for particular distributions.
Examples
Uses colored marbles, dice and simulated events, beta-shaped densities, dart-area geometry, random text, and posterior distributions.
What it assumes
Builds from probability distributions, PMFs, PDFs, priors, likelihoods, and posterior distributions.
What you can do after
Apply sampling to generate discrete outcomes, continuous values, Markov sequences, and Metropolis-based posterior samples.
How it connects
Connects probability mass functions, probability density functions, Bayesian inference, Markov processes, posterior distributions, and the Metropolis algorithm.

Also explained in

Compare how 23 books explain sampling →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Fabulous Adventures in Data Structures and Algorithms. The section links point to the parts of the book it draws on.