mihajlv
mihajlv

Reputation: 2315

C probability library similar to R

Does anyone know a C library that has some standard probability functions like Rs 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.

Upvotes: 3

Views: 1217

Answers (3)

bluelou
bluelou

Reputation: 1

Have you looked at meta numerics. Its mostly a stats library. Open source, c#.

Upvotes: 0

Dirk is no longer here
Dirk is no longer here

Reputation: 368221

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).

Upvotes: 7

Paul Hiemstra
Paul Hiemstra

Reputation: 60924

Googling for C statistics library, got me some good hits with among others the GSL. See also this SO question for more tips. However, I think your best option is to integrate R into your C code. You can do this in two ways:

  • Call R through a system call. This is a very simple, yet effective option. Especially when there is not a great deal of data going back and forth between R and C, this works really well. Debugging R code from within Python was quite hard for example.
  • Create a direct connection form within C to an R session. This works really well when there is a lot of data going back and forth between R and C because all goes through memory and not the disk. I predict however that it will be harder to write than the first solution. See this SO post from more information.

Upvotes: 3

Related Questions