Synthetic teaching data · Type-7 quantiles where applicable · Python and TypeScript verification
Start with the human question
How can order and repetition describe center without giving every magnitude equal influence? That question is more important than the formula. A statistic is useful only when its mechanical definition matches the thing a reader is trying to learn.
The median is the middle of sorted data; for an even count it is the mean of the two middle values. A mode is any value tied for the greatest frequency. It compresses a list into a smaller description. Compression is helpful, but it always loses information. This tutorial repeatedly shows both the preserved signal and the discarded detail so that a beginner does not mistake a neat number for a complete explanation.
One-minute answer
The frozen rule is median = middle ordered value(s); mode set = {x : frequency(x) is maximal}. On the canonical synthetic values, the verified answer is median 2 · modes [2] · mode frequency 2. Keep the input count, method convention, and data-cleaning policy next to that answer.
See the data before summarizing it
Each mark in the picture is an observation. The emphasized layer is the summary produced by this topic. Notice that several different raw datasets can produce the same summary. That is not a bug; it is the price of compression. It is why a responsible report pairs a compact statistic with a distribution view or a clearly scoped statement.
The teaching fixture uses [1, 2, 2, 4, 9]. It is deliberately small enough to sort on paper and deliberately uneven enough to expose an endpoint, a tie, and a gap. The values are synthetic. They make no claim about a real asset, provider, or period.
Build the answer one line at a time
Sorted values are [1, 2, 2, 4, 9]. The middle value is 2. Value 2 occurs twice, more than any other value, so the median is 2 and the only mode is 2. The ledger uses full-precision arithmetic. Display rounding occurs only after the algorithm returns its result, so a rounded intermediate value cannot leak into the next step.
Try the same work manually:
- Write the supplied values and count them.
- Sort a copy when the method depends on order; do not mutate the original record silently.
- Apply the named rule exactly, including its denominator, tie policy, interpolation method, or bin boundary.
- Keep enough digits to verify the result.
- Explain the answer in a sentence that includes its boundary.
If your manual result disagrees with the fixture, stop before interpretation. Check the input, the method, and the policy—not only the arithmetic.
The decision contract
The implementation accepts a nonempty array of finite numbers. Topic-specific fields such as weights, trim proportion, or bin count are explicit. It rejects an undefined state instead of converting it to zero. That strictness is educational: “not computable from this input” is an important result.
The package also freezes type-7 linear interpolation for quartiles and medians where interpolation is needed. Percentile methods can differ, particularly in small samples. Naming the method makes Python, TypeScript, an article, and a future data pipeline reproduce the same answer.
What can go wrong?
A dataset can have no uniquely informative mode: every distinct value may tie, or several values may share the top frequency.
Mode does not mean the largest value, and median does not mean the halfway point between minimum and maximum.
The right-hand side of the illustration is tempting because it sounds decisive. The left-hand side is more useful because it states exactly what was calculated and what remains unknown. This habit—claiming only what the method supports—is foundational to every later financial model.
Compare nearby tools
A02 uses magnitudes; the median uses ranks. Report which center answers the actual question.
No summary wins every comparison. Magnitude-sensitive measures notice changes that rank-based measures can resist. Compact plots support group comparison but hide detail that a histogram or ECDF retains. The correct question is not “Which statistic is best?” It is “Which information must this decision preserve?”
Five scenarios that create intuition
The guided lab supplies five datasets with 41 observations each:
- Canonical: a stable, mildly skewed baseline.
- Surprise value: one endpoint moves while the other 40 observations remain fixed.
- Symmetric: changes on one side are mirrored on the other.
- Tied or flat: repeated values expose mode and rank policies.
- Boundary: constant or nearly constant data reveals degenerate ranges, bins, quartiles, and outlier rules.
Before revealing a new state, predict which output should change. Prediction converts a moving picture into an experiment. The audit panel then explains what moved, what stayed fixed, and why.
A financial-data example without a market claim
Imagine the values are five validated transaction costs in basis points. The arithmetic is identical, but the interpretation now needs units, sampling rules, timestamps, venue scope, and a missing-value policy. A summary of costs is not evidence that an execution strategy caused those costs, and it is not a forecast of the next trade.
That distinction is why the package keeps the example synthetic. The lesson transfers to prices, returns, spreads, volumes, durations, and fundamental ratios only after those data contracts are defined.
Implementation notes
The narrow Python and TypeScript topic facades delegate arithmetic to the reviewed shared D00 engine. Both consume the same JSON fixture. The implementation returns unrounded values and makes undefined denominators or empty inputs visible through errors. This package does not invent a separate browser-only definition: the playground labels its method and mirrors the canonical contract.
Pseudocode
validate the required finite inputs
copy the observations
apply the frozen median and mode rule
return the numerical result plus useful audit fields
never silently change the method or missing-value policy
Checklist for a trustworthy explanation
- Did I show the observation count and units?
- Did I name any weighting, tie, quantile, trimming, bin, or whisker convention?
- Did I keep synthetic and historical evidence clearly labeled?
- Did I show at least one dataset where the statistic gives an incomplete picture?
- Did I avoid treating a possible outlier as an automatic error?
- Can another implementation reproduce the full-precision output?
- Does my sentence claim only what this summary actually measures?
Final takeaway
The median is the middle of sorted data; for an even count it is the mean of the two middle values. A mode is any value tied for the greatest frequency. For the worked case, median 2 · modes [2] · mode frequency 2. The number becomes trustworthy only when the input, method, and limitation travel with it.
Open the guided lab next and move the surprise value. Watch which summaries react immediately and which barely move. That contrast is the fastest route from memorizing a formula to understanding it.
Previous: D00-F04-A03 · Next: D00-F04-A05
Rendered from the canonical Mermaid sources linked by this article.
Reasoning flow — D00-F04-A04
ReferencesPrimary sources and evidence notesExpand the source trail, evidence role, and limitations behind the engineering choices.
Expand the source trail, evidence role, and limitations behind the engineering choices.
Primary and official references support the mathematical definitions. The numerical example is independently derived from synthetic values and is not evidence about any market, company, or investment outcome.
NIST/SEMATECH e-Handbook — Measures of Location
- URL: https://www.itl.nist.gov/div898/handbook/eda/section3/eda351.htm
- Accessed: 2026-08-10
- Supports: mean, median, mode, trimmed mean, Winsorized mean, and robustness context.
- Limitations: Definitions still require an explicit missing-value and finite-number policy in software.
Historical-example decision
A named historical market case is not useful here. The learning objective is to expose each arithmetic step and method choice. A small synthetic dataset permits exact verification without implying that one security or date is representative.
Full dependency-light reference implementations in both supported languages.
import { runTopic as runD00Topic, type D00Input, type D00Output } from "../../../../shared/typescript/d00Engine.ts";
/** Run the canonical D00-F04-A04 calculation. */
export function medianAndMode(input: D00Input): D00Output {
return runD00Topic("D00-F04-A04", input);
}
The embedded lab now expands to its full document height, keeping the article as the only scroll surface.