Code › codeit-ai-sprint
Reading Data Distributions with Means and Quartiles
Reviewing how descriptive statistics and visualizations explain center, spread, and distribution shape
I used a separate Codeit course to review introductory statistics and data visualization. The coding exercises are still in progress, so this note stays with the theory: means, medians, quartiles, IQR, probability density, KDE, skewness, kurtosis, and box plots.
None of the individual calculations is especially complicated. The important part is knowing which feature of the data each number describes. A mean alone cannot explain a dataset. Center, spread, and distribution shape need to be read together.
Population, sample, and random variable
Separating the basic terms makes the later distinction between an average and a probability density function much easier.
Suppose the subject of a study is the height of adult men in South Korea. Every adult man in that group forms the population. Measuring the whole population is usually impractical, so the study selects a smaller group. That selected group is the sample.
The measured heights in the sample, such as 172.3 cm and 168.7 cm, are observations. A value that describes the population, such as its true mean height, is a parameter. A value calculated from the observations, such as a sample mean or sample median, is a statistic. In practice, sample statistics provide evidence for estimating unknown population parameters.
A random variable connects this setup to probability. Imagine choosing one person from the population at random. The selected person’s height depends on who was chosen. The rule that maps that random outcome to a number is a random variable, which can be written as X.
Despite its name, a random variable is mathematically a function. It maps the result of a random process to a number. A coin toss can map heads to 1 and tails to 0. A random selection from the population can map the selected person to that person’s height. The output changes with the random outcome, which is why the term variable is still useful.
Random variables can be divided by the values they produce.
| Type | Values | Examples |
|---|---|---|
| Discrete random variable | Countable values | Daily visitor count, number of defects, die result |
| Continuous random variable | Values over a continuous interval | Height, weight, time, temperature |
Height is modeled as a continuous random variable because there are possible values between 170 cm and 170.1 cm. The probability density function describes how densely the possible values of X are distributed across different regions.
X and its density function, written as , are not the same function.
| Term | Input | Output | Role |
|---|---|---|---|
| Random variable X | A random outcome | The selected person’s height | Converts an outcome into a number for analysis |
| Density function | A possible height x | Probability density at x | Describes where values are concentrated |
Both are functions, but their inputs and responsibilities differ. The random variable produces the number to analyze. The density function describes the distribution of those numbers.
Describing the center
Descriptive statistics summarize collected data with a small set of numbers and charts. They help identify the center of a dataset, its spread, its asymmetry, and unusually distant observations.
The most familiar measure of center is the mean. It is the sum of all values divided by the number of observations.
The mean uses every observation, which also makes it sensitive to extreme values. If most salaries are similar but a few are exceptionally high, the mean can rise above the amount earned by most people in the dataset.
The median is the middle value after sorting the observations. When there is an even number of observations, it is the mean of the two central values. Because it depends on order rather than the magnitude of every value, it is less sensitive to extremes than the mean.
Categorical data often needs a different measure. Blood types and product categories have no numerical distance or natural middle, so addition and averaging are not meaningful. The mode, the most frequent value, works for these categories.
| Data type | Common measure of center | Examples |
|---|---|---|
| Numerical | Mean, median | Height, price, test score |
| Ordered categorical | Median category, mode | Satisfaction rating, letter grade |
| Unordered categorical | Mode | Blood type, product category |
Ordered categories such as A, B, and C have a meaningful rank, so a middle category can be identified. Their intervals are not guaranteed to be equal, though. An average calculated after assigning arbitrary numbers to the categories depends on the encoding and may not preserve the original meaning.
Quartiles and IQR
Sorting the data and dividing it into four sections gives the quartiles. The first quartile, Q1, marks the point at which about 25 percent of observations are at or below that value. Q2 is the 50 percent point and equals the median. Q3 marks the 75 percent point.
The minimum and maximum describe the full observed range. Q1 and Q3 describe the region containing the middle half of the data. Their difference is the interquartile range, or IQR.
IQR measures spread without depending heavily on extreme observations. A small IQR means that the middle 50 percent is concentrated in a narrow interval. A large IQR means that it is spread across a wider interval.
A common rule uses the following fences to identify outlier candidates.
Observations outside those fences are marked as potential outliers. The rule does not prove that they are errors. A traffic surge or sudden increase in sales may be rare and still be a real, important event. The source of the data needs to be checked before removing such observations.
Why probability at one point is zero
Let X represent the height of one randomly selected adult man from the population. Under a continuous model, the probability that X is exactly 170 cm is zero. Exactly 170 cm here means one infinitely precise value, not a narrow interval such as 169.999 cm to 170.001 cm.
For a continuous distribution, the height of the density function is not itself a probability. Probability is the area under the curve over an interval. A single point has zero width, so it contributes zero area.
Probability zero does not mean that the value is logically impossible. It means that one exact point occupies no area among infinitely many continuous values. Real measurements also have limited precision. A person recorded as 170 cm usually belongs to a range that was rounded to 170.
If height was rounded to the nearest centimeter, the relevant event would be an interval such as:
The same distinction applies to IQR fences. The analysis is not interested in the probability of landing exactly on the fence. It examines observations in the ranges below the lower fence or above the upper fence. More importantly, the 1.5 IQR rule is a descriptive convention derived from sample quartiles, not a probability boundary derived from a particular probability distribution.
Histograms and KDE
A histogram divides the observed values into bins and displays how many observations fall into each one. It quickly reveals where values are concentrated, but its shape changes with the number and width of the bins.
Kernel density estimation, or KDE, places a small curve around every observation and combines those curves into a smooth density estimate. It removes the hard bin boundaries of a histogram, but the vertical value of a KDE curve is still density rather than point probability. Probability comes from the area under the curve over an interval.
The bandwidth controls how strongly the estimate is smoothed. A bandwidth that is too small follows local fluctuations and creates a jagged curve. A bandwidth that is too large can merge distinct features into one broad shape. A smooth curve is not automatically an accurate explanation of the data, so the KDE should be checked alongside the histogram.
Skewness and kurtosis
Skewness describes asymmetry. A distribution with a longer right tail has positive skewness, while one with a longer left tail has negative skewness.
Large values in a right tail pull the mean in that direction, so the mean is often greater than the median. A long left tail can produce the opposite relationship. This is a useful clue when reading a distribution, but it is not a formula that guarantees the ordering for every possible dataset.
Kurtosis is often introduced as the sharpness of a peak. More precisely, it reflects how much weight the distribution places far from its mean and in its tails. Higher kurtosis can indicate a greater influence from extreme values and heavier tails.
Definitions also differ by convention. Ordinary kurtosis assigns the normal distribution a value of 3. Excess kurtosis subtracts 3, giving the normal distribution a value of 0. Any reported kurtosis value needs to be interpreted with its convention.
What a box plot displays
A box plot summarizes quartiles and outlier candidates in one figure. The lower edge of the box is Q1, the line inside the box is the median Q2, and the upper edge is Q3. The height of the box is the IQR.
The lines extending from the box are the whiskers. Under the common 1.5 IQR rule, they do not necessarily reach the minimum and maximum. Each whisker reaches the most extreme observed value that still lies inside its fence. Observations beyond the fences are drawn separately as potential outliers.
outlier lower whisker Q1 median Q3 upper whisker outlier
* |------------[----------|----------]------------| *
<---------- IQR ---------->
A box plot cannot reveal every feature of a distribution. Different datasets can share the same quartiles and produce similar box plots while having different numbers of peaks or gaps. A histogram or KDE provides the missing shape information.
This review made the selection criterion more important than any individual calculation. The mean uses every value but reacts strongly to extremes. The median and IQR use order to summarize center and spread. Histograms and KDE reveal shapes hidden by summary numbers, while box plots compress quartiles and outlier candidates into a compact view.
When I use Seaborn for the exercises, I want to check more than the visual appearance: whether the vertical axis represents counts or density, why a point was marked as an outlier candidate, and what caused the mean and median to separate.