Module 4 · Location, Ranking, and Exploratory Summaries Lesson 37 of 120

Ranks, Ties, and Percentile Rank

Tied scores need a fair, explicit rank convention.

2:38 clip1:55:34–1:58:12 in the full courseWatch on YouTube

Transcript

21 sentences · select one to jump there

Check your understanding

Do the tied twos receive ranks two and three under average ties?

Choose one answer

Code lab

Run it yourself

The lesson source in 7 languages. Edit it, run TypeScript and Python right here, and compare with the expected output.

037-ranks-ties-and-percentile-rank.ts
Start from GitHub
/**
 * Fintech Math Bootcamp · Lesson 037 of 120
 * Ranks, Ties, and Percentile Rank
 * Module 04: Location, Ranking, and Exploratory Summaries
 *
 * Scenario: Tied scores need a fair, explicit rank convention
 * Rule:     average ties; percentile rank = 100(r−1)/(n−1)
 *
 * Try it:   Do the tied twos receive ranks two and three under average ties?
 *
 * Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/location-ranking-and-exploratory-summaries/ranks-ties-and-percentile-rank/
 * Free course:    https://courses.thefintechbuilder.com
 * Synthetic teaching example, not financial advice or a production library.
 */

export function lesson037() {
  const x = [1,2,2,4,9];
  const ranks = x.map(v => {
    const below = x.filter(y => y<v).length;
    const equal = x.filter(y => y===v).length;
    return below + (equal+1)/2;
  });
  const result = {ranks, percentileRanks:ranks.map(r=>100*(r-1)/(x.length-1))};
  return result;
}

export const checkedResult = {"ranks":[1,2.5,2.5,4,5],"percentileRanks":[0,37.5,37.5,75,100]};

// Run this file directly: npx tsx lessons/04-location-ranking-and-exploratory-summaries/037-ranks-ties-and-percentile-rank.ts
if (process.argv[1] && import.meta.url.endsWith(process.argv[1].replace(/\\/g, "/").split("/").pop()!)) {
  console.log(JSON.stringify(lesson037(), null, 2));
}

Your output

Press Run to execute the code in your browser.

Expected output

{
  "ranks": [
    1,
    2.5,
    2.5,
    4,
    5
  ],
  "percentileRanks": [
    0,
    37.5,
    37.5,
    75,
    100
  ]
}

Prefer your own machine? Every file is in the course repository · open it in Codespaces.

Lesson notes

The rule

average ties; percentile rank = 100(r−1)/(n−1)