C probability library similar to R

c, r, statistics

Solution

You can always embed R itself in your C application. That is doable, and documented, but a tad tedious as the API is pretty bare.

If you are open to C++, it gets much easier thanks to RInside. If you can do this in R:

R> set.seed(123); sample(LETTERS[1:5], 10, replace=TRUE)
 [1] "B" "D" "C" "E" "E" "A" "C" "E" "C" "C"
R> 

you can do the same in C++ pretty easily thanks to RInside:

edd@max:~/svn/rinside/pkg/inst/examples/standard$ cat rinside_sample12.cpp
// Simple example motivated by StackOverflow question on using sample() from C
//
// Copyright (C) 2012  Dirk Eddelbuettel and Romain Francois

#include <RInside.h>                    // for the embedded R via RInside

int main(int argc, char *argv[]) {

  RInside R(argc, argv);                // create an embedded R instance

  std::string cmd = "set.seed(123); sample(LETTERS[1:5], 10, replace=TRUE)";

  Rcpp::CharacterVector res = R.parseEval(cmd);   // parse, eval + return result 

  for (int i=0; i<res.size(); i++) {
    std::cout << res[i] << " ";
  }
  std::cout << std::endl;

  exit(0);
}

edd@max:~/svn/rinside/pkg/inst/examples/standard$ 

and given that it runs the same code with the same RNG seed it also returns the same result:

edd@max:~/svn/rinside/pkg/inst/examples/standard$ ./rinside_sample12
B D C E E A C E C C 
edd@max:~/svn/rinside/pkg/inst/examples/standard$ 

If you just drop the code I showed above into the directory `examples/standard` of an existing RInside installation and say `make`, the executable will be made and given the same basename as your source file (here `rinside_sample12` from `rinside_sample12.cpp`).

Problem

Does anyone know a C library that has some standard probability functions like `R`s sample function? I found this: http://www.gnu.org/software/gsl/ I was wondering if anyone has had any experience with it (how efficient it is) and if there are any other ones. Thanks.

Original source

Related problems