All checks were successful
CI / lint (push) Successful in 31s
CI / test (push) Successful in 4m1s
CI / notebooks-smoke (push) Successful in 1m25s
Deploy / changes (push) Successful in 13s
Infra CI / notebooks (push) Successful in 1m6s
Infra CI / zotero (push) Successful in 16s
Infra CI / docs (push) Successful in 1m16s
Infra CI / api (push) Successful in 1m13s
Infra CI / bcda (push) Successful in 2m23s
Infra CI / mc (push) Successful in 15s
Deploy / notebooks (push) Has been skipped
Deploy / zotero (push) Has been skipped
Deploy / docs (push) Has been skipped
Infra CI / llm (push) Successful in 57s
Deploy / api (push) Successful in 1m17s
Deploy / bcda (push) Successful in 1m48s
Deploy / mc (push) Has been skipped
Deploy / llm (push) Successful in 1m25s
Deploy / report (push) Successful in 13s
Renovate / renovate (push) Successful in 2m2s
LLM Golden / llm-golden (push) Successful in 23m11s
225 lines
7.3 KiB
Python
225 lines
7.3 KiB
Python
"""market.abnormal — market-model CARs, CLM intervals, pooled bootstrap."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import date, timedelta
|
|
|
|
import pytest
|
|
|
|
from market.abnormal import Car, car_from_arrays, event_car, fit, pooled
|
|
|
|
_ASSOC = "association, not a causal estimate"
|
|
|
|
|
|
def _weekdays(d0: date, n_before: int, n_after: int) -> list[date]:
|
|
back: list[date] = []
|
|
day = d0
|
|
while len(back) < n_before:
|
|
day -= timedelta(days=1)
|
|
if day.weekday() < 5:
|
|
back.append(day)
|
|
fwd: list[date] = []
|
|
day = d0
|
|
while len(fwd) < n_after:
|
|
day += timedelta(days=1)
|
|
if day.weekday() < 5:
|
|
fwd.append(day)
|
|
return [*reversed(back), d0, *fwd]
|
|
|
|
|
|
def _market(cal: list[date]) -> tuple[dict[date, float], dict[str, dict[date, float]]]:
|
|
"""Firm equals SPY. SPY varies, so the market model is identified."""
|
|
spy: dict[date, float] = {}
|
|
firm: dict[date, float] = {}
|
|
for i, day in enumerate(cal):
|
|
spy[day] = ((i % 5) - 2) * 0.01
|
|
firm[day] = spy[day]
|
|
return firm, {"SPY": spy}
|
|
|
|
|
|
def _car(value: float | None, **kw: object) -> Car:
|
|
return Car(
|
|
event_id="e",
|
|
unit=kw.get("unit", "U"),
|
|
ticker="T",
|
|
model=kw.get("model", "market"),
|
|
window=kw.get("window", (-1, 1)),
|
|
car=value,
|
|
se=None if value is None else 0.01,
|
|
ci_low=None if value is None else value - 0.02,
|
|
ci_high=None if value is None else value + 0.02,
|
|
n_est=200,
|
|
n_members=1,
|
|
caveats=(_ASSOC,),
|
|
)
|
|
|
|
|
|
def test_fit_recovers_alpha_beta():
|
|
x = [i - 65 for i in range(130)]
|
|
y = [0.001 + 1.5 * xi for xi in x]
|
|
fitted = fit(y, x)
|
|
assert fitted.alpha == pytest.approx(0.001, abs=1e-12)
|
|
assert fitted.betas[0] == pytest.approx(1.5, abs=1e-12)
|
|
assert fitted.sigma2 == pytest.approx(0.0, abs=1e-12)
|
|
assert fit([1.0, 2.0], [1.0, 2.0]) is None
|
|
|
|
|
|
def test_car_variance_clm():
|
|
fitted = fit([-0.9, -0.2, 1.1], [-1.0, 0.0, 1.0])
|
|
assert fitted.alpha == pytest.approx(0.0, abs=1e-9)
|
|
assert fitted.betas[0] == pytest.approx(1.0, abs=1e-9)
|
|
assert fitted.sigma2 == pytest.approx(0.06)
|
|
car, se = car_from_arrays(fitted, [1.5, 2.0], [1.0, 2.0])
|
|
assert car == pytest.approx(0.5)
|
|
assert se == pytest.approx(0.6855654600, abs=1e-9)
|
|
|
|
d0 = date(2026, 1, 8)
|
|
cal = [
|
|
date(2026, 1, 5),
|
|
date(2026, 1, 6),
|
|
date(2026, 1, 7),
|
|
d0,
|
|
date(2026, 1, 9),
|
|
date(2026, 1, 10),
|
|
]
|
|
xs = {
|
|
date(2026, 1, 5): -1.0,
|
|
date(2026, 1, 6): 0.0,
|
|
date(2026, 1, 7): 1.0,
|
|
date(2026, 1, 9): 1.0,
|
|
date(2026, 1, 10): 2.0,
|
|
}
|
|
ys = {
|
|
date(2026, 1, 5): -0.9,
|
|
date(2026, 1, 6): -0.2,
|
|
date(2026, 1, 7): 1.1,
|
|
date(2026, 1, 9): 1.5,
|
|
date(2026, 1, 10): 2.0,
|
|
}
|
|
out = event_car(
|
|
ys,
|
|
{"SPY": xs},
|
|
cal,
|
|
d0,
|
|
est=(-3, -1),
|
|
window=(1, 2),
|
|
min_est=3,
|
|
)
|
|
assert out.car == pytest.approx(0.5)
|
|
assert out.se == pytest.approx(0.6855654600, abs=1e-9)
|
|
assert out.ci_low == pytest.approx(0.5 - 1.96 * 0.6855654600)
|
|
assert out.ci_high == pytest.approx(0.5 + 1.96 * 0.6855654600)
|
|
assert out.n_est == 3
|
|
assert _ASSOC in out.caveats
|
|
|
|
|
|
def test_car_window_sum():
|
|
d0 = date(2026, 11, 9)
|
|
cal = _weekdays(d0, 250, 2)
|
|
firm, factors = _market(cal)
|
|
firm[d0] = factors["SPY"][d0] + 0.02
|
|
day = event_car(firm, factors, cal, d0, window=(0, 0))
|
|
three = event_car(firm, factors, cal, d0, window=(-1, 1))
|
|
assert day.car == pytest.approx(0.02)
|
|
assert three.car == pytest.approx(0.02)
|
|
assert _ASSOC in day.caveats and _ASSOC in three.caveats
|
|
|
|
|
|
def test_short_estimation_window_caveat():
|
|
d0 = date(2026, 11, 9)
|
|
# 101 trading days fall in the default window; one has no firm return.
|
|
cal = _weekdays(d0, 131, 2)
|
|
firm, factors = _market(cal)
|
|
del firm[cal[cal.index(d0) - 131]]
|
|
out = event_car(firm, factors, cal, d0)
|
|
assert out.car is None and out.n_est == 100
|
|
assert "short estimation window (n=100)" in out.caveats
|
|
assert _ASSOC in out.caveats
|
|
|
|
|
|
def test_missing_window_return():
|
|
d0 = date(2026, 11, 9)
|
|
cal = _weekdays(d0, 250, 2)
|
|
firm, factors = _market(cal)
|
|
del firm[date(2026, 11, 10)]
|
|
out = event_car(firm, factors, cal, d0, window=(-1, 1))
|
|
assert out.car is None
|
|
assert "missing returns in event window" in out.caveats
|
|
assert _ASSOC in out.caveats
|
|
|
|
|
|
def test_offsets_count_trading_days():
|
|
d0 = date(2026, 11, 9)
|
|
cal = _weekdays(d0, 250, 3)
|
|
firm, factors = _market(cal)
|
|
shocks = {
|
|
date(2026, 11, 5): 0.50, # trading offset -2, outside (-1, 1)
|
|
date(2026, 11, 6): 0.01, # Friday
|
|
date(2026, 11, 9): 0.02,
|
|
date(2026, 11, 10): 0.04, # Tuesday
|
|
date(2026, 11, 11): 0.50, # trading offset +2, outside
|
|
}
|
|
for day, shock in shocks.items():
|
|
firm[day] = factors["SPY"][day] + shock
|
|
out = event_car(firm, factors, cal, d0, window=(-1, 1))
|
|
assert out.window == (-1, 1)
|
|
assert out.car == pytest.approx(0.07)
|
|
assert _ASSOC in out.caveats
|
|
|
|
|
|
def test_two_factor_and_sector_models():
|
|
x1 = [((i % 5) - 2) * 0.01 for i in range(130)]
|
|
x2 = [((i % 7) - 3) * 0.01 for i in range(130)]
|
|
y = [0.2 * a + 0.3 * b for a, b in zip(x1, x2, strict=True)]
|
|
assert len(fit(y, [x1, x2]).betas) == 2
|
|
assert len(fit(x2, x2).betas) == 1
|
|
|
|
d0 = date(2026, 11, 9)
|
|
cal = _weekdays(d0, 250, 2)
|
|
spy: dict[date, float] = {}
|
|
xlv: dict[date, float] = {}
|
|
firm: dict[date, float] = {}
|
|
for i, day in enumerate(cal):
|
|
spy[day] = ((i % 5) - 2) * 0.01
|
|
xlv[day] = ((i % 7) - 3) * 0.01
|
|
firm[day] = 0.2 * spy[day] + 0.3 * xlv[day]
|
|
factors = {"SPY": spy, "XLV": xlv}
|
|
sector = event_car(firm, factors, cal, d0, model="sector")
|
|
two = event_car(firm, factors, cal, d0, model="two-factor")
|
|
note = "sector index absorbs sector-wide effects"
|
|
assert note in sector.caveats and note in two.caveats
|
|
assert sector.model == "sector" and two.model == "two-factor"
|
|
assert _ASSOC in sector.caveats and _ASSOC in two.caveats
|
|
|
|
|
|
def test_estimation_overlap_caveat():
|
|
d0 = date(2026, 11, 9)
|
|
cal = _weekdays(d0, 250, 2)
|
|
inside = cal[cal.index(d0) - 40]
|
|
outside = cal[cal.index(d0) - 10]
|
|
assert inside == date(2026, 9, 14)
|
|
assert outside == date(2026, 10, 26)
|
|
firm, factors = _market(cal)
|
|
out = event_car(firm, factors, cal, d0, other_events=(inside, outside))
|
|
assert f"estimation window overlaps event on {inside}" in out.caveats
|
|
assert f"estimation window overlaps event on {outside}" not in out.caveats
|
|
assert out.car is not None
|
|
assert _ASSOC in out.caveats
|
|
|
|
|
|
def test_pooled_bootstrap_deterministic():
|
|
cars = [_car(0.01), _car(0.03), _car(-0.02), _car(None)]
|
|
first = pooled(cars)
|
|
second = pooled(cars)
|
|
assert (first.ci_low, first.ci_high) == (second.ci_low, second.ci_high)
|
|
assert first.ci_low is not None and first.ci_high is not None
|
|
assert first.mean_car == pytest.approx((0.01 + 0.03 - 0.02) / 3)
|
|
assert first.n_events == 3
|
|
assert first.unit == "U" and first.model == "market"
|
|
assert "fewer than 5 events" in first.caveats
|
|
assert _ASSOC in first.caveats
|
|
one = pooled([_car(0.05)])
|
|
assert one.ci_low is None and one.ci_high is None
|
|
assert one.mean_car == pytest.approx(0.05)
|
|
assert one.n_events == 1
|