-1

How to generate a exponential distribution for a set of data using boost c++ .

I have a vector containing float variables named vararr and using Boost c++ and I want to fit it Exponential distribution and getting respective rate parameter(lambda) for the fit . The equivalance code in Matlab is :

PD = fitdist(vararr,'exponential'); 
vararr = sort(vararr); 
fxx = pdf(PD,vararr);

I need to implement it in C++

  • AFAIK boost has *no method* to fit data to a distribution, only to generate data. I remember that Numerical Recipes has, bot for your sanity I hope you find a better option. you could e.g. log all elements, then apply linear regression. – peterchen Jul 05 '16 at 14:47

1 Answers1

1

Exponential distribution is there to generate a random number following a certain distribution. To use exponential distribution, you do something like that:

#include <iostream>
#include <boost/random.hpp>

int main() {
  boost::mt19937 seed(5u); 
  boost::variate_generator<boost::mt19937&, boost::exponential_distribution<>> random_n(seed, boost::exponential_distribution<>()) ;
  cout << random_n() << endl; return 0;
}

Now, I suspect you want to do something but it is not so clear for me. You want to sort or whatever? Pick a random number in your vector?

Edit: OK. In brief and short, even if you have access to C++11 you cannot go very far. Most of the random generator are now in the standard. But not the PDF. If you are curious about why: here it is: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n1398.html. So you definitly need the boost.math toolkit, this is your only way to compute your PDF. Here is some sample code for you:

#include <boost/math/distributions/exponential.hpp>

int main()  {
    const auto lbda = 1.0;
    const auto d = exponential_distribution<> { lbda };
    cout << pdf(d, 0) << endl; // e(0) = 1
    cout << pdf(d, 1) << endl; // e(-1) = 0.3678
    return 0;
}

In short: for using boost PDF, you do not need per se the vector cause the PDF function knows how to compute its distribution.

Edit 2: So if you need to populate a vector with some distribution, you can simply use std::generate and apply the distribution to it, here is an example with a std::exponential_distribution (c++11, but you can use boost).

std::vector<float> v(20);

random_device rd;
mt19937_64 gen(rd());
exponential_distribution<float> dis(1);
auto rand = bind(dis, gen);
generate(begin(v), end(v), rand);
for (auto& e : v) cout << e << endl;

The code I am showing here is filling a vector of 20 elements with an exponential distribution. You can switch to an std::array, change the float to whatever you need, increase the size of the vector or array.

You can also take a look at this old C++ library (nowaday a pytonic one) which does what you need : http://myfitter.hepforge.org/. This is probably your best bet. It uses a non parametric method. The C++ version is quite old and I am not so sure fully functional but perhaps this will go for you.

  • I have a vector containing float variables named **vararr** and using Boost c++ and I want to fit it Exponential distribution and getting respective rate parameter for the fit . The equivalance code in Matlab is : PD = fitdist(vararr,'exponential'); vararr = sort(vararr); fxx = pdf(PD,vararr); – Kumar Abhinav Jul 05 '16 at 13:05
  • As much as I understand OP, he wants to *fit* data to an exponential distribution. – peterchen Jul 05 '16 at 14:47
  • @Gibet : I need to fit data to an exponential distribution and not generate random numbers based on pdf . – Kumar Abhinav Jul 07 '16 at 04:02
  • @Gibet : I already have a vector set , I need to fit the best exponential distribution to that vector set and hence get the learning rate – Kumar Abhinav Jul 07 '16 at 07:55