Spaces:
Running
Running
| import pandas as pd | |
| from scripts.validate_accuracy_target_gate import ( | |
| TargetThresholds, | |
| evaluate_accuracy_target, | |
| factor_names_for_configs, | |
| ) | |
| def test_factor_names_union_keeps_candidate_order_then_production_extras(): | |
| candidate = {"weights": {"high_base": 0.02, "short_term_battle": 0.03}, "hold_bias": 0.0} | |
| production = {"weights": {"oldwang_trend": 0.01, "high_base": 0.02}, "hold_bias": 0.0} | |
| assert factor_names_for_configs(candidate, production) == [ | |
| "high_base", | |
| "short_term_battle", | |
| "oldwang_trend", | |
| ] | |
| def test_accuracy_target_gate_passes_against_current_production_baseline(): | |
| rows = [] | |
| for idx in range(12): | |
| bullish = idx in {7, 8, 9} | |
| bearish = idx in {6, 10} | |
| rows.append( | |
| { | |
| "stock": "2330", | |
| "date": f"2026-05-{idx + 1:02d}", | |
| "y_true": 1 if bullish else (-1 if bearish else 0), | |
| "p_buy": 0.44, | |
| "p_hold": 0.48, | |
| "p_sell": 0.08, | |
| "base_pred": 0, | |
| "factor__new_signal": 1.0 if bullish else (-1.0 if bearish else 0.0), | |
| "factor__old_signal": 0.0, | |
| } | |
| ) | |
| result = evaluate_accuracy_target( | |
| pd.DataFrame(rows), | |
| candidate_config={"weights": {"new_signal": 0.05, "old_signal": 0.01}, "hold_bias": 0.0}, | |
| production_config={"weights": {"old_signal": 0.01}, "hold_bias": 0.0}, | |
| optimize_ratio=0.5, | |
| thresholds=TargetThresholds(min_accuracy_delta_pp=1.0), | |
| ) | |
| assert result["passed"] is True | |
| assert result["checks"]["accuracy_delta_at_least_target"] is True | |
| assert result["checks"]["buy_precision_not_down"] is True | |
| assert result["checks"]["sell_precision_not_down"] is True | |
| assert result["checks"]["all_candidate_factor_weights_nonzero"] is True | |
| assert result["price_gate"]["passed"] is True | |
| assert result["proposed_golden_config"]["weights"]["new_signal"] == 0.05 | |
| def test_accuracy_target_gate_blocks_zero_weight_candidate(): | |
| rows = [ | |
| { | |
| "stock": "2330", | |
| "date": f"2026-06-{idx + 1:02d}", | |
| "y_true": 1, | |
| "p_buy": 0.44, | |
| "p_hold": 0.48, | |
| "p_sell": 0.08, | |
| "base_pred": 0, | |
| "factor__new_signal": 1.0, | |
| } | |
| for idx in range(8) | |
| ] | |
| result = evaluate_accuracy_target( | |
| pd.DataFrame(rows), | |
| candidate_config={"weights": {"new_signal": 0.0}, "hold_bias": 0.0}, | |
| production_config={"weights": {"new_signal": 0.01}, "hold_bias": 0.0}, | |
| optimize_ratio=0.5, | |
| thresholds=TargetThresholds(min_accuracy_delta_pp=1.0), | |
| ) | |
| assert result["passed"] is False | |
| assert result["checks"]["all_candidate_factor_weights_nonzero"] is False | |
| assert result["proposed_golden_config"] is None | |