"""ε sensitivity: is the per-episode ranking stable, or an artifact of over-wide state clusters?""" import sys, json from pathlib import Path import numpy as np sys.path.insert(0, "/workspace/teleop_std_poc") import os os.environ.setdefault("TSP_ROOT", "/workspace/std_mm_teleop") from action_variance import load, score eps_list = [0.5, 0.3, 0.2, 0.1] episodes = load("isr") S = np.concatenate([p for _, p, _ in episodes]) mu, sd = S.mean(0), S.std(0) + 1e-9 Z = (S - mu) / sd res = {} rank0 = None out = {} for e in eps_list: per, ds, _ = score(episodes, e, 2) names = list(per) vals = np.array([per[n]["score"] if per[n]["score"] is not None else np.nan for n in names]) cov = np.mean([per[n]["scored_points"] / per[n]["total_points"] for n in names]) # mean cluster size, sampled (full N^2 already done inside score, recompute cheaply on a sample) idx = np.random.default_rng(0).choice(len(Z), size=min(2000, len(Z)), replace=False) sizes = [(np.linalg.norm(Z - Z[i], axis=1) <= e).sum() for i in idx] r = np.argsort(np.argsort(np.nan_to_num(vals, nan=-1))) if rank0 is None: rank0 = r corr = 1.0 else: corr = float(np.corrcoef(rank0, r)[0, 1]) out[str(e)] = {"dataset_av": None if ds is None else round(ds, 4), "coverage": round(100 * cov, 1), "mean_cluster_size": round(float(np.mean(sizes)), 1), "median_cluster_size": int(np.median(sizes)), "cluster_pct_of_dataset": round(100 * float(np.mean(sizes)) / len(Z), 2), "rank_corr_vs_eps0.5": round(corr, 3)} print(e, out[str(e)]) Path("/workspace/std_mm_teleop/out/eps_sensitivity.json").write_text(json.dumps( {"note": "per-episode ActionVariance ranking vs cluster radius on ISR-resampled data", "n_points": int(len(Z)), "eps": out}, indent=2)) print("wrote out/eps_sensitivity.json")