"""yield-curve-inversions-nber-announcement-lag.py - each 10y-2y inversion run since 1976 against the date the NBER announced the recession that followed, so the lead can be read as it was seen at the time. Inputs: datasets/yield-curve-inversions-since-1976.csv (FRED T10Y2Y daily, retrieved 2026-09-05; the same file and the same episode rule as yield-curve-inversions-since-1976.py) datasets/yield-curve-inversions-nber-announcements.csv (NBER Business Cycle Dating Committee: every peak and trough since 1976 and the date it was announced, read from nber.org on 2026-11-04) Rule : an episode = consecutive negative T10Y2Y closes, gaps of MAX_GAP or fewer non-negative closes bridged, kept if it has at least MIN_DAYS negative closes. A "run" = the episodes whose first negative close falls after the previous NBER trough month and no later than the peak month. Months are calendar-month differences ((year2 - year1) * 12 + month2 - month1), as in the episode table. Output: a Markdown table per recession, then the open run (2022) measured to the NBER page's read date. Run : python code/yield-curve-inversions-nber-announcement-lag.py Needs : Python 3.13, pandas 3.0.2. Makes no network request. """ import pandas as pd SPREAD_CSV = "datasets/yield-curve-inversions-since-1976.csv" NBER_CSV = "datasets/yield-curve-inversions-nber-announcements.csv" NBER_READ = pd.Timestamp("2026-11-04") # the day the announcements page was read; no peak listed after 2020-02 MAX_GAP = 5 MIN_DAYS = 5 def months(a, b): return (b.year - a.year) * 12 + b.month - a.month def episodes(spread): out, start, last_neg, gap = [], None, None, 0 for d, is_neg in (spread < 0).items(): if is_neg: if start is None: start = d last_neg, gap = d, 0 elif start is not None: gap += 1 if gap > MAX_GAP: out.append((start, last_neg)) start = None if start is not None: out.append((start, last_neg)) kept, blips = [], [] for s, e in out: n = int((spread[s:e] < 0).sum()) (kept if n >= MIN_DAYS else blips).append((s, e, n)) return kept, blips def close_on_or_before(spread, day): s = spread[:day] return s.index[-1], float(s.iloc[-1]) def main(): df = pd.read_csv(SPREAD_CSV, parse_dates=["date"], index_col="date") spread = df["T10Y2Y"].dropna() nber = pd.read_csv(NBER_CSV, parse_dates=["announced"]) nber["month"] = pd.to_datetime(nber["month"] + "-01") peaks = nber[nber.turn == "peak"].reset_index(drop=True) troughs = nber[nber.turn == "trough"].reset_index(drop=True) kept, blips = episodes(spread) ep_no = {s: i for i, (s, _, _) in enumerate(kept, 1)} def month_end(m): return m + pd.offsets.MonthEnd(0) print("| peak | first negative close of the run (episode) | months to peak | peak announced | peak to announcement, months " "| first negative close to announcement, months | last negative close before announcement | curve positive when announced, months " "| T10Y2Y on announcement day | trough | trough announced | recession over when the peak was announced |") print("|---|---|---|---|---|---|---|---|---|---|---|---|") prev_trough = pd.Timestamp("1975-03-01") # the NBER trough before the sample (March 1975) rows = [] for i, p in peaks.iterrows(): t = troughs.iloc[i] run = [(s, e, n) for s, e, n in kept if month_end(prev_trough) < s <= month_end(p.month)] ann = p.announced if run: first, last_neg = run[0][0], max(e for _, e, _ in run if e <= ann) first_txt = f"{first:%Y-%m-%d} (#{ep_no[first]}" + (f"-#{ep_no[run[-1][0]]})" if len(run) > 1 else ")") to_peak, to_ann = months(first, p.month), months(first, ann) pos = months(last_neg, ann) last_txt = f"{last_neg:%Y-%m-%d}" else: bl = [b for b in blips if month_end(prev_trough) < b[0] <= month_end(p.month)] first_txt = "none tabulated" + (f" (blip {bl[0][0]:%Y-%m-%d}, {bl[0][2]} closes)" if bl else "") to_peak = to_ann = pos = None last_txt = "-" d, v = close_on_or_before(spread, ann) over = "yes" if month_end(t.month) <= ann else "no" after = months(last_neg, p.month) if run else None # > 0: the peak came after the run's last negative close rows.append(dict(peak=p.month, ann=ann, lag=months(p.month, ann), to_peak=to_peak, to_ann=to_ann, pos=pos, after=after, trough=t.month, tann=t.announced, tlag=months(t.month, t.announced), over=over, v=v)) fmt = lambda x: "-" if x is None else str(x) print(f"| {p.month:%Y-%m} | {first_txt} | {fmt(to_peak)} | {ann:%Y-%m-%d} | {months(p.month, ann)} | {fmt(to_ann)} " f"| {last_txt} | {fmt(pos)} | {v:+.2f} ({d:%Y-%m-%d}) | {t.month:%Y-%m} | {t.announced:%Y-%m-%d} | {over} |") prev_trough = t.month print() lags = [r["lag"] for r in rows] tl = [r["tlag"] for r in rows] print(f"peak to announcement: {min(lags)} to {max(lags)} months, mean {sum(lags) / len(lags):.1f} (n={len(lags)})") print(f"trough to announcement: {min(tl)} to {max(tl)} months, mean {sum(tl) / len(tl):.1f}") runs = [r for r in rows if r["to_ann"] is not None] print(f"first negative close to peak announcement: {min(r['to_ann'] for r in runs)} to {max(r['to_ann'] for r in runs)} months (n={len(runs)})") print(f"curve back above zero when the peak was announced: {sum(r['pos'] > 0 for r in runs)} of {len(runs)}; " f"positive for {min(r['pos'] for r in runs)} to {max(r['pos'] for r in runs)} months; " f"T10Y2Y on the day {min(r['v'] for r in runs):+.2f} to {max(r['v'] for r in runs):+.2f}") print("peak month after the run's last negative close: " + ", ".join(f"{r['peak']:%Y-%m} ({r['after']} months after)" for r in runs if r["after"] > 0) + "; peak before it: " + ", ".join(f"{r['peak']:%Y-%m}" for r in runs if r["after"] <= 0)) print(f"recession already over (trough month ended) when the peak was announced: " f"{', '.join(f'{r['peak']:%Y-%m}' for r in rows if r['over'] == 'yes') or 'none'}") last_t = troughs.month.max() open_run = [(s, e, n) for s, e, n in kept if s > month_end(last_t)] later = peaks[peaks.month > last_t] print() if open_run and later.empty: s, e, n = open_run[0] print(f"open run: episode #{ep_no[s]} first negative close {s:%Y-%m-%d}, last {e:%Y-%m-%d} ({n} closes); " f"no peak after {last_t:%Y-%m} on the NBER page read {NBER_READ:%Y-%m-%d}") print(f"months from its first negative close to {NBER_READ:%Y-%m-%d}: {months(s, NBER_READ)}; " f"longest earlier wait from a run's first negative close to a peak announcement: {max(r['to_ann'] for r in runs)}") print(f"months from its last negative close to the end of the spread file ({spread.index[-1]:%Y-%m-%d}): {months(e, spread.index[-1])}; " f"longest earlier positive stretch before a peak announcement: {max(r['pos'] for r in runs)}") print(f"spread file ends {spread.index[-1]:%Y-%m-%d} at {spread.iloc[-1]:+.2f}; no negative close after {e:%Y-%m-%d} in it") if __name__ == "__main__": main()