---
title: "Scoring multivariate forecasts"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Scoring multivariate forecasts}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```
This Vignette provides an overview about how to score multivariate forecasts.


## Univariate forecasts

Let's start with a simple univariate forecast: The number of cases of COVID-19 in Germany on 2021-05-15, forecasted by the EuroCOVIDhub-ensemble model on 2021-05-03. In our example, this forecast is represented by a set of 40 samples from the predictive distribution.

```{r}
library(scoringutils)

example_univ_single <- example_sample_continuous[
  target_type == "Cases" &
    location == "DE" &
    forecast_date == "2021-05-03" &
    target_end_date == "2021-05-15" &
    horizon == 2 &
    model == "EuroCOVIDhub-ensemble"
]
example_univ_single
```

We can score this forecast and will receive a single score.
```{r}
score(example_univ_single)
```

Now, of course, we can also score multiple similar forecasts at the same time. Let's say we're not only interested in Germany, but other countries as well.

```{r}
example_univ_multi <- example_sample_continuous[
  target_type == "Cases" &
    forecast_date == "2021-05-03" &
    target_end_date == "2021-05-15" &
    horizon == 2 &
    model == "EuroCOVIDhub-ensemble"
]
example_univ_multi
```

Now, we have a set of 4 forecasts for 4 different countries, each of them represented by a set of 40 samples from the predictive distribution.

When we score these forecasts, we will get 4 scores, one for each forecast and observed value.
```{r}
score(example_univ_multi)
```

## Multivariate forecasts

Now, instead of treating the four observations as independent, we could also think of them as a single realisation of a draw from the multivariate distribution of COVID-19 cases across several countries.

The corresponding multivariate forecast would similarly specify a predictive distribution for the number of cases across all 4 countries. The samples are then not draws from four independent distributions, but instead samples from a joint multivariate predictive distribution.

In the following, let's assume that our samples were draws from a multivariate distribution all along (we just treated them as independent for the univariate case).

> **Note on the demonstration.** The forecasts used in this vignette are for demonstration only and do not represent joint draws of a multivariate distribution. This means the dependence structure scored by multivariate scoring is more an artefact of how the data were prepared than a property of the original forecasts. (See `create-example-data.R` in the `inst/` folder for detail.)

To tell `scoringutils` that we want to treat these as a multivariate forecast, we need to specify the columns that are pooled together to form a single multivariate forecast. We do this via the `joint_across` argument. For example, if we want to pool forecasts across locations and treat them as a single multivariate forecast, we could set `joint_across = c("location", "location_name")` (in our example, the two columns contain essentially the same information - we therefore have to include both in `joint_across` (or could alternatively delete one of them)).

```{r}
example_multiv <- as_forecast_multivariate_sample(
  data = example_univ_multi,
  c("location", "location_name")
)
example_multiv
```

The column `.mv_group_id` is created automatically and represents an identifier for each multivariate forecast. `.mv_group_id` is 1 everywhere, because we only have a single multivariate forecast. When scoring this forecast using an appropriate multivariate scoring function, we will get a single score, even though we have 4 observations, one for each country. (Note that for the purposes of scoring, it doesn't matter that sample ids are still 1-40, repeated 4 times, instead of 1-160. `scoringutils` handles this appropriately.)

```{r}
score(example_multiv)
```

By default, `score()` computes both the energy score and the variogram score for multivariate sample forecasts.
The energy score is a multivariate generalisation of the CRPS that measures overall forecast accuracy.
The variogram score (Scheuerer and Hamill, 2015) specifically targets the correlation structure between the targets being forecast jointly.
For each pair of targets (e.g. two countries), it compares the observed absolute difference |y_i - y_j|^p against what the forecast distribution predicts for that difference.
A forecast that gets the correlations between targets wrong will predict pairwise differences that do not match the observations, producing a higher score.
This makes the variogram score more sensitive to misspecified correlations than the energy score.

You can customise parameters using `purrr::partial()`.
The order parameter `p` controls how differences are scaled: `p = 0.5` (the default) is more robust to outliers, while `p = 1` gives a standard absolute difference.
See `?variogram_score_multivariate` for full parameter documentation.
For example, to use `p = 1`:

```{r}
score(
  example_multiv,
  metrics = list(
    energy_score = energy_score_multivariate,
    variogram_score = purrr::partial( # nolint: namespace_linter.
      variogram_score_multivariate, p = 1
    )
  )
)
```

A set of multivariate targets can be pooled to account for different correlation structures. 
If, at any point, you want to score the same forecast using different groupings, you'd have create a new separate forecast object with a different grouping and score that new forecast object.
For example, to pool across horizons, we bring `target_end_date` along with `horizon`, because the two both vary within a trajectory.

```{r}
example_cases <- na.omit(
  example_sample_continuous[
    target_type == "Cases" &
      model == "EuroCOVIDhub-ensemble"
  ]
)

example_traj <- as_forecast_multivariate_sample(
  data = example_cases,
  joint_across = c("horizon", "target_end_date")
)

head(score(example_traj), 3)
```

Each single score now covers one whole trajectory over three horizons. 

## Multivariate point forecasts

If you have point forecasts rather than samples, you can score them using the variogram score via `as_forecast_multivariate_point()`.
This treats each point forecast as a single-sample ensemble.

```{r}
example_point_multi <- example_point[
  target_type == "Cases" &
    forecast_date == "2021-05-03" &
    target_end_date == "2021-05-15" &
    horizon == 2 &
    model == "EuroCOVIDhub-ensemble"
]

example_mv_point <- as_forecast_multivariate_point(
  data = na.omit(example_point_multi),
  joint_across = c("location", "location_name")
)
score(example_mv_point)
```

## Multivariate quantile forecasts

Both the energy score and the variogram score need samples drawn from the joint predictive distribution. We cannot use the same multivariate scoring for quantile forecasts as they do not describe the relationship between targets in this way. It is tempting to line the quantile levels up across targets and treat them as samples (for instance by passing `sample_id = "quantile_level"` to `as_forecast_multivariate_sample()`). `scoringutils` will warn you about the leftover `quantile_level` column, but it will still return a score. However that score is not the one you want, as in this case, every quantile forecast ends up scored as though it had predicted perfect dependence (regardless of what the underlying model actually predicted).

## Comparing the energy and variogram scores

The energy and variogram scores are complementary to each other. The energy score summarises overall accuracy across all the pooled targets at once, a multivariate generalisation of the CRPS. The variogram score instead looks only at the differences between the specific targets that were pooled together to compare the size of the observed difference against the size of predicted differences. It is pairwise on whatever dimension `joint_across` specified. 

For example, when pooling across forecast horizon, the variogram compares the differences between weeks and is therefore sensitive to the shape of the trajectory. But because it uses only differences between targets, it is insensitive to any bias that shifts the overall trajectory away from the observed data.

We can see this comparison between the two scores, comparing two models' forecasts of 1-3 week ahead cases in Germany as above. The dashed line shows the observed trajectory, with cases falling after the first week.

```{r}
#| fig.width: 7
#| fig.height: 3.4
#| fig.alt: >-
#|   Median forecasts with 50% intervals for two models over three weekly
#|   horizons in Germany, alongside the observed trajectory as a dashed line.
#|   The ensemble stays roughly flat near the first observed value while EpiNow2
#|   rises away from it, and the observed cases fall steeply.
library(ggplot2)
library(data.table)

cases_de <- as.data.table(example_sample_continuous)[
  target_type == "Cases" &
    location == "DE" &
    forecast_date == "2021-05-03" &
    model %in% c("EuroCOVIDhub-ensemble", "epiforecasts-EpiNow2")
]

band <- cases_de[, .(
  median = median(predicted),
  lower = quantile(predicted, 0.25),
  upper = quantile(predicted, 0.75)
), by = .(model, target_end_date)]

observed <- unique(cases_de[, .(target_end_date, observed)])

ggplot(band, aes(target_end_date, median, colour = model, fill = model)) +
  geom_ribbon(aes(ymin = lower, ymax = upper), alpha = 0.2, colour = NA) +
  geom_line() +
  geom_line(
    data = observed, aes(target_end_date, observed),
    inherit.aes = FALSE, linetype = "dashed"
  ) +
  geom_point(
    data = observed, aes(target_end_date, observed),
    inherit.aes = FALSE
  ) +
  labs(x = "Target date", y = "Weekly cases", colour = "Model", fill = "Model") +
  theme_scoringutils() +
  theme(legend.position = "bottom")
```

We pool the three horizons into a single multivariate forecast for each model, then score both.

```{r}
cases_mv <- as_forecast_multivariate_sample(
  data = na.omit(cases_de),
  joint_across = c("horizon", "target_end_date")
)
score(cases_mv)[, c("model", "energy_score", "variogram_score")]
```

The two scores disagree about which model performed better. The energy score prefers the ensemble, whose level stays close to the observed cases, while the variogram score prefers EpiNow2. The observed cases move sharply from week to week, and EpiNow2 predicts changes of a similar size, whereas the near-flat ensemble predicts changes that are too small. Note that EpiNow2 wins on the variogram score even though it has the wrong direction of the change. The energy score rewards getting the overall level right; the variogram score rewards getting the size of the joint movement right.
