from math import isclose # Illustrative synthetic inputs; not execution output. GROUPS = ( (0.1, 10, 2), (0.3, 10, 4), (0.7, 10, 6), (0.9, 10, 8), ) def make_rows(mapped=False): rows = [] for probability, count, positives in GROUPS: p = probability if mapped: p = positives / count for i in range(count): rows.append((p, int(i < positives))) return rows def summarize(rows): n = len(rows) correct = sum( int(p >= 0.5) == y for p, y in rows ) brier = sum((p - y) ** 2 for p, y in rows) / n groups = {} for p, y in rows: groups.setdefault(p, []).append(y) gap = 0.0 for p, labels in groups.items(): fraction = sum(labels) / len(labels) gap += len(labels) * abs(p - fraction) return correct / n, brier, gap / n def main(): cases = ( (False, (0.70, 0.21, 0.10)), (True, (0.70, 0.20, 0.00)), ) for mapped, expected in cases: actual = summarize(make_rows(mapped)) for value, target in zip(actual, expected): assert isclose(value, target, abs_tol=1e-12) print("mapped:", mapped) for name, value in zip( ("accuracy", "brier", "grouped_gap"), actual ): print(name, round(value, 2)) if __name__ == "__main__": main()