File size: 8,326 Bytes
9fa1ee6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/usr/bin/env python3
"""C20: Sector-conditional model — pool training data within sector.

Instead of training per-stock in isolation, each stock's walk-forward window
is augmented with date-aligned rows from its sector peers.  The sector model
then predicts only the target stock's test window.

Sector groups:
  semis:       2330, 2454, 2303
  electronics: 2317, 2382, 2308
  financials:  2881, 2882, 2886
  etfs:        0050, 0056
  telecom:     2412  (solo — no peer benefit, same as baseline)

Hypothesis: pooling intra-sector reduces label noise from cross-sector mixing
(C9 failure) while giving the model more examples of sector-specific patterns
that hurt the worst stocks (2454 semi, 2412 telecom, 2886 financial).
"""
import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT))

import warnings; warnings.filterwarnings("ignore")
import json
import argparse
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import StandardScaler

from scripts.improvement_harness import (
    fetch_df, build_triple_barrier_labels, compute_metrics,
    CURRENT_FEATURES, DEFAULT_STOCKS, EXTENDED_STOCKS,
    PASS_DIR_ACC, PASS_UP_PREC,
    MIN_TRAIN, STEP, LABEL_HORIZON, RF_PARAMS,
)
from models.predictor import _build_features

# All stocks needed for sector pools (superset of both eval sets)
ALL_STOCKS = list(dict.fromkeys(DEFAULT_STOCKS + EXTENDED_STOCKS))

SECTOR_MAP = {
    "semis":       ["2330", "2454", "2303"],
    "electronics": ["2317", "2382", "2308"],
    "financials":  ["2881", "2882", "2886"],
    "etfs":        ["0050", "0056"],
    "telecom":     ["2412"],
}

# Sectors where intra-sector businesses are too heterogeneous to benefit from pooling.
# electronics: Foxconn (contract mfg) / Delta (power components) / Quanta (laptop ODM)
# are structurally different — pooling adds noise, not signal.
NO_POOL_SECTORS = {"electronics", "telecom"}

# Reverse map: stock → sector name
STOCK_SECTOR = {s: sec for sec, stocks in SECTOR_MAP.items() for s in stocks}


def _get_close(df):
    return (df.set_index("date")["close"] if "date" in df.columns else df["close"]).values


def _get_dates(df):
    if "date" in df.columns:
        return df["date"].values
    return None


def walk_forward_sector(target_no, target_feat, target_labels, target_dates,
                        peers: list[tuple], cols):
    """Walk-forward on target stock, training augmented with sector peer rows."""
    avail = [c for c in cols if c in target_feat.columns]
    X_target = target_feat[avail].fillna(0).values
    n = len(target_feat)

    y_true_all, y_pred_all = [], []

    cutoff = MIN_TRAIN
    while cutoff + STEP + LABEL_HORIZON <= n:
        train_end = cutoff - LABEL_HORIZON
        if train_end < MIN_TRAIN - LABEL_HORIZON:
            cutoff += STEP; continue

        y_tr  = target_labels[:train_end]
        valid = ~np.isnan(y_tr)
        y_v   = y_tr[valid].astype(int)
        if len(y_v) < 10 or len(np.unique(y_v)) < 2:
            cutoff += STEP; continue

        X_train_list = [X_target[:train_end][valid]]
        y_train_list = [y_v]

        # Cutoff date for peer alignment (avoid future leakage)
        cutoff_date = target_dates[train_end - 1] if (target_dates is not None and train_end > 0) else None

        for (peer_feat, peer_labels, peer_dates) in peers:
            peer_avail = [c for c in cols if c in peer_feat.columns]

            if cutoff_date is not None and peer_dates is not None:
                peer_end = int((peer_dates <= cutoff_date).sum())
            else:
                peer_end = int(train_end * len(peer_feat) / n)

            peer_end = min(peer_end, len(peer_feat) - LABEL_HORIZON)
            if peer_end < 15:
                continue

            y_p   = peer_labels[:peer_end]
            valid_p = ~np.isnan(y_p)
            y_vp  = y_p[valid_p].astype(int)
            if len(y_vp) < 5 or len(np.unique(y_vp)) < 2:
                continue

            X_p = peer_feat[peer_avail].fillna(0).values[:peer_end][valid_p]
            X_train_list.append(X_p)
            y_train_list.append(y_vp)

        X_train = np.vstack(X_train_list)
        y_train = np.concatenate(y_train_list)
        if len(np.unique(y_train)) < 2:
            cutoff += STEP; continue

        scaler = StandardScaler()
        X_train_s = scaler.fit_transform(X_train)

        rf = RandomForestClassifier(**RF_PARAMS)
        rf.fit(X_train_s, y_train)

        test_end = min(cutoff + STEP, n - LABEL_HORIZON)
        y_te     = target_labels[cutoff:test_end]
        valid_te = ~np.isnan(y_te)
        if valid_te.sum() == 0:
            cutoff += STEP; continue

        X_te_s = scaler.transform(X_target[cutoff:test_end][valid_te])
        y_pred = rf.predict(X_te_s)

        y_true_all.extend(y_te[valid_te].astype(int).tolist())
        y_pred_all.extend(y_pred.tolist())
        cutoff += STEP

    if not y_true_all:
        return {}
    return compute_metrics(np.array(y_true_all), np.array(y_pred_all))


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--extended", action="store_true")
    args = parser.parse_args()

    eval_stocks = EXTENDED_STOCKS if args.extended else DEFAULT_STOCKS
    tag = "12-stock" if args.extended else "5-stock"

    print(f"\n=== C20: Sector-conditional model [{tag}] ===")
    print("  Loading all stock data...", flush=True)

    # Load all stocks upfront (needed for sector pools)
    stock_data: dict[str, dict] = {}
    for s in ALL_STOCKS:
        df = fetch_df(s)
        if df is None or df.empty:
            print(f"    {s}: no data")
            continue
        feat   = _build_features(df)
        close  = _get_close(df)
        labels = build_triple_barrier_labels(close)
        dates  = _get_dates(feat) if "date" in feat.columns else _get_dates(df)
        stock_data[s] = {"feat": feat, "labels": labels, "dates": dates}
        print(f"    {s}: {len(feat)} rows, sector={STOCK_SECTOR.get(s, '?')}")

    print()
    per_stock, metrics_list = {}, []

    for target_no in eval_stocks:
        if target_no not in stock_data:
            print(f"  {target_no}: missing data, skip")
            continue

        sector = STOCK_SECTOR.get(target_no, "unknown")
        if sector in NO_POOL_SECTORS:
            peer_stocks = []
        else:
            peer_stocks = [s for s in SECTOR_MAP.get(sector, []) if s != target_no and s in stock_data]

        peers = [
            (stock_data[p]["feat"], stock_data[p]["labels"], stock_data[p]["dates"])
            for p in peer_stocks
        ]

        td = stock_data[target_no]
        print(f"  {target_no} [{sector}, peers={peer_stocks}]...", end=" ", flush=True)

        m = walk_forward_sector(
            target_no,
            td["feat"], td["labels"], td["dates"],
            peers, CURRENT_FEATURES,
        )

        per_stock[target_no] = {**m, "sector": sector, "peers": peer_stocks}
        metrics_list.append(m)
        if m:
            print(f"dir={m['dir_accuracy']}%  ↑prec={m['up_precision']}%")
        else:
            print("no output")

    def _avg(key):
        vals = [m[key] for m in metrics_list
                if m and not np.isnan(m.get(key, float("nan")))]
        return round(float(np.mean(vals)), 1) if vals else float("nan")

    avg_dir = _avg("dir_accuracy")
    avg_up  = _avg("up_precision")
    passed  = avg_dir >= PASS_DIR_ACC and avg_up >= PASS_UP_PREC

    print(f"\n  Avg: dir={avg_dir}%  ↑prec={avg_up}%")
    print(f"  Gate (dir≥{PASS_DIR_ACC}% AND ↑prec≥{PASS_UP_PREC}%): {'PASS ✓' if passed else 'FAIL ✗'}")

    result = {
        "experiment":  "C20",
        "description": "Sector-conditional model: pool training within sector",
        "stocks":      eval_stocks,
        "aggregate":   {"dir_accuracy": avg_dir, "up_precision": avg_up},
        "per_stock":   per_stock,
        "passed":      passed,
        "pass_gate":   {"dir_accuracy": PASS_DIR_ACC, "up_precision": PASS_UP_PREC},
    }

    suffix = "_12stock" if args.extended else ""
    out    = ROOT / f"docs/c20_result{suffix}.json"
    out.parent.mkdir(exist_ok=True)
    out.write_text(json.dumps(result, indent=2))
    print(f"\n  Saved: {out}")


if __name__ == "__main__":
    main()