Plots to compare between quantile regression and diffusion
This commit is contained in:
@@ -10,7 +10,9 @@ import plotly.graph_objects as go
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
from scipy.interpolate import CubicSpline
|
||||
|
||||
import matplotlib.pyplot as plt
|
||||
import seaborn as sns
|
||||
import matplotlib.patches as mpatches
|
||||
|
||||
def sample_from_dist(quantiles, preds):
|
||||
if isinstance(preds, torch.Tensor):
|
||||
@@ -261,35 +263,35 @@ class AutoRegressiveQuantileTrainer(AutoRegressiveTrainer):
|
||||
name="test_CRPS_from_samples_transformed", value=np.mean(crps_from_samples_metric)
|
||||
)
|
||||
|
||||
def get_plot_error(
|
||||
self,
|
||||
next_day,
|
||||
predictions,
|
||||
):
|
||||
metric = PinballLoss(quantiles=self.quantiles)
|
||||
fig = go.Figure()
|
||||
# def get_plot_error(
|
||||
# self,
|
||||
# next_day,
|
||||
# predictions,
|
||||
# ):
|
||||
# metric = PinballLoss(quantiles=self.quantiles)
|
||||
# fig = go.Figure()
|
||||
|
||||
next_day_np = next_day.view(-1).cpu().numpy()
|
||||
predictions_np = predictions.cpu().numpy()
|
||||
# next_day_np = next_day.view(-1).cpu().numpy()
|
||||
# predictions_np = predictions.cpu().numpy()
|
||||
|
||||
if True:
|
||||
next_day_np = self.data_processor.inverse_transform(next_day_np)
|
||||
predictions_np = self.data_processor.inverse_transform(predictions_np)
|
||||
# if True:
|
||||
# next_day_np = self.data_processor.inverse_transform(next_day_np)
|
||||
# predictions_np = self.data_processor.inverse_transform(predictions_np)
|
||||
|
||||
# for each time step, calculate the error using the metric
|
||||
errors = []
|
||||
for i in range(96):
|
||||
# # for each time step, calculate the error using the metric
|
||||
# errors = []
|
||||
# for i in range(96):
|
||||
|
||||
target_tensor = torch.tensor(next_day_np[i]).unsqueeze(0)
|
||||
prediction_tensor = torch.tensor(predictions_np[i]).unsqueeze(0)
|
||||
# target_tensor = torch.tensor(next_day_np[i]).unsqueeze(0)
|
||||
# prediction_tensor = torch.tensor(predictions_np[i]).unsqueeze(0)
|
||||
|
||||
errors.append(metric(prediction_tensor, target_tensor))
|
||||
# errors.append(metric(prediction_tensor, target_tensor))
|
||||
|
||||
# plot the error
|
||||
fig.add_trace(go.Scatter(x=np.arange(96), y=errors, name=metric.__class__.__name__))
|
||||
fig.update_layout(title=f"Error of {metric.__class__.__name__} for each time step")
|
||||
# # plot the error
|
||||
# fig.add_trace(go.Scatter(x=np.arange(96), y=errors, name=metric.__class__.__name__))
|
||||
# fig.update_layout(title=f"Error of {metric.__class__.__name__} for each time step")
|
||||
|
||||
return fig
|
||||
# return fig
|
||||
|
||||
|
||||
def get_plot(
|
||||
@@ -312,26 +314,59 @@ class AutoRegressiveQuantileTrainer(AutoRegressiveTrainer):
|
||||
next_day_np = self.data_processor.inverse_transform(next_day_np)
|
||||
predictions_np = self.data_processor.inverse_transform(predictions_np)
|
||||
|
||||
ci_99_upper = np.quantile(predictions_np, 0.995, axis=0)
|
||||
ci_99_lower = np.quantile(predictions_np, 0.005, axis=0)
|
||||
|
||||
ci_95_upper = np.quantile(predictions_np, 0.975, axis=0)
|
||||
ci_95_lower = np.quantile(predictions_np, 0.025, axis=0)
|
||||
|
||||
ci_90_upper = np.quantile(predictions_np, 0.95, axis=0)
|
||||
ci_90_lower = np.quantile(predictions_np, 0.05, axis=0)
|
||||
|
||||
ci_50_lower = np.quantile(predictions_np, 0.25, axis=0)
|
||||
ci_50_upper = np.quantile(predictions_np, 0.75, axis=0)
|
||||
|
||||
# Add traces for current and next day
|
||||
fig.add_trace(go.Scatter(x=np.arange(96), y=current_day_np, name="Current Day"))
|
||||
fig.add_trace(go.Scatter(x=96 + np.arange(96), y=next_day_np, name="Next Day"))
|
||||
# fig.add_trace(go.Scatter(x=np.arange(96), y=current_day_np, name="Current Day"))
|
||||
# fig.add_trace(go.Scatter(x=96 + np.arange(96), y=next_day_np, name="Next Day"))
|
||||
|
||||
for i, q in enumerate(self.quantiles):
|
||||
fig.add_trace(
|
||||
go.Scatter(
|
||||
x=96 + np.arange(96),
|
||||
y=predictions_np[:, i],
|
||||
name=f"Prediction (Q={q})",
|
||||
line=dict(dash="dash"),
|
||||
)
|
||||
)
|
||||
# for i, q in enumerate(self.quantiles):
|
||||
# fig.add_trace(
|
||||
# go.Scatter(
|
||||
# x=96 + np.arange(96),
|
||||
# y=predictions_np[:, i],
|
||||
# name=f"Prediction (Q={q})",
|
||||
# line=dict(dash="dash"),
|
||||
# )
|
||||
# )
|
||||
|
||||
# Update the layout
|
||||
fig.update_layout(
|
||||
title="Predictions and Quantiles of the Linear Model",
|
||||
showlegend=show_legend,
|
||||
)
|
||||
# # Update the layout
|
||||
# fig.update_layout(
|
||||
# title="Predictions and Quantiles of the Linear Model",
|
||||
# showlegend=show_legend,
|
||||
# )
|
||||
|
||||
sns.set_theme()
|
||||
time_steps = np.arange(0, 96)
|
||||
|
||||
fig, ax = plt.subplots(figsize=(20, 10))
|
||||
ax.plot(time_steps, predictions_np.mean(axis=0), label="Mean of NRV samples", linewidth=3)
|
||||
# ax.fill_between(time_steps, ci_lower, ci_upper, color='b', alpha=0.2, label='Full Interval')
|
||||
|
||||
ax.fill_between(time_steps, ci_99_lower, ci_99_upper, color='b', alpha=0.2, label='99% Interval')
|
||||
ax.fill_between(time_steps, ci_95_lower, ci_95_upper, color='b', alpha=0.2, label='95% Interval')
|
||||
ax.fill_between(time_steps, ci_90_lower, ci_90_upper, color='b', alpha=0.2, label='90% Interval')
|
||||
ax.fill_between(time_steps, ci_50_lower, ci_50_upper, color='b', alpha=0.2, label='50% Interval')
|
||||
|
||||
ax.plot(next_day_np, label="Real NRV", linewidth=3)
|
||||
# full_interval_patch = mpatches.Patch(color='b', alpha=0.2, label='Full Interval')
|
||||
ci_99_patch = mpatches.Patch(color='b', alpha=0.3, label='99% Interval')
|
||||
ci_95_patch = mpatches.Patch(color='b', alpha=0.4, label='95% Interval')
|
||||
ci_90_patch = mpatches.Patch(color='b', alpha=0.5, label='90% Interval')
|
||||
ci_50_patch = mpatches.Patch(color='b', alpha=0.6, label='50% Interval')
|
||||
|
||||
|
||||
ax.legend(handles=[ci_99_patch, ci_95_patch, ci_90_patch, ci_50_patch, ax.lines[0], ax.lines[1]])
|
||||
return fig
|
||||
|
||||
def auto_regressive(self, dataset, idx_batch, sequence_length: int = 96):
|
||||
|
||||
Reference in New Issue
Block a user