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

Add C++ log-likelihood function to speed up optimization in dev_beta_binom() #66

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Depends:
Imports:
chk,
lifecycle,
Rcpp (>= 1.0.12),
stats
Suggests:
aods3,
Expand All @@ -43,6 +44,7 @@ Suggests:
tidyr,
viridis,
withr
LinkingTo: Rcpp
Config/testthat/edition: 3
Encoding: UTF-8
Language: en-US
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,10 @@ export(xtr_sd)
export(zeros)
export(zscore)
import(chk)
importFrom(Rcpp,sourceCpp)
importFrom(stats,dbinom)
importFrom(stats,dlnorm)
importFrom(stats,dnbinom)
importFrom(stats,dnorm)
importFrom(stats,dpois)
useDynLib(extras)
7 changes: 7 additions & 0 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Generated by using Rcpp::compileAttributes() -> do not edit by hand
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393

log_lik_beta_binom2 <- function(x, size, prob, theta) {
.Call('_extras_log_lik_beta_binom2', PACKAGE = 'extras', x, size, prob, theta)
}

2 changes: 1 addition & 1 deletion R/dev.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#' dev_beta_binom(c(0, 1, 2), 1, 0.5, 0)
dev_beta_binom <- function(x, size = 1, prob = 0.5, theta = 0, res = FALSE) {
opt_beta_binom <- function(prob, x, size = size, theta = theta) {
-log_lik_beta_binom(x = x, size = size, prob = prob, theta = theta)
-log_lik_beta_binom2(x = x, size = size, prob = prob, theta = theta)
}
if (length(size) == 1) {
size <- rep(size, length(x))
Expand Down
2 changes: 2 additions & 0 deletions R/namespace.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#' @import chk
#' @importFrom stats dbinom dlnorm dnorm dpois dnbinom
#' @useDynLib extras
#' @importFrom Rcpp sourceCpp
NULL
36 changes: 36 additions & 0 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Generated by using Rcpp::compileAttributes() -> do not edit by hand
// Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393

#include <Rcpp.h>

using namespace Rcpp;

#ifdef RCPP_USE_GLOBAL_ROSTREAM
Rcpp::Rostream<true>& Rcpp::Rcout = Rcpp::Rcpp_cout_get();
Rcpp::Rostream<false>& Rcpp::Rcerr = Rcpp::Rcpp_cerr_get();
#endif

// log_lik_beta_binom2
double log_lik_beta_binom2(int x, int size, double prob, double theta);
RcppExport SEXP _extras_log_lik_beta_binom2(SEXP xSEXP, SEXP sizeSEXP, SEXP probSEXP, SEXP thetaSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< int >::type x(xSEXP);
Rcpp::traits::input_parameter< int >::type size(sizeSEXP);
Rcpp::traits::input_parameter< double >::type prob(probSEXP);
Rcpp::traits::input_parameter< double >::type theta(thetaSEXP);
rcpp_result_gen = Rcpp::wrap(log_lik_beta_binom2(x, size, prob, theta));
return rcpp_result_gen;
END_RCPP
}

static const R_CallMethodDef CallEntries[] = {
{"_extras_log_lik_beta_binom2", (DL_FUNC) &_extras_log_lik_beta_binom2, 4},
{NULL, NULL, 0}
};

RcppExport void R_init_extras(DllInfo *dll) {
R_registerRoutines(dll, NULL, CallEntries, NULL, NULL);
R_useDynamicSymbols(dll, FALSE);
}
Binary file added src/RcppExports.o
Binary file not shown.
Binary file added src/extras.so
Binary file not shown.
34 changes: 34 additions & 0 deletions src/log_lik_beta_binom.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <Rcpp.h>
using namespace Rcpp;

// @noRd
// [[Rcpp::export]]
double log_lik_beta_binom2(int x, int size, double prob, double theta) {
double alpha = prob * 2 * (1 / theta);
double beta = (1 - prob) * 2 * (1 / theta);
double lbeta_binom = R::lgammafn(size + 1) - R::lgammafn(x + 1) - R::lgammafn(size - x + 1) +
R::lgammafn(x + alpha) + R::lgammafn(size - x + beta) - R::lgammafn(size + alpha + beta) +
R::lgammafn(alpha + beta) - R::lgammafn(alpha) - R::lgammafn(beta);

bool use_binom = theta == 0;

if (use_binom) {
return(R::dbinom(x, size, prob, 1));
}
if ((((x == 0) & (prob == 0)) | ((x == size) & (prob == 1)))) {
return(0.0);
}
if ((x != 0) & (prob == 0)) {
return(R_NegInf);
}
if ((x != size) & (prob == 1)) {
return(R_NegInf);
}
if ((x > size)) {
return(R_NegInf);
}
if ((theta < 0)) {
return(R_NaN);
}
return(lbeta_binom);
}
Binary file added src/log_lik_beta_binom.o
Binary file not shown.
Loading