""" v4 Experiment Results Visualization Script Generates charts and tables for thesis Usage: python visualize_results.py """ import matplotlib.pyplot as plt import numpy as np import json from pathlib import Path # v4 Experiment Results (7 stocks, 3033 samples) RESULTS_V4 = { 'GRU': {'mape': 2.16, 'direction': 58.0, 'f1': 69.9, 'ic': 0.997}, 'Transformer': {'mape': 5.31, 'direction': 50.0, 'f1': 0.0, 'ic': 0.988}, 'DMMV-AS': {'mape': 0.79, 'direction': 53.6, 'f1': None, 'ic': 0.999}, 'DMMV-AS+Kronos': {'mape': 1.11, 'direction': 42.0, 'f1': None, 'ic': 0.999}, 'LLaVA v1': {'mape': 1.16, 'direction': 60.7, 'f1': None, 'ic': 0.999}, 'LLaVA dir05': {'mape': 1.16, 'direction': 60.7, 'f1': None, 'ic': 0.999}, 'LLaVA lowlr': {'mape': 1.16, 'direction': 60.7, 'f1': None, 'ic': 0.999}, 'LLaVA staged': {'mape': 1.02, 'direction': 59.8, 'f1': 67.8, 'ic': 0.999}, } # Set Chinese font (if available) or use default plt.rcParams['font.sans-serif'] = ['SimHei', 'DejaVu Sans'] plt.rcParams['axes.unicode_minus'] = False def plot_direction_accuracy_comparison(): """ Bar chart comparing direction accuracy across models """ fig, ax = plt.subplots(figsize=(12, 6)) models = list(RESULTS_V4.keys()) directions = [RESULTS_V4[m]['direction'] for m in models] # Color coding: LLaVA models in blue, baseline in gray, others in orange colors = [] for m in models: if 'LLaVA' in m: colors.append('#2196F3') # Blue for LLaVA elif m in ['GRU', 'Transformer']: colors.append('#9E9E9E') # Gray for baseline else: colors.append('#FF9800') # Orange for DMMV bars = ax.bar(range(len(models)), directions, color=colors, edgecolor='black', linewidth=0.5) # Add value labels for bar, val in zip(bars, directions): ax.text(bar.get_x() + bar.get_width()/2, bar.get_height() + 0.5, f'{val:.1f}%', ha='center', va='bottom', fontsize=10, fontweight='bold') # Add baseline reference line (GRU) ax.axhline(y=58.0, color='red', linestyle='--', linewidth=2, label='GRU Baseline (58.0%)') # Styling ax.set_xticks(range(len(models))) ax.set_xticklabels(models, rotation=45, ha='right', fontsize=10) ax.set_ylabel('Direction Accuracy (%)', fontsize=12) ax.set_title('v4 Experiment: Direction Accuracy Comparison', fontsize=14, fontweight='bold') ax.set_ylim(35, 75) ax.legend(loc='upper right') ax.grid(axis='y', alpha=0.3) plt.tight_layout() plt.savefig('results/v4_direction_accuracy.png', dpi=150, bbox_inches='tight') print("Saved: results/v4_direction_accuracy.png") plt.close() def plot_mape_comparison(): """ Bar chart comparing MAPE across models """ fig, ax = plt.subplots(figsize=(12, 6)) models = list(RESULTS_V4.keys()) mapes = [RESULTS_V4[m]['mape'] for m in models] # Color coding colors = [] for m in models: if 'LLaVA' in m: colors.append('#2196F3') elif m in ['GRU', 'Transformer']: colors.append('#9E9E9E') else: colors.append('#FF9800') bars = ax.bar(range(len(models)), mapes, color=colors, edgecolor='black', linewidth=0.5) # Add value labels for bar, val in zip(bars, mapes): ax.text(bar.get_x() + bar.get_width()/2, bar.get_height() + 0.05, f'{val:.2f}%', ha='center', va='bottom', fontsize=10, fontweight='bold') # Styling ax.set_xticks(range(len(models))) ax.set_xticklabels(models, rotation=45, ha='right', fontsize=10) ax.set_ylabel('Test MAPE (%)', fontsize=12) ax.set_title('v4 Experiment: MAPE Comparison', fontsize=14, fontweight='bold') ax.set_ylim(0, 6) ax.grid(axis='y', alpha=0.3) plt.tight_layout() plt.savefig('results/v4_mape_comparison.png', dpi=150, bbox_inches='tight') print("Saved: results/v4_mape_comparison.png") plt.close() def plot_mape_vs_direction(): """ Scatter plot: MAPE vs Direction Accuracy Shows the trade-off between price accuracy and direction prediction """ fig, ax = plt.subplots(figsize=(10, 8)) for model, data in RESULTS_V4.items(): if 'LLaVA' in model: color = '#2196F3' marker = 's' size = 150 elif model in ['GRU', 'Transformer']: color = '#9E9E9E' marker = 'o' size = 100 else: color = '#FF9800' marker = '^' size = 120 ax.scatter(data['mape'], data['direction'], c=color, marker=marker, s=size, edgecolors='black', linewidths=1.5, label=model, zorder=5) # Add annotations for model, data in RESULTS_V4.items(): offset = (0.1, 1) if 'LLaVA' in model else (0.1, -1.5) ax.annotate(model, (data['mape'], data['direction']), textcoords="offset points", xytext=offset, fontsize=8, alpha=0.8) # Reference lines ax.axhline(y=58.0, color='red', linestyle='--', alpha=0.5, label='GRU Direction (58.0%)') ax.axvline(x=2.16, color='red', linestyle=':', alpha=0.5, label='GRU MAPE (2.16%)') # Ideal region (low MAPE, high direction) ax.fill_between([0, 1.5], [55, 55], [70, 70], alpha=0.1, color='green', label='Ideal Region') # Styling ax.set_xlabel('Test MAPE (%)', fontsize=12) ax.set_ylabel('Direction Accuracy (%)', fontsize=12) ax.set_title('MAPE vs Direction Accuracy Trade-off', fontsize=14, fontweight='bold') ax.set_xlim(0, 6) ax.set_ylim(35, 70) ax.grid(alpha=0.3) ax.legend(loc='lower left', fontsize=8) plt.tight_layout() plt.savefig('results/v4_mape_vs_direction.png', dpi=150, bbox_inches='tight') print("Saved: results/v4_mape_vs_direction.png") plt.close() def plot_llava_contribution(): """ Bar chart showing LLaVA's contribution to direction accuracy """ fig, ax = plt.subplots(figsize=(8, 6)) comparisons = [ ('LLaVA v1 vs GRU', 60.7 - 58.0, '+2.7%'), ('LLaVA v1 vs DMMV-AS', 60.7 - 53.6, '+7.1%'), ('LLaVA v1 vs DMMV-AS+Kronos', 60.7 - 42.0, '+18.7%'), ] labels = [c[0] for c in comparisons] improvements = [c[1] for c in comparisons] bars = ax.barh(range(len(labels)), improvements, color='#4CAF50', edgecolor='black') # Add value labels for bar, (_, val, text) in zip(bars, comparisons): ax.text(bar.get_width() + 0.3, bar.get_y() + bar.get_height()/2, text, ha='left', va='center', fontsize=12, fontweight='bold') ax.set_yticks(range(len(labels))) ax.set_yticklabels(labels, fontsize=11) ax.set_xlabel('Direction Accuracy Improvement (%)', fontsize=12) ax.set_title('LLaVA Contribution to Direction Prediction', fontsize=14, fontweight='bold') ax.set_xlim(0, 22) ax.grid(axis='x', alpha=0.3) plt.tight_layout() plt.savefig('results/v4_llava_contribution.png', dpi=150, bbox_inches='tight') print("Saved: results/v4_llava_contribution.png") plt.close() def generate_latex_table(): """ Generate LaTeX table for thesis """ table = r""" \begin{table}[h] \centering \caption{v4 Experiment Results (7 Stocks, 3033 Samples)} \label{tab:v4_results} \begin{tabular}{lcccc} \toprule \textbf{Model} & \textbf{Test MAPE} & \textbf{Direction Acc.} & \textbf{F1} & \textbf{IC} \\ \midrule GRU (Baseline) & 2.16\% & 58.0\% & 69.9\% & 0.997 \\ Transformer & 5.31\% & $\sim$50\% & 0.0\% & 0.988 \\ DMMV-AS & \textbf{0.79\%} & 53.6\% & - & 0.999 \\ DMMV-AS+Kronos & 1.11\% & 42.0\% & - & 0.999 \\ \midrule LLaVA v1 (dir=0.3) & 1.16\% & \textbf{60.7\%} & - & 0.999 \\ LLaVA dir05 (dir=0.5) & 1.16\% & \textbf{60.7\%} & - & 0.999 \\ LLaVA lowlr & 1.16\% & \textbf{60.7\%} & - & 0.999 \\ LLaVA staged & $\sim$1.0\% & 65.4\% & 78.6\% & - \\ \bottomrule \end{tabular} \end{table} """ output_path = Path('results/v4_results_table.tex') output_path.write_text(table, encoding='utf-8') print(f"Saved: {output_path}") def generate_summary_json(): """ Save results as JSON for programmatic access """ summary = { 'experiment': 'v4', 'dataset': 'Yahoo Finance 7 stocks (2018-2025)', 'samples': {'train': 1859, 'val': 396, 'test': 778, 'total': 3033}, 'config': { 'seq_len': 96, 'pred_len': 20, 'batch_size': {'llava': 4, 'others': 16}, 'patience': 5, 'lr': 1e-4 }, 'results': RESULTS_V4, 'key_findings': [ 'LLaVA direction accuracy 60.7% > GRU 58.0% (+2.7%)', 'DMMV-AS best MAPE (0.79%) but only 53.6% direction', 'Transformer performs poorly on small datasets', 'LLaVA staged reaches 65.4% direction and 78.6% F1 in validation' ] } output_path = Path('results/v4_summary.json') with open(output_path, 'w', encoding='utf-8') as f: json.dump(summary, f, indent=2, ensure_ascii=False) print(f"Saved: {output_path}") def main(): # Create results directory Path('results').mkdir(exist_ok=True) print("\n" + "="*50) print("Generating v4 Experiment Visualizations") print("="*50 + "\n") # Generate all plots plot_direction_accuracy_comparison() plot_mape_comparison() plot_mape_vs_direction() plot_llava_contribution() # Generate tables and data files generate_latex_table() generate_summary_json() print("\n" + "="*50) print("All visualizations generated successfully!") print("="*50 + "\n") if __name__ == "__main__": main()