liveBook · Manning

Sample size

As explained in Statistics Every Programmer Needs

Sample size is the number of observations or trials included in a statistical sample or simulation.

Sample size is the number of observations or trials included in a sample, simulation, or sampling period. It is commonly denoted by n and influences the precision and reliability of statistical results. section 14.1.5 section 9.4

Sample size affects analysis in several practical ways:

  • In Monte Carlo simulation, increasing the number of trials smooths small-sample anomalies and produces more stable estimates of outcome frequencies. section 9.4
  • In quality-control charts, larger samples generally narrow control limits and improve sensitivity to small process shifts. section 14.1.5
  • Larger samples improve estimates of process parameters and increase the ability to detect genuine process variation. section 14.1.5
  • For p-charts, increasing n reduces the standard deviation of the observed proportion because variability decreases with sample size. section 14.1.3

Monte Carlo trials

A simulation can set num_trials = 500, draw outcomes from normalized probabilities, and calculate observed frequencies and percentages. Increasing the parameter to 1,000 or 10,000 trials can stabilize results, although gains eventually diminish. section 9.4

>>> 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

Shows how NumPy uses the sample size to generate repeated outcomes and summarize their frequencies. section 9.4

How Statistics Every Programmer Needs covers it

How deep it goes
Explains effects on precision, control limits, statistical power, and proportion variability, but does not derive sample-size selection rules.
Examples
Uses 500 simulated trials, scalable trial counts, control-chart samples, and p-chart proportions as concrete examples.
What you can do after
Apply it to choose trial counts, interpret control-chart sensitivity, and assess how sampling affects variability estimates.
How it connects
Connects Monte Carlo simulation, probability distributions, control charts, statistical power, standard deviation, and process variation.

Also explained in

Compare how 3 books explain sample size →

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.