<commit_before>#include <esm.hpp>
#include <algorithm>
#include <functional>
#include <cmath>

using namespace std;
using namespace Rcpp;

//' Association stat from Thornton, Foran, and Long (2013) PLoS Genetics
//' @param scores A vector of single-marker association test scores, on a -log10 scale
//' @param K the number of markers used to calculate ESM_K
//' @return The ESM_K test statistic value
//' @references <NAME>., <NAME>., & <NAME>. (2013). Properties and Modeling of GWAS when Complex Disease Risk Is Due to Non-Complementing, Deleterious Mutations in Genes of Large Effect. PLoS Genetics, 9(2), e1003258. doi:10.1371/journal.pgen.1003258
//' @examples
//' data(rec.ccdata)
//' status = c(rep(0,rec.ccdata$ncontrols),rep(1,rec.ccdata$ncases))
//' #filter out common alleles and marker pairs in high LD
//' keep = filter_sites(rec.ccdata$genos,status,0,0.05,0.8)
//' rec.ccdata.chisq = chisq_per_marker(rec.ccdata$genos[,which(keep==1)],status)
//' rec.ccdata.esm = esm( rec.ccdata.chisq, 50 )
// [[Rcpp::export]]
double esm( const NumericVector & scores, const unsigned & K )
{
  NumericVector s(scores);

  sort(s.begin(),s.end(),greater<double>());

  unsigned ntests = scores.size();
  unsigned nm = min(K,unsigned(scores.size()));

  double rv=0;
  for( unsigned i = 0 ; i < nm ; ++i )
    {
      rv += (s[i] - ( -log10( double(i+1)/double(ntests) ) ) );
    }
  return rv;
}<commit_msg>now implemented using boost accumulator<commit_after>#include <esm.hpp>
#include <algorithm>
#include <functional>
#include <cmath>
#include <numeric>

#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics/stats.hpp>
#include <boost/accumulators/statistics/tail.hpp>

using namespace std;
//using namespace Rcpp;
using namespace boost::accumulators;

namespace {
  /*
    Awaiting the days when CRAN will take cpp11...
  */
  struct __esm_accumulator {
    mutable unsigned i,ntests;
    __esm_accumulator(const unsigned & n) : i(0u),ntests(n) {}
    typedef double result_type;
    inline result_type operator()(const double & a, const double & b) const
    {
      double rv = a + (b - (-log10((i+1)/double(ntests))));
      ++i;
      return rv;
    }
  };
}


//' Association stat from Thornton, Foran, and Long (2013) PLoS Genetics
//' @param scores A vector of single-marker association test scores, on a -log10 scale
//' @param K the number of markers used to calculate ESM_K
//' @return The ESM_K test statistic value
//' @references <NAME>., <NAME>., & <NAME>. (2013). Properties and Modeling of GWAS when Complex Disease Risk Is Due to Non-Complementing, Deleterious Mutations in Genes of Large Effect. PLoS Genetics, 9(2), e1003258. doi:10.1371/journal.pgen.1003258
//' @examples
//' data(rec.ccdata)
//' status = c(rep(0,rec.ccdata$ncontrols),rep(1,rec.ccdata$ncases))
//' #filter out common alleles and marker pairs in high LD
//' keep = filter_sites(rec.ccdata$genos,status,0,0.05,0.8)
//' rec.ccdata.chisq = chisq_per_marker(rec.ccdata$genos[,which(keep==1)],status)
//' rec.ccdata.esm = esm( rec.ccdata.chisq, 50 )
// [[Rcpp::export]]
double esm( const Rcpp::NumericVector & scores, const unsigned & K )
{
  typedef tag::tail_variate< int, tag::covariate1, boost::accumulators::left > my_tail_variate_tag;
  unsigned ntests = scores.size();
  accumulator_set<double, stats<tag::tail<boost::accumulators::right> > > acc(tag::tail<boost::accumulators::right>::cache_size =std::min(K,ntests));
  for( Rcpp::NumericVector::const_iterator itr = scores.begin() ; 
       itr != scores.end() ; ++itr )
    {
      acc(*itr);
    }
  __esm_accumulator b(ntests);
  double rv = accumulate(tail(acc).begin(),tail(acc).end(),0.,b);
  return rv;
}
{'fraction_non_alphanumeric': 0.15451341827053403, 'fraction_numerical': 0.02683654106804012, 'mean_word_length': 4.340086830680174, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3767115', 'n_tokens_mistral': 1389, 'n_tokens_neox': 1270, 'n_words': 333}