#include "DropletUtils.h" SEXP montecarlo_pval (SEXP totalval, SEXP totallen, SEXP prob, SEXP ambient, SEXP iter) { BEGIN_RCPP Rcpp::IntegerVector Totalval(totalval); Rcpp::IntegerVector Totallen(totallen); Rcpp::NumericVector Prob(prob); Rcpp::NumericVector Ambient(ambient); if (Totalval.size()!=Totallen.size()) { throw std::runtime_error("length of run value/length vectors are not the same"); } const int nvalues=std::accumulate(Totallen.begin(), Totallen.end(), 0); if (nvalues!=Prob.size()) { throw std::runtime_error("sum of run lengths does not equal length of 'P' vector"); } const int niter=check_integer_scalar(iter, "number of iterations"); if (niter < 1) { throw std::runtime_error("number of iterations should be a positive integer"); } // Setting up temporary objects. Rcpp::IntegerVector above(nvalues); const size_t ngenes=Ambient.size(); if (ngenes==0) { return above; } std::vector tracker(ngenes); std::vector logprob(Ambient.begin(), Ambient.end()); for (auto& l : logprob) { l=std::log(l); } std::vector cumprob(ngenes); std::partial_sum(Ambient.begin(), Ambient.end(), cumprob.begin()); const double sumprob=cumprob.back(); Rcpp::RNGScope rng; // after the IntegerVector! // Looping across iterations. for (int it=0; it= ngenes) { chosen=ngenes-1; // Some protection against the very-unlikely case. } curp += logprob[chosen] - std::log(++tracker[chosen]); ++curtotal; } // Figuring out where it lies in the probability vector. size_t higher=std::lower_bound(pIt, pIt+curlen, curp) - pIt; if (higher tracker(ngenes); std::vector tmpprob(ngenes), cumprob(ngenes); Rcpp::RNGScope rng; // after the IntegerVector! // Looping across iterations, using a new probability vector per iteration. for (int it=0; it= ngenes) { chosen=ngenes-1; // Some protection against the very-unlikely case. } auto& curnum=tracker[chosen]; curp += std::log(Ambient[chosen] * Alpha + curnum); // Difference of Gamma's in data-dependent component of Dirichlet-multinomial. curp -= std::log(++curnum); // corresponds to division by factorial on observed count. ++curtotal; } // Figuring out where it lies in the probability vector. size_t higher=std::lower_bound(pIt, pIt+curlen, curp) - pIt; if (higher