`forecast.VAR` takes sqrt of potentially negative covariance
Nobody has claimed this yet.
- Dominant language
- R
- Stars
- 588
- Forks
- 71
- Avg merge
- 1h 58m
- Merged PRs (30d)
- 1
Description
Currently, forecast.VAR calculates sqrt of the covariance matrix:
https://github.com/tidyverts/fable/blob/9b926f8be993594c3ae448b912366aca09ab78d7/R/var.R#L244
and then recovers the covariance matrix before constructing the distribution at the end:
https://github.com/tidyverts/fable/blob/9b926f8be993594c3ae448b912366aca09ab78d7/R/var.R#L266
This produces NaN when the covariance between variables is negative. For example:
library(tidyverse)
library(fable)
#> Loading required package: fabletools
lung_deaths <- cbind(mdeaths, fdeaths) %>%
as_tsibble(pivot_longer = FALSE)
fit <- lung_deaths %>%
mutate(fdeaths = -fdeaths) %>%
model(VAR(vars(mdeaths, fdeaths) ~ AR(3)))
fc <- forecast(fit, h=1)
#> Warning: There was 1 warning in `mutate()`.
#> ℹ In argument: `VAR(vars(mdeaths, fdeaths) ~ AR(3)) = (function (object, ...)
#> ...`.
#> Caused by warning in `FUN()`:
#> ! NaNs produced
distributional::covariance(pull(fc, .distribution))
#> [[1]]
#> mdeaths fdeaths
#> mdeaths 58985.95 NaN
#> fdeaths NaN 9983.95
Created on 2023-04-12 with reprex v2.0.2
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the example in the issue with the forecast.VAR path, then inspect R/var.R around lines 244 and 266 where the covariance matrix is transformed and reconstructed. Verify the resulting distribution's covariance for negative off-diagonal values and add or update coverage so the example no longer produces NaN.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- r
- Domain
- data
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100