> ## Documentation Index
> Fetch the complete documentation index at: https://calendar-effects.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# 7. Comparing the answers

> Checking that the stream and the batch study agree, window by window.

The stream writes `data/stream/event_car/`; the batch study writes `data/results/batch_event_car.parquet`. They have the same columns (decision 0006), so they can be joined window by window and compared value by value.

## The big idea

`streaming/compare.py` holds the rules, and `scripts/compare_stream_batch.py` runs them on real output:

* Join on ticker, event type and event date. Every column must agree to rounding error.
* A window missing from the stream is only acceptable if it is **still open**: its day +5 falls after the last trading day the stream processed. Any other missing window is a failure.
* Duplicate windows, an empty stream, or a stream with no overlap with the batch fail too.
* Both sides must make the same **significance call** per window (|car\_z| ≥ 1.96).

With 38 retailers, all 2,280 windows are equal, and all 2,242 significance calls match.

### The picture: How we know the numbers are right, and what stops mistakes

```mermaid theme={null}
%% Calendar Effects · 7 of 8 · How we know the numbers are right, and what stops mistakes.
%% Paste into Miro: Creation bar > Diagram > Build with code.
flowchart TB
  subgraph THREE["Three independent answers to the same question must agree"]
    stream[("Streaming job output<br/>day by day, with state")]
    direct["scripts/check_streaming.py<br/>recomputes every daily AR and every event CAR<br/>from master with plain numpy, no streaming at all"]
    batch[("Batch answer key<br/>car.py, whole arrays")]
    cs{"check_streaming: OK<br/>95,190 daily rows + 2,280 CARs<br/>equal to about 1e-16"}
    cmp{"scripts/compare_stream_batch.py<br/>rules in src/streaming/compare.py<br/>joins on ticker, event_type, event_date<br/>every column must match within 1e-9<br/>2,280 of 2,280 equal at 38 retailers<br/>600 of 600 on the SFU cluster at 10"}
  end

  subgraph TESTS["tests/ · python3 -m pytest -q · run before every commit"]
    tc["test_contracts · docs and code contracts agree"]
    tcf["test_config · every config row is valid"]
    tcl["test_clean_files · clean tables match their contracts,<br/>cover 2016–2025, carry exactly the calendar's events;<br/>if not, names the script to re-run"]
    tnh["test_no_hardcoding · no ticker, event, year, path<br/>or Kafka setting typed into etl/, src/, scripts/"]
    tev["test_event_calendar + test_expand_events<br/>date rules give the right dates, e.g. Black Friday 2024"]
    tsig["test_streaming_signals · the per-stock logic"]
    tes["test_eventstudy · the batch study"]
    tcmp["test_compare · an empty, cut-short or duplicated stream fails"]
    thook["test_commit_hook · the commit hook itself"]
  end

  subgraph GUARDS["Guardrails around every change"]
    req["require_contract before every save<br/>a script refuses to write a wrong table"]
    hook["Commit hook · .claude/hooks/pre-commit-tests.sh<br/>runs all tests on exactly what a commit contains<br/>blocks the commit if any fail · Claude Code and Cursor"]
    brief["Session brief · .claude/hooks/session-brief.sh<br/>shows STATUS and the newest handoffs<br/>at the start of every Claude session"]
    checker["contract-checker subagent<br/>reviews code that reads or writes a contracted table<br/>its memory is committed for the whole team"]
    team["Team records · docs/STATUS.md · docs/handoffs/<br/>docs/decisions/ · docs/ai-usage/<br/>AGENTS.md rules 1–12 · docs-site/"]
  end

  stream --> cs
  direct --> cs
  stream --> cmp
  batch --> cmp
  TESTS --> hook
  req --> tcl
```

The [full diagram and its guide](https://calendar-effects.mintlify.site/diagrams/07-checks-and-guardrails.md) explain every box.

### Read these files in this order

**1. src/streaming/compare.py**

Compare the streaming job's event CARs with the batch event study's answer key (task #9). [Read it](https://calendar-effects.mintlify.site/reference/src/streaming/compare.md)

**2. scripts/compare\_stream\_batch.py**

Streaming vs batch: does the stream reproduce the batch event study's answer key? (task #9) [Read it](https://calendar-effects.mintlify.site/reference/scripts/compare_stream_batch.md)

## Questions you should be able to answer

> [accordion] What would make the comparison fail?
>
> A watermark that drops a late row, a market model fitted on the wrong days, a window closed a day early, or an event matched to the wrong day 0. Each changes at least one CAR.

> [accordion] Why is 'still open' allowed?
>
> A replay can stop before the last events' day +5 arrives. Those windows are not wrong, just not finished, so the rule checks dates against what the stream actually processed.

## Ideas to improve it

* Run the comparison automatically at the end of every streaming run and show the result on the dashboard.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.