Skip to content

Instantly share code, notes, and snippets.

@wckdouglas
Created May 28, 2015 21:12
Show Gist options
  • Save wckdouglas/de53b659c08e0a25b592 to your computer and use it in GitHub Desktop.
Save wckdouglas/de53b659c08e0a25b592 to your computer and use it in GitHub Desktop.
#include <Rcpp.h>
using namespace Rcpp;
double getProb(double p, int success, int fail){
double e = success * log(p) + fail * log(1-p);
return e;
}
//[[Rcpp::export]]
NumericVector probOfProb(NumericVector p, int success, int fail){
int size = p.size();
IntegerVector a = seq_len(success), b = seq_len(fail), c = seq_len(fail+success);
NumericVector result(size), b1 = log(a), b2 = log(b), b3 = log(c);
double c1 = sum(b1), c2 = sum(b2), c3 = sum(b3);
double d = c1 + c2 - c3;
for (int i = 0 ; i < size; i++){
result[i] = exp(getProb(p[i],success,fail) - d);
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment