"""Post hoc calibration check, not in the registered plan: the distribution of the
registered leave-one-out AUC (analyze.loo_auc) when features are pure noise, at the
analysis's sample size and class balance (13 DM, 14 Control).

Usage: python code/loo_null.py <out.json>
"""
import json
import sys

import numpy as np

sys.path.insert(0, "code")
from analyze import loo_auc  # noqa: E402

SEED = 20261008
REPS = 500


def main():
    rng = np.random.default_rng(SEED)
    y = np.r_[np.ones(13), np.zeros(14)].astype(int)
    out = {}
    for k in (1, 4):
        v = np.array([loo_auc(rng.normal(size=(len(y), k)), y) for _ in range(REPS)])
        out[f"features_{k}"] = {"replicates": REPS, "median_auc": round(float(np.median(v)), 3),
                                "share_at_or_below_0_1": round(float(np.mean(v <= 0.1)), 3)}
    with open(sys.argv[1], "w") as fh:
        json.dump(out, fh, indent=2, sort_keys=True)
        fh.write("\n")
    print(out)


if __name__ == "__main__":
    main()
