STATISTICS CHEATSHEET

Comprehensive review guide with Python code examples

Shapiro-WilkLevene T-TestZ-Score ANOVAChi-Square A/B TestRegression BayesianPower Analysis

πŸ“‘ Table of Contents

πŸ“ 1. Descriptive Statistics

Measures of Central Tendency

MeasureFormulaWhen?
MeanxΜ„ = Ξ£xα΅’ / nSymmetric distributions
MedianMiddle of sorted dataSkewed data, outliers
ModeMost frequent valueCategorical data

Spread & Shape

Sample VariancesΒ² = Ξ£(xα΅’ - xΜ„)Β² / (n - 1)
Standard Deviations = √s²
Measure= 0> 0< 0
SkewnessSymmetricRight-skewedLeft-skewed
KurtosisNormalHeavy-tailedLight-tailed
Pythonimport numpy as np
from scipy import stats

data = [23, 45, 12, 67, 34, 89, 56, 78, 90, 43]
print(f"Mean:     {np.mean(data):.2f}")
print(f"Median:   {np.median(data):.2f}")
print(f"Std Dev:  {np.std(data, ddof=1):.2f}")
print(f"Skewness: {stats.skew(data):.4f}")
print(f"Kurtosis: {stats.kurtosis(data):.4f}")

πŸ”” 2. Probability Distributions

Normal Distribution

Central Limit Theorem: When nβ‰₯30, sample means are approximately normal.

68-95-99.7 Rule68% β†’ ΞΌ Β± 1Οƒ    95% β†’ ΞΌ Β± 2Οƒ    99.7% β†’ ΞΌ Β± 3Οƒ

Binomial Distribution

PMFP(X = k) = C(n,k) Β· p^k Β· (1-p)^(n-k)

Poisson Distribution

PMFP(X = k) = (Ξ»^k Β· e^(-Ξ»)) / k!
Pythonfrom scipy.stats import norm, binom, poisson

print(f"P(Z < 1.96) = {norm.cdf(1.96):.4f}")
print(f"Binom(10,0.5) P(X=6) = {binom.pmf(6, 10, 0.5):.4f}")
print(f"Poisson(3) P(X=5) = {poisson.pmf(5, 3):.4f}")

πŸ“Š 3. Z-Score

How many standard deviations a value is from the mean.

Single Valuez = (x - ΞΌ) / Οƒ
Sample Meanz = (xΜ„ - ΞΌβ‚€) / (Οƒ / √n)
zOne-tailed PTwo-tailed P
1.6450.0500.100
1.9600.0250.050
2.5760.0050.010
Pythonfrom scipy.stats import norm

x, mu, sigma = 85, 70, 10
z = (x - mu) / sigma
print(f"z = {z:.2f}")
print(f"P(Z < {z})  = {norm.cdf(z):.4f}")
print(f"P(Z > {z})  = {1 - norm.cdf(z):.4f}")
print(f"Two-tailed  = {2*(1 - norm.cdf(abs(z))):.4f}")
print(f"Critical z (Ξ±=0.05) = {norm.ppf(0.975):.4f}")

🎯 4. Confidence Intervals

Οƒ known or n β‰₯ 30CI = xΜ„ Β± z* Β· (Οƒ / √n)
Οƒ unknown and n < 30CI = xΜ„ Β± t* Β· (s / √n)
Levelz*
90%1.645
95%1.960
99%2.576
Pythonimport numpy as np
from scipy import stats

data = np.random.normal(100, 15, size=50)
se = stats.sem(data)
ci = stats.t.interval(0.95, df=len(data)-1, loc=np.mean(data), scale=se)
print(f"Mean: {np.mean(data):.2f}")
print(f"95% CI: ({ci[0]:.2f}, {ci[1]:.2f})")
Interpretation: If we repeat this 100 times, ~95 of those intervals would contain the true parameter.

βš–οΈ 5. Hypothesis Testing

Steps

  1. Hβ‚€: No effect / no difference
  2. H₁: Effect exists
  3. Set Ξ±: Usually 0.05
  4. Compute test statistic
  5. Find p-value
  6. Decision: p < Ξ± β†’ Reject Hβ‚€

Error Types

Hβ‚€ TrueHβ‚€ False
Reject Hβ‚€βŒ Type I (Ξ±)βœ… Correct (Power)
Fail to Rejectβœ… Correct❌ Type II (Ξ²)

πŸ“ˆ 6. Normality Tests

Shapiro-Wilk

Most reliable normality test (n < 5000). Hβ‚€: Data is normal. p > 0.05 β†’ Normal βœ…

Pythonfrom scipy.stats import shapiro, normaltest
import numpy as np

normal_data = np.random.normal(50, 10, 100)
skewed_data = np.random.exponential(5, 100)

stat, p = shapiro(normal_data)
print(f"Shapiro β†’ W={stat:.4f}, p={p:.4f}")
print("Normal βœ…" if p > 0.05 else "Not normal ❌")
Practical: When n > 30, CLT makes parametric tests generally safe.

βš–οΈ 7. Variance Homogeneity β€” Levene

Tests if groups have equal variances. Prerequisite for t-test and ANOVA.

Hβ‚€: σ₁² = Οƒβ‚‚Β²  |  p > 0.05 β†’ Homogeneous βœ…

TestAdvantageDisadvantage
LeveneDoesn't assume normalitySlightly less powerful
BartlettPowerful under normalitySensitive to violations
Pythonfrom scipy.stats import levene
import numpy as np

group_a = np.random.normal(50, 10, 50)
group_c = np.random.normal(48, 25, 50)

stat, p = levene(group_a, group_c)
print(f"Levene W={stat:.4f}, p={p:.4f}")
print("Homogeneous βœ…" if p > 0.05 else "Heterogeneous ❌")
If not homogeneous: Use equal_var=False (Welch) for t-test, Kruskal-Wallis for ANOVA.

πŸ”¬ 8. T-Test

One-Sample T-Test

Formulat = (xΜ„ - ΞΌβ‚€) / (s / √n),   df = n - 1
Pythonfrom scipy.stats import ttest_1samp
scores = [78, 82, 85, 90, 74, 88, 92, 79, 83, 87]
t, p = ttest_1samp(scores, popmean=80)
print(f"t={t:.4f}, p={p:.4f} β†’ {'Reject' if p<0.05 else 'Fail to reject'}")

Independent Two-Sample

Prerequisites: β‘  Normality β‘‘ Variance homogeneity β‘’ Independence

Pythonfrom scipy.stats import ttest_ind, levene
import numpy as np

drug = np.random.normal(120, 15, 30)
placebo = np.random.normal(130, 15, 30)

_, p_lev = levene(drug, placebo)
t, p = ttest_ind(drug, placebo, equal_var=(p_lev > 0.05))
print(f"t={t:.4f}, p={p:.4f}")
print("Significant βœ…" if p < 0.05 else "Not significant ❌")

Paired T-Test

Pythonfrom scipy.stats import ttest_rel
before  = [120, 135, 128, 140, 132, 145, 138, 130, 142, 136]
after   = [115, 125, 122, 130, 128, 135, 130, 120, 132, 128]
t, p = ttest_rel(before, after)
print(f"t={t:.4f}, p={p:.4f} β†’ {'Effective βœ…' if p<0.05 else 'Ineffective ❌'}")

πŸ“ 9. Z-Test

Large-sample (nβ‰₯30) version of t-test when Οƒ is known.

Formulaz = (xΜ„ - ΞΌβ‚€) / (Οƒ / √n)
Pythonimport numpy as np
from scipy.stats import norm

measurements = np.random.normal(503, 10, 50)
z = (np.mean(measurements) - 500) / (10 / np.sqrt(50))
p = 2 * (1 - norm.cdf(abs(z)))
print(f"z={z:.4f}, p={p:.4f}")
FeatureZ-TestT-Test
Οƒ known?YesNo
Samplen β‰₯ 30Any
DistributionNormal (CLT)t distribution

πŸ“Š 10. ANOVA

One-Way ANOVA

Compares 3+ group means. Hβ‚€: μ₁ = ΞΌβ‚‚ = ... = ΞΌβ‚–

F StatisticF = MSB / MSW = (Between-group variance) / (Within-group variance)
Pythonfrom scipy.stats import f_oneway
import numpy as np

a = np.random.normal(75, 8, 30)
b = np.random.normal(80, 8, 30)
c = np.random.normal(78, 8, 30)

F, p = f_oneway(a, b, c)
print(f"F={F:.4f}, p={p:.4f}")

from statsmodels.stats.multicomp import pairwise_tukeyhsd
data = np.concatenate([a, b, c])
groups = ['A']*30 + ['B']*30 + ['C']*30
print(pairwise_tukeyhsd(data, groups, alpha=0.05))

🎲 11. Chi-Square Test

Test of Independence

Is there a relationship between two categorical variables?

Pythonfrom scipy.stats import chi2_contingency, chisquare
import numpy as np

table = np.array([[50, 30], [20, 100]])
chi2, p, df, expected = chi2_contingency(table)
print(f"χ²={chi2:.4f}, p={p:.6f} β†’ {'Related' if p<0.05 else 'Independent'}")

# Goodness of fit
observed = [18, 22, 20, 25, 15]
chi2, p = chisquare(observed, f_exp=[20]*5)
print(f"χ²={chi2:.4f}, p={p:.4f} β†’ {'Biased' if p<0.05 else 'Fair'}")

πŸ“‰ 12. Correlation & Regression

|r|Interpretation
0.00 – 0.29Weak
0.30 – 0.69Moderate
0.70 – 1.00Strong
Simple Linear RegressionΕ· = Ξ²β‚€ + β₁·x    RΒ² = Explained / Total variance
Pythonfrom scipy.stats import pearsonr, spearmanr
from sklearn.linear_model import LinearRegression
import numpy as np

x = np.random.uniform(1, 10, 50)
y = 50 + 4*x + np.random.normal(0, 5, 50)

r, p = pearsonr(x, y)
rho, p2 = spearmanr(x, y)
print(f"Pearson r={r:.4f}, Spearman ρ={rho:.4f}")

model = LinearRegression().fit(x.reshape(-1,1), y)
print(f"Ε· = {model.intercept_:.2f} + {model.coef_[0]:.2f}Β·x")
print(f"RΒ² = {model.score(x.reshape(-1,1), y):.4f}")

πŸ”„ 13. Non-Parametric Tests

ParametricNon-ParametricScenario
Independent tMann-Whitney U2 independent groups
Paired tWilcoxon2 dependent groups
One-way ANOVAKruskal-Wallis3+ independent groups
Pythonfrom scipy.stats import mannwhitneyu, wilcoxon, kruskal
import numpy as np

g1 = np.random.exponential(5, 30)
g2 = np.random.exponential(8, 30)

U, p = mannwhitneyu(g1, g2, alternative='two-sided')
print(f"Mann-Whitney U={U:.0f}, p={p:.4f}")

before = [85, 90, 78, 92, 88, 76, 95, 80, 83, 89]
after  = [90, 95, 82, 96, 92, 82, 98, 86, 88, 93]
W, p2 = wilcoxon(before, after)
print(f"Wilcoxon W={W:.0f}, p={p2:.4f}")

πŸ“ 14. Effect Size

p-value: "Is there a difference?" β†’ Effect size: "How big?"

Cohen's dd = (x̄₁ - xΜ„β‚‚) / s_pooled    (0.2=Small, 0.5=Medium, 0.8=Large)
Eta-squared (ANOVA)Ξ·Β² = SS_between / SS_total    (0.01=Small, 0.06=Medium, 0.14=Large)
Pythonimport numpy as np
def cohens_d(g1, g2):
    n1, n2 = len(g1), len(g2)
    pooled = np.sqrt(((n1-1)*np.var(g1,ddof=1)+(n2-1)*np.var(g2,ddof=1))/(n1+n2-2))
    return (np.mean(g1) - np.mean(g2)) / pooled

d = cohens_d(np.random.normal(120,15,30), np.random.normal(130,15,30))
print(f"Cohen's d = {d:.4f}")

⚑ 15. Power Analysis

Done BEFORE the test. 4 components (give 3, compute 4th):

  1. Effect size (d)
  2. Ξ± (0.05)
  3. Power (0.80)
  4. Sample size (n)
Pythonfrom statsmodels.stats.power import TTestIndPower

analysis = TTestIndPower()
for d in [0.2, 0.5, 0.8]:
    n = analysis.solve_power(effect_size=d, alpha=0.05, power=0.8)
    print(f"d={d} β†’ n={n:.0f} (per group)")

πŸ§ͺ 16. A/B Testing

Workflow

  1. State hypothesis
  2. Define metric (conversion, CTR, revenue)
  3. Calculate sample size
  4. Run experiment
  5. Evaluate results
Pythonfrom statsmodels.stats.proportion import proportions_ztest, proportion_confint
from statsmodels.stats.proportion import proportion_effectsize
from statsmodels.stats.power import NormalIndPower
import numpy as np

# Control: 120/1000, Test: 145/1000
z, p = proportions_ztest([120,145], [1000,1000], alternative='smaller')
print(f"z={z:.4f}, p={p:.4f}")
if p < 0.05:
    lift = (145/1000 - 120/1000) / (120/1000) * 100
    print(f"βœ… Lift: +{lift:.1f}%")

effect = proportion_effectsize(0.10, 0.12)
n = NormalIndPower().solve_power(effect, alpha=0.05, power=0.80)
print(f"MDE 10%β†’12%: n = {n:.0f} per group")

Common Pitfalls

PitfallSolution
PeekingPre-determine n, wait
Multiple testingBonferroni: Ξ±_new = Ξ±/k
Simpson's paradoxSegment analysis
Novelty effectWait 2+ weeks

🧠 17. Bayesian Basics

Bayes' TheoremP(A|B) = P(B|A) Β· P(A) / P(B) β†’ Posterior = Likelihood Γ— Prior / Evidence
Python# Medical test: 99% accuracy, 1% prevalence
p_sick = 0.01
p_pos = 0.99 * 0.01 + 0.01 * 0.99
posterior = (0.99 * 0.01) / p_pos
print(f"P(Sick | Positive) = {posterior:.2%}")
print("β†’ Even 99% accurate test misleads with rare diseases!")

Frequentist

  • Probability = long-run frequency
  • Fixed parameter
  • p-value, confidence interval

Bayesian

  • Probability = degree of belief
  • Random parameter
  • Posterior, credible interval

πŸ—ΊοΈ 18. Which Test to Use?

WHAT IS YOUR DATA TYPE? β”‚ β”œβ”€β”€ Numerical (Continuous) β”‚ β”œβ”€β”€ 1 Group β†’ One-sample t-test β”‚ β”œβ”€β”€ 2 Groups β”‚ β”‚ β”œβ”€β”€ Independent β†’ Normal? β†’ Yes: t-test | No: Mann-Whitney U β”‚ β”‚ └── Dependent β†’ Normal? β†’ Yes: Paired t | No: Wilcoxon β”‚ └── 3+ Groups β”‚ β”œβ”€β”€ Independent β†’ Normal? β†’ Yes: ANOVA | No: Kruskal-Wallis β”‚ └── Dependent β†’ Repeated Measures ANOVA / Friedman β”‚ β”œβ”€β”€ Categorical β”‚ β”œβ”€β”€ One variable β†’ Chi-Square Goodness of Fit β”‚ └── Two variables β†’ Chi-Square Independence β”‚ └── Relationship β”œβ”€β”€ Linear? β†’ Normal? β†’ Pearson r | Spearman ρ └── Prediction? β†’ Regression (Simple / Multiple)

Quick Checklist

#StepTool
1Identify data typedf.dtypes
2Explore distributionHistogram, QQ-plot
3Test normalityshapiro()
4Check variancelevene()
5Apply testDecision tree
6Effect sizeCohen's d, Ξ·Β²
7Reportp + effect + CI
Golden Rule: p-value ALONE is not enough. Always report with effect size and confidence intervals!