Extract unique forecasts and their probabilities. Args: samples: Array of shape (n_hyp, forecast_length, target_dim) for TimeGrad or (n_hyp, forecast_length, target_dim+1) for timeMCL is_mcl: Whether the samples come from timeMCL model (with scores)
(samples, is_mcl=True)
| 21 | #%% |
| 22 | |
| 23 | def extract_unique_forecasts(samples, is_mcl=True): |
| 24 | """Extract unique forecasts and their probabilities. |
| 25 | |
| 26 | Args: |
| 27 | samples: Array of shape (n_hyp, forecast_length, target_dim) for TimeGrad |
| 28 | or (n_hyp, forecast_length, target_dim+1) for timeMCL |
| 29 | is_mcl: Whether the samples come from timeMCL model (with scores) |
| 30 | """ |
| 31 | n_hyp = samples.shape[0] |
| 32 | |
| 33 | reshaped_forecasts = samples.reshape(n_hyp, -1) |
| 34 | unique_forecasts, unique_indices = np.unique(reshaped_forecasts, axis=0, return_index=True) |
| 35 | hypothesis_forecasts = samples[unique_indices] |
| 36 | |
| 37 | if is_mcl: |
| 38 | # Use provided scores for probabilities |
| 39 | probabilities = np.zeros_like(hypothesis_forecasts) |
| 40 | for i in range(hypothesis_forecasts.shape[0]): |
| 41 | probabilities[i] = 0 |
| 42 | for j in range(samples.shape[0]): |
| 43 | if np.all(samples[j] == hypothesis_forecasts[i]): |
| 44 | probabilities[i] += 1 |
| 45 | probabilities = probabilities / probabilities.sum(axis=0, keepdims=True) |
| 46 | else: |
| 47 | # For TimeGrad, use equal probabilities |
| 48 | probabilities = np.ones_like(hypothesis_forecasts) / probabilities.sum(axis=0, keepdims=True) |
| 49 | |
| 50 | return hypothesis_forecasts, probabilities |
| 51 | |
| 52 | def plot_mcl(target_df, hypothesis_forecasts, forecast_length, rows=4, cols=4, plot_mean=True, context_points=None, freq_type='H', fname='Predictions_plot.png', extract_unique=True, save_path=None, is_mcl=True, plot_p=True, main_color='lightcoral', mean_color='blue', dataset=None, axs=None, ax_cbar=None, global_min_prob=None, global_max_prob=None, dims_to_plot=None, seed=None): |
| 53 | """ |
no outgoing calls
no test coverage detected