MarzleyTech Learn

Home / Learn / Data analytics: from zero to job-ready (statistics, Excel, SQL, Python, Power BI) / Sampling and probability: surveys, bias, margin of error and why size matters

Sampling and probability: surveys, bias, margin of error and why size matters

You rarely have data on everyone. Pollsters ask about 2,000 people to estimate how millions will vote; a county surveys a few hundred households. Learning how sampling works helps you trust good surveys and spot bad ones.

Population and sample

  • Population: everyone or everything you want to know about (all customers, all voters, all farms in a county).
  • Sample: the smaller group you actually measure.
  • A statistic describes the sample; we use it to estimate the population.

Good and bad samples

MethodHowRisk
Simple random sampleEveryone has an equal chance of being pickedBest basic method
Stratified sampleSplit into groups (counties, age groups) and sample from eachEnsures every group is represented
Systematic samplePick every 10th customerFine unless there's a hidden pattern
Convenience sampleWhoever is easy to reachOften biased
Self-selected (voluntary)People choose to answer (online polls)Very biased: people with strong opinions answer

Types of bias

BiasExample
Selection biasSurveying only Nairobi shoppers about national habits
Non-response biasBusy people don't answer, so results over-represent people with free time
Leading questions"Don't you agree our excellent service is great?"
Survivorship biasStudying only successful businesses and ignoring the ones that closed

Probability basics

Probability is how likely something is, from 0 (impossible) to 1 (certain). Probability of an event = favourable outcomes ÷ all possible outcomes. A fair coin landing heads: 1 ÷ 2 = 0.5.

The law of large numbers: the more times you repeat a random experiment, the closer the results get to the true probability. Watch it happen:

Python · runs live in the interactive lesson
import random
random.seed(7)

for flips in [10, 100, 1000, 10000]:
    heads = sum(random.random() < 0.5 for _ in range(flips))
    print(f"{flips:>6} flips: {heads / flips:.3f} heads")

With 10 flips the share of heads can be far from 0.5; with 10,000 it's very close.

Why sample size matters

Imagine 40% of a county's farmers use certified seed. Let's draw random samples of different sizes and see how close the estimates get:

Python · runs live in the interactive lesson
import random
random.seed(1)

population = [1] * 40000 + [0] * 60000   # 1 = uses certified seed (40%)

for n in [20, 100, 500, 2000]:
    estimates = []
    for _ in range(200):
        sample = random.sample(population, n)
        estimates.append(sum(sample) / n)
    print(f"sample of {n:>4}: estimates ranged from {min(estimates):.0%} to {max(estimates):.0%}")

Small samples bounce around a lot; bigger samples cluster near the true 40%.

Margin of error (the rough rule)

Surveys report results like "52% ± 3%". For a simple random sample, a quick approximation of the 95% margin of error for a percentage is:

margin of error ≈ 1 ÷ √(sample size)

Python · runs live in the interactive lesson
for n in [100, 400, 1000, 2000, 4000]:
    moe = 1 / n ** 0.5
    print(f"sample of {n:>4}: about ±{moe:.1%}")

Notice that to halve the margin of error you need four times the sample. That's why national polls rarely survey more than a few thousand people.

Think about it: A radio station runs a text-in poll: "Should fuel prices be reduced?" 95% vote yes. Does this tell you what Kenyans think?Show answer

No. It's self-selected (only listeners who chose to text), and the question is leading (almost everyone wants cheaper fuel). It says little about public opinion on real policy choices. A random sample with a neutral question would be needed.

Summary

  • A sample estimates a population; random sampling is the foundation.
  • Watch for selection, non-response, leading-question and survivorship bias.
  • Bigger random samples give estimates closer to the truth (law of large numbers).
  • Margin of error ≈ 1 ÷ √n for percentages; quadruple the sample to halve it.

Check yourself

  1. A sample where everyone has an equal chance of being chosen is called a simple ___ sample (one word)

    Show answer

    random

  2. Probability ranges from 0 to what number?

    Show answer

    1

  3. About how many times bigger must a sample be to halve the margin of error? (a number)

    Show answer

    4

Lesson 5 of 15 in Data analytics: from zero to job-ready (statistics, Excel, SQL, Python, Power BI) · Written by · Course notes