scipy.stats.ttest_ind() gives you a p-value in one line. I wanted to know where that number comes from, so for every test here I worked it out by hand first (the standard error, the test statistic, the p-value from the distribution) and only then checked it against SciPy or statsmodels.
These are lab assignments from the Business Statistics course in the ITI Data Science diploma. The course set the questions, and the answers and code are mine.
| Notebook | What it covers |
|---|---|
Sampling, CLT and Hypothesis Testing.ipynb |
Simple random, systematic and stratified sampling. The central limit theorem by simulation. Confidence intervals with z and t. Six one-sample tests on salaries, both one- and two-tailed. |
Two-Sample Tests, Chi-Square and ANOVA.ipynb |
Comparing two proportions and two means. A paired t-test. Building the chi-square and F distributions from normal samples. Chi-square goodness of fit and independence. One-way ANOVA. |
The CLT, by simulation. I drew 10,000 samples from a uniform distribution on [1, 11] and looked at the variance of the sample mean. Theory says it should be σ²/n, with σ² = 8.33:
| Sample size n | Variance of the sample mean | σ²/n |
|---|---|---|
| 1 | 8.38 | 8.33 |
| 5 | 1.63 | 1.67 |
| 10 | 0.82 | 0.83 |
| 50 | 0.16 | 0.17 |
The histograms go from flat to bell-shaped as n grows, even though the population is flat.
Where chi-square and F come from. Squaring and adding k standard normal samples gives a chi-square distribution with k degrees of freedom, and dividing two scaled chi-squares gives an F distribution. My simulated histograms sit right on top of scipy.stats.chi2 and scipy.stats.f, and the means come out close to the theory (k for chi-square, and 1.250 vs 1.242 for F with 5 and 10 degrees of freedom).
Salary claims (764 employees, mean $90,433, standard deviation $32,865):
| Claim (H₀) | p-value | Decision at 5% |
|---|---|---|
| μ = 90,000 | 0.72 | Not rejected |
| μ = 100,000 | < 0.001 | Rejected |
| μ ≥ 92,000 | 0.094 | Not rejected |
| μ ≥ 93,000 | 0.015 | Rejected |
| μ ≤ 88,000 | 0.020 | Rejected |
| μ ≤ 90,000 | 0.36 | Not rejected |
z and t gave almost the same p-values every time. With 764 people the t distribution is practically normal.
Ad clicks (1,000 site visitors):
| Question | Result |
|---|---|
| Do women and men click ads at different rates? | 51.8% vs 48.0%. The 95% CI for the difference is −2.4 to +10.0 points, p = 0.23. No evidence of a difference. |
| Do they spend different time on the site? | A 0.6-minute difference, CI −1.4 to +2.6, p = 0.55. No. |
| Did time on site change from April to May? | p = 0.39 (Welch's t-test). No. |
| Are visits spread evenly over the months? | χ² = 15.9, p = 0.014. No, but that's mostly because the data stops on 24 July. |
| Does clicking depend on the month? | χ² = 2.0 with 6 degrees of freedom, p = 0.92. No. |
| Does time on site depend on age group? | F = 59.3, p < 0.001. Yes: about 70 minutes for 19–33, 63 for 34–47 and 55 for 48 and over. |
Going back through the notebooks I found a few mistakes, and fixed them before putting them here:
- The April vs May test wasn't really paired. The assignment asked for a paired t-test, but April and May visitors are different people, so
ttest_relwas just pairing rows by their order. I kept the original and added Welch's two-sample t-test after it. The conclusion stayed the same (p = 0.39 instead of 0.37). - Two samples were drawn with replacement. The 50% simple random sample and the stratified sample had
replace=True, so some employees could show up twice. They're now drawn without replacement. - The simulations had no seed, so the numbers changed on every run. They use
np.random.seed(42)now.
- Lots of tests on the same data. Six salary tests at 5% each means a decent chance of at least one false rejection. They were practice in setting up tests, not separate findings.
- The advertising data looks synthetic. Exactly 500 people clicked and 500 didn't, which is unlikely in real data, so I wouldn't read anything about real ad behaviour into these results.
- The age groups are my own choice. Different cut-offs would give a different F statistic.
pip install numpy pandas scipy statsmodels matplotlib seaborn jupyter
jupyter notebookOpen a notebook and run the cells in order. The data is in data/. Both datasets were supplied by the course.
Add a bootstrap version of the confidence intervals and compare them with the z and t ones, and correct the salary tests for multiple comparisons.