T-75: M1.2e — tools/curate-etl: pure fx_1d builder (merge CAD.FOREX daily + teMarketData.USDCAD into one USD/CAD series) with local benchmark (no new crates)

This commit is contained in:
Studio 2026-11-27 21:59:58 -05:00
parent a494defef5
commit 65aed1c2b5
4 changed files with 113 additions and 19 deletions

View file

@ -624,6 +624,17 @@ fn shuffle_slice<T>(items: &mut [T], rng: &mut SplitMix64) {
}
}
#[inline]
fn next_weekday(date: i32) -> i32 {
let next = date + 1;
// 1970-01-01 was Thursday (0=Thu, 1=Fri, 2=Sat, 3=Sun, 4=Mon).
match next.rem_euclid(7) {
2 => next + 2,
3 => next + 1,
_ => next,
}
}
/// Runs the fixed-seed `build_fx_1d` + [`fx_rate_on_or_before`] benchmark for `rows` total daily
/// FX rows across `CAD.FOREX` (~80%) and `teMarketData.USDCAD` (~20%).
///
@ -686,16 +697,10 @@ pub fn run_fx_benchmark(rows: usize) -> FxBenchReport {
}
// Advance calendar date, skipping weekends and occasional interior gaps.
cur_date += 1;
let dow = cur_date.rem_euclid(7); // 1970-01-01 was Thursday (0=Thu, 2=Sat, 3=Sun)
if dow == 2 {
cur_date += 2;
} else if dow == 3 {
cur_date += 1;
}
cur_date = next_weekday(cur_date);
if rng.next_u64() % 20 == 0 {
interior_gap_dates.push(cur_date);
cur_date += 1;
cur_date = next_weekday(cur_date);
}
let step = ((rng.next_u64() % 201) as f64 - 100.0) * 0.0001;
@ -838,13 +843,7 @@ pub fn run_fx_benchmark(rows: usize) -> FxBenchReport {
let mut tail_date = last_eod_date;
let mut tail_rate = rate;
for _ in 0..te_tail_target {
tail_date += 1;
let dow = tail_date.rem_euclid(7);
if dow == 2 {
tail_date += 2;
} else if dow == 3 {
tail_date += 1;
}
tail_date = next_weekday(tail_date);
let step = ((rng.next_u64() % 201) as f64 - 100.0) * 0.0001;
tail_rate = (tail_rate + step).clamp(0.92, 1.60);
let half_spread = ((rng.next_u64() % 80) + 10) as f64 * 0.0001;
@ -1132,4 +1131,18 @@ mod tests {
assert_eq!(report.lookups_per_sec, 0.0);
assert_eq!(report.ns_per_lookup, 0.0);
}
#[test]
fn next_weekday_skips_saturdays_and_sundays() {
let thu = ymd_to_epoch_days(2025, 1, 2).unwrap();
let fri = ymd_to_epoch_days(2025, 1, 3).unwrap();
let sat = ymd_to_epoch_days(2025, 1, 4).unwrap();
let sun = ymd_to_epoch_days(2025, 1, 5).unwrap();
let mon = ymd_to_epoch_days(2025, 1, 6).unwrap();
assert_eq!(next_weekday(thu), fri);
assert_eq!(next_weekday(fri), mon);
assert_eq!(next_weekday(sat), mon);
assert_eq!(next_weekday(sun), mon);
}
}

View file

@ -26,6 +26,11 @@ pub const FX_PAIR_USDCAD: &str = "USDCAD";
/// Relative close-price tolerance (1%) for the read-only EOD vs. TE overlap cross-check.
pub const FX_OVERLAP_REL_TOLERANCE: f64 = 0.01;
/// IEEE-754 rounding slack for the inclusive [`FX_OVERLAP_REL_TOLERANCE`] boundary so decimal
/// USD/CAD quotes that are mathematically exactly 1% apart (e.g. `1.4000` vs. `1.4140`, where
/// `(1.4140 - 1.4000) / 1.4000 = 0.010000000000000009`) are not falsely flagged as mismatches.
const FX_OVERLAP_REL_ROUNDING_EPS: f64 = 4.0 * f64::EPSILON;
/// Provenance source for a curated `fx_1d` USD/CAD row.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum FxSource {
@ -262,7 +267,7 @@ pub fn build_fx_1d(eod: &[FxDailyBar], te: &[FxDailyBar]) -> Fx1dBuild {
if rel > stats.max_overlap_rel {
stats.max_overlap_rel = rel;
}
if rel <= FX_OVERLAP_REL_TOLERANCE {
if rel <= FX_OVERLAP_REL_TOLERANCE + FX_OVERLAP_REL_ROUNDING_EPS {
stats.overlap_within_tolerance += 1;
} else {
stats.overlap_mismatched += 1;
@ -563,6 +568,55 @@ mod tests {
}
}
#[test]
fn overlap_crosscheck_decimal_usdcad_one_percent_boundary_is_rounding_aware() {
let base = day(2025, 4, 1);
// Decimal USD/CAD pairs at exact ±1% (including 1.4000 vs 1.4140 where raw f64 division
// produces 0.010000000000000009) and just outside ±1%.
let eod = [
flat(base, 1.4000), // te = 1.4140 (+1.0000%, within)
flat(base + 1, 1.4000), // te = 1.3860 (-1.0000%, within)
flat(base + 2, 1.3500), // te = 1.3635 (+1.0000%, within)
flat(base + 3, 1.3500), // te = 1.3365 (-1.0000%, within)
flat(base + 4, 1.3700), // te = 1.3837 (+1.0000%, within)
flat(base + 5, 1.3700), // te = 1.3563 (-1.0000%, within)
flat(base + 6, 1.4000), // te = 1.4141 (+1.0071%, mismatched)
flat(base + 7, 1.4000), // te = 1.3859 (-1.0071%, mismatched)
flat(base + 8, 1.4000), // te = 1.4140001 (+1.000007%, mismatched)
];
let te = [
flat(base, 1.4140),
flat(base + 1, 1.3860),
flat(base + 2, 1.3635),
flat(base + 3, 1.3365),
flat(base + 4, 1.3837),
flat(base + 5, 1.3563),
flat(base + 6, 1.4141),
flat(base + 7, 1.3859),
flat(base + 8, 1.4140001),
];
let built = build_fx_1d(&eod, &te);
assert_invariants(&built);
assert_eq!(built.stats.overlap_checked, 9);
assert_eq!(built.stats.overlap_within_tolerance, 6);
assert_eq!(built.stats.overlap_mismatched, 3);
// max_overlap_rel retains the raw calculated relative difference.
let expected_max = (1.4000_f64 - 1.4141_f64).abs() / 1.4000_f64;
assert_eq!(built.stats.max_overlap_rel.to_bits(), expected_max.to_bits());
// Exact 1.4000 vs 1.4140 alone still records its calculated rel in max_overlap_rel.
let single = build_fx_1d(&[flat(base, 1.4000)], &[flat(base, 1.4140)]);
assert_invariants(&single);
assert_eq!(single.stats.overlap_within_tolerance, 1);
assert_eq!(single.stats.overlap_mismatched, 0);
assert_eq!(
single.stats.max_overlap_rel.to_bits(),
((1.4000_f64 - 1.4140_f64).abs() / 1.4000_f64).to_bits()
);
}
#[test]
fn lookup_on_or_before_covers_exact_carry_forward_stale_and_no_lookahead() {
let fri = day(2025, 1, 3);