vnl_sample.h
Go to the documentation of this file.
1 // This is core/vnl/vnl_sample.h
2 #ifndef vnl_sample_h_
3 #define vnl_sample_h_
4 
5 #include "vnl/vnl_export.h"
6 //:
7 // \file
8 // \brief easy ways to sample from various probability distributions
9 // \author fsm
10 // \verbatim
11 // Modifications
12 // 2007-03-26 Peter Vanroose - avoid returning log(0.0) by switching params
13 // 2010-09-12 Peter Vanroose - added implementation for binomial sampling
14 // 2010-09-12 Peter Vanroose - added Bernoulli (unfair coin toss) sampling
15 // \endverbatim
16 
17 //: re-seed the random number generator.
18 VNL_EXPORT void vnl_sample_reseed();
19 
20 //: re-seed the random number generator given a seed.
21 VNL_EXPORT void vnl_sample_reseed(int seed);
22 
23 //: return a random number uniformly drawn on [0, 1.0)
24 VNL_EXPORT double vnl_sample_uniform01();
25 
26 //: return a random number uniformly drawn on [a, b)
27 VNL_EXPORT double vnl_sample_uniform(double a, double b);
28 
29 //: two independent samples from a standard normal distribution.
30 VNL_EXPORT void vnl_sample_normal_2(double *x, double *y);
31 
32 //: Normal distribution with given mean and standard deviation
33 VNL_EXPORT double vnl_sample_normal(double mean, double sigma);
34 
35 //: Return random k, where P(X = k) = [kth term in binomial expansion of (q + (1-q))^n].
36 // The returned value will lie between 0 and n (but will be -1 when input is nonsense).
37 VNL_EXPORT int vnl_sample_binomial(int n, double q);
38 
39 //: Bernoulli distribution ("coin toss").
40 // The returned value will be 0 (with probability q) or 1 (with probability 1-q).
41 // For a "fair" coin toss, use q=0.5.
42 // When q does not lie between 0 and 1, the value -1 is returned.
43 VNL_EXPORT int vnl_sample_bernoulli(double q);
44 
45 // ----------------------------------------
46 
47 //: handy function to fill a range of values.
48 template <class I>
49 inline void vnl_sample_uniform(I begin, I end, double a, double b)
50 {
51  for (I p=begin; p!=end; ++p)
52  (*p) = vnl_sample_uniform(a, b);
53 }
54 
55 //: handy function to fill a range of values.
56 template <class I>
57 inline void vnl_sample_normal(I begin, I end, double mean, double sigma)
58 {
59  for (I p=begin; p!=end; ++p)
60  (*p) = vnl_sample_normal(mean, sigma);
61 }
62 
63 //: handy function to fill a range of values.
64 template <class I>
65 inline void vnl_sample_binomial(I begin, I end, int n, double q)
66 {
67  for (I p=begin; p!=end; ++p)
68  (*p) = vnl_sample_binomial(n, q);
69 }
70 
71 //: handy function to fill a range of values.
72 template <class I>
73 inline void vnl_sample_bernoulli(I begin, I end, double q)
74 {
75  for (I p=begin; p!=end; ++p)
76  (*p) = vnl_sample_bernoulli(q);
77 }
78 
79 //: handy function to fill a range of values.
80 template <class I, class T>
81 inline void vnl_sample_uniform(I begin, I end, double a, double b, T /*dummy*/)
82 {
83  for (I p=begin; p!=end; ++p)
84  (*p) = T(vnl_sample_uniform(a, b));
85 }
86 
87 //: handy function to fill a range of values.
88 template <class I, class T>
89 inline void vnl_sample_normal(I begin, I end, double mean, double sigma, T /*dummy*/)
90 {
91  for (I p=begin; p!=end; ++p)
92  (*p) = T(vnl_sample_normal(mean, sigma));
93 }
94 
95 #endif // vnl_sample_h_
VNL_EXPORT int vnl_sample_binomial(int n, double q)
Return random k, where P(X = k) = [kth term in binomial expansion of (q + (1-q))^n].
Definition: vnl_sample.cxx:60
VNL_EXPORT void vnl_sample_normal_2(double *x, double *y)
two independent samples from a standard normal distribution.
Definition: vnl_sample.cxx:30
VNL_EXPORT double vnl_sample_uniform01()
return a random number uniformly drawn on [0, 1.0).
Definition: vnl_sample.cxx:25
VNL_EXPORT double vnl_sample_uniform(double a, double b)
return a random number uniformly drawn on [a, b).
Definition: vnl_sample.cxx:19
VNL_EXPORT double vnl_sample_normal(double mean, double sigma)
Normal distribution with given mean and standard deviation.
Definition: vnl_sample.cxx:41
VNL_EXPORT void vnl_sample_reseed()
re-seed the random number generator.
Definition: vnl_sample.cxx:9
iterator end()
Iterator pointing to element beyond end of data.
VNL_EXPORT int vnl_sample_bernoulli(double q)
Bernoulli distribution ("coin toss").
Definition: vnl_sample.cxx:49
T mean() const
Return mean of all matrix elements.
iterator begin()
Iterator pointing to start of data.