Data ScienceIntermediate16 min readUpdated March 2025
Probability & Distributions
Probability is the language of uncertainty. Learn core rules, conditional probability and Bayes theorem, then meet the distributions — normal, binomial and Poisson — that model real data.
Probability Basics
The probability of an event is a number between 0 and 1. Key rules:
- Complement — P(not A) = 1 - P(A).
- Addition — P(A or B) = P(A) + P(B) - P(A and B).
- Independence — P(A and B) = P(A) x P(B) when events do not affect each other.
- Conditional — P(A | B) = P(A and B) / P(B).
Bayes' Theorem
Bayes' theorem updates a belief given new evidence: P(A | B) = P(B | A) x P(A) / P(B).
It explains why a positive result on a very accurate test can still be more likely to be a false alarm when the condition is rare.
python
# Disease affects 1% of people. Test: 95% sensitivity, 90% specificity.
p_disease = 0.01
sensitivity = 0.95 # P(+ | disease)
false_positive = 0.10 # P(+ | no disease)
p_positive = sensitivity * p_disease + false_positive * (1 - p_disease)
p_disease_given_positive = sensitivity * p_disease / p_positive
print(f"P(disease | positive) = {p_disease_given_positive:.1%}") # ~8.8%Common Distributions
Distributions describe how values are spread out:
- Normal — Bell curve, defined by mean and standard deviation; heights, measurement error.
- Binomial — Number of successes in n independent yes/no trials.
- Poisson — Count of events in a fixed interval, e.g. support tickets per hour.
- Uniform — Every value in a range equally likely.
Working with Distributions in SciPy
SciPy provides the PDF/PMF, CDF and random sampling for each distribution:
python
from scipy import stats
# Normal: P(score between 70 and 90) with mean 75, sd 10
norm = stats.norm(loc=75, scale=10)
print(norm.cdf(90) - norm.cdf(70)) # ~0.62
# Binomial: P(exactly 7 conversions out of 20 visits at 25%)
print(stats.binom.pmf(7, n=20, p=0.25)) # ~0.11
# Poisson: P(more than 5 tickets in an hour, avg 3)
print(1 - stats.poisson.cdf(5, mu=3)) # ~0.084
samples = norm.rvs(size=1000, random_state=0)
print(samples.mean(), samples.std())Key Takeaways
- Conditional probability and Bayes theorem update beliefs with evidence.
- Base rates matter — rare events produce many false positives.
- Choose a distribution that matches how the data is generated.
- SciPy gives you PDFs, CDFs and sampling for every distribution.
Contact Us
Have a question or feedback? Fill out the form below or reach us directly at support@nvaitraining.com