core/vnl/vnl_sample.h
Go to the documentation of this file.
00001 // This is core/vnl/vnl_sample.h
00002 #ifndef vnl_sample_h_
00003 #define vnl_sample_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 //  \file
00009 //  \brief easy ways to sample from various probability distributions
00010 // \author fsm
00011 // \verbatim
00012 //  Modifications
00013 //   2005-01-01 Peter Vanroose - use simple (but robust) rng when no DRAND48
00014 //   2007-03-26 Peter Vanroose - avoid returning log(0.0) by switching params
00015 //   2010-09-12 Peter Vanroose - added implementation for binomial sampling
00016 //   2010-09-12 Peter Vanroose - added Bernoulli (unfair coin toss) sampling
00017 // \endverbatim
00018 
00019 //: re-seed the random number generator.
00020 void vnl_sample_reseed();
00021 
00022 //: re-seed the random number generator given a seed.
00023 void vnl_sample_reseed(int seed);
00024 
00025 //: return a random number uniformly drawn on [a, b)
00026 double vnl_sample_uniform(double a, double b);
00027 
00028 //: two independent samples from a standard normal distribution.
00029 void vnl_sample_normal_2(double *x, double *y);
00030 
00031 //: Normal distribution with given mean and standard deviation
00032 double vnl_sample_normal(double mean, double sigma);
00033 
00034 //: Return random k, where P(X = k) = [kth term in binomial expansion of (q + (1-q))^n].
00035 // The returned value will lie between 0 and n (but will be -1 when input is nonsense).
00036 int vnl_sample_binomial(int n, double q);
00037 
00038 //: Bernoulli distribution ("coin toss").
00039 // The returned value will be 0 (with probability q) or 1 (with probability 1-q).
00040 // For a "fair" coin toss, use q=0.5.
00041 // When q does not lie between 0 and 1, the value -1 is returned.
00042 int vnl_sample_bernoulli(double q);
00043 
00044 // ----------------------------------------
00045 
00046 //: handy function to fill a range of values.
00047 template <class I>
00048 inline void vnl_sample_uniform(I begin, I end, double a, double b)
00049 {
00050   for (I p=begin; p!=end; ++p)
00051     (*p) = vnl_sample_uniform(a, b);
00052 }
00053 
00054 //: handy function to fill a range of values.
00055 template <class I>
00056 inline void vnl_sample_normal(I begin, I end, double mean, double sigma)
00057 {
00058   for (I p=begin; p!=end; ++p)
00059     (*p) = vnl_sample_normal(mean, sigma);
00060 }
00061 
00062 //: handy function to fill a range of values.
00063 template <class I>
00064 inline void vnl_sample_binomial(I begin, I end, int n, double q)
00065 {
00066   for (I p=begin; p!=end; ++p)
00067     (*p) = vnl_sample_binomial(n, q);
00068 }
00069 
00070 //: handy function to fill a range of values.
00071 template <class I>
00072 inline void vnl_sample_bernoulli(I begin, I end, double q)
00073 {
00074   for (I p=begin; p!=end; ++p)
00075     (*p) = vnl_sample_bernoulli(q);
00076 }
00077 
00078 //: handy function to fill a range of values.
00079 template <class I, class T>
00080 inline void vnl_sample_uniform(I begin, I end, double a, double b, T /*dummy*/)
00081 {
00082   for (I p=begin; p!=end; ++p)
00083     (*p) = T(vnl_sample_uniform(a, b));
00084 }
00085 
00086 //: handy function to fill a range of values.
00087 template <class I, class T>
00088 inline void vnl_sample_normal(I begin, I end, double mean, double sigma, T /*dummy*/)
00089 {
00090   for (I p=begin; p!=end; ++p)
00091     (*p) = T(vnl_sample_normal(mean, sigma));
00092 }
00093 
00094 #endif // vnl_sample_h_