Library/Financial Mathematics, Statistics, and Data Foundations/Location, Ranking, and Exploratory Summaries/Ranks, Ties, and Percentile Rank

D00-F04-A07 / Complete engineering topic

Ranks, Ties, and Percentile Rank

A production-minded guide to Ranks, Ties, and Percentile Rank.

Ranks, Ties, and Percentile RankD00 / D00-F04

Synthetic teaching data · Type-7 quantiles where applicable · Python and TypeScript verification

A beginner visual guide to Ranks, Ties, and Percentile Rank

Start with the human question

How do we turn ordered positions into comparable scores when values tie? 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.

A rank labels an observation's ordered position. This package gives tied observations the average of the positions they occupy, then maps rank 1 to 0% and rank n to 100%. 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 average tied rank; percentile rank = 100(r−1)/(n−1), defined here for n ≥ 2. On the canonical synthetic values, the verified answer is ranks [1, 2.5, 2.5, 4, 5] · one duplicate beyond unique values. Keep the input count, method convention, and data-cleaning policy next to that answer.

See the data before summarizing it

Observations and the summary on one scale

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

Calculation ledger for the canonical dataset

The two values equal to 2 occupy positions 2 and 3, so each receives rank 2.5. The ranks [1, 2.5, 2.5, 4, 5] map to [0, 37.5, 37.5, 75, 100]. 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:

  1. Write the supplied values and count them.
  2. Sort a copy when the method depends on order; do not mutate the original record silently.
  3. Apply the named rule exactly, including its denominator, tie policy, interpolation method, or bin boundary.
  4. Keep enough digits to verify the result.
  5. 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?

Average, minimum, maximum, dense, and ordinal tie rules answer different questions. A ranking system must freeze one policy.

Percentile rank is not the same operation as requesting a percentile value from A06.

A correct reading beside a misleading reading

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

Use A06 for value-at-position and A07 for position-of-value. State ascending versus descending order.

Comparison map for nearby exploratory summaries

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

Canonical, surprise, symmetric, tied, and boundary scenarios

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

Plain text
validate the required finite inputs
copy the observations
apply the frozen ranks, ties, and percentile rank 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

A rank labels an observation's ordered position. This package gives tied observations the average of the positions they occupy, then maps rank 1 to 0% and rank n to 100%. For the worked case, ranks [1, 2.5, 2.5, 4, 5] · one duplicate beyond unique values. 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-A06 · Next: D00-F04-A08

Reasoning flow — D00-F04-A07

Rendering system map…
ReferencesPrimary sources and evidence notes

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.

SciPy Reference — rankdata

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.

algorithm.ts
import { runTopic as runD00Topic, type D00Input, type D00Output } from "../../../../shared/typescript/d00Engine.ts";

/** Run the canonical D00-F04-A07 calculation. */
export function ranksTiesAndPercentileRank(input: D00Input): D00Output {
  return runD00Topic("D00-F04-A07", input);
}
Full-height labguided labOpen full screen