|
19 | 19 | MultivariateNormalDistributionLoss, |
20 | 20 | NegativeBinomialDistributionLoss, |
21 | 21 | NormalDistributionLoss, |
| 22 | + QuantileLoss, |
22 | 23 | ) |
23 | 24 | from pytorch_forecasting.metrics.base_metrics import ( |
24 | 25 | AggregationMetric, |
@@ -570,3 +571,22 @@ def test_MASE(): |
570 | 571 |
|
571 | 572 | assert scaling.shape == (batch_size,) |
572 | 573 | assert (scaling > 0).all(), "Scaling should be positive" |
| 574 | + |
| 575 | + |
| 576 | +def test_QuantileLoss_to_prediction_fallback(): |
| 577 | + """Test to_prediction selects median when present, nearest quantile otherwise.""" |
| 578 | + |
| 579 | + loss_with_median = QuantileLoss(quantiles=[0.1, 0.5, 0.9]) |
| 580 | + y_pred_3d = torch.tensor([[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]]) |
| 581 | + result = loss_with_median.to_prediction(y_pred_3d) |
| 582 | + expected = torch.tensor([[2.0, 5.0]]) |
| 583 | + assert torch.equal(result, expected) |
| 584 | + |
| 585 | + loss_no_median = QuantileLoss(quantiles=[0.1, 0.4, 0.9]) |
| 586 | + result_fallback = loss_no_median.to_prediction(y_pred_3d) |
| 587 | + expected_fallback = torch.tensor([[2.0, 5.0]]) |
| 588 | + assert torch.equal(result_fallback, expected_fallback) |
| 589 | + |
| 590 | + y_pred_2d = torch.tensor([[10.0, 20.0]]) |
| 591 | + result_2d = loss_no_median.to_prediction(y_pred_2d) |
| 592 | + assert torch.equal(result_2d, y_pred_2d) |
0 commit comments