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

Optimize res_beta_binom() #63

Open
joethorley opened this issue Jun 10, 2024 · 1 comment
Open

Optimize res_beta_binom() #63

joethorley opened this issue Jun 10, 2024 · 1 comment

Comments

@joethorley
Copy link
Member

Currently runs very slowly. Consider using C++ code.

@nehill197
Copy link
Member

I wrote a log-likelihood function in C++ to speed up the optimization routine (#66).
We decided to try improving the R code itself before adding the dependency to Rcpp.

First we added a scalar case to the log-likelihood function, which improves the speed for the scalar case required by stats::optimize().

As this was still slower than we would like, we next implemented a function to optimize a vector of all the probability values required for the deviance calculation at the same time.

Lastly, we added memoization to the log-likelihood function for the lgamma() calls that are functions of the data (x and size, which stay constant).

Based on the code below, we chose n = 800 as the criterion to use the memoized version of the function.

x <- 1:1000 + 0.5

lgamma3 <- function(x) {
  lgamma(x) + lgamma(x + 1) + lgamma(x + 2)
}

lgamma3_mem <- memoise::memoise(lgamma3)

bench::mark(
  for (i in 1:250) lgamma(x) + lgamma(x + 1) + lgamma(x + 2),
  for (i in 1:250) lgamma3_mem(x)
)

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