File size: 2,483 Bytes
3f316da | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | #!/usr/bin/env python3
"""Regression tests for the locked 50-stock sampling protocol."""
from __future__ import annotations
import sys
import unittest
from pathlib import Path
import pandas as pd
ROOT = Path(__file__).resolve().parents[3]
SCRIPT_DIR = ROOT / "experiments/tbd_revision/scripts"
sys.path.insert(0, str(SCRIPT_DIR))
from make_ashare_50_stock_config import select_representative_stocks # noqa: E402
class SamplingProtocolTests(unittest.TestCase):
def test_selection_is_invariant_to_label_balance(self) -> None:
rows = []
bins = ("low", "mid", "high")
for index in range(18):
rows.append(
{
"stock": f"stock_{index:02d}",
"board": "SZSE Main",
"liquidity_bin": bins[index % 3],
"volatility_bin": bins[(index // 3) % 3],
"spread_bin": bins[(index // 9) % 3],
"label_down_ratio": 0.2,
"label_flat_ratio": 0.6,
"label_up_ratio": 0.2,
"label_imbalance": 0.6,
"label_imbalance_bin": "mid",
}
)
eligible = pd.DataFrame(rows)
anchors = {"stock_00"}
selected_a, _, _ = select_representative_stocks(
eligible,
target_size=9,
seed=2026,
anchors=anchors,
board_allocation="proportional",
)
perturbed = eligible.copy()
perturbed["label_down_ratio"] = [index / 100 for index in range(18)]
perturbed["label_flat_ratio"] = [1.0 - index / 100 for index in range(18)]
perturbed["label_up_ratio"] = 0.0
perturbed["label_imbalance"] = perturbed[
["label_down_ratio", "label_flat_ratio", "label_up_ratio"]
].max(axis=1)
perturbed["label_imbalance_bin"] = [
bins[(index * 2) % 3] for index in range(18)
]
selected_b, _, _ = select_representative_stocks(
perturbed,
target_size=9,
seed=2026,
anchors=anchors,
board_allocation="proportional",
)
self.assertEqual(
list(selected_a["stock"]),
list(selected_b["stock"]),
)
self.assertEqual(
list(selected_a["selection_order"]),
list(selected_b["selection_order"]),
)
if __name__ == "__main__":
unittest.main()
|