Repository navigation
Expand file tree
/
Copy pathvisualization.py
More file actions
348 lines (280 loc) · 12.4 KB
/
Copy pathvisualization.py
File metadata and controls
348 lines (280 loc) · 12.4 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
"""
Visualization module for plotting results and charts.
"""
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
import plotly.graph_objects as go
from plotly.subplots import make_subplots
class Visualizer:
"""Create visualizations for experiment results."""
def __init__(self, style='seaborn-v0_8-darkgrid'):
"""Initialize visualizer with plotting style."""
try:
plt.style.use(style)
except Exception:
plt.style.use('default')
sns.set_palette("husl")
def plot_price_history(self, df, title="BTC Price History", save_path=None):
"""
Plot historical price data.
Args:
df: DataFrame with OHLCV data
title: Plot title
save_path: Path to save figure (optional)
"""
fig, axes = plt.subplots(2, 1, figsize=(14, 8), sharex=True)
# Price plot
axes[0].plot(df.index, df['close'], label='Close Price', linewidth=1.5)
axes[0].set_ylabel('Price (USD)', fontsize=12)
axes[0].set_title(title, fontsize=14, fontweight='bold')
axes[0].legend()
axes[0].grid(True, alpha=0.3)
# Volume plot
axes[1].bar(df.index, df['volume'], alpha=0.5, label='Volume')
axes[1].set_ylabel('Volume', fontsize=12)
axes[1].set_xlabel('Date', fontsize=12)
axes[1].legend()
axes[1].grid(True, alpha=0.3)
plt.tight_layout()
if save_path:
plt.savefig(save_path, dpi=300, bbox_inches='tight')
print(f"Plot saved to {save_path}")
plt.close()
return fig
def plot_predictions_vs_actual(self, y_true, y_pred, model_name='Model',
save_path=None):
"""
Plot predictions vs actual values.
Args:
y_true: Actual values
y_pred: Predicted values
model_name: Name of the model
save_path: Path to save figure (optional)
"""
fig, axes = plt.subplots(2, 1, figsize=(14, 10))
# Time series comparison
indices = range(len(y_true))
axes[0].plot(indices, y_true, label='Actual', linewidth=1.5, alpha=0.7)
axes[0].plot(indices, y_pred, label='Predicted', linewidth=1.5, alpha=0.7)
axes[0].set_xlabel('Sample Index', fontsize=12)
axes[0].set_ylabel('Value', fontsize=12)
axes[0].set_title(f'{model_name}: Predictions vs Actual', fontsize=14, fontweight='bold')
axes[0].legend()
axes[0].grid(True, alpha=0.3)
# Scatter plot
axes[1].scatter(y_true, y_pred, alpha=0.5)
# Perfect prediction line
min_val = min(y_true.min(), y_pred.min())
max_val = max(y_true.max(), y_pred.max())
axes[1].plot([min_val, max_val], [min_val, max_val], 'r--',
linewidth=2, label='Perfect Prediction')
axes[1].set_xlabel('Actual Values', fontsize=12)
axes[1].set_ylabel('Predicted Values', fontsize=12)
axes[1].set_title('Scatter Plot: Actual vs Predicted', fontsize=14, fontweight='bold')
axes[1].legend()
axes[1].grid(True, alpha=0.3)
plt.tight_layout()
if save_path:
plt.savefig(save_path, dpi=300, bbox_inches='tight')
print(f"Plot saved to {save_path}")
plt.close()
return fig
def plot_training_history(self, history, model_name='Model', save_path=None):
"""
Plot training history for deep learning models.
Args:
history: Training history object
model_name: Name of the model
save_path: Path to save figure (optional)
"""
if history is None:
print("No training history available")
return None
fig, axes = plt.subplots(1, 2, figsize=(14, 5))
# Loss plot
axes[0].plot(history.history['loss'], label='Training Loss', linewidth=2)
if 'val_loss' in history.history:
axes[0].plot(history.history['val_loss'], label='Validation Loss', linewidth=2)
axes[0].set_xlabel('Epoch', fontsize=12)
axes[0].set_ylabel('Loss', fontsize=12)
axes[0].set_title(f'{model_name}: Training Loss', fontsize=14, fontweight='bold')
axes[0].legend()
axes[0].grid(True, alpha=0.3)
# Metric plot
metric_key = 'mae' if 'mae' in history.history else 'accuracy'
axes[1].plot(history.history[metric_key], label=f'Training {metric_key.upper()}', linewidth=2)
if f'val_{metric_key}' in history.history:
axes[1].plot(history.history[f'val_{metric_key}'],
label=f'Validation {metric_key.upper()}', linewidth=2)
axes[1].set_xlabel('Epoch', fontsize=12)
axes[1].set_ylabel(metric_key.upper(), fontsize=12)
axes[1].set_title(f'{model_name}: Training {metric_key.upper()}',
fontsize=14, fontweight='bold')
axes[1].legend()
axes[1].grid(True, alpha=0.3)
plt.tight_layout()
if save_path:
plt.savefig(save_path, dpi=300, bbox_inches='tight')
print(f"Plot saved to {save_path}")
plt.close()
return fig
def plot_model_comparison(self, results_df, metric='rmse', save_path=None):
"""
Compare multiple models.
Args:
results_df: DataFrame with model results
metric: Metric to compare
save_path: Path to save figure (optional)
"""
if results_df.empty:
print("No results to plot")
return None
fig, ax = plt.subplots(figsize=(12, 6))
models = results_df.index
values = results_df[metric]
bars = ax.bar(models, values, alpha=0.7, edgecolor='black')
# Color bars by performance
colors = plt.cm.RdYlGn_r(np.linspace(0.2, 0.8, len(bars)))
for bar, color in zip(bars, colors):
bar.set_color(color)
ax.set_xlabel('Model', fontsize=12)
ax.set_ylabel(metric.upper(), fontsize=12)
ax.set_title(f'Model Comparison: {metric.upper()}', fontsize=14, fontweight='bold')
ax.grid(True, alpha=0.3, axis='y')
# Add value labels on bars
for i, (model, value) in enumerate(zip(models, values)):
ax.text(i, value, f'{value:.4f}', ha='center', va='bottom', fontsize=10)
plt.xticks(rotation=45, ha='right')
plt.tight_layout()
if save_path:
plt.savefig(save_path, dpi=300, bbox_inches='tight')
print(f"Plot saved to {save_path}")
plt.close()
return fig
def plot_feature_importance(self, feature_names, importance_scores,
top_n=20, save_path=None):
"""
Plot feature importance.
Args:
feature_names: List of feature names
importance_scores: Importance scores
top_n: Number of top features to show
save_path: Path to save figure (optional)
"""
# Sort features by importance
indices = np.argsort(importance_scores)[::-1][:top_n]
top_features = [feature_names[i] for i in indices]
top_scores = importance_scores[indices]
fig, ax = plt.subplots(figsize=(10, 8))
y_pos = np.arange(len(top_features))
ax.barh(y_pos, top_scores, alpha=0.7)
ax.set_yticks(y_pos)
ax.set_yticklabels(top_features)
ax.invert_yaxis()
ax.set_xlabel('Importance Score', fontsize=12)
ax.set_title(f'Top {top_n} Feature Importance', fontsize=14, fontweight='bold')
ax.grid(True, alpha=0.3, axis='x')
plt.tight_layout()
if save_path:
plt.savefig(save_path, dpi=300, bbox_inches='tight')
print(f"Plot saved to {save_path}")
plt.close()
return fig
def plot_portfolio_performance(self, portfolio_values, trades=None,
initial_capital=10000, save_path=None):
"""
Plot portfolio performance over time.
Args:
portfolio_values: Series of portfolio values
trades: List of trade dictionaries (optional)
initial_capital: Initial capital
save_path: Path to save figure (optional)
"""
fig, axes = plt.subplots(2, 1, figsize=(14, 10), sharex=True)
# Portfolio value
axes[0].plot(portfolio_values, linewidth=2, label='Portfolio Value')
axes[0].axhline(y=initial_capital, color='r', linestyle='--',
linewidth=1, label='Initial Capital')
if trades:
buy_trades = [t for t in trades if t['type'] == 'BUY']
sell_trades = [t for t in trades if t['type'] == 'SELL']
if buy_trades:
buy_times = [t['time'] for t in buy_trades]
buy_values = [portfolio_values[t['time']] for t in buy_trades]
axes[0].scatter(buy_times, buy_values, color='green',
marker='^', s=100, label='Buy', zorder=5)
if sell_trades:
sell_times = [t['time'] for t in sell_trades]
sell_values = [portfolio_values[t['time']] for t in sell_trades]
axes[0].scatter(sell_times, sell_values, color='red',
marker='v', s=100, label='Sell', zorder=5)
axes[0].set_ylabel('Portfolio Value (USD)', fontsize=12)
axes[0].set_title('Portfolio Performance', fontsize=14, fontweight='bold')
axes[0].legend()
axes[0].grid(True, alpha=0.3)
# Returns
returns = pd.Series(portfolio_values).pct_change() * 100
axes[1].plot(returns, linewidth=1, alpha=0.7, label='Returns')
axes[1].axhline(y=0, color='black', linestyle='-', linewidth=0.5)
axes[1].fill_between(range(len(returns)), returns, 0,
where=(returns > 0), alpha=0.3, color='green')
axes[1].fill_between(range(len(returns)), returns, 0,
where=(returns <= 0), alpha=0.3, color='red')
axes[1].set_xlabel('Time Step', fontsize=12)
axes[1].set_ylabel('Returns (%)', fontsize=12)
axes[1].set_title('Portfolio Returns', fontsize=14, fontweight='bold')
axes[1].legend()
axes[1].grid(True, alpha=0.3)
plt.tight_layout()
if save_path:
plt.savefig(save_path, dpi=300, bbox_inches='tight')
print(f"Plot saved to {save_path}")
plt.close()
return fig
def create_interactive_candlestick(self, df, predictions=None, save_path=None):
"""
Create interactive candlestick chart with Plotly.
Args:
df: DataFrame with OHLCV data
predictions: Optional predictions to overlay
save_path: Path to save HTML file (optional)
"""
fig = make_subplots(rows=2, cols=1, shared_xaxes=True,
vertical_spacing=0.03,
row_heights=[0.7, 0.3])
# Candlestick chart
fig.add_trace(go.Candlestick(
x=df.index,
open=df['open'],
high=df['high'],
low=df['low'],
close=df['close'],
name='BTC Price'
), row=1, col=1)
# Volume bars
colors = ['red' if df['close'].iloc[i] < df['open'].iloc[i] else 'green'
for i in range(len(df))]
fig.add_trace(go.Bar(
x=df.index,
y=df['volume'],
name='Volume',
marker_color=colors,
opacity=0.5
), row=2, col=1)
# Update layout
fig.update_layout(
title='BTC Price and Volume',
yaxis_title='Price (USD)',
yaxis2_title='Volume',
xaxis2_title='Date',
template='plotly_dark',
height=800,
showlegend=True
)
fig.update_xaxes(rangeslider_visible=False)
if save_path:
fig.write_html(save_path)
print(f"Interactive chart saved to {save_path}")
return fig