Module 3 · Data, Variables, Samples, and Measurement Lesson 24 of 120
Cross-Sectional, Time-Series, Panel, and Event Data
Customer histories and payment events have different grains.
Transcript
21 sentences · select one to jump thereCheck your understanding
Is an absent customer-month record necessarily a zero balance?
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.
/**
* Fintech Math Bootcamp · Lesson 024 of 120
* Cross-Sectional, Time-Series, Panel, and Event Data
* Module 03: Data, Variables, Samples, and Measurement
*
* Scenario: Customer histories and payment events have different grains
* Rule: snapshot · sequence · panel · event stream
*
* Try it: Is an absent customer-month record necessarily a zero balance?
*
* Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/data-variables-samples-and-measurement/cross-sectional-time-series-panel-and-event-data/
* Free course: https://courses.thefintechbuilder.com
* Synthetic teaching example, not financial advice or a production library.
*/
export function lesson024() {
const rows = [
{id: "C1", month: "Jan", balance: 10},
{id: "C1", month: "Feb", balance: 12},
{id: "C2", month: "Jan", balance: 8}
];
const key = (r: typeof rows[number]) => `${r.id}|${r.month}`;
const result = new Set(rows.map(key)).size;
return result;
}
export const checkedResult = 3;
// Run this file directly: npx tsx lessons/03-data-variables-samples-and-measurement/024-cross-sectional-time-series-panel-and-event-data.ts
if (process.argv[1] && import.meta.url.endsWith(process.argv[1].replace(/\\/g, "/").split("/").pop()!)) {
console.log(JSON.stringify(lesson024(), null, 2));
}
Your output
Press Run to execute the code in your browser.
Expected output
3"""
Fintech Math Bootcamp · Lesson 024 of 120
Cross-Sectional, Time-Series, Panel, and Event Data
Module 03: Data, Variables, Samples, and Measurement
Scenario: Customer histories and payment events have different grains
Rule: snapshot · sequence · panel · event stream
Try it: Is an absent customer-month record necessarily a zero balance?
Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/data-variables-samples-and-measurement/cross-sectional-time-series-panel-and-event-data/
Free course: https://courses.thefintechbuilder.com
Synthetic teaching example, not financial advice or a production library.
Run it: python main.py
"""
import json
def lesson024() -> int:
rows = [
{"id": "C1", "month": "Jan", "balance": 10},
{"id": "C1", "month": "Feb", "balance": 12},
{"id": "C2", "month": "Jan", "balance": 8},
]
def key(r: dict) -> str:
return f"{r['id']}|{r['month']}"
result = len({key(r) for r in rows})
return result
if __name__ == "__main__":
print(json.dumps(lesson024(), indent=2))
Your output
Press Run to execute the code in your browser.
Expected output
3/**
* Fintech Math Bootcamp · Lesson 024 of 120
* Cross-Sectional, Time-Series, Panel, and Event Data
* Module 03: Data, Variables, Samples, and Measurement
*
* Scenario: Customer histories and payment events have different grains
* Rule: snapshot · sequence · panel · event stream
*
* Try it: Is an absent customer-month record necessarily a zero balance?
*
* Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/data-variables-samples-and-measurement/cross-sectional-time-series-panel-and-event-data/
* Free course: https://courses.thefintechbuilder.com
* Synthetic teaching example, not financial advice or a production library.
*
* Run it: javac Main.java && java Main
*/
import java.util.HashSet;
import java.util.Set;
import java.util.function.Function;
public class Main {
static final class BalanceRow {
final String id;
final String month;
final double balance;
BalanceRow(String id, String month, double balance) {
this.id = id;
this.month = month;
this.balance = balance;
}
}
static int lesson024() {
BalanceRow[] rows = {
new BalanceRow("C1", "Jan", 10),
new BalanceRow("C1", "Feb", 12),
new BalanceRow("C2", "Jan", 8)
};
Function<BalanceRow, String> key = r -> r.id + "|" + r.month;
Set<String> keys = new HashSet<String>();
for (BalanceRow r : rows) keys.add(key.apply(r));
int result = keys.size();
return result;
}
public static void main(String[] args) {
System.out.println(lesson024());
}
}
No browser runner for Java yet
Read the code here, then run it in your own toolchain or a ready-made cloud workspace.
Expected output
3// Fintech Math Bootcamp · Lesson 024 of 120
// Cross-Sectional, Time-Series, Panel, and Event Data
// Module 03: Data, Variables, Samples, and Measurement
//
// Scenario: Customer histories and payment events have different grains
// Rule: snapshot · sequence · panel · event stream
//
// Try it: Is an absent customer-month record necessarily a zero balance?
//
// Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/data-variables-samples-and-measurement/cross-sectional-time-series-panel-and-event-data/
// Free course: https://courses.thefintechbuilder.com
// Synthetic teaching example, not financial advice or a production library.
//
// Run it: go run main.go
package main
import (
"encoding/json"
"fmt"
)
type BalanceRow struct {
ID string
Month string
Balance float64
}
func lesson024() int {
rows := []BalanceRow{
{ID: "C1", Month: "Jan", Balance: 10},
{ID: "C1", Month: "Feb", Balance: 12},
{ID: "C2", Month: "Jan", Balance: 8},
}
key := func(r BalanceRow) string { return r.ID + "|" + r.Month }
keys := map[string]bool{}
for _, r := range rows {
keys[key(r)] = true
}
result := len(keys)
return result
}
func main() {
printJSON(lesson024())
}
// printJSON prints a value as JSON indented with two spaces, like JSON.stringify(value, null, 2).
func printJSON(value any) {
out, err := json.MarshalIndent(value, "", " ")
if err != nil {
panic(err)
}
fmt.Println(string(out))
}
No browser runner for Go yet
Read the code here, then run it in your own toolchain or a ready-made cloud workspace.
Expected output
3/**
* Fintech Math Bootcamp · Lesson 024 of 120
* Cross-Sectional, Time-Series, Panel, and Event Data
* Module 03: Data, Variables, Samples, and Measurement
*
* Scenario: Customer histories and payment events have different grains
* Rule: snapshot · sequence · panel · event stream
*
* Try it: Is an absent customer-month record necessarily a zero balance?
*
* Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/data-variables-samples-and-measurement/cross-sectional-time-series-panel-and-event-data/
* Free course: https://courses.thefintechbuilder.com
* Synthetic teaching example, not financial advice or a production library.
*
* Run it: g++ -std=c++17 -o main main.cpp && ./main
*/
#include <iostream>
#include <set>
#include <string>
#include <vector>
struct BalanceRow {
std::string id;
std::string month;
double balance;
};
std::size_t lesson024() {
const std::vector<BalanceRow> rows{
{"C1", "Jan", 10},
{"C1", "Feb", 12},
{"C2", "Jan", 8}
};
auto key = [](const BalanceRow& r) { return r.id + "|" + r.month; };
std::set<std::string> keys;
for (const BalanceRow& r : rows) keys.insert(key(r));
const std::size_t result = keys.size();
return result;
}
int main() {
std::cout << lesson024() << "\n";
}
No browser runner for C++ yet
Read the code here, then run it in your own toolchain or a ready-made cloud workspace.
Expected output
3//! Fintech Math Bootcamp · Lesson 024 of 120
//! Cross-Sectional, Time-Series, Panel, and Event Data
//! Module 03: Data, Variables, Samples, and Measurement
//!
//! Scenario: Customer histories and payment events have different grains
//! Rule: snapshot · sequence · panel · event stream
//!
//! Try it: Is an absent customer-month record necessarily a zero balance?
//!
//! Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/data-variables-samples-and-measurement/cross-sectional-time-series-panel-and-event-data/
//! Free course: https://courses.thefintechbuilder.com
//! Synthetic teaching example, not financial advice or a production library.
//!
//! Run it: rustc main.rs && ./main
use std::collections::HashSet;
#[allow(dead_code)] // balance is carried by the row but not needed for the key count
struct BalanceRow {
id: &'static str,
month: &'static str,
balance: f64,
}
fn lesson024() -> usize {
let rows = [
BalanceRow { id: "C1", month: "Jan", balance: 10.0 },
BalanceRow { id: "C1", month: "Feb", balance: 12.0 },
BalanceRow { id: "C2", month: "Jan", balance: 8.0 },
];
let key = |r: &BalanceRow| format!("{}|{}", r.id, r.month);
let result = rows.iter().map(key).collect::<HashSet<String>>().len();
result
}
fn main() {
println!("{}", lesson024());
}
No browser runner for Rust yet
Read the code here, then run it in your own toolchain or a ready-made cloud workspace.
Expected output
3/**
* Fintech Math Bootcamp · Lesson 024 of 120
* Cross-Sectional, Time-Series, Panel, and Event Data
* Module 03: Data, Variables, Samples, and Measurement
*
* Scenario: Customer histories and payment events have different grains
* Rule: snapshot · sequence · panel · event stream
*
* Try it: Is an absent customer-month record necessarily a zero balance?
*
* Lesson article: https://thefintechbuilder.com/financial-mathematics-statistics-and-data-foundations/data-variables-samples-and-measurement/cross-sectional-time-series-panel-and-event-data/
* Free course: https://courses.thefintechbuilder.com
* Synthetic teaching example, not financial advice or a production library.
*
* Run it: dotnet run (inside a console project that holds this Program.cs)
*/
using System.Text.Json;
Console.WriteLine(JsonSerializer.Serialize(Lesson024()));
static int Lesson024()
{
var rows = new[]
{
new BalanceRow("C1", "Jan", 10),
new BalanceRow("C1", "Feb", 12),
new BalanceRow("C2", "Jan", 8),
};
string Key(BalanceRow r) => $"{r.Id}|{r.Month}";
var result = rows.Select(Key).ToHashSet().Count;
return result;
}
record BalanceRow(string Id, string Month, double Balance);
No browser runner for C# yet
Read the code here, then run it in your own toolchain or a ready-made cloud workspace.
Expected output
3Prefer your own machine? Every file is in the course repository · open it in Codespaces.
Lesson notes
The rule
snapshot · sequence · panel · event stream