FinMM-Project-FinMM / src /utils /plot_prediction_curves.py
GoldenGrapeGentleman1's picture
Upload Project_FinMM before machine wipe (part 2)
d702e58 verified
Raw History Blame Contribute Delete
10 kB
"""
Generate prediction curves visualization for paper
Shows PRISM vs all SOTA baselines on representative test samples
"""
import matplotlib.pyplot as plt
import numpy as np
from pathlib import Path
# Set publication-quality settings
plt.rcParams['font.family'] = 'serif'
plt.rcParams['font.size'] = 10
plt.rcParams['axes.linewidth'] = 0.8
plt.rcParams['grid.alpha'] = 0.3
def generate_prediction_curves():
"""
Generate Fig. 4: Prediction vs ground truth for 3 representative stocks
- AAPL: Uptrend case
- MSFT: Downtrend case
- SPY: Volatile case
"""
# Color scheme EXACTLY matching original figure
colors = {
'historical': '#5B9BD5', # Blue (historical, days 0-100)
'ground_truth': '#000000', # Black (complete truth, days 0-120)
'prism': '#FF0000', # Red (PRISM - OUR METHOD)
'dlinear': '#70AD47', # Green
'tsmixer': '#FFC000', # Orange
'timesnet': '#C55A11', # Dark orange
'autoformer': '#7030A0', # Purple
'patchtst': '#DA9694', # Light pink
'informer': '#44546A', # Dark gray
'itransformer': '#00B0F0', # Light blue
}
# Create sample data (NOTE: Based on real MAPE from logs, but positions are simulated)
np.random.seed(42)
days = np.arange(0, 120)
pred_days = np.arange(100, 120)
# AAPL - Uptrend (Days 0-120)
aapl_ground_truth = 150 + np.linspace(0, 18, 120) + np.cumsum(np.random.randn(120) * 0.3)
aapl_historical = aapl_ground_truth[:100] # Historical = first 100 days
# Model predictions (last 20 days) - simulate based on MAPE from logs
pred_days = np.arange(100, 120)
# AAPL predictions (MAPE as error scale)
aapl_prism = aapl_ground_truth[100:] + np.random.randn(20) * 0.88 * aapl_ground_truth[100:].mean() / 100
aapl_dlinear = aapl_ground_truth[100:] + np.random.randn(20) * 1.32 * aapl_ground_truth[100:].mean() / 100
aapl_tsmixer = aapl_ground_truth[100:] + np.random.randn(20) * 1.61 * aapl_ground_truth[100:].mean() / 100
aapl_timesnet = aapl_ground_truth[100:] + np.random.randn(20) * 1.68 * aapl_ground_truth[100:].mean() / 100
aapl_autoformer = aapl_ground_truth[100:] + np.random.randn(20) * 2.50 * aapl_ground_truth[100:].mean() / 100
aapl_patchtst = aapl_ground_truth[100:] + np.random.randn(20) * 2.65 * aapl_ground_truth[100:].mean() / 100
aapl_informer = aapl_ground_truth[100:] + np.random.randn(20) * 2.77 * aapl_ground_truth[100:].mean() / 100
aapl_itransformer = aapl_ground_truth[100:] + np.random.randn(20) * 7.17 * aapl_ground_truth[100:].mean() / 100
# MSFT - Downtrend
msft_ground_truth = 180 - np.linspace(0, 20, 120) + np.cumsum(np.random.randn(120) * 0.4)
msft_historical = msft_ground_truth[:100]
msft_prism = msft_ground_truth[100:] + np.random.randn(20) * 0.88 * msft_ground_truth[100:].mean() / 100
msft_dlinear = msft_ground_truth[100:] + np.random.randn(20) * 1.32 * msft_ground_truth[100:].mean() / 100
msft_tsmixer = msft_ground_truth[100:] + np.random.randn(20) * 1.61 * msft_ground_truth[100:].mean() / 100
msft_timesnet = msft_ground_truth[100:] + np.random.randn(20) * 1.68 * msft_ground_truth[100:].mean() / 100
msft_autoformer = msft_ground_truth[100:] + np.random.randn(20) * 2.50 * msft_ground_truth[100:].mean() / 100
msft_patchtst = msft_ground_truth[100:] + np.random.randn(20) * 2.65 * msft_ground_truth[100:].mean() / 100
msft_informer = msft_ground_truth[100:] + np.random.randn(20) * 2.77 * msft_ground_truth[100:].mean() / 100
# SPY - Volatile
spy_ground_truth = 90 + 5 * np.sin(np.linspace(0, 4*np.pi, 120)) + np.cumsum(np.random.randn(120) * 0.5)
spy_historical = spy_ground_truth[:100]
spy_prism = spy_ground_truth[100:] + np.random.randn(20) * 0.88 * spy_ground_truth[100:].mean() / 100
spy_dlinear = spy_ground_truth[100:] + np.random.randn(20) * 1.32 * spy_ground_truth[100:].mean() / 100
spy_tsmixer = spy_ground_truth[100:] + np.random.randn(20) * 1.61 * spy_ground_truth[100:].mean() / 100
spy_timesnet = spy_ground_truth[100:] + np.random.randn(20) * 1.68 * spy_ground_truth[100:].mean() / 100
spy_autoformer = spy_ground_truth[100:] + np.random.randn(20) * 2.50 * spy_ground_truth[100:].mean() / 100
spy_patchtst = spy_ground_truth[100:] + np.random.randn(20) * 2.65 * spy_ground_truth[100:].mean() / 100
spy_informer = spy_ground_truth[100:] + np.random.randn(20) * 2.77 * spy_ground_truth[100:].mean() / 100
# Create figure with 3 subplots
fig, axes = plt.subplots(1, 3, figsize=(15, 4))
# Plot 1: AAPL (Uptrend) - WITH LEGEND
# Historical (blue thin line, days 0-100)
axes[0].plot(days[:100], aapl_historical, color=colors['historical'],
linewidth=1.2, label='Historical', linestyle='-')
# Ground Truth (black thick line, days 0-120)
axes[0].plot(days, aapl_ground_truth, color=colors['ground_truth'],
linewidth=2.5, label='Ground Truth', linestyle='-')
# PRISM (red thick line with markers, days 100-120) - HIGHLIGHTED
axes[0].plot(pred_days, aapl_prism, color=colors['prism'],
linewidth=2.5, label='PRISM (Ours)', marker='o', markersize=5)
# ALL Baselines (dashed lines, days 100-120)
axes[0].plot(pred_days, aapl_dlinear, color=colors['dlinear'],
linewidth=1.5, label='DLinear', linestyle='--')
axes[0].plot(pred_days, aapl_tsmixer, color=colors['tsmixer'],
linewidth=1.5, label='TSMixer', linestyle='--')
axes[0].plot(pred_days, aapl_timesnet, color=colors['timesnet'],
linewidth=1.5, label='TimesNet', linestyle='--')
axes[0].plot(pred_days, aapl_autoformer, color=colors['autoformer'],
linewidth=1.5, label='Autoformer', linestyle='--')
axes[0].plot(pred_days, aapl_patchtst, color=colors['patchtst'],
linewidth=1.5, label='PatchTST', linestyle='--')
axes[0].plot(pred_days, aapl_informer, color=colors['informer'],
linewidth=1.5, label='Informer', linestyle='--')
axes[0].plot(pred_days, aapl_itransformer, color=colors['itransformer'],
linewidth=1.5, label='iTransformer', linestyle='--')
axes[0].set_title('AAPL (Uptrend)', fontweight='bold', fontsize=12)
axes[0].set_xlabel('Days', fontsize=10)
axes[0].set_ylabel('Price ($)', fontsize=10)
axes[0].legend(fontsize=7.5, loc='upper left', frameon=True, fancybox=False, edgecolor='black')
axes[0].grid(True, alpha=0.2)
# Plot 2: MSFT (Downtrend) - NO LEGEND
# Historical (blue thin line)
axes[1].plot(days[:100], msft_historical, color=colors['historical'], linewidth=1.2)
# Ground Truth (black thick line)
axes[1].plot(days, msft_ground_truth, color=colors['ground_truth'], linewidth=2)
# PRISM (red thick line with markers)
axes[1].plot(pred_days, msft_prism, color=colors['prism'], linewidth=2.5, marker='o', markersize=5)
# ALL Baselines
axes[1].plot(pred_days, msft_dlinear, color=colors['dlinear'], linewidth=1.5, linestyle='--')
axes[1].plot(pred_days, msft_tsmixer, color=colors['tsmixer'], linewidth=1.5, linestyle='--')
axes[1].plot(pred_days, msft_timesnet, color=colors['timesnet'], linewidth=1.5, linestyle='--')
axes[1].plot(pred_days, msft_autoformer, color=colors['autoformer'], linewidth=1.5, linestyle='--')
axes[1].plot(pred_days, msft_patchtst, color=colors['patchtst'], linewidth=1.5, linestyle='--')
axes[1].plot(pred_days, msft_informer, color=colors['informer'], linewidth=1.5, linestyle='--')
axes[1].set_title('MSFT (Downtrend)', fontweight='bold', fontsize=12)
axes[1].set_xlabel('Days', fontsize=10)
axes[1].set_ylabel('Price ($)', fontsize=10)
axes[1].grid(True, alpha=0.2)
# Plot 3: SPY (Volatile) - NO LEGEND
# Historical (blue thin line)
axes[2].plot(days[:100], spy_historical, color=colors['historical'], linewidth=1.2)
# Ground Truth (black thick line)
axes[2].plot(days, spy_ground_truth, color=colors['ground_truth'], linewidth=2)
# PRISM (red thick line with markers)
axes[2].plot(pred_days, spy_prism, color=colors['prism'], linewidth=2.5, marker='o', markersize=5)
# ALL Baselines
axes[2].plot(pred_days, spy_dlinear, color=colors['dlinear'], linewidth=1.5, linestyle='--')
axes[2].plot(pred_days, spy_tsmixer, color=colors['tsmixer'], linewidth=1.5, linestyle='--')
axes[2].plot(pred_days, spy_timesnet, color=colors['timesnet'], linewidth=1.5, linestyle='--')
axes[2].plot(pred_days, spy_autoformer, color=colors['autoformer'], linewidth=1.5, linestyle='--')
axes[2].plot(pred_days, spy_patchtst, color=colors['patchtst'], linewidth=1.5, linestyle='--')
axes[2].plot(pred_days, spy_informer, color=colors['informer'], linewidth=1.5, linestyle='--')
axes[2].set_title('SPY (Volatile)', fontweight='bold', fontsize=12)
axes[2].set_xlabel('Days', fontsize=10)
axes[2].set_ylabel('Price ($)', fontsize=10)
axes[2].grid(True, alpha=0.2)
plt.tight_layout()
# Save
output_path = Path('/home/amd/yueyuan/Vision-TimeSeries/paper/figures/prediction_curves.pdf')
plt.savefig(output_path, bbox_inches='tight', dpi=300)
print(f"✓ Saved: {output_path}")
# Also save PNG version
png_path = output_path.with_suffix('.png')
plt.savefig(png_path, bbox_inches='tight', dpi=150)
print(f"✓ Saved: {png_path}")
plt.close()
if __name__ == "__main__":
print("\n" + "="*60)
print("Generating Prediction Curves for Paper (All Baselines)")
print("="*60 + "\n")
print("NOTE: This generates SAMPLE data for layout.")
print("Replace with actual model predictions from saved checkpoints.\n")
generate_prediction_curves()
print("\n" + "="*60)
print("Done! Review the generated figure and update with real data.")
print("="*60 + "\n")