Spaces:
Sleeping
Sleeping
Download eval_simple.py from ymo2017/jumper-dashboard: direct link, hf CLI and curl.
- Browser
- Download file 4.5 kB
-
https://huggingface.co/spaces/ymo2017/jumper-dashboard/resolve/main/eval_simple.py
- Command line
-
hf download hf://spaces/ymo2017/jumper-dashboard/eval_simple.py
-
curl -L -o eval_simple.py https://huggingface.co/spaces/ymo2017/jumper-dashboard/resolve/main/eval_simple.py
4.5 kB
| import argparse | |
| import pickle | |
| import sys | |
| import os | |
| import pandas as pd | |
| from lib.backtest_utils import evaluate_portfolio | |
| from lib.policy_utils import _load_policy_data, _make_policy | |
| # Ensure we can import from the root | |
| sys.path.append(os.getcwd()) | |
| def get_or_fit_policy(policy_id, df, model_path, label): | |
| """Load model from disk if it exists, otherwise fit a temporary one.""" | |
| if os.path.exists(model_path): | |
| with open(model_path, 'rb') as f: | |
| policy = pickle.load(f) | |
| print(f"[{policy_id}] Loaded {label} model from {model_path}") | |
| return policy | |
| else: | |
| print(f"[{policy_id}] WARNING: {label} model not found at {model_path}. Fitting temporary model...") | |
| policy = _make_policy(policy_id) | |
| if hasattr(policy, 'fit'): | |
| policy.fit(df) | |
| return policy | |
| def main(): | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("--policy", required=True) | |
| parser.add_argument("--model", required=True) | |
| parser.add_argument("--output", required=True) | |
| args = parser.parse_args() | |
| print(f"[SIMPLE_EVAL] {args.policy}") | |
| df = _load_policy_data(args.policy) | |
| start_amount = 1_000_000 | |
| models_dir = "models" | |
| # --- 1. Master Evaluation (2 Year OOS) --- | |
| held_out_2y = 2 * 252 | |
| if len(df) > held_out_2y: | |
| train_df_2y = df.iloc[:-held_out_2y] | |
| oos_2y_start = df.index[-held_out_2y] | |
| else: | |
| train_df_2y = df | |
| oos_2y_start = df.index[-1] | |
| model_2y_path = os.path.join(models_dir, f"{args.policy}_model_2y.pkl") | |
| policy_2y = get_or_fit_policy(args.policy, train_df_2y, model_2y_path, "2-Year Holdout") | |
| print(f"[{args.policy}] Master Evaluation (2y IS / 2y OOS)...") | |
| eval_start_idx = max(0, len(df) - held_out_2y * 2) | |
| eval_df_2y = df.iloc[eval_start_idx:] | |
| values_2y, holdings_2y, metrics_2y = evaluate_portfolio(eval_df_2y, start_amount, policy_2y) | |
| # --- 2. Holdout Evaluation (1 Month OOS) --- | |
| held_out_1m = 21 | |
| if len(df) > held_out_1m: | |
| train_df_1m = df.iloc[:-held_out_1m] | |
| oos_1m_start = df.index[-held_out_1m] | |
| else: | |
| train_df_1m = df | |
| oos_1m_start = df.index[0] | |
| model_1m_path = os.path.join(models_dir, f"{args.policy}_model_1m.pkl") | |
| policy_1m = get_or_fit_policy(args.policy, train_df_1m, model_1m_path, "1-Month Holdout") | |
| print(f"[{args.policy}] Running Holdout Evaluation (Last Month)...") | |
| val_h_full, hold_h_full, met_h_full = evaluate_portfolio(df, start_amount, policy_1m) | |
| values_1m = val_h_full.iloc[-held_out_1m:] | |
| holdings_1m = hold_h_full.iloc[-held_out_1m:] | |
| metrics_1m = met_h_full.iloc[-held_out_1m:] | |
| # Reset PV for holdout period display | |
| ret_1m = values_1m.pct_change().fillna(0) | |
| new_v = [start_amount] | |
| cur = start_amount | |
| for r in ret_1m.iloc[1:]: | |
| cur *= (1 + r) | |
| new_v.append(cur) | |
| values_1m = pd.Series(new_v, index=values_1m.index) | |
| # --- 3. Latest Model --- | |
| # use args.model since that's what the Makefile explicitly provides as the dependency | |
| if os.path.exists(args.model): | |
| with open(args.model, 'rb') as f: | |
| policy_latest = pickle.load(f) | |
| print(f"[{args.policy}] Loaded Latest model from {args.model}") | |
| else: | |
| print(f"[{args.policy}] WARNING: Latest model not found at {args.model}. Fitting temporary model...") | |
| policy_latest = _make_policy(args.policy) | |
| if hasattr(policy_latest, 'fit'): | |
| policy_latest.fit(df) | |
| print(f"[{args.policy}] Running Latest Model Evaluation...") | |
| val_latest, hold_latest, met_latest = evaluate_portfolio(df, start_amount, policy_latest) | |
| result = { | |
| "name": args.policy, | |
| "raw_results": { | |
| "values": values_2y, | |
| "holdings": holdings_2y, | |
| "metrics_df": metrics_2y | |
| }, | |
| "oos_start_date": oos_2y_start, | |
| "holdout_results": { | |
| "values": values_1m, | |
| "holdings": holdings_1m, | |
| "metrics_df": metrics_1m, | |
| "oos_start_date": oos_1m_start, | |
| "values_full": val_h_full, | |
| "holdings_full": hold_h_full | |
| }, | |
| "latest_results": { | |
| "values": val_latest, | |
| "holdings": hold_latest, | |
| "metrics_df": met_latest | |
| } | |
| } | |
| with open(args.output, 'wb') as f: | |
| pickle.dump(result, f) | |
| print(f"[SIMPLE_EVAL] Saved Consolidated Results → {args.output}") | |
| if __name__ == "__main__": | |
| main() | |