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

Frequency Tables and Relative Frequency

Turning payment status counts into a denominator-aware summary.

2:39 clip1:58:12–2:00:52 in the full courseWatch on YouTube

Transcript

18 sentences · select one to jump there

Check your understanding

Must shares sum to 100% when a payment can have several labels?

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.

038-frequency-tables-and-relative-frequency.ts
Start from GitHub
/**
 * Fintech Math Bootcamp · Lesson 038 of 120
 * Frequency Tables and Relative Frequency
 * Module 04: Location, Ranking, and Exploratory Summaries
 *
 * Scenario: Turning payment status counts into a denominator-aware summary
 * Rule:     relative frequency = category count / total count
 *
 * Try it:   Must shares sum to 100% when a payment can have several labels?
 *
 * Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/location-ranking-and-exploratory-summaries/frequency-tables-and-relative-frequency/
 * Free course:    https://courses.thefintechbuilder.com
 * Synthetic teaching example, not financial advice or a production library.
 */

export function lesson038() {
  const statuses = ["ok","ok","review","ok","failed"];
  const counts = new Map<string,number>();
  for (const s of statuses) counts.set(s,(counts.get(s)??0)+1);
  const result = [...counts].map(([status,count]) =>
    ({status,count,frequency:count/statuses.length}));
  return result;
}

export const checkedResult = [{"status":"ok","count":3,"frequency":0.6},{"status":"review","count":1,"frequency":0.2},{"status":"failed","count":1,"frequency":0.2}];

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

Your output

Press Run to execute the code in your browser.

Expected output

[
  {
    "status": "ok",
    "count": 3,
    "frequency": 0.6
  },
  {
    "status": "review",
    "count": 1,
    "frequency": 0.2
  },
  {
    "status": "failed",
    "count": 1,
    "frequency": 0.2
  }
]

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

Lesson notes

The rule

relative frequency = category count / total count