"""固定数据下的单阈值成员集合检查,不使用收益选择阈值。""" import hashlib import json rows = [("AAA", 17, .12), ("BBB", 18, .12), ("CCC", 20, .12), ("DDD", 21, .12), ("EEE", 23, .12), ("FFF", 19, .08), ("GGG", None, .12)] snapshot = hashlib.sha256(json.dumps(rows).encode()).hexdigest() grid = [18, 20, 22] members = [{symbol for symbol, pe, growth in rows if pe is not None and 0 < pe < cutoff and growth >= .10} for cutoff in grid] def check_nested(sets): if any(not left <= right for left, right in zip(sets, sets[1:])): raise ValueError("non_monotone_membership") check_nested(members) stable = set.intersection(*members) union = set.union(*members) assert [sorted(s) for s in members] == [["AAA"], ["AAA", "BBB"], ["AAA", "BBB", "CCC", "DDD"]] assert stable == {"AAA"} and union-stable == {"BBB", "CCC", "DDD"} assert sum(map(len, members)) == 7 and len(union) == 4 assert hashlib.sha256(json.dumps(rows).encode()).hexdigest() == snapshot try: check_nested([{ "AAA" }, { "BBB" }]) except ValueError as exc: assert str(exc) == "non_monotone_membership" else: raise AssertionError("非单调配置必须拒绝") print(json.dumps({"grid": grid, "members": [sorted(s) for s in members], "stable": sorted(stable), "boundary": sorted(union-stable), "unique_candidates": len(union), "summed_memberships": 7}, sort_keys=True))