TExcelTAD
Home › Data Science › DS Statistics Intro

DS Statistics Intro

Statistics is the science of learning from data in the presence of variation. Riverside Bikes never rents exactly the same number of bikes twice; statistics helps us describe that variation and decide which patterns are real and which are just noise.

Two branches

Branch Question Examples
Descriptive statistics What does this data look like? mean, median, percentiles, standard deviation, charts
Inferential statistics What can we conclude beyond this data? confidence intervals, p-values, regression tests

This section covers descriptive statistics and correlation; the DS Advanced section moves into inference with regression.

Population and sample

  • The population is everything you want to know about — e.g. every day Riverside Bikes will ever operate.
  • A sample is the part you actually observed — our 60 days in spring 2024.

We compute statistics on the sample (e.g. the sample mean) to estimate parameters of the population (the true long-run mean). A good sample is representative: 60 spring days tell us little about December.

The Riverside Bikes summary

describe() is the quickest descriptive summary. We select the numeric weather and ride columns:

python · try it yourself
import numpy as np
import pandas as pd

rng = np.random.default_rng(42)
dates = pd.date_range("2024-04-01", periods=60)
temp = rng.uniform(4, 30, 60).round(1)
rain = np.where(rng.random(60) < 0.3, rng.uniform(1, 15, 60), 0).round(1)
humidity = rng.integers(35, 95, 60)
weekend = (dates.dayofweek >= 5).astype(int)
rides = (150 + 12 * temp - 8 * rain + 60 * weekend + rng.normal(0, 30, 60)).round().astype(int)
bikes = pd.DataFrame({"date": dates, "temp_c": temp, "rain_mm": rain,
                      "humidity": humidity, "weekend": weekend, "rides": rides})

print(bikes[["temp_c", "rain_mm", "humidity", "rides"]].describe().round(1))
       temp_c  rain_mm  humidity  rides
count    60.0     60.0      60.0   60.0
mean     17.5      2.2      62.0  355.4
std       7.4      4.0      19.2  108.6
min       4.2      0.0      35.0  124.0
25%      11.1      0.0      45.0  263.2
50%      17.4      0.0      57.0  364.5
75%      24.1      3.3      79.5  444.5
max      29.4     14.4      94.0  572.0
Row Meaning
count Number of non-missing values
mean Arithmetic average
std Standard deviation — typical distance from the mean
min, max Smallest and largest value
25%, 50%, 75% Percentiles (the 50 % percentile is the median)

Already we learn a lot: an average day has about 355 rides, rides vary from 124 to 572, and the median rain is 0 — more than half of the days were dry.

Measures of centre

There are three common "typical values":

  • Mean — the sum divided by the count. Uses every value, but is pulled by extremes.
  • Median — the middle value of the sorted data. Robust to extremes.
  • Mode — the most frequent value. Useful for categories.
python · try it yourself
import numpy as np
import pandas as pd

rng = np.random.default_rng(42)
dates = pd.date_range("2024-04-01", periods=60)
temp = rng.uniform(4, 30, 60).round(1)
rain = np.where(rng.random(60) < 0.3, rng.uniform(1, 15, 60), 0).round(1)
humidity = rng.integers(35, 95, 60)
weekend = (dates.dayofweek >= 5).astype(int)
rides = (150 + 12 * temp - 8 * rain + 60 * weekend + rng.normal(0, 30, 60)).round().astype(int)
bikes = pd.DataFrame({"date": dates, "temp_c": temp, "rain_mm": rain,
                      "humidity": humidity, "weekend": weekend, "rides": rides})

print("rain mean:  ", round(bikes["rain_mm"].mean(), 2))
print("rain median:", bikes["rain_mm"].median())
print("rain mode:  ", bikes["rain_mm"].mode()[0])
print()
print("rides mean:  ", round(bikes["rides"].mean(), 2))
print("rides median:", bikes["rides"].median())
rain mean:   2.21
rain median: 0.0
rain mode:   0.0

rides mean:   355.43
rides median: 364.5

For rain the three measures disagree strongly: most days are dry (median and mode 0), but a few heavy downpours lift the mean above 2 mm. For rides, mean and median are close, a sign that the values are spread fairly symmetrically.

Why one number is never enough

Two sets of days can share the same mean and still be completely different. Compare two invented stations with the same average:

python · try it yourself
import numpy as np

steady = np.array([50, 52, 48, 51, 49, 50, 50])
erratic = np.array([10, 95, 20, 88, 35, 80, 22])

for name, x in [("steady", steady), ("erratic", erratic)]:
    print(f"{name:8} mean={x.mean():.1f}  median={np.median(x):.0f}  "
          f"min={x.min()}  max={x.max()}  std={x.std():.1f}")
steady   mean=50.0  median=50  min=48  max=52  std=1.2
erratic  mean=50.0  median=35  min=10  max=95  std=33.5

Both stations average 50 rides a day, but the erratic one needs far more spare bikes on its busy days. That's why a summary always pairs a centre with a spread — the topic of the next three lessons.

Tip: Report the median alongside the mean whenever data is skewed (incomes, trip lengths, rainfall). If the two differ a lot, say so.

Test yourself

Which statistic is most affected by a single extreme value?

  1. Median
  2. Mode
  3. Mean
  4. Count

Our 60 recorded days are a ___ of all days the service will operate.

  1. population
  2. sample
  3. parameter
  4. census

Two stations both average 50 rides/day. What else do you need to compare them?

  1. Nothing, they are the same
  2. A measure of spread such as the standard deviation
  3. The mode only
  4. The station names