File size: 6,548 Bytes
9cb3002
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Visualization utilities for plotting training results.
"""

import numpy as np
import pandas as pd
import matplotlib
matplotlib.use("Agg")     # Non-interactive backend for scripts
import matplotlib.pyplot as plt
from typing import List, Dict, Optional
import os


PLOT_DIR = "plots"


def _ensure_plot_dir(save_dir: str = PLOT_DIR):
    os.makedirs(save_dir, exist_ok=True)


def plot_equity_curve(
    episode_values: List[float],
    title: str = "Equity Curve",
    save_path: Optional[str] = None,
):
    """Plot portfolio value over time within an episode."""
    _ensure_plot_dir()
    fig, ax = plt.subplots(figsize=(12, 5))
    ax.plot(episode_values, color="#2196F3", linewidth=1.5)
    ax.set_title(title, fontsize=14)
    ax.set_xlabel("Step")
    ax.set_ylabel("Portfolio Value ($)")
    ax.grid(True, alpha=0.3)
    ax.fill_between(range(len(episode_values)), episode_values,
                    alpha=0.1, color="#2196F3")
    plt.tight_layout()
    path = save_path or os.path.join(PLOT_DIR, "equity_curve.png")
    fig.savefig(path, dpi=150)
    plt.close(fig)
    print(f"Saved: {path}")
    return path


def plot_drawdown(
    episode_values: List[float],
    title: str = "Drawdown Chart",
    save_path: Optional[str] = None,
):
    """Plot drawdown over time within an episode."""
    _ensure_plot_dir()
    values = np.array(episode_values)
    peak = np.maximum.accumulate(values)
    drawdown = (peak - values) / (peak + 1e-10)

    fig, ax = plt.subplots(figsize=(12, 4))
    ax.fill_between(range(len(drawdown)), drawdown, alpha=0.4, color="#F44336")
    ax.plot(drawdown, color="#F44336", linewidth=1)
    ax.set_title(title, fontsize=14)
    ax.set_xlabel("Step")
    ax.set_ylabel("Drawdown (%)")
    ax.grid(True, alpha=0.3)
    ax.invert_yaxis()
    plt.tight_layout()
    path = save_path or os.path.join(PLOT_DIR, "drawdown.png")
    fig.savefig(path, dpi=150)
    plt.close(fig)
    print(f"Saved: {path}")
    return path


def plot_reward_curve(
    metrics: List[Dict],
    baseline_metrics: Optional[List[Dict]] = None,
    title: str = "Reward Curve Across Episodes",
    save_path: Optional[str] = None,
):
    """Plot total reward per episode across training, optionally with baseline."""
    _ensure_plot_dir()
    rewards = [m["total_reward"] for m in metrics]

    fig, ax = plt.subplots(figsize=(12, 5))
    ax.plot(rewards, color="#4CAF50", linewidth=1.5, label="Trained Agent", alpha=0.8)

    # Smoothed trend
    if len(rewards) > 5:
        window = max(5, len(rewards) // 10)
        smoothed = pd.Series(rewards).rolling(window=window, min_periods=1).mean()
        ax.plot(smoothed, color="#2E7D32", linewidth=2.5, label="Trend (smoothed)")

    # Baseline
    if baseline_metrics:
        bl_rewards = [m["total_reward"] for m in baseline_metrics]
        bl_mean = float(np.mean(bl_rewards))
        ax.axhline(y=bl_mean, color="#FF5722", linestyle="--", linewidth=2,
                    label=f"Random Baseline (avg={bl_mean:.3f})")

    ax.set_title(title, fontsize=14)
    ax.set_xlabel("Episode")
    ax.set_ylabel("Total Reward")
    ax.legend()
    ax.grid(True, alpha=0.3)
    plt.tight_layout()
    path = save_path or os.path.join(PLOT_DIR, "reward_curve.png")
    fig.savefig(path, dpi=150)
    plt.close(fig)
    print(f"Saved: {path}")
    return path


def plot_grade_progression(
    metrics: List[Dict],
    baseline_metrics: Optional[List[Dict]] = None,
    title: str = "Grade Progression (0 → 1)",
    save_path: Optional[str] = None,
):
    """Plot grade progression across episodes."""
    _ensure_plot_dir()
    grades = [m["final_grade"] for m in metrics]

    fig, ax = plt.subplots(figsize=(12, 5))
    ax.plot(grades, color="#9C27B0", linewidth=1.5, label="Trained Agent", alpha=0.8)

    if len(grades) > 5:
        window = max(5, len(grades) // 10)
        smoothed = pd.Series(grades).rolling(window=window, min_periods=1).mean()
        ax.plot(smoothed, color="#6A1B9A", linewidth=2.5, label="Trend (smoothed)")

    if baseline_metrics:
        bl_grades = [m["final_grade"] for m in baseline_metrics]
        bl_mean = float(np.mean(bl_grades))
        ax.axhline(y=bl_mean, color="#FF5722", linestyle="--", linewidth=2,
                    label=f"Random Baseline (avg={bl_mean:.3f})")

    ax.set_title(title, fontsize=14)
    ax.set_xlabel("Episode")
    ax.set_ylabel("Grade [0, 1]")
    ax.set_ylim(-0.05, 1.05)
    ax.legend()
    ax.grid(True, alpha=0.3)
    plt.tight_layout()
    path = save_path or os.path.join(PLOT_DIR, "grade_progression.png")
    fig.savefig(path, dpi=150)
    plt.close(fig)
    print(f"Saved: {path}")
    return path


def plot_comparison_table(
    trained_metrics: List[Dict],
    baseline_metrics: List[Dict],
    save_path: Optional[str] = None,
):
    """Create a comparison table figure: random agent vs trained agent."""
    _ensure_plot_dir()

    def avg(metrics, key):
        return np.mean([m[key] for m in metrics])

    data = {
        "Metric": ["Avg Reward", "Avg Grade", "Avg PnL %", "Avg Max DD", "Avg Sharpe"],
        "Random Agent": [
            f"{avg(baseline_metrics, 'total_reward'):.3f}",
            f"{avg(baseline_metrics, 'final_grade'):.3f}",
            f"{avg(baseline_metrics, 'pnl_pct'):.2%}",
            f"{avg(baseline_metrics, 'max_drawdown'):.3f}",
            f"{avg(baseline_metrics, 'sharpe_ratio'):.3f}",
        ],
        "Trained Agent": [
            f"{avg(trained_metrics, 'total_reward'):.3f}",
            f"{avg(trained_metrics, 'final_grade'):.3f}",
            f"{avg(trained_metrics, 'pnl_pct'):.2%}",
            f"{avg(trained_metrics, 'max_drawdown'):.3f}",
            f"{avg(trained_metrics, 'sharpe_ratio'):.3f}",
        ],
    }

    fig, ax = plt.subplots(figsize=(8, 3))
    ax.axis("off")
    table = ax.table(
        cellText=list(zip(data["Metric"], data["Random Agent"], data["Trained Agent"])),
        colLabels=["Metric", "Random Agent", "Trained Agent"],
        cellLoc="center",
        loc="center",
    )
    table.auto_set_font_size(False)
    table.set_fontsize(11)
    table.scale(1.2, 1.8)

    # Style header
    for j in range(3):
        table[0, j].set_facecolor("#37474F")
        table[0, j].set_text_props(color="white", fontweight="bold")

    ax.set_title("Random vs Trained Agent Comparison", fontsize=14, pad=20)
    plt.tight_layout()
    path = save_path or os.path.join(PLOT_DIR, "comparison_table.png")
    fig.savefig(path, dpi=150, bbox_inches="tight")
    plt.close(fig)
    print(f"Saved: {path}")
    return path