Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

quantiles on square transformed densities incorrect #129

Closed
robjhyndman opened this issue Oct 3, 2024 · 4 comments
Closed

quantiles on square transformed densities incorrect #129

robjhyndman opened this issue Oct 3, 2024 · 4 comments

Comments

@robjhyndman
Copy link
Contributor

library(distributional)
dist_normal()^2 |> hilo()
#> <hilo[1]>
#> [1] [3.841459, 3.841459]95
dist_normal(500,20)^2 |> hilo()
#> Error in `new_hilo()`:
#> ! `upper` can't be lower than `lower`.

Created on 2024-10-04 with reprex v2.1.1

Possibly because transformation is not monotonic over sample space of distribution?

@robjhyndman
Copy link
Contributor Author

This is the cause of the problem described at https://forum.posit.co/t/fable-and-distributional-packages-hilo-function-error/193716

@mitchelloharawild
Copy link
Owner

Yes, transformations are assumed monotonic over the support of the distribution.
I can't think of an efficient way to test this yet, currently this test is used:

monotonic_increasing <- function(f, support) {
# Shortcut for identity function (used widely in ggdist)
if(!is.primitive(f) && identical(body(f), as.name(names(formals(f))))) {
return(TRUE)
}
# Currently assumes (without checking, #9) monotonicity of f over the domain
x <- f(field(support, "lim")[[1]])
isTRUE(x[[2L]] > x[[1L]])
}

@mitchelloharawild
Copy link
Owner

Also noting that this is mentioned in the documentation of dist_transformed().

#' @param transform A function used to transform the distribution. This
#' transformation should be monotonic over appropriate domain.

@mitchelloharawild
Copy link
Owner

This is now patch fixed, essentially since the domain goes from (-Inf, Inf) to [0, Inf) the monotonicity requirement of dist_transformed() is violated. Of course for most of the density here it isn't a problem, and the error was due to a boundary condition (defaulting monotonic decreasing rather than increasing).

The better solution requires a symbolic understanding of function monotonicity like:

  • expr: x^2
  • monotonic: {(-Inf, 0), [0, Inf)}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants