> ## 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.

# 2. Shared rules

> The settings, schemas and config lists every other file obeys.

Every number the study depends on (years, window lengths, thresholds) and every list it studies (retailers, events, Wikipedia articles) is written down **once**. Code imports it; it never types it again. This is what lets four people work on separate parts and still get answers that line up.

## The big idea

There are three kinds of shared rules:

* **How the study works** (`src/common/study.py`): the study years, the market ticker, the event window (−5..+5), the estimation window (120 days, at least 100), the alert thresholds (|z| ≥ 1.96 for an abnormal return, z ≥ 3 for an attention spike) and the model's split years.
* **Where things live** (`src/common/settings.py`): every folder and file path, and the Kafka servers.
* **What the data looks like** (`src/common/contracts.py`): the columns and types of every dataset (the **data contracts**), the loaders for the `config/` lists, and the expander that turns event rules into dates.

The `config/` folder holds the lists as CSV files. Adding a retailer or an event is adding a row, with no code change.

## Words you will meet

> [accordion] Data contract
>
> A promise about a dataset's columns and types. It is written in `docs/data-contracts.md` and repeated in `contracts.py`; a test keeps the two equal, and every script calls `require_contract` before it writes a file.

> [accordion] Date rule
>
> How an event's date is found each year: `fixed:10-31` (Halloween), `nth_weekday:11:thu:4` (US Thanksgiving), `offset:us_thanksgiving:+1` (Black Friday), or `listed` (dates typed into `event_dates.csv` with a source, for Prime Day and Chinese New Year).

> [accordion] In scope
>
> A `yes` in a config row means the study uses it; candidates stay listed with `no` so they can be switched on later.

### The picture: What is studied, and the rules every script follows

```mermaid theme={null}
%% Calendar Effects · 2 of 8 · What is studied, and the rules every script follows.
%% Paste into Miro: Creation bar > Diagram > Build with code.
flowchart LR
  subgraph CONFIG["config/ · WHAT is studied · hand-edited CSV registries (decisions 0003, 0005)"]
    retailers["retailers.csv<br/>one row per stock: ticker, company,<br/>country, retailer_type online or store, in_scope<br/>38 in scope: 7 online, 31 store<br/>e.g. AMZN WMT TGT COST LOW TJX KR"]
    etypes["event_types.csv<br/>one row per kind of event + a DATE RULE<br/>fixed:10-31 = same day every year<br/>nth_weekday:11:thu:4 = 4th Thursday of Nov<br/>offset:us_thanksgiving:+1 = day after another event<br/>listed = look the dates up"]
    edates["event_dates.csv<br/>dates for listed events with a source<br/>Prime Day, Chinese New Year"]
    wiki["wiki_articles.csv<br/>Wikipedia title per ticker<br/>2 titles when an article was renamed"]
    series["retail_series.csv<br/>FRED code to category, e.g. RSXFS = all retail"]
  end

  subgraph COMMON["src/common/ · HOW it is studied · imported by every script"]
    study["study.py · the numbers, defined once<br/>STUDY_START/END 2016–2025 · MARKET_TICKER SPY<br/>EVENT_WINDOW −5..+5 trading days<br/>ESTIMATION_DAYS 120 · ESTIMATION_GAP_DAYS 10<br/>ABNORMAL_RETURN_Z 1.96 · ATTENTION_SPIKE_Z 3.0<br/>CONFIDENCE 0.95"]
    settings["settings.py · WHERE things are<br/>raw/ clean/ data/ checkpoints/ paths<br/>clean_path name · KAFKA_BOOTSTRAP servers"]
    contracts["contracts.py · the DATA CONTRACTS in code<br/>CONTRACTS: exact columns and types of 15 tables<br/>load_retailers, load_event_types... read config/<br/>expand_events: date rules to real dates<br/>require_contract: refuse to save a wrong table<br/>spark_schema: the same contract for Spark"]
  end

  doc["docs/data-contracts.md<br/>the same contracts in words<br/>a test fails if doc and code disagree"]
  agents["AGENTS.md rules 1–11<br/>e.g. contracts are law · all times UTC<br/>no look-ahead · never type a value twice"]

  events[("events table, made in memory<br/>every occurrence of every in-scope event<br/>e.g. black_friday 2024-11-29<br/>never saved, so never out of date")]

  etypes --> contracts
  edates --> contracts
  retailers --> contracts
  wiki --> contracts
  series --> contracts
  contracts -->|expand_events| events
  doc <-->|kept in sync by tests/test_contracts.py| contracts
  agents --> COMMON
  events --> users["Used by: join_master.py, the streaming job,<br/>the batch study, the checks"]
  study --> users
  settings --> users
```

The [full diagram and its guide](https://calendar-effects.mintlify.site/diagrams/02-config-and-shared-rules.md) explain every box.

### Read these files in this order

**1. src/common/**init**.py**

Code shared by every part of the pipeline (schemas, helpers). [Read it](https://calendar-effects.mintlify.site/reference/src/common/init.md)

**2. src/common/study.py**

Study settings shared by the streaming job (src/streaming/) and the batch event study (src/eventstudy/). [Read it](https://calendar-effects.mintlify.site/reference/src/common/study.md)

**3. src/common/settings.py**

Where the project's files live, and how to reach Kafka. [Read it](https://calendar-effects.mintlify.site/reference/src/common/settings.md)

**4. src/common/contracts.py**

Single source of truth for dataset schemas, in code, plus the loaders for the config/ registries that are themselves contracts. [Read it](https://calendar-effects.mintlify.site/reference/src/common/contracts.md)

**5. config/event\_types.csv**

A registry with 12 rows. Columns: `event_type`, `name`, `category`, `region`, `date_rule`, `in_scope`, `notes`. [Read it](https://calendar-effects.mintlify.site/reference/config/event_types-csv.md)

**6. config/event\_dates.csv**

A registry with 20 rows. Columns: `event_type`, `event_date`, `event_end_date`, `source`. [Read it](https://calendar-effects.mintlify.site/reference/config/event_dates-csv.md)

**7. config/retailers.csv**

A registry with 40 rows. Columns: `ticker`, `company`, `country`, `retailer_type`, `in_scope`, `notes`. [Read it](https://calendar-effects.mintlify.site/reference/config/retailers-csv.md)

**8. config/wiki\_articles.csv**

A registry with 44 rows. Columns: `article`, `ticker`, `notes`. [Read it](https://calendar-effects.mintlify.site/reference/config/wiki_articles-csv.md)

**9. config/retail\_series.csv**

A registry with 6 rows. Columns: `series_id`, `category`, `notes`. [Read it](https://calendar-effects.mintlify.site/reference/config/retail_series-csv.md)

## Questions you should be able to answer

> [accordion] What happens if someone types 2016 into a script?
>
> `tests/test_no_hardcoding.py` scans every script's literals and fails, naming the file and line and what to import instead (`common.study.STUDY_START_YEAR`). The commit is refused until it is fixed.

> [accordion] Why is the estimation gap 10 days?
>
> The streaming job re-fits the market model every day. Skipping the latest 10 trading days means that on day +5 of an event its baseline ends at day −6, so no day of the window is inside its own baseline, exactly as in the batch study (decision 0004).

> [accordion] Why are some companies listed with two Wikipedia titles?
>
> Articles get renamed (Amazon.com became Amazon (company) in 2017). Views follow the title readers asked for, so both titles are listed and their views are added together per day.

## Ideas to improve it

* Load `config/` into a small database so the dashboard can edit the study's scope.
* Generate `docs/data-contracts.md` from `contracts.py` instead of keeping two copies in step with a test.


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