Tutorials › Exam PA - Predictive Analytics › Empirical Cumulative Distribution Plots

Exam PA · Visualization and Anomaly Detection · Tutorial 136 of 966

Empirical Cumulative Distribution Plots

Use empirical cumulative distribution plots to read cumulative proportions and compare the locations, spreads, and tails of observed distributions.

Beginner 10 min read

What You'll Learn

  • Define the empirical cumulative distribution function for a finite sample
  • Read the proportion of observations at or below a selected value
  • Interpret jumps, ties, and plateaus on an ECDF plot
  • Compare groups using percentiles, spread, and tail behavior
  • Create and overlay ECDF plots in base R
  • Avoid confusing empirical percentiles with interpolated sample quantiles

Why Use an Empirical Cumulative Distribution Plot?

A histogram groups observations into intervals, and a density curve summarizes their distribution with a smooth shape. An empirical cumulative distribution function (ECDF) answers a different, direct question: what fraction of the observed values are at or below a specified value? This makes it useful when a question concerns thresholds, percentiles, or how two groups accumulate across the range of a numeric variable.

For example, an analyst might compare hypothetical claim-processing times for two teams and ask what fraction of each team's cases were completed within 10 days. An ECDF lets the analyst read that fraction from the plot without choosing histogram bins or a smoothing bandwidth. It describes the observations in the data; by itself, it does not establish why groups differ or what will happen in a future population.

Definition: For observed numeric values \(x_1,\ldots,x_n\), the empirical cumulative distribution function is $$ F_n(x)=\frac{1}{n}\sum_{i=1}^{n} I(x_i\le x), $$ where \(I(x_i\le x)\) equals 1 when observation \(x_i\) is at or below \(x\), and equals 0 otherwise. Thus \(F_n(x)\) is the proportion of the \(n\) observations that are at or below \(x\).

An ECDF is a nondecreasing step function. It starts at 0 to the left of the smallest observed value and reaches 1 at or above the largest. Each distinct observed value produces an upward jump. If that value occurs \(k\) times, the jump is \(k/n\); with no ties, each jump is \(1/n\). The function is right-continuous: at an observed value \(x\), the observations equal to \(x\) are included in \(F_n(x)\).

Reading the Axes and the Steps

The horizontal axis uses the original units of the variable, such as days or dollars. The vertical axis runs from 0 to 1 and gives a cumulative proportion. A point at height 0.70 means that 70% of the sample is at or below the corresponding horizontal value. Equivalently, 30% is above that value, since the two proportions sum to 1.

A steep rise over a short horizontal interval indicates that many observations lie in that interval. A long flat section indicates that few or no observations lie there. A curve that rises earlier than another tends to have more observations at or below common values in that region. A longer horizontal extent can indicate a wider observed range, while a slow approach to 1 can signal a small number of relatively large values. These are visual descriptions, not proof that one group's population distribution is different.

The ECDF avoids choices about histogram bin width and density bandwidth, but it does not make the sample infinitely informative. Its tail is determined by the largest few observations, so a single extreme value can extend the curve far to the right. Also, groups with different sample sizes have different jump sizes: a jump of \(1/5\) is not the same amount of evidence as a jump of \(1/500\).

Percentiles and the Empirical Quantile

A percentile can be read from an ECDF by finding the smallest horizontal value at which the cumulative proportion reaches or exceeds the desired fraction. For a proportion \(p\) between 0 and 1, define the empirical quantile as

$$ Q_n(p)=\inf\{x:F_n(x)\ge p\}. $$

For a sorted sample \(x_{(1)}\le \cdots \le x_{(n)}\), this definition selects the observation at rank \(\lceil np\rceil\), for \(p>0\). In particular, the empirical median under this definition is the first observed value at which the ECDF reaches at least 0.5. Other software quantile conventions may interpolate between observations, so their reported percentiles need not equal this inverse-ECDF value.

Worked Example: Reading a Threshold and Percentile

A hypothetical service unit records completion times, in days, for eight files: 4, 6, 6, 8, 10, 10, 13, and 20. Find the ECDF at 10 days, the empirical median under the inverse-ECDF definition, and the empirical 90th percentile.

Method. Use the sorted observations and count how many are at or below each requested threshold. The data are already sorted, and \(n=8\).

ECDF at 10. The observations at or below 10 are 4, 6, 6, 8, 10, and 10: there are 6. Therefore \(F_8(10)=6/8=0.75\). In this sample, 75% of files were completed within 10 days, including files completed exactly on day 10.

Empirical median. Here \(p=0.5\), so the selected rank is \(\lceil 8(0.5)\rceil=\lceil4\rceil=4\). The fourth ordered value is 8 days. Checking the ECDF, \(F_8(8)=4/8=0.5\), so the definition is satisfied.

Empirical 90th percentile. The selected rank is \(\lceil8(0.9)\rceil=\lceil7.2\rceil=8\). The eighth value is 20 days. The check is \(F_8(20)=8/8=1\), while just below 20 days only 7 observations have accumulated, giving \(7/8=0.875\), which is less than 0.9.

Common mistake avoided. Averaging the fourth and fifth values gives 9 days, a common median convention for an even-sized sample. That is not the inverse-ECDF median defined here: under this definition, the first value reaching 0.5 is 8 days. Always identify the percentile convention before comparing numerical quantiles.

Why the ECDF Gives a Proportion

The ECDF formula follows directly from counting. For each observation, the indicator \(I(x_i\le x)\) contributes one if the observation meets the threshold and zero if it does not. Summing those indicators therefore counts exactly the observations at or below \(x\). Dividing that count by the total sample size converts it to a proportion. This also explains why the ECDF cannot decrease: as \(x\) increases, an observation already counted remains counted, and additional observations may enter.

$$ \sum_{i=1}^{n} I(x_i\le x) =\#\{i:x_i\le x\} \quad\Longrightarrow\quad F_n(x)=\frac{\#\{i:x_i\le x\}}{n}. $$

This count-based interpretation is the central reading rule: locate a value on the horizontal axis, move vertically to the ECDF, and read the fraction of observations at or below it. To obtain the fraction strictly above the value, subtract from 1. For a value that is itself an observed tie, the ECDF includes every tied observation at that value.

Comparing Groups and Tails

When two groups are plotted on the same axes, compare cumulative proportions at values relevant to the analysis, as well as the locations where each curve reaches selected proportions. If one curve reaches 0.5 at a smaller value, its inverse-ECDF median is smaller. If one curve is farther to the right at a given cumulative fraction, that group requires a larger value to reach that fraction. Differences in the upper part of the curves can reveal differences among larger observations, but the number of observations supporting that part of the plot may be small.

Worked Example: Comparing Two Groups at a Threshold

A hypothetical analyst compares completion times, in days, for two teams. Team A records 2, 4, 4, 6, 8, and 10 days; Team B records 1, 3, 5, 7, 9, and 11 days. Compare their empirical medians, ranges, and proportions completed within 6 days.

Method. Each group has \(n=6\). Use the inverse-ECDF definition for the median, subtract the smallest value from the largest for the observed range, and count values at or below 6 for the threshold comparison.

Team A. The median rank is \(\lceil6(0.5)\rceil=3\), and the third value is 4 days. The range is \(10-2=8\) days. Values at or below 6 are 2, 4, 4, and 6, so \(F_A(6)=4/6\approx0.6667\).

Team B. The median rank is also 3, and the third value is 5 days. The range is \(11-1=10\) days. Values at or below 6 are 1, 3, and 5, so \(F_B(6)=3/6=0.5000\).

Interpretation and check. The observed median is one day lower for Team A, and Team A has a higher fraction completed within 6 days. Team B's observed range is two days wider. At the threshold, the counts check directly: four of six is two-thirds for A, while three of six is one-half for B. These descriptive differences do not show that team membership caused the difference.

Common mistake avoided. Comparing only the endpoints or medians would miss the threshold result. An ECDF comparison should use the question's relevant cumulative proportions as well as broad features such as location and spread.

Worked Example: Interpreting an Upper Tail

For a hypothetical set of ten repair costs, measured in thousands of dollars, the sorted observations are 1, 1, 2, 2, 3, 4, 4, 5, 8, and 20. Find the fraction costing at most 5 thousand dollars, the fraction exceeding 5 thousand dollars, and explain what the curve's final rise represents.

Method. Count observations on each side of the threshold, taking care that the ECDF includes values equal to the threshold.

There are eight observations at or below 5: 1, 1, 2, 2, 3, 4, 4, and 5. Thus \(F_{10}(5)=8/10=0.8000\). The fraction strictly above 5 is \(1-F_{10}(5)=1-0.8=0.2\), or 2 of 10 observations. Those observations are 8 and 20.

The final rise to 1 occurs at 20, the largest observed cost. The horizontal distance from 8 to 20 reflects the gap between the two largest values; it does not mean that many observations lie throughout that interval. The tail proportion is only two observations, so it is important not to treat its shape as a stable description of all future repair costs.

Check. The below-or-equal count (8) plus the strictly-above count (2) is 10, the full sample size. The two proportions, 0.8 and 0.2, sum to 1.

Constructing ECDF Plots in R

In base R, `ecdf(x)` constructs the empirical cumulative distribution function of a numeric vector, and `plot()` displays it. For a vector named `days`, use `plot(ecdf(days), verticals=TRUE, do.points=FALSE, xlab="Completion time (days)", ylab="Cumulative proportion")`. The vertical segments emphasize the jumps, and suppressing points avoids adding a marker at every step. The plotting defaults can vary with options, so explicit axis labels are useful for interpretation.

To compare two numeric vectors, such as `teamA` and `teamB`, draw the first curve with `plot(ecdf(teamA), xlim=range(c(teamA, teamB)), ylim=c(0,1), verticals=TRUE, do.points=FALSE, col="blue", xlab="Completion time (days)", ylab="Cumulative proportion")`. Add the second using `lines(ecdf(teamB), verticals=TRUE, do.points=FALSE, col="red")`, then identify the colors with `legend("bottomright", legend=c("Team A", "Team B"), col=c("blue", "red"), lty=1)`.

Before plotting, ensure the vector contains the numeric observations intended for analysis. If it contains missing values, remove or otherwise handle them explicitly and report the number of observations used; the denominator in the ECDF is the number actually included. Do not compare raw counts from curves with different sample sizes when the question asks for proportions: each ECDF is scaled by its own sample size.

Common Interpretation Traps

  • Reversing the cumulative direction. The value is the fraction at or below \(x\), not the fraction above \(x\). The strictly-above fraction is \(1-F_n(x)\).
  • Excluding ties at the threshold. The definition uses \(x_i\le x\). Observations equal to \(x\) are included in \(F_n(x)\).
  • Reading height as a count. An ECDF's vertical axis is a proportion. Multiply by the relevant sample size to recover a count, if needed.
  • Treating an ECDF percentile as automatically interpolated. The inverse-ECDF definition selects an observed value. Some software quantile conventions interpolate instead.
  • Overstating what the tail shows. The plotted tail summarizes the observed sample. A small number of large observations can dominate its appearance, so note the sample size and context.
Key takeaway: An ECDF is a step-by-step record of the fraction of observations at or below each value. Use its height to answer threshold questions, its horizontal locations to compare percentiles, and its full shape to describe how groups accumulate—while keeping sample size and the empirical nature of the evidence in view.

Check Your Understanding

For each question, use the ECDF definition and the stated inverse-ECDF percentile convention.

  1. A sample of six hypothetical inspection times, in hours, is 3, 3, 5, 7, 7, 9. Find the ECDF at 7 hours. (Answer: \(5/6\approx0.8333\))
  2. Five hypothetical payment amounts, in hundreds of dollars, are 12, 14, 14, 18, 25. What fraction is at or below 14, and what fraction is strictly above 14? (Answer: \(3/5=0.6000\) at or below; \(2/5=0.4000\) above)
  3. Group A has values 5, 6, 8, 9 and Group B has values 4, 7, 10, 11. At \(x=8\), calculate each group's ECDF and state which has the higher cumulative proportion. (Answer: A \(=3/4=0.7500\); B \(=2/4=0.5000\); Group A is higher)
  4. For the sorted values 1, 2, 2, 4, 5, 7, 9, 12, find the 75th percentile using the inverse-ECDF definition, and find the ECDF at 5. (Answer: 75th percentile \(=7\); \(F_8(5)=5/8=0.6250\))
  5. Two groups have values C: 3, 4, 5, 8, 9 and D: 2, 6, 7, 10, 12. Find each inverse-ECDF median and each ECDF at 6. (Answer: C median \(=5\), \(F_C(6)=3/5=0.6000\); D median \(=7\), \(F_D(6)=2/5=0.4000\))
Clinical Biostats