[Math/Statistical Distributions+Random] Random number generation
Hi all! I'm a "boost newbie" so sorry for too trivial questions. Just trying to use boost for some statistical experiments, involving statistical distribution fitting, quantile generation (for goodness-of-fit tests), random variate generation, and so on. . The "Math/Statistical Distribution" library is great and almost complete. But I'm just curious why the random number generation (provided by the "Random" lib) and this library have not been integrated. For instance, the boost::random::exponential_distribution and the boost::math::exponential_distribution are two different worlds. I think a function like this below would be very helpful: --- [snip] --- template <typename Generator, typename RealType, typename Policy> inline RealType rand(Generator& rng, const exponential_distribution<RealType, Policy>& dist) { return -1.0 / dist.lambda() * std::log(1.0-rng()); } --- [snip] --- or something similar (e.g., rand implemented as a class method) In this case only two objects are needed: * boost::random::mersenne_twister * boost::math::exponential_distribution Instead, actually I have to keep different objects. For instance, for the exponential distribution: * boost::random::exponential_distribution * boost::random::mersenne_twister * boost::random::variate_generator * boost::math::exponential_distribution Unluckely I can't use boost::math::exponential_distribution in place of boost::random::exponential_distribution since boost::random::variate_generator expects a specific interface for the distribution. Well, what is the rationale behind the choice of this separation? Do you think would be useful to provide this kind of integration? Thank you very much in advance!! Cheers -- Marco
Maybe automate the stated requirement? (not tested): template<typename D> struct math_to_random{}; template<typename T,typename P> struct math_to_random<math::exponential_distribution<T,P> >{ typedef exponential_distribution<T> type; }; etc. template<typename G,typename T,typename P> variate_generator< G&, math_to_random<math::exponential_distribution<T,P> >::type
make_variate_generator( G& rng, const math::exponential_distribution<T,P>& dist0 ){ typedef typename math_to_random<exponential_distribution<T,P> >::type rd_t; typename variate_generator< G&, rd_t
vg_t;
rd_t rd(dist0.lambda()); return vg_t(rng,dist) } etc. Usage: ... gen; ... vec; math::exponential_distribution<T,P> dist(lambda); std::generate_n( back_inserter(vec), n, make_variate_generator(rng,dist) )
On Fri, Apr 17, 2009 at 9:24 PM, er <erwann.rogard@gmail.com> wrote:
Maybe automate the stated requirement?
(not tested):
template<typename D> struct math_to_random{};
template<typename T,typename P> struct math_to_random<math::exponential_distribution<T,P> >{ typedef exponential_distribution<T> type; };
etc.
template<typename G,typename T,typename P> variate_generator< G&, math_to_random<math::exponential_distribution<T,P> >::type
make_variate_generator( G& rng, const math::exponential_distribution<T,P>& dist0 ){ typedef typename math_to_random<exponential_distribution<T,P> >::type rd_t; typename variate_generator< G&, rd_t
vg_t;
rd_t rd(dist0.lambda()); return vg_t(rng,dist) }
Thanks er! This would be a good starting point, even if the resulting code might be penalized in performance and resource consumption (e.g., for the (math::) exponential distribution it is needed to instantiate another (random::) exponential distribution -- and duplicating state information like the distribution parameters -- only for the random generation function). The point is that, from the point of view of a "user" of the lib, I think that writing the "gateway" function "make_variate_generator" for every distribution of interest may result in overwork. I think that (always from a point of view of a user) the use of the "rand" function (as the one I suggested above), "stoling" the random generation from the boost::random::*_distribution classes would take less time and may result in faster code. Maybe this integration can be done by the library developers if they think it would be useful to provide to library users a simplified "rand" function facility for generating random number generators distributed according to a given distribution. Thank you very much!! Cheers -- Marco
Marco Guazzone wrote:
This would be a good starting point, even if the resulting code might be penalized in performance and resource consumption (e.g., for the
... and here it is : typedef math::exponential_distribution<value_type> dexp_t; typedef exponential_distribution<value_type> rexp_t; typedef variate_generator<urng_t&, rexp_t> gen_t; nn = 1e2; boost::random::make_variate_generator(urng,dexp); //t0 std::generate_n(back_inserter(vec),nn,random::make_variate_generator(urng,dexp)); //t2 gen_t gen(urng,rexp); //t1 std::generate_n(back_inserter(vec),nn,gen_t(urng,rexp));//t3 time per 1000000 iterations : t0 = 1.54508 t1 = 1.49743 t2 = 5.95946 t3 = 5.91909
On Sat, Apr 18, 2009 at 1:26 AM, er <erwann.rogard@gmail.com> wrote:
Marco Guazzone wrote:
This would be a good starting point, even if the resulting code might be penalized in performance and resource consumption (e.g., for the
... and here it is :
...
std::generate_n(back_inserter(vec),nn,random::make_variate_generator(urng,dexp)); //t2 ... std::generate_n(back_inserter(vec),nn,gen_t(urng,rexp));//t3
time per 1000000 iterations : t0 = 1.54508 t1 = 1.49743 t2 = 5.95946 t3 = 5.91909
Just tried and yessss you're right: t2 ~ 14.70 t3 ~ 14.68 (compiler: g++ with flags: -DNDEBUG -O3 -Wall -ansi -pedantic) Also, differences in memory consumption seem negligible Many thanks for your useful hint!!! But there is a hope that a "rand" function is added to boost::math in order to bridge the two worlds? Thank you so much! Cheers -- Marco
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
On Sat, Apr 18, 2009 at 10:50 AM, John Maddock <john@johnmaddock.co.uk> wrote:
Well, what is the rationale behind the choice of this separation?
We haven't had time to provide it yet :-(
Do you think would be useful to provide this kind of integration?
Yes for sure.
Cool! Time is short also for me. However maybe I can help you in writing it Note: I'm not an expert in metaprogramming, neither in boost library; so I don't want to become a bottleneck for library release. A possible solution might be: --- [snip] --- template<typename G,typename T,typename P> T rand(G& rng, const boost::math::exponential_distribution<T,P>& dist) { typedef typename boost::exponential_distribution<T> rdist_type; typedef typename boost::variate_generator<G&,rdist_type> variate_type; return variate_type(rng, rdist_type(dist.lambda()))(); } --- [/snip] --- This is not the *best* solution since each time rand is called a temporary boost::variate_generator and boost::exponential_distribution objects are created. Another possible solution is to copy the code from the math/random/*.hpp classes although this duplication is not so good. Further in some case we have to move the implemention inside the (math::) distribution for keeping a kind of state (e.g., the normal variate generation include the generation of two uniform numbers at time) and the call this method from the rand function, like this below: --- [snip] --- template<typename G,typename T,typename P> T rand(G& rng, const boost::math::normal_distribution<T,P>& dist) { return dist.rand(rng); } --- [/snip] --- Let me know. -- Marco
HTH, John. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Marco Guazzone wrote:
I think a function like this below would be very helpful: --- [snip] --- template <typename Generator, typename RealType, typename Policy> inline RealType rand(Generator& rng, const exponential_distribution<RealType, Policy>& dist) { return -1.0 / dist.lambda() * std::log(1.0-rng()); }
Here's something similar: https://svn.boost.org/svn/boost/sandbox/statistics/dist_random/
On Sun, Aug 16, 2009 at 7:45 AM, er<erwann.rogard@gmail.com> wrote:
Marco Guazzone wrote:
I think a function like this below would be very helpful: --- [snip] --- template <typename Generator, typename RealType, typename Policy> inline RealType rand(Generator& rng, const exponential_distribution<RealType, Policy>& dist) { return -1.0 / dist.lambda() * std::log(1.0-rng()); }
Here's something similar: https://svn.boost.org/svn/boost/sandbox/statistics/dist_random/
Thank you! I've taken a look... Very interesting. But. why using different statistical distribution classes #include <boost/standard_distribution/distributions/gamma.hpp> instead of the ones included in boost #include <boost/math/distributions/gamma.hpp> ? What I mean is that I think an integration between Boost.Random and Boost.StatisticalDistribution would be very useful for users -- Marco
Marco Guazzone wrote:
Thank you! I've taken a look... Very interesting.
Thanks.
But. why using different statistical distribution classes
#include <boost/standard_distribution/distributions/gamma.hpp>
This file does not define a new gamma distribution, only extra, non overlapping functionality. For example, you can map the gamma distribution to the gamma random engine : typedef boost::math::gamma_distribution<T,P> gamma_; typedef dist_random< gamma_ > meta; typedef meta::type random_; gamma_ gamma(5.0); random_ r = meta::make(gamma);
What I mean is that I think an integration between Boost.Random and Boost.StatisticalDistribution would be very useful for users
Perhaps, but it's not under my control. Anyhow, if you do e.g generate_n(it,n,gamma,urng); the interface make no reference to boost/random (hidden in implementation)
On Sat, Aug 22, 2009 at 4:22 AM, er<erwann.rogard@gmail.com> wrote:
Marco Guazzone wrote:
Thank you! I've taken a look... Very interesting.
Thanks.
But. why using different statistical distribution classes
#include <boost/standard_distribution/distributions/gamma.hpp>
This file does not define a new gamma distribution, only extra, non overlapping functionality. For example, you can map the gamma distribution to the gamma random engine :
typedef boost::math::gamma_distribution<T,P> gamma_; typedef dist_random< gamma_ > meta; typedef meta::type random_; gamma_ gamma(5.0); random_ r = meta::make(gamma);
Got it!
What I mean is that I think an integration between Boost.Random and Boost.StatisticalDistribution would be very useful for users
Perhaps, but it's not under my control. Anyhow, if you do e.g
generate_n(it,n,gamma,urng);
the interface make no reference to boost/random (hidden in implementation)
Hope the authors of Statistical Distribution toolkit consider to merge your code in the near future :) Thank you very much! Cheers, -- Marco
What I mean is that I think an integration between Boost.Random and Boost.StatisticalDistribution would be very useful for users
The links that I gave previously on this issue will be phased out and are replaced by https://svn.boost.org/svn/boost/sandbox/statistics/distribution_toolkit/ which is hopefully cleaner and more tested (kolmogorov smirnov to verify distribution cdf agrees with random generation).
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of er Sent: Sunday, September 27, 2009 12:02 AM To: boost-users@lists.boost.org Subject: Re: [Boost-users] [Math/Statistical Distributions+Random] Random number generation
What I mean is that I think an integration between Boost.Random and Boost.StatisticalDistribution would be very useful for users
The links that I gave previously on this issue will be phased out and are replaced by
https://svn.boost.org/svn/boost/sandbox/statistics/distribution_toolkit/
which is hopefully cleaner and more tested (kolmogorov smirnov to verify distribution cdf agrees with random generation).
I note that the link to math toolkit is still at version 1_35. [ Useful links ] http://www.boost.org/doc/libs/1_35_0/libs/math/doc/sf_and_dist/html/index.ht... Should this be 1_40_0? (Assuming it works with the latest release). It would be nice to have confirmation that this works with MSVC and gcc on other platforms too. Paul
It would be nice to have confirmation that this works with MSVC and gcc on other platforms too.
Paul
Thanks for inquiring. Here's an update, if late in the day : the files in /libs compile and run OK under these platforms: Win7-32 V6.1.7100 Dev-C++ 5.0 beta 9.2 with Mingw/GCC 3.4.2 Win7-32 V6.1.7100 Visual Studio 2010 Mac OS X 10.6 gcc 4.2
participants (4)
-
er
-
John Maddock
-
Marco Guazzone
-
Paul A. Bristow