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

Trimmed and Winsorized Means

Reducing outlier influence without pretending data vanished.

2:34 clip1:50:25–1:53:00 in the full courseWatch on YouTube

Transcript

20 sentences · select one to jump there

Check your understanding

Which operation preserves the number of observations?

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.

035-trimmed-and-winsorized-means.ts
Start from GitHub
/**
 * Fintech Math Bootcamp · Lesson 035 of 120
 * Trimmed and Winsorized Means
 * Module 04: Location, Ranking, and Exploratory Summaries
 *
 * Scenario: Reducing outlier influence without pretending data vanished
 * Rule:     trim drops tails; winsorization caps tails
 *
 * Try it:   Which operation preserves the number of observations?
 *
 * Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/location-ranking-and-exploratory-summaries/trimmed-and-winsorized-means/
 * Free course:    https://courses.thefintechbuilder.com
 * Synthetic teaching example, not financial advice or a production library.
 */

export function lesson035() {
  const x = [1, 2, 2, 4, 9].sort((a,b) => a-b);
  const k = Math.floor(x.length * 0.2); // 20% from each tail
  const trimmed = x.slice(k, x.length - k);
  const lo = x[k], hi = x[x.length - 1 - k];
  const winsorized = x.map(v => Math.max(lo, Math.min(hi, v)));
  const avg = (a: number[]) => a.reduce((s,v)=>s+v,0)/a.length;
  const result = {trimmedMean: avg(trimmed),
    winsorizedMean: avg(winsorized)};
  return result;
}

export const checkedResult = {"trimmedMean":2.6666666666666665,"winsorizedMean":2.8};

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

Your output

Press Run to execute the code in your browser.

Expected output

{
  "trimmedMean": 2.6666666666666665,
  "winsorizedMean": 2.8
}

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

Lesson notes

The rule

trim drops tails; winsorization caps tails