Skip to content

Avoid bounded density discontinuity #5642

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

Merged
merged 5 commits into from
May 20, 2024

Conversation

teunbrand
Copy link
Collaborator

This PR aims to fix #5641.

Briefly, the way bounded densities were estimated may result in discontinuities due to incomplete estimation of the tails.
As densities are 'mirrored' across finite bounds, in this PR we ensure that the 'mirror image' is the same size as the original by multiplying the to/from range in density estimation. We also increase n to not lose any detail in the bounded density.

The reprex from #5641 with this PR now shows a flatter line in the uniform density:

devtools::load_all("~/packages/ggplot2")
#> ℹ Loading ggplot2

x <- ppoints(100)
p <- ggplot(data.frame(x), aes(x)) + 
  geom_density(bounds = c(0,1))
p

p + coord_cartesian(ylim = c(.99, 1.01))

Created on 2024-01-15 with reprex v2.0.2

@teunbrand
Copy link
Collaborator Author

@mjskay Could I ask you if you think this implementation is correct?

@mjskay
Copy link
Contributor

mjskay commented Jan 16, 2024

Yeah this looks right to me!

Copy link
Member

@thomasp85 thomasp85 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@teunbrand teunbrand merged commit e22cff2 into tidyverse:main May 20, 2024
11 checks passed
@teunbrand teunbrand deleted the density_discontinuity branch May 20, 2024 09:57
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

Successfully merging this pull request may close these issues.

Bounded densities have discontinuities due to incomplete estimation of tails in the reflection method
3 participants