import pandas as pd

df = pd.read_csv("approval-gap-monthly.csv")
df["month"] = pd.to_datetime(df["month"])

# Same-period ratio is descriptive only. It is NOT a conversion rate because
# buildings completed this month can relate to plans approved many months earlier.
df["same_period_ratio_pct"] = (
    df["buildings_completed_current_r000"]
    / df["plans_passed_current_r000"] * 100
)

# Exploratory lag test: do plans at t align with completions at t + lag?
# With only 17 months in this extracted release window, treat the result as
# exploratory and do not claim a stable construction lag.
for lag in range(0, 7):
    left = df["plans_passed_constant_2019_r000"].iloc[:-lag or None].reset_index(drop=True)
    right = df["buildings_completed_constant_2019_r000"].iloc[lag:].reset_index(drop=True)
    print(lag, round(left.corr(right), 3), len(left))
