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

88 create a rejection sampling function to sample from non standard distributions #227

Conversation

kgoldfeld
Copy link
Owner

@kgoldfeld kgoldfeld commented May 29, 2024

I've created genDataDensity and addDataDensity so that it is now possible to generate data from an arbitrary distribution specified by a vector of integers:

base_data <- c(1, 2, 2, 2, 2, 2, 3, 3, 4, 4, 4, 5, 6, 6, 7, 7, 7, 8, 9, 10, 10, 10, 10, 10)

dd <- genDataDensity(10000, dataDist = base_data, varname = "x1")

Plotting the generated data with the original density:

de <- density(base_data, n = 100000)
de <- data.table(x = de$x, y = de$y)

ggplot(data = dd, aes(x=x1)) +
  geom_histogram(aes(y = after_stat(count / sum(count))), binwidth = 1, fill = "lightblue", color = "black") +
  geom_line(data = de, aes(x = x, y = y), color = "red") +
  theme(panel.grid = element_blank()) +
  scale_y_continuous(name = "density\n")

Copy link

This is how benchmark results would change (along with a 95% confidence interval in relative change) if 823090c is merged into main:

  • ✔️define_data: 17ms -> 17.2ms [-1.6%, +2.96%]
  • ✔️dist_beta: 212ms -> 212ms [-0.65%, +0.92%]
  • ✔️dist_binary: 8.76ms -> 8.82ms [-1.25%, +2.53%]
  • ✔️dist_binomial: 13.2ms -> 13.2ms [-1.39%, +1.4%]
  • ✔️dist_categorical: 58.3ms -> 58.3ms [-0.69%, +0.93%]
  • 🚀dist_exponential: 9.11ms -> 8.93ms [-3.56%, -0.29%]
  • ✔️dist_gamma: 16.5ms -> 16.6ms [-1.85%, +2.95%]
  • ✔️dist_mixture: 228ms -> 228ms [-2.54%, +2.92%]
  • ✔️dist_normal: 10.8ms -> 10.7ms [-2.55%, +0.95%]
  • ✔️gen_all_dists: 48.8ms -> 49.2ms [-1.6%, +3.3%]
    Further explanation regarding interpretation and methodology can be found in the documentation.

@kgoldfeld kgoldfeld marked this pull request as ready for review May 29, 2024 18:34
@kgoldfeld
Copy link
Owner Author

@assignUser Let me know if you want to take a look before I go ahead and merge this.

Copy link
Collaborator

@assignUser assignUser left a comment

Choose a reason for hiding this comment

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

Cool, great work on the tests too!

@kgoldfeld kgoldfeld merged commit ff26538 into main May 30, 2024
9 checks passed
@kgoldfeld kgoldfeld deleted the 88-create-a-rejection-sampling-function-to-sample-from-non-standard-distributions branch May 30, 2024 12:32
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.

Create a rejection-sampling function to sample from non-standard distributions
2 participants