# Descriptive Statistics

Summarising a dataset's central tendency, spread and shape before any inference — means, medians, quantiles, standard deviations and the distributional picture behind them.

**Leads to:** Data Mining, Linear Regression, Mixed-Effects Models, Pooling as a Statistic, Psychometrics and Traits, Q-Q Plots and Normality, Scatterplots and Relationships, Speech Emotion Recognition, Sufficient Statistics

**Rests on nothing.** A place to start.

*Mathematical Foundations · Statistical Inference*


## Why learn this

This is the first step in using data to make decisions. Everything else in statistics—tests, models, even machine learning—starts with understanding what your data looks like. By the end of this page, you'll be able to summarize any list of numbers (like exam scores, product ratings, or sensor readings) with a few key figures: the typical value, how spread out the values are, and whether there are any unusual extremes. This is the foundation for later concepts like hypothesis testing, cross-validation, and even understanding how AI models evaluate their own performance. Without descriptive statistics, you're just staring at a wall of numbers.


## The intuition

Imagine you're handed a list of 10,000 numbers: the daily sales figures for a shop over about 27 years. You can't look at every single number and conclude anything. You need a way to compress all that information into a few digestible facts. That's descriptive statistics. It's the art of summarising a dataset's central tendency (where the 'middle' is), spread (how far from the middle things get), and shape (is it symmetric, or does it lean one way). It tells you the story of the data without reading every line. The mean (average) tells you the typical sale, but it doesn't tell you if some days are wildly different. The standard deviation tells you how much sales typically fluctuate. The median tells you the middle point, which is more robust if a few days were extraordinary. So you walk away with a mental picture: 'Sales average ₹50,000, typically swing by ₹10,000, and there's one weird day where someone bought a hundred refrigerators.' That's the whole point—turning chaos into insight.


## An analogy

Think of a restaurant owner trying to understand how many customers come in each day. They scrawl the day's count on a calendar. After a month, they have 30 numbers. To make sense of it, they could do a few things. First, they could find the average: 'About 80 customers a day.' That's the mean. But they might also notice that most days are around 60-70, and only on weekends it jumps to 150. The average of 80 is pulled up by those busy weekends. So they also look at the median—the middle count when they line up all 30 days in order. That's 65. 'Ah, the typical day is 65 customers,' they say. Then they want to know how much variation there is. Are there quiet days with 20 and packed days with 200? The standard deviation tells them, 'On average, a day's count is about 25 customers away from the mean.' That's the spread. Now, the owner also wants to know if there are any weird days—maybe the day they were closed for renovation (0 customers) or the day of a festival (300). These are outliers, and the median is more resilient to them than the mean. Where does the analogy break down? The owner is doing this on a small scale with simple arithmetic. A real dataset might have millions of rows, and we use software to do exactly the same calculations. But the intuition—the compression of a big pile of numbers into a few meaningful figures—is exactly what descriptive statistics is.


## Definition

Descriptive statistics is the practice of summarising a dataset—its central tendency, spread, and shape—into a small set of numbers that describe the whole, without making inferences about a larger population.


## How it connects

This concept is the first rung on the ladder of statistical inference. You have no prior knowledge to build on, so we start from scratch. Once you master descriptive statistics, you'll move on to its parent concept: Statistical Inference. That's where you use a sample to say something about a whole population, with a measure of how confident you are. Descriptive statistics is the observation part; inference is the conclusion part. For example, you might use descriptive statistics on a sample of 100 users to find their average session length, then use inference to claim 'all users on average have a session length in this range.' Your library also lists neighboring topics like Sufficient Statistics and Cross-Validation Partitions. The idea of a sufficient statistic is that a well-chosen summary (like the mean) captures all the useful information in the data—that's a formal extension of what you'll learn here. Cross-validation, used to evaluate machine learning models, relies on partitioning a dataset and computing statistics (like accuracy) on each fold. So this basic toolkit is the precursor to everything else.


## The mathematics

Descriptive statistics uses only basic arithmetic: adding, subtracting, multiplying, dividing, and ordering numbers. You do not need algebra, calculus, or advanced notation to understand these concepts. We use simple labels for the steps so we can talk about them clearly.

**The Mean (Average)**
The mean is the balance point of your data. To find it, you add all the numbers together and then divide by how many numbers there are.

Let n be the count of numbers in your list.
Let x1, x2, ..., xn be the individual numbers in your list. We read 'x sub i' as 'the i-th number'.

The formula for the mean (often called mu, or μ) is:
μ = (x1 + x2 + ... + xn) divided by n

In words: Sum all the values, then divide by the total count.

**The Median**
The median is the middle value when the data is sorted. It tells you what a 'typical' value looks like without being pulled up or down by extreme outliers.

1. Sort your list of numbers from smallest to largest.
2. If n (the count) is odd, the median is the number exactly in the middle. Its position is (n + 1) divided by 2.
3. If n is even, there is no single middle number. The median is the average of the two middle numbers. Their positions are n divided by 2, and (n divided by 2) plus 1.

**The Variance**
Variance measures how spread out the data is. It tells you, on average, how far each number is from the mean. Because distances can be negative, we square them to make them positive before averaging.

There are two versions of variance depending on your goal:
1. Population Variance: Use this if your list contains *every* member of the group you care about (the entire population). Divide by n.
2. Sample Variance: Use this if your list is just a *sample* taken from a larger group, and you want to estimate the spread of that larger group. Divide by (n - 1). This adjustment makes the estimate more accurate for the larger group.

The steps to calculate variance are:
1. Find the mean (μ).
2. For each number xi, find its difference from the mean: (xi - μ).
3. Square that difference: (xi - μ) squared.
4. Add up all these squared differences.
5. Divide by n (for population) or (n - 1) (for sample).

**The Standard Deviation**
The standard deviation is the square root of the variance. We take the square root to bring the number back into the same units as your original data. If your data is in rupees, the variance is in 'rupees squared' (which is hard to interpret), but the standard deviation is back in 'rupees'.

**Quantiles and Percentiles**
Quantiles split your sorted data into equal parts.
- The median is the 50th percentile. Half the data is below this value, half is above.
- Quartiles split the data into four parts. The first quartile (Q1) is the 25th percentile. The third quartile (Q3) is the 75th percentile.

To find a specific percentile:
1. Sort the data.
2. Calculate the position index: (percentile / 100) * n.
3. If the result is a whole number, average that value and the next one. If it is not a whole number, round up to the next integer and take that value from the sorted list.

**Skewness**
Skewness describes the shape of the data distribution. You can get a rough idea by comparing the mean and the median.
- If Mean > Median, the data is skewed right (has a long tail on the high end).
- If Mean < Median, the data is skewed left (has a long tail on the low end).
- If Mean ≈ Median, the data is roughly symmetric.


## A worked example

Let us walk through the calculations for a small tea stall's daily sales over 7 days.

**The Data:**
The sales in rupees are: [400, 500, 600, 450, 700, 300, 550].
Here, n = 7 (there are 7 days).

**Step 1: Calculate the Mean**
First, add all the sales together:
400 + 500 + 600 + 450 + 700 + 300 + 550 = 3500.

Next, divide by the count (n=7):
Mean (μ) = 3500 / 7 = 500.
The average daily sale is ₹500.

**Step 2: Calculate the Median**
First, sort the sales from smallest to largest:
[300, 400, 450, 500, 550, 600, 700].

Since n=7 is an odd number, the median is the middle value.
The position of the middle value is (7 + 1) / 2 = 4.
The 4th number in the sorted list is 500.
Median = ₹500.

**Step 3: Calculate the Variance**
We will calculate the population variance (dividing by n=7) because we have the data for all 7 days of interest.

1. Find the difference from the mean (xi - μ) for each day:
   Day 1: 400 - 500 = -100
   Day 2: 500 - 500 = 0
   Day 3: 600 - 500 = 100
   Day 4: 450 - 500 = -50
   Day 5: 700 - 500 = 200
   Day 6: 300 - 500 = -200
   Day 7: 550 - 500 = 50

2. Square each difference:
   (-100)² = 10,000
   (0)² = 0
   (100)² = 10,000
   (-50)² = 2,500
   (200)² = 40,000
   (-200)² = 40,000
   (50)² = 2,500

3. Sum the squared differences:
   10,000 + 0 + 10,000 + 2,500 + 40,000 + 40,000 + 2,500 = 105,000.

4. Divide by n (for population variance):
   Variance (σ²) = 105,000 / 7 = 15,000.

**Step 4: Calculate the Standard Deviation**
Take the square root of the variance:
Standard Deviation (σ) = √15,000 ≈ 122.47.

**Result Interpretation:**
The typical daily sale is ₹500. The standard deviation of approximately ₹122.47 tells us that on a typical day, the sales deviate from the average by about ₹122. This gives you a sense of the volatility of the business.


## From scratch, in Python

```python
# First, we need to store our data. Python uses lists: [ ]
sales = [400, 500, 600, 450, 700, 300, 550]  # a list of numbers

# We need a few basic operations: sum, division, sorting, squaring.

# Function to compute the mean (average)
def mean(data):
    total = 0
    for value in data:
        total = total + value
    return total / len(data)

# Function to compute the median
def median(data):
    sorted_data = sorted(data)  # sorts a copy, doesn't change original
    n = len(sorted_data)
    if n % 2 == 1:
        # odd length, middle index
        return sorted_data[n // 2]
    else:
        # even length, average of two middle values
        mid1 = sorted_data[n // 2 - 1]
        mid2 = sorted_data[n // 2]
        return (mid1 + mid2) / 2

# Function to compute the standard deviation
def std_dev(data):
    avg = mean(data)
    squared_diffs = []
    for value in data:
        diff = value - avg
        squared_diffs.append(diff * diff)  # square it
    variance = sum(squared_diffs) / len(data)
    return variance ** 0.5  # square root

# Let's compute
average = mean(sales)
middle = median(sales)
sd = std_dev(sales)

print("Mean:", average)
print("Median:", middle)
print("Standard Deviation:", sd)
```


## The same thing, with a framework

```python
# The same thing using NumPy, which is a library for fast numerical operations.
# Under the hood, numpy.mean() does the same sum-and-divide loop you wrote above.

import numpy as np

sales = np.array([400, 500, 600, 450, 700, 300, 550])

mean_np = np.mean(sales)
median_np = np.median(sales)
std_np = np.std(sales)

print("Mean:", mean_np)
print("Median:", median_np)
print("Standard Deviation:", std_np)
```


## What the code does

The from-scratch code starts with a list `sales`—that's a Python list, a container that holds items in order, and you can change it. We then define three functions. 

- `mean(data)`: We set `total` to 0, then loop over each `value` in `data`, adding it to `total`. That's the sum. Then we divide by `len(data)` (which gives the count). This is exactly the same as doing it by hand.
- `median(data)`: We use `sorted(data)` to get a new list with the numbers in increasing order. Then we check if the count is odd or even using `n % 2` (remainder when dividing by 2). If odd, we take the middle element. If even, we take the average of the two middle ones. 
- `std_dev(data)`: We compute the mean, then for each value we make `squared_diffs` list containing `(value - avg)^2`, then we sum all those, divide by `len(data)` (population), and take the square root via `** 0.5`.

Finally, we print the results. The NumPy version does the same thing in one line each. Under the hood, `np.mean` is a compiled loop that adds up all the numbers and divides—same math, faster for huge datasets. `np.std` also does the same mean-subtract-square-average-root process.


## Common misconceptions

**Mean, median, and mode are all the same** — They all tell me the average, so I can use mean all the time.

The mean is sensitive to outliers—a single extremely high value can pull it up, making it unrepresentative. The median resists this. The mode is the most frequent value and can be very different from both. In fact: These are three different summaries of 'typical'. The mean is the arithmetic average, the median is the middle value when sorted, and the mode is the most common value. Choose based on your data and what question you're answering.

**Standard deviation is the average deviation from the mean** — I can just find the average of the absolute differences from the mean, no need to square them.

The standard deviation is the square root of the average squared deviation, not the average absolute deviation. Squaring gives more weight to large deviations, which is mathematically useful (it's related to variance, which has nice properties) and also necessary for the algebra to work out in later statistics. In fact: The standard deviation is defined as the square root of the average of the squared differences from the mean. It's not the same as the mean absolute deviation. It's a specific measure of spread that is used throughout statistics because of its mathematical properties.

**Bias is a bad thing in statistics** — The word 'bias' always means someone is being unfair or the model is prejudiced.

In statistics, bias is a technical term for the difference between an estimator's expected value and the true value. It's not about fairness. A biased estimator can still be useful (e.g., a ridge regression estimator is biased but has lower variance). In fact: In statistics, bias refers to the systematic error in an estimate. It's a property of an estimator, not a moral judgment. It's unrelated to demographic fairness, though both are important in different contexts.

**More data is always better, so big samples need no statistics** — With millions of data points, I can just look at the raw data and understand it.

The human brain can't process millions of numbers. Even with big data, you need to summarise it to make sense of it. Descriptive statistics are even more critical as data grows—you can't eyeball it. In fact: Descriptive statistics are a way to compress information, not a crutch for small data. With big data, you absolutely need statistics to understand central tendency and spread.

**One summary number tells the whole story** — If I know the average, I know everything about the data.

The mean can be the same for completely different datasets (e.g., Anscombe's quartet). Summary statistics can hide the shape of the data. Always plot your data—a histogram or scatterplot reveals clusters, outliers, and patterns that a single number cannot. In fact: Always compute multiple summaries (mean, median, SD, quantiles) and, importantly, plot the data. The summary numbers are a starting point, not the whole story.

**Median and mean are always close** — The median is usually close to the mean, so I can just compute one.

In skewed distributions (e.g., income data), the mean is pulled toward the long tail, while the median stays at the center. They can differ substantially. In fact: Compare the mean and median. If they differ greatly, it's a sign of skew or outliers. Use the median as a more robust measure of central tendency in such cases.


## In short

Descriptive statistics is about summarising data: the mean and median for central tendency, the standard deviation for spread, and quantiles for shape. The median and IQR resist outliers, while the mean and standard deviation do not. Always plot your data—a picture often reveals what a single number hides. Choosing the right summary statistic is already a modelling decision; there is no one-size-fits-all.


## Where this is used

Descriptive statistics is the foundation of every data-driven decision, including in AI systems. It's so fundamental that it's often implicit, but a data scientist or ML engineer is computing these numbers constantly.

**Large Language Models (LLMs):** Before training a model, you need to understand the corpus of text you're using. That means computing statistics on token lengths, word frequencies, and sequence lengths. The mean token length per document tells you how to pad sequences for batching. The standard deviation of sequence lengths helps you determine how much padding to add—too much padding wastes compute, too little breaks training. When analyzing model outputs, you might compute the mean and standard deviation of sentence lengths in generated text to detect degenerate repetition. A model that generates the same 50 tokens might have a very low standard deviation in sequence-to-sequence similarity.

**Small Language Models (on devices):** When you compress a large model to run on a phone or laptop, you need to evaluate how its predictions degrade. You compute the mean absolute error (a descriptive statistic) between the original model's outputs and the compressed model's outputs across a test set. The standard deviation of that error tells you if the degradation is consistent or sporadic. A high standard deviation suggests some inputs are being approximated very poorly—you might need to adjust the quantization thresholds.

**Retrieval-Augmented Generation (RAG):** RAG systems retrieve chunks of text and feed them to an LLM. A critical preprocessing step is to compute the distribution of embedding distances. You compute the mean and standard deviation of the cosine similarity between queries and retrieved chunks. This lets you set a threshold for what counts as 'relevant'. If you set the threshold too high, you retrieve too little; too low, you retrieve noise. Descriptive statistics of the retrieval scores tell you where to cut.

**Agentic Systems (planning, tool use):** An agent might make a series of decisions based on observations. To evaluate its performance, you track metrics like the number of tools called per task, the timing, and the success rate. The mean and standard deviation of these metrics across many runs give you a sense of the agent's typical behavior and variance. If the standard deviation of tool calls is high, the agent might be erratic, sometimes calling ten tools, sometimes one—that's a diagnostic clue.

**Everyday Applied ML (recommendation, forecasting, fraud detection):** 
- *Recommendation:* When building a recommender, you look at the distribution of user ratings. The mean rating of an item is a baseline predictor. But you also look at the standard deviation of ratings to see if there's consensus or disagreement. An item with mean 4.5 and low SD is universally liked; an item with mean 4.5 and high SD is polarizing.
- *Forecasting:* For time-series, you compute the mean and variance of the target variable to decide on preprocessing. A non-stationary series (one whose mean shifts over time) needs differencing—you detect that by comparing the mean of early windows to late windows.
- *Fraud detection:* You profile features like transaction amount. The mean and standard deviation of transaction amounts per user help establish a baseline; a new transaction that is 5 standard deviations from the user's mean is flagged as an anomaly. That's exactly using descriptive statistics to set a threshold.
- *Vision:* When normalizing images for a model, you compute the mean and standard deviation of pixel values across the training set, then subtract and divide by them. That's why you often see 'normalize with mean [0.485, 0.456, 0.406] and std [0.229, 0.224, 0.225]' in computer vision—those numbers come from descriptive statistics on ImageNet.
- *Speech:* You compute the mean and standard deviation of audio amplitudes to normalize volume across samples.

In practice, a developer reaches for `pandas` in Python to compute these: `df['column'].mean()`, `df['column'].median()`, `df['column'].std()`. They might write a line like `print(df.describe())` which outputs all of these at once. What breaks when done badly? If you compute the mean on data with outliers, your model's normalization will be skewed, leading to poor performance. If you ignore the standard deviation, you might not detect that your data is highly variable and needs more robust methods like median-based normalization. A common mistake is computing the mean on the entire dataset including the test set, which leaks information—a violation of proper evaluation. So descriptive statistics is not just a nice-to-have; it's a critical first step in any data pipeline.


---

Read this on the web: https://basavapurushottam.com/learn-ai/descriptive-statistics
