Download src/utils/analyze_stock_direction.py from GoldenGrapeGentleman1/FinMM-Project-FinMM: direct link, hf CLI and curl.
- Browser
- Download file 3.41 kB
-
https://huggingface.co/GoldenGrapeGentleman1/FinMM-Project-FinMM/resolve/main/src/utils/analyze_stock_direction.py
- Command line
-
hf download hf://GoldenGrapeGentleman1/FinMM-Project-FinMM/src/utils/analyze_stock_direction.py
-
curl -L -o analyze_stock_direction.py https://huggingface.co/GoldenGrapeGentleman1/FinMM-Project-FinMM/resolve/main/src/utils/analyze_stock_direction.py
3.41 kB
| """ | |
| 统计训练数据中股票的实际涨跌分布 | |
| 用于建立方向预测的公平baseline | |
| """ | |
| import pandas as pd | |
| import numpy as np | |
| import os | |
| import sys | |
| print("=== 100只股票涨跌统计分析 ===\n") | |
| DATA_DIR = '/localhome/local-yueyuan/fin-mm/data/finmultitime_hf/time_series' | |
| seq_len, pred_len = 96, 20 | |
| stock_count = 0 | |
| max_stocks = 100 | |
| # 统计数据 | |
| stats = { | |
| 'train': {'up': 0, 'down': 0, 'total': 0}, | |
| 'val': {'up': 0, 'down': 0, 'total': 0}, | |
| 'test': {'up': 0, 'down': 0, 'total': 0} | |
| } | |
| for market in ['HS300_time_series/HS300_time_series', 'S&P500_time_series/S&P500_time_series']: | |
| if stock_count >= max_stocks: | |
| break | |
| csv_dir = os.path.join(DATA_DIR, market) | |
| if not os.path.exists(csv_dir): | |
| continue | |
| for csv_file in sorted(os.listdir(csv_dir)): | |
| if stock_count >= max_stocks: | |
| break | |
| if csv_file.endswith('.csv'): | |
| try: | |
| df = pd.read_csv(f'{csv_dir}/{csv_file}') | |
| if len(df) < 5000: | |
| continue | |
| df['Date'] = pd.to_datetime(df['Date'], utc=True).dt.tz_convert(None) | |
| # 按时间段划分 | |
| for split_name, (start, end) in [ | |
| ('train', ('2006-01-01', '2022-12-31')), | |
| ('val', ('2023-01-01', '2023-12-31')), | |
| ('test', ('2024-01-01', '2025-12-31')) | |
| ]: | |
| df_split = df[(df['Date'] >= start) & (df['Date'] <= end)] | |
| if len(df_split) < seq_len + pred_len: | |
| continue | |
| data = df_split[['Open', 'High', 'Low', 'Close', 'Volume']].values | |
| # 滑动窗口,统计每个样本的实际涨跌 | |
| for i in range(len(data) - seq_len - pred_len): | |
| # 预测窗口的Close价格 | |
| pred_prices = data[i + seq_len:i + seq_len + pred_len, 3] | |
| # 判断:最后一天 vs 第一天 | |
| direction = 1 if pred_prices[-1] > pred_prices[0] else 0 | |
| stats[split_name]['total'] += 1 | |
| if direction == 1: | |
| stats[split_name]['up'] += 1 | |
| else: | |
| stats[split_name]['down'] += 1 | |
| stock_count += 1 | |
| except Exception as e: | |
| continue | |
| print(f"统计股票数: {stock_count}\n") | |
| # 输出统计结果 | |
| for split_name in ['train', 'val', 'test']: | |
| s = stats[split_name] | |
| if s['total'] > 0: | |
| up_pct = s['up'] / s['total'] * 100 | |
| down_pct = s['down'] / s['total'] * 100 | |
| print(f"{split_name.upper()}集:") | |
| print(f" 总样本: {s['total']}") | |
| print(f" 上涨: {s['up']} ({up_pct:.1f}%)") | |
| print(f" 下跌: {s['down']} ({down_pct:.1f}%)") | |
| print(f" Baseline(预测全涨): {up_pct:.1f}%") | |
| print(f" Baseline(预测全跌): {down_pct:.1f}%") | |
| print() | |
| print("=== 结论 ===") | |
| print("如果分布接近50-50,说明市场平衡,方向预测确实困难") | |
| print("如果明显偏向一边,说明简单策略就能达到高准确率") | |