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:
parent
a494defef5
commit
65aed1c2b5
4 changed files with 113 additions and 19 deletions
|
|
@ -85,7 +85,7 @@ Rules:
|
|||
- `dropped_inconsistent_ohlc`: `high < low`, or `open`/`close` outside `[low, high]`.
|
||||
3. **Per-source deduplication on `session_date`:** Bitwise-identical duplicates collapse to one (`duplicate_collapsed += n - 1`); conflicting duplicate groups drop entirely (`duplicate_conflict_dropped += n`).
|
||||
4. **Merge precedence:** `EodCadForex` wins on overlapping dates (`te_superseded_by_eod += 1`); `TeUsdCad` fills interior gaps and all dates after EOD's last date (as well as dates where EOD dropped an invalid or conflicting group).
|
||||
5. **Read-only overlap cross-check:** On every date where both sources survive, evaluates `|eod.close - te.close| / eod.close <= FX_OVERLAP_REL_TOLERANCE` (`0.01`, 1% inclusive), counts `overlap_checked`, `overlap_within_tolerance`, and `overlap_mismatched`, and tracks `max_overlap_rel` without modifying emitted rows.
|
||||
5. **Read-only overlap cross-check:** On every date where both sources survive, evaluates `|eod.close - te.close| / eod.close <= FX_OVERLAP_REL_TOLERANCE` (`0.01`, 1% inclusive with a small IEEE-754 rounding slack so exact decimal 1% pairs like `1.4000` vs. `1.4140` are not falsely flagged), counts `overlap_checked`, `overlap_within_tolerance`, and `overlap_mismatched`, and tracks raw `max_overlap_rel` without modifying emitted rows.
|
||||
6. **No-look-ahead lookup (`fx_rate_on_or_before`):** Binary-searches sorted `Fx1dRow` slices for the latest row with `session_date <= date` and `date - session_date <= max_stale_days`. Carries forward across weekends/holidays within `max_stale_days`, returns `None` before the first row or when stale, and never returns a later date.
|
||||
7. **Determinism & accounting invariants:** Any permutation of `eod` or `te` yields bitwise-identical `rows` and `stats`, and:
|
||||
- Per source: `rows_in == rows_kept + dropped_invalid_ohlc + dropped_inconsistent_ohlc + duplicate_collapsed + duplicate_conflict_dropped`
|
||||
|
|
@ -162,8 +162,8 @@ cargo run --release -p curate-etl --offline -- bench-fx [rows]
|
|||
Command: `cargo run --release -p curate-etl --offline -- bench-fx`
|
||||
|
||||
- Input: `rows=20000`, `eod_rows=16000`, `te_rows=4000`
|
||||
- Build: `rows_out=17172`, `eod_out=15336`, `te_out=1836`, `elapsed=0.745 ms`, `throughput=26834724 rows/s`, `latency=37.27 ns/row`
|
||||
- Lookup: `lookups=1000000`, `hits=999266`, `elapsed=26.76 ms`, `throughput=37370069 lookups/s`, `latency=26.76 ns/lookup`, `checksum=0xf41e7ae8ca6a26aa` (identical across back-to-back runs)
|
||||
- Build: `rows_out=17172`, `eod_out=15336`, `te_out=1836`, `elapsed=0.741 ms`, `throughput=26993285 rows/s`, `latency=37.05 ns/row`
|
||||
- Lookup: `lookups=1000000`, `hits=999224`, `elapsed=25.68 ms`, `throughput=38946650 lookups/s`, `latency=25.68 ns/lookup`, `checksum=0x21e746bd53f3e7ff` (identical across back-to-back runs)
|
||||
- Stats: `eod_invalid=164`, `eod_inconsistent=170`, `eod_dup_collapsed=165`, `eod_dup_conflict=165`, `te_invalid=21`, `te_inconsistent=23`, `te_dup_collapsed=21`, `te_dup_conflict=34`, `te_superseded=2065`, `overlap_checked=2065`, `within_tol=1954`, `mismatched=111`, `max_overlap_rel=0.025000`
|
||||
|
||||
## Deferred Follow-Ups (`M1.2f+` / `M2`)
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -266,6 +266,33 @@ fn overlap_one_percent_boundary_is_inclusive_and_strictly_read_only() {
|
|||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn overlap_decimal_usdcad_quotes_at_one_percent_boundary_avoid_f64_false_mismatches() {
|
||||
let base = day(2025, 5, 1);
|
||||
let eod = [
|
||||
flat(base, 1.4000), // te = 1.4140 -> exact +1% (within despite f64 rounding)
|
||||
flat(base + 1, 1.4000), // te = 1.3860 -> exact -1% (within)
|
||||
flat(base + 2, 1.4000), // te = 1.4141 -> just above +1% (mismatch)
|
||||
flat(base + 3, 1.4000), // te = 1.3859 -> just below -1% (mismatch)
|
||||
];
|
||||
let te = [
|
||||
flat(base, 1.4140),
|
||||
flat(base + 1, 1.3860),
|
||||
flat(base + 2, 1.4141),
|
||||
flat(base + 3, 1.3859),
|
||||
];
|
||||
|
||||
let built = build_fx_1d(&eod, &te);
|
||||
assert_accounting_and_order(&built);
|
||||
assert_eq!(built.stats.overlap_checked, 4);
|
||||
assert_eq!(built.stats.overlap_within_tolerance, 2);
|
||||
assert_eq!(built.stats.overlap_mismatched, 2);
|
||||
assert_eq!(
|
||||
built.stats.max_overlap_rel.to_bits(),
|
||||
((1.4000_f64 - 1.4141_f64).abs() / 1.4000_f64).to_bits()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn permutation_invariance_holds_across_multiple_fixed_seed_shuffles() {
|
||||
let base = day(2020, 1, 2);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue