r"""
regime_percentiles.py
=====================
Diagnostic script — computes percentile distribution of each macro input
across 23 years. We use this to calibrate the scoring thresholds in
regime_monitor.py so the regime classification actually reflects history.

Run on home machine:
  cd C:\Users\paul_\OneDrive\fx_macro_intraday
  python src\research\regime_percentiles.py

Output: percentile table for each input. We pick thresholds that align with
meaningful percentiles (e.g. p90 = score max, p50 = mid score).
"""
import sys
from pathlib import Path

SRC = Path(__file__).resolve().parent
sys.path.insert(0, str(SRC))

from regime_monitor import _build_spread_series
import pandas as pd
import numpy as np

print("=" * 70)
print("  PERCENTILE DIAGNOSTIC — 23-year macro input distributions")
print("=" * 70)

spreads = _build_spread_series()
spreads["us_de_change"]      = spreads["us_de_2y"].diff()
spreads["us_de_vol_30d"]     = spreads["us_de_change"].rolling(30).std()
spreads["us_de_abs"]         = spreads["us_de_2y"].abs()
spreads["us_jp_abs"]         = spreads["us_jp_2y"].abs()

# Drop the warm-up period (first 30 days have NaN vol)
clean = spreads.dropna(subset=["us_de_vol_30d"]).copy()

# To avoid heavily weighting the recent stable period, compute percentiles
# on monthly snapshots, not daily readings (matches our scoring approach)
# Take first-of-month rows for fair sampling
clean["month"] = clean["date"].dt.to_period("M")
monthly = clean.groupby("month").last().reset_index()

print(f"\nUsing {len(monthly)} monthly snapshots (one per month, 2005-2026)")
print()

# Percentiles to display
PCTS = [5, 10, 25, 50, 75, 90, 95, 99]

def show_percentiles(name, series):
    print(f"\n{name}:")
    print("  Percentile    Value")
    print("  " + "-" * 22)
    for p in PCTS:
        v = np.percentile(series, p)
        print(f"  p{p:>2}           {v:>8.4f}")
    print(f"  Min            {series.min():>8.4f}")
    print(f"  Max            {series.max():>8.4f}")
    print(f"  Mean           {series.mean():>8.4f}")

show_percentiles("US-DE 2Y spread (abs value, %)", monthly["us_de_abs"])
show_percentiles("US-JP 2Y spread (abs value, %)", monthly["us_jp_abs"])
show_percentiles("US-DE 30d vol (std of daily changes, %)", monthly["us_de_vol_30d"])

print()
print("=" * 70)
print("  HOW TO INTERPRET")
print("=" * 70)
print("""
For each input, the threshold ladder in regime_monitor.py should map to
percentiles like:
  Max score (e.g. 30)   → p90+ (top 10% of months historically)
  High score            → p75
  Mid score             → p50 (median)
  Low score             → p25
  Min score (0)         → p10 or below

Send this output back and I'll write the corrected thresholds in
regime_monitor.py based on the actual distribution.
""")
