liveBook · Manning

Probability distribution

As explained in Statistics Every Programmer Needs

A probability distribution specifies how probability is assigned across the possible values of a random variable.

A probability distribution assigns likelihoods to the possible values of a random variable. It turns uncertainty into something that can be calculated, plotted, and used for decisions. Choose a distribution that fits the variable and its assumptions: normal for some continuous measurements, binomial for discrete events such as coin flips, uniform for roughly equally likely outcomes, or Poisson for counts of events occurring at a steady average rate. section 3.1 section 3.0 section 9.3.1

In a Monte Carlo simulation, define distributions for uncertain inputs, sample from them repeatedly, and examine the resulting distribution of outcomes. A discrete distribution can also be converted to a cumulative distribution function (CDF): probabilities of 0.2, 0.3, and 0.5 become cumulative probabilities of 0.2, 0.5, and 1.0. These thresholds map random numbers to outcomes. Select parameters using historical data, domain knowledge, or stated assumptions; the simulation is only as useful as those choices. section 9.1 section 9.2

Plan for employee absences

Suppose daily call-center absences follow a Poisson distribution with an average rate of two employees. The probabilities of zero, one, and two absences are 13.53%, 27.07%, and 27.07%, respectively. A staffing plan based only on the average of two would miss days with more or fewer absences; simulated trials expose that range. The Poisson choice depends on the count-event assumptions, including independence and a constant average rate. section 9.3.1 section 9.3.5

>>> import numpy as np    #1
>>> import pandas as pd     #2
>>> from scipy.stats import poisson     #3
>>> lam = 2    #4
>>> k_values = np.arange(0, 9)     #5
>>> probabilities = poisson.pmf(k_values, lam)    #6
>>> probabilities_percentage = \
>>>     np.round(probabilities * 100, 2)     #7
>>> poisson_table = pd.DataFrame({'k': k_values, \
>>>   'Probability (%)': probabilities_percentage})    #8
>>> print(poisson_table)  #9
   k  Probability (%)
0  0            13.53
1  1            27.07
2  2            27.07
3  3            18.04
4  4             9.02
5  5             3.61
6  6             1.20
7  7             0.34
8  8             0.09

The Python listing calculates Poisson probabilities with SciPy, places them in a pandas table, and makes the likelihood of each absentee count visible. section 9.3.1

For a larger discrete simulation, normalize the selected probabilities so they sum to one, then pass them to `np.random.choice` with a fixed seed when reproducibility matters. Here the selected counts stop at eight, so normalization redistributes the omitted tail probability among those counts. More trials generally smooth out small-sample anomalies, but they do not validate a poorly chosen input distribution. For continuous inputs, simulations can instead produce a range of possible future stock prices rather than a single forecast. section 9.4 section 9.5

>>> np.random.seed(1)     #1
>>> num_trials = 500     #2
>>> normalized_probabilities = \
>>>     probabilities / probabilities.sum()    #3
>>> simulated_values = np.random.choice(k_values,     #4
>>>                      size = num_trials,     #5
>>>                      p = normalized_probabilities)     #6
>>> unique, counts = \
>>>     np.unique(simulated_values,     #7
>>>                 return_counts = True)   #8
>>> simulated_counts = \
>>>     dict(zip(unique, counts))       #9
>>> simulated_percentages = \
>>>     {k: (v / num_trials) * 100 for k, \
>>>     v in simulated_counts.items()}     #10

This listing shows the normalized probabilities, seeded sampling of 500 trials, and outcome-frequency calculation used to inspect a simulated distribution. section 9.4

How Statistics Every Programmer Needs covers it

How deep it goes
Develops distribution selection and parameters into CDF-based sampling and automated trials, with worked discrete probabilities and continuous-data applications.
Examples
Uses Poisson call-center absences, simulated stock prices, and uniform-versus-Benford leading-digit plots.
What it assumes
Builds on basic probability and random variables before applying distributions to more advanced methods.
What you can do after
Apply it via SciPy and NumPy to calculate discrete probabilities, sample staffing scenarios, and interpret variation in simulated outcomes.
How it connects
Links random variables and conditional probability to Monte Carlo simulation, cumulative distribution functions, and statistical inference.

Also explained in

Compare how 27 books explain probability distribution →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Statistics Every Programmer Needs. The section links point to the parts of the book it draws on.