# Distributions and Summary Statistics

> How to read the centre and spread of a set of numbers, and one value's relative position, using the mean, median, percentile, standard deviation, z-score, and log transformation.

A number is not high or low on its own. You can describe its position only by also looking at **where the values used for comparison cluster and how widely they spread**. The overall shape made by multiple values is called a **distribution**.

## Place one response time among 100 requests

Suppose you sort the response times of 100 API requests from smallest to largest. If 95 requests are faster than a particular request, that request is at the 95th percentile. It took longer than 95% of all requests.

A percentile does not assume a particular distribution shape. It cannot, however, tell you how many milliseconds separate the 95th and 99th percentiles.

## Mean and median as measures of the centre

For response times of `90, 95, 100, 105, 610 ms`, the mean is `200 ms` and the median is `100 ms`. One slow request pulls the mean upward, while the middle value, the median, better represents a typical request.

The mean uses every value, so it is useful for calculating an overall total or scale. The median is less affected by extreme values, so it is useful for describing the centre of a distribution with a long tail. Neither is always better; choose the one that fits the question.

## Standard deviation describes spread

Two servers can both have a mean response time of `100 ms` yet differ in stability. If the first server's values cluster between `95 and 105 ms`, while the second server's values range from `20 to 300 ms`, the mean alone cannot show the difference.

**Standard deviation** summarizes how widely values spread around the mean. It is small when values cluster tightly and large when they are widely dispersed. Because it is affected by extreme values and skewed distributions, it is usually best to inspect the distribution's shape with a histogram or box plot as well.

## A z-score is the number of standard deviations from the mean

A z-score expresses the difference between a value and the mean in units of standard deviation.

$$
z = \frac{x-\mu}{\sigma}
$$

Here, $x$ is the value to inspect, $\mu$ is the comparison group's mean, and $\sigma$ is the standard deviation. In a group with a mean of `100 ms` and a standard deviation of `20 ms`, `140 ms` has a z-score of 2. It sits two standard deviations above the mean.

A large z-score does not reveal the cause of that value. If the distribution is heavily skewed or contains many extreme values, the mean and standard deviation themselves may not be stable reference points.

## Ratios and log2

A ratio can express the relative sizes of two values. If the new version's throughput is four times that of the old version, the fold change is 4. A base-2 logarithm is commonly used to make increases and decreases symmetric.

| log2 fold change | Actual ratio |
| --- | --- |
| -2 | 1/4 as large |
| -1 | 1/2 as large |
| 0 | Equal |
| 1 | 2 times as large |
| 2 | 4 times as large |

A logarithm compresses gaps between large values. Plotted directly, the values `1, 10, 100, 1,000` are dominated by the largest values. Taking log10 produces `0, 1, 2, 3`, placing multiplicative differences at equal intervals.

The logarithm of 0 is undefined, so a small value is sometimes added, as in `log2(x + 1)`. The `+1` is not data but a choice that makes the calculation possible, so record what value was added.

## Correlation describes how values move together

Suppose you measure server load and response time at the same moments. If response time rises as load rises and the scatter-plot points gather along an upward-right direction, the two values have a positive correlation.

A high correlation does not mean the values are equal or that one causes the other. If one measurement is always twice the other, their correlation can still be high because they move in the same order. A common cause may also be moving both values together.

## Summary

A percentile gives a position in an ordered list. The mean and median describe the centre, standard deviation describes spread, and a z-score gives a standardized distance from the mean. Ratios and logarithms make relative size easier to read, while correlation summarizes how two values move together. Each statistic answers a different question, so do not judge a distribution from a single number alone.