Module 10 · Financial Time-Series Foundations Lesson 95 of 120

Resampling, Aggregation, and Time Alignment

Aggregating event payments without double-counting a boundary.

2:33 clip5:06:18–5:08:51 in the full courseWatch on YouTube

Transcript

18 sentences · select one to jump there

Check your understanding

Which bucket owns the event exactly at second sixty?

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.

095-resampling-aggregation-and-time-alignment.ts
Start from GitHub
/**
 * Fintech Math Bootcamp · Lesson 095 of 120
 * Resampling, Aggregation, and Time Alignment
 * Module 10: Financial Time-Series Foundations
 *
 * Scenario: Aggregating event payments without double-counting a boundary
 * Rule:     bucket(t)=floor(t/width) for left-closed, right-open buckets
 *
 * Try it:   Which bucket owns the event exactly at second sixty?
 *
 * Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/financial-time-series-foundations/resampling-aggregation-and-time-alignment/
 * Free course:    https://courses.thefintechbuilder.com
 * Synthetic teaching example, not financial advice or a production library.
 */

export function lesson095() {
  const events=[{t:0,v:10},{t:59,v:20},{t:60,v:30},{t:119,v:40}];
  const sums=new Map<number,number>();
  for(const e of events){const bucket=Math.floor(e.t/60);
    sums.set(bucket,(sums.get(bucket)??0)+e.v);}
  const result=[...sums].map(([bucket,total])=>({bucket,total}));
  return result;
}

export const checkedResult = [{"bucket":0,"total":30},{"bucket":1,"total":70}];

// Run this file directly: npx tsx lessons/10-financial-time-series-foundations/095-resampling-aggregation-and-time-alignment.ts
if (process.argv[1] && import.meta.url.endsWith(process.argv[1].replace(/\\/g, "/").split("/").pop()!)) {
  console.log(JSON.stringify(lesson095(), null, 2));
}

Your output

Press Run to execute the code in your browser.

Expected output

[
  {
    "bucket": 0,
    "total": 30
  },
  {
    "bucket": 1,
    "total": 70
  }
]

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

Lesson notes

The rule

bucket(t)=floor(t/width) for left-closed, right-open buckets