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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import plotly.graph_objects as go
2
+ import pandas as pd
3
+ import numpy as np
4
+ import seaborn as sns
5
+ import networkx as nx
6
+
7
+ def plot_forecast(result):
8
+ """Interactive backtest plot with zoom and pan functionality using Plotly"""
9
+ forecast = result["forecast"]
10
+ actual = result["actual"]
11
+
12
+ # Convert to numpy arrays and flatten if needed
13
+ forecast = np.array(forecast).flatten()
14
+ actual = np.array(actual).flatten()
15
+
16
+ # Ensure both arrays have the same length
17
+ min_len = min(len(forecast), len(actual))
18
+ forecast = forecast[:min_len]
19
+ actual = actual[:min_len]
20
+
21
+ # Create time indices
22
+ time_indices = np.arange(len(actual))
23
+
24
+ # Initialize Plotly figure
25
+ fig = go.Figure()
26
+
27
+ if len(actual) == 0 or len(forecast) == 0:
28
+ fig.add_annotation(
29
+ x=0.5, y=0.5, xref="paper", yref="paper",
30
+ text="No data available for plotting",
31
+ showarrow=False, font=dict(size=12)
32
+ )
33
+ return fig
34
+
35
+ # Plot full historical actual
36
+ fig.add_trace(go.Scatter(
37
+ x=time_indices, y=actual,
38
+ mode='lines', name="Historical Actual",
39
+ line=dict(color="blue", width=2), opacity=0.7
40
+ ))
41
+
42
+ # Plot full historical forecast
43
+ fig.add_trace(go.Scatter(
44
+ x=time_indices, y=forecast,
45
+ mode='lines', name="Historical Forecast",
46
+ line=dict(color="orange", width=2, dash="dash"), opacity=0.7
47
+ ))
48
+
49
+ if len(actual) > 1 and len(forecast) > 1:
50
+ last_idx = len(actual) - 1
51
+
52
+ # Highlight last day actual segment
53
+ last_actual_segment = [float(actual[last_idx-1]), float(actual[last_idx])]
54
+ last_time_segment = [time_indices[last_idx-1], time_indices[last_idx]]
55
+ fig.add_trace(go.Scatter(
56
+ x=last_time_segment, y=last_actual_segment,
57
+ mode='lines', name="Last Day Actual",
58
+ line=dict(color="blue", width=4), showlegend=False
59
+ ))
60
+
61
+ # Add markers for last day comparison
62
+ fig.add_trace(go.Scatter(
63
+ x=[last_idx], y=[float(actual[last_idx])],
64
+ mode='markers', name="Last Day Actual",
65
+ marker=dict(color="blue", size=10, line=dict(color="darkblue", width=2)),
66
+ showlegend=False
67
+ ))
68
+ fig.add_trace(go.Scatter(
69
+ x=[last_idx], y=[float(forecast[last_idx])],
70
+ mode='markers', name="Last Day Predicted",
71
+ marker=dict(color="red", size=10, line=dict(color="darkred", width=2)),
72
+ showlegend=False
73
+ ))
74
+
75
+ # Add value annotations for last day
76
+ actual_val = float(actual[last_idx])
77
+ forecast_val = float(forecast[last_idx])
78
+ fig.add_annotation(
79
+ x=last_idx, y=actual_val,
80
+ text=f"Actual: {actual_val:.2f}",
81
+ showarrow=True, arrowhead=1, ax=20, ay=-30,
82
+ font=dict(size=10, color="white"),
83
+ bgcolor="blue", opacity=0.8, bordercolor="darkblue"
84
+ )
85
+ fig.add_annotation(
86
+ x=last_idx, y=forecast_val,
87
+ text=f"Predicted: {forecast_val:.2f}",
88
+ showarrow=True, arrowhead=1, ax=20, ay=30,
89
+ font=dict(size=10, color="white"),
90
+ bgcolor="red", opacity=0.8, bordercolor="darkred"
91
+ )
92
+ elif len(actual) == 1:
93
+ # Handle single point case
94
+ fig.add_trace(go.Scatter(
95
+ x=[0], y=[float(actual[0])],
96
+ mode='markers', name="Actual",
97
+ marker=dict(color="blue", size=10), showlegend=False
98
+ ))
99
+ fig.add_trace(go.Scatter(
100
+ x=[0], y=[float(forecast[0])],
101
+ mode='markers', name="Predicted",
102
+ marker=dict(color="red", size=10), showlegend=False
103
+ ))
104
+
105
+ # Configure layout
106
+ fig.update_layout(
107
+ xaxis_title="Time Index",
108
+ yaxis_title="Value",
109
+ showlegend=True,
110
+ legend=dict(
111
+ orientation="h",
112
+ yanchor="bottom",
113
+ y=1.1,
114
+ xanchor="center",
115
+ x=0.5
116
+ ),
117
+ hovermode="x unified",
118
+ plot_bgcolor="white",
119
+ grid=dict(rows=1, columns=1),
120
+ xaxis=dict(showgrid=True, gridcolor="rgba(0,0,0,0.1)", gridwidth=0.8),
121
+ yaxis=dict(showgrid=True, gridcolor="rgba(0,0,0,0.1)", gridwidth=0.8),
122
+ margin=dict(t=50) # Reduced top margin to accommodate legend
123
+ )
124
+
125
+ return fig
126
+
127
+
128
+ def plot_future_forecast(df, result, future_df):
129
+ """Interactive future forecast plot with zoom, pan and hover functionality using Plotly"""
130
+ # Initialize Plotly figure
131
+ fig = go.Figure()
132
+
133
+ # Validate and convert data
134
+ if df.empty or 'Date' not in df.columns or 'value' not in df.columns:
135
+ fig.add_annotation(
136
+ x=0.5, y=0.5, xref="paper", yref="paper",
137
+ text="No valid historical data available",
138
+ showarrow=False, font=dict(size=12)
139
+ )
140
+ return fig
141
+
142
+ # Plot historical data
143
+ dates = pd.to_datetime(df['Date'])
144
+ values = np.array(df['value']).flatten()
145
+ fig.add_trace(go.Scatter(
146
+ x=dates, y=values,
147
+ mode='lines', name="Historical Data",
148
+ line=dict(color="blue", width=2.5), opacity=0.9
149
+ ))
150
+
151
+ if "latest_prediction" in result and len(result["latest_prediction"]) > 0:
152
+ # Convert predictions to flat array
153
+ predictions = np.array(result["latest_prediction"]).flatten()
154
+
155
+ # Create future dates
156
+ last_date = dates.iloc[-1] if len(dates) > 0 else pd.Timestamp.now()
157
+ horizon = len(predictions)
158
+
159
+ try:
160
+ future_dates = pd.date_range(start=last_date + pd.Timedelta(days=1), periods=horizon, freq='B')
161
+ except:
162
+ # Fallback to daily frequency if business day fails
163
+ future_dates = pd.date_range(start=last_date + pd.Timedelta(days=1), periods=horizon, freq='D')
164
+
165
+ if len(values) > 0 and len(predictions) > 0:
166
+ # Create connection from last historical point to first prediction
167
+ connection_dates = [last_date, future_dates[0]]
168
+ connection_values = [float(values[-1]), float(predictions[0])]
169
+ fig.add_trace(go.Scatter(
170
+ x=connection_dates, y=connection_values,
171
+ mode='lines', name="Connection",
172
+ line=dict(color="orange", width=2, dash="dot"), opacity=0.7, showlegend=False
173
+ ))
174
+
175
+ # Plot forecast
176
+ predictions_float = [float(p) for p in predictions]
177
+ fig.add_trace(go.Scatter(
178
+ x=future_dates, y=predictions_float,
179
+ mode='lines+markers', name="Forecast",
180
+ line=dict(color="orange", width=3),
181
+ marker=dict(size=8, color="orange", line=dict(color="darkorange", width=2)),
182
+ opacity=0.9
183
+ ))
184
+
185
+ # Plot actual future values if available
186
+ if not future_df.empty and "future_actuals" in result and 'Date' in future_df.columns and 'value' in future_df.columns:
187
+ actual_future_dates = pd.to_datetime(future_df['Date'])
188
+ actual_future_values = np.array(future_df['value']).flatten()
189
+ actual_future_values_float = [float(v) for v in actual_future_values]
190
+
191
+ fig.add_trace(go.Scatter(
192
+ x=actual_future_dates, y=actual_future_values_float,
193
+ mode='lines+markers', name="Actual Future",
194
+ line=dict(color="green", width=3),
195
+ marker=dict(size=8, color="green", line=dict(color="darkgreen", width=2)),
196
+ opacity=0.9
197
+ ))
198
+
199
+ # Configure layout
200
+ fig.update_layout(
201
+ xaxis_title="Date",
202
+ yaxis_title="Stock Price",
203
+ showlegend=True,
204
+ legend=dict(
205
+ orientation="h",
206
+ yanchor="bottom",
207
+ y=1.1,
208
+ xanchor="center",
209
+ x=0.5
210
+ ),
211
+ hovermode="x unified",
212
+ plot_bgcolor="white",
213
+ grid=dict(rows=1, columns=1),
214
+ xaxis=dict(showgrid=True, gridcolor="rgba(0,0,0,0.1)", gridwidth=0.8),
215
+ yaxis=dict(showgrid=True, gridcolor="rgba(0,0,0,0.1)", gridwidth=0.8),
216
+ margin=dict(t=50)
217
+ )
218
+
219
+ return fig
220
+
221
+
222
+ def plot_metrics_precision(result):
223
+ """Plot precision metrics using Plotly"""
224
+ metrics = {k: v for k, v in result['metrics'].items() if k in ['R² (%)', 'Explained Variance (%)', 'MDA (%)'] and v is not None}
225
+ if not metrics:
226
+ fig = go.Figure()
227
+ fig.add_annotation(
228
+ x=0.5, y=0.5, xref="paper", yref="paper",
229
+ text="No valid precision metrics available",
230
+ showarrow=False, font=dict(size=12)
231
+ )
232
+ return fig
233
+
234
+ # Create bar plot
235
+ fig = go.Figure()
236
+ fig.add_trace(go.Bar(
237
+ x=list(metrics.keys()),
238
+ y=list(metrics.values()),
239
+ marker_color=sns.color_palette("Blues_d", len(metrics)).as_hex(),
240
+ text=[f"{v:.2f}%" for v in metrics.values()],
241
+ textposition='auto'
242
+ ))
243
+
244
+ # Configure layout
245
+ max_val = max(metrics.values(), default=100)
246
+ min_val = min(metrics.values(), default=0)
247
+ fig.update_layout(
248
+ yaxis_title="Value (%)",
249
+ showlegend=False,
250
+ plot_bgcolor="white",
251
+ yaxis=dict(range=[min(min_val - 5, -10), max_val + 10], showgrid=True, gridcolor="rgba(0,0,0,0.1)", gridwidth=0.8),
252
+ xaxis=dict(showgrid=False),
253
+ margin=dict(t=50)
254
+ )
255
+
256
+ return fig
257
+
258
+
259
+ def plot_metrics_risk(result):
260
+ """Plot risk metrics using Plotly"""
261
+ metrics = {k: v for k, v in result['metrics'].items() if k in ['RMSE', 'MAE', 'MAPE (%)', 'MASE'] and v is not None}
262
+ if not metrics:
263
+ fig = go.Figure()
264
+ fig.add_annotation(
265
+ x=0.5, y=0.5, xref="paper", yref="paper",
266
+ text="No valid risk metrics available",
267
+ showarrow=False, font=dict(size=12)
268
+ )
269
+ return fig
270
+
271
+ # Create bar plot
272
+ fig = go.Figure()
273
+ fig.add_trace(go.Bar(
274
+ x=list(metrics.keys()),
275
+ y=list(metrics.values()),
276
+ marker_color=sns.color_palette("Reds_d", len(metrics)).as_hex(),
277
+ text=[f"{v:.2f}" for v in metrics.values()],
278
+ textposition='auto'
279
+ ))
280
+
281
+ # Configure layout
282
+ max_val = max(metrics.values(), default=1)
283
+ fig.update_layout(
284
+ yaxis_title="Value",
285
+ showlegend=False,
286
+ plot_bgcolor="white",
287
+ yaxis=dict(range=[0, max_val + 0.2 * max_val], showgrid=True, gridcolor="rgba(0,0,0,0.1)", gridwidth=0.8),
288
+ xaxis=dict(showgrid=False),
289
+ margin=dict(t=50)
290
+ )
291
+
292
+ return fig
293
+
294
+
295
+ def plot_loss_curve(result):
296
+ """Plot loss curve using Plotly"""
297
+ train_losses = result.get('train_loss', [])
298
+ val_losses = result.get('val_loss', [])
299
+
300
+ fig = go.Figure()
301
+ fig.add_trace(go.Scatter(
302
+ x=list(range(len(train_losses))), y=train_losses,
303
+ mode='lines', name="Train Loss",
304
+ line=dict(color="blue", width=2)
305
+ ))
306
+ if val_losses:
307
+ fig.add_trace(go.Scatter(
308
+ x=list(range(len(val_losses))), y=val_losses,
309
+ mode='lines', name="Validation Loss",
310
+ line=dict(color="orange", width=2)
311
+ ))
312
+
313
+ # Configure layout
314
+ fig.update_layout(
315
+ xaxis_title="Epoch",
316
+ yaxis_title="Loss (MSE)",
317
+ showlegend=True,
318
+ legend=dict(
319
+ orientation="h",
320
+ yanchor="bottom",
321
+ y=1.1,
322
+ xanchor="center",
323
+ x=0.5
324
+ ),
325
+ plot_bgcolor="white",
326
+ grid=dict(rows=1, columns=1),
327
+ xaxis=dict(showgrid=True, gridcolor="rgba(0,0,0,0.1)", gridwidth=0.8),
328
+ yaxis=dict(showgrid=True, gridcolor="rgba(0,0,0,0.1)", gridwidth=0.8),
329
+ margin=dict(t=50)
330
+ )
331
+
332
+ return fig
333
+
334
+
335
+ def plot_model_architecture(result):
336
+ """Plot model architecture using matplotlib (static, as Plotly is less suited for network graphs)"""
337
+ fig = plt.figure(figsize=(10, 6))
338
+ ax = fig.add_subplot(111)
339
+ ax.axis('off')
340
+ G = nx.DiGraph()
341
+
342
+ if "architecture" not in result:
343
+ ax.text(0.5, 0.5, "No architecture details available", ha='center', va='center', fontsize=12)
344
+ return fig
345
+
346
+ arch = result["architecture"]
347
+ model_name = arch["model_name"]
348
+ num_layers = arch["num_layers"]
349
+ hidden_units = arch["hidden_units"]
350
+ dropout = arch["dropout"]
351
+ batch_size = arch["batch_size"]
352
+ input_size = arch["input_size"]
353
+ output_size = arch["output_size"]
354
+
355
+ # Handle model-specific hidden units for visualization
356
+ if model_name == "MLPModel":
357
+ hidden_nodes = min(hidden_units[0], 5)
358
+ units_label = f"{hidden_units[0]},{hidden_units[1]}"
359
+ elif model_name == "CNNModel":
360
+ hidden_nodes = 5
361
+ units_label = f"{hidden_units} filters"
362
+ elif model_name == "TransformerModel":
363
+ hidden_nodes = min(hidden_units, 5)
364
+ units_label = f"{hidden_units}"
365
+ else:
366
+ hidden_nodes = min(hidden_units, 5)
367
+ units_label = f"{hidden_units}"
368
+
369
+ # Simplified block diagram for complex models
370
+ if model_name in ["CNNModel", "HybridModel", "CNN_GRU"]:
371
+ G = nx.DiGraph()
372
+ pos = {}
373
+ nodes = []
374
+ y_pos = 0.5
375
+ layer_width = 1.0 / 4
376
+
377
+ if model_name == "CNNModel":
378
+ components = [
379
+ ("Input", f"{input_size} units"),
380
+ ("Conv1D", f"{hidden_units} filters"),
381
+ ("MaxPool", ""),
382
+ ("Output", f"{output_size} units")
383
+ ]
384
+ elif model_name == "HybridModel":
385
+ components = [
386
+ ("Input", f"{input_size} units"),
387
+ ("Conv1D", "32 filters"),
388
+ (f"BiLSTM ({num_layers} layers)", f"{hidden_units*2} units"),
389
+ ("Output", f"{output_size} units")
390
+ ]
391
+ elif model_name == "CNN_GRU":
392
+ components = [
393
+ ("Input", f"{input_size} units"),
394
+ ("Conv1D", "32 filters"),
395
+ (f"GRU ({num_layers} layers)", f"{hidden_units} units"),
396
+ ("Output", f"{output_size} units")
397
+ ]
398
+
399
+ for i, (comp, label) in enumerate(components):
400
+ G.add_node(comp, layer=comp)
401
+ pos[comp] = (i * layer_width, y_pos)
402
+ nodes.append([comp])
403
+ if i > 0:
404
+ G.add_edge(components[i-1][0], comp)
405
+
406
+ nx.draw(G, pos, ax=ax, with_labels=False, node_color='lightblue', edge_color='gray',
407
+ node_size=2000, node_shape='s', arrowsize=10)
408
+
409
+ for node, (x, y) in pos.items():
410
+ label = [comp[1] for comp in components if comp[0] == node][0]
411
+ ax.text(x, y + 0.05, f"{node}\n{label}", ha='center', va='bottom', fontsize=8,
412
+ bbox=dict(facecolor='white', alpha=0.8, edgecolor='black'))
413
+
414
+ else:
415
+ max_nodes_display = 5
416
+ input_nodes = min(input_size, max_nodes_display)
417
+ output_nodes = min(output_size, max_nodes_display)
418
+
419
+ nodes = []
420
+ pos = {}
421
+ layer_width = 1.0 / (num_layers + 2)
422
+ y_pos = 0.5
423
+
424
+ for i in range(input_nodes):
425
+ node = f"input_{i}"
426
+ G.add_node(node, layer="input")
427
+ pos[node] = (0, y_pos + (i - input_nodes / 2) * 0.1)
428
+ nodes.append([f"input_{i}" for i in range(input_nodes)])
429
+
430
+ for layer in range(num_layers):
431
+ layer_nodes = []
432
+ for i in range(hidden_nodes):
433
+ node = f"hidden_{layer}_{i}"
434
+ G.add_node(node, layer=f"hidden_{layer+1}")
435
+ pos[node] = ((layer + 1) * layer_width, y_pos + (i - hidden_nodes / 2) * 0.1)
436
+ layer_nodes.append(node)
437
+ nodes.append(layer_nodes)
438
+
439
+ output_layer_nodes = []
440
+ for i in range(output_nodes):
441
+ node = f"output_{i}"
442
+ G.add_node(node, layer="output")
443
+ pos[node] = ((num_layers + 1) * layer_width, y_pos + (i - output_nodes / 2) * 0.1)
444
+ output_layer_nodes.append(node)
445
+ nodes.append(output_layer_nodes)
446
+
447
+ for layer in range(len(nodes) - 1):
448
+ for src in nodes[layer]:
449
+ for dst in nodes[layer + 1]:
450
+ G.add_edge(src, dst)
451
+
452
+ nx.draw(G, pos, ax=ax, with_labels=False, node_color='lightblue', edge_color='gray',
453
+ node_size=500, arrowsize=10)
454
+
455
+ for node in G.nodes(data=True):
456
+ layer = node[1]['layer']
457
+ x, y = pos[node[0]]
458
+ if layer.startswith("hidden"):
459
+ label = f"Layer {layer.split('_')[1]}: {units_label} units"
460
+ elif layer == "input":
461
+ label = f"Input: {input_size} units"
462
+ elif layer == "output":
463
+ label = f"Output: {output_size} units"
464
+ ax.text(x, y + 0.05, label, ha='center', va='bottom', fontsize=8)
465
+
466
+ # Add model details as annotation
467
+ details = f"Dropout: {dropout:.2f}\nBatch Size: {batch_size}"
468
+ ax.text(0.5, 0.05, details, ha='center', va='bottom', fontsize=10, transform=ax.transAxes,
469
+ bbox=dict(facecolor='white', alpha=0.8, edgecolor='black'))
470
+
471
+ plt.tight_layout()
472
+ return fig