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






















