Download code/experiments/tbd_revision/tests/test_sampling_protocol.py from mythezone/LOBench-A-share-processed: direct link, hf CLI and curl.
- Browser
- Download file 2.48 kB
-
https://huggingface.co/datasets/mythezone/LOBench-A-share-processed/resolve/main/code/experiments/tbd_revision/tests/test_sampling_protocol.py
- Command line
-
hf download hf://datasets/mythezone/LOBench-A-share-processed/code/experiments/tbd_revision/tests/test_sampling_protocol.py
-
curl -L -o test_sampling_protocol.py https://huggingface.co/datasets/mythezone/LOBench-A-share-processed/resolve/main/code/experiments/tbd_revision/tests/test_sampling_protocol.py
2.48 kB
| #!/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() | |