libcrn  3.9.5
A document image processing library
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CRNUnivariateRandomTools.cpp
Go to the documentation of this file.
1 /* Copyright 2008-2014 INSA Lyon
2  *
3  * This file is part of libcrn.
4  *
5  * libcrn is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * libcrn is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with libcrn. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * file: CRNUnivariateRandomTools.cpp
19  * \author Jean DUONG, Yann LEYDIER
20  */
21 
23 #include <CRNMath/CRNMatrixInt.h>
27 
28 using namespace crn;
29 
30 /*****************************************************************************/
38 std::vector<double> UnivariateRandomTools::NewUniformSample(size_t n, bool reseed)
39 {
40  if (reseed)
41  srand((unsigned int)time(nullptr));
42 
43  std::vector<double> sample(n);
44 
45  for (size_t k = 0; k < n; ++k)
46  sample[k] = ((double)(rand())) / ((double)(RAND_MAX));
47 
48  return sample;
49 }
50 
51 /*****************************************************************************/
65 std::vector<double> UnivariateRandomTools::NewGaussianSample(double mu, double sigma, size_t n, size_t m, bool reseed)
66 {
67  if (reseed)
68  srand((unsigned int)time(nullptr));
69 
70  std::vector<double> G(n);
71 
72  for (size_t k = 0; k < n; ++k)
73  {
74  // cumulate a uniform sample with m numbers
75 
76  double s = 0.0;
77 
78  for (size_t i = 0; i < m; ++i)
79  s += ((double)(rand())) / ((double)(RAND_MAX));
80 
81  // Gaussian random number from uniform distribution
82 
83  s -= double(m) / 2.0;
84  s *= sigma * sqrt(12.0 / double(m));
85  s += mu;
86 
87  G[k] = s;
88  }
89 
90  return G;
91 }
92 
93 /*****************************************************************************/
103 std::vector<double> UnivariateRandomTools::NewGaussianMixtureSample(const UnivariateGaussianMixture& Mx, size_t n, size_t m, bool reseed)
104 {
105  size_t nbPDF = Mx.GetNbMembers();
106 
107  std::vector<double> Patterns(n);
108  std::vector<double> CumulWeights(nbPDF);
109  std::vector<double> IndexeSample(UnivariateRandomTools::NewUniformSample(n, reseed));
110  std::vector<int> Pop(nbPDF, 0);
111 
112  // Cumulative weights from the mixture
113 
114  double Mass = 0.0;
115 
116  for (size_t k = 0; k < nbPDF; ++k)
117  {
118  Mass += Mx.GetWeight(k);
119 
120  CumulWeights[k] = Mass;
121  }
122 
123  for (size_t k = 0; k < nbPDF; ++k)
124  CumulWeights[k] /= Mass;
125 
126  // Build indexes to indicate which PDF of the mixture is used to generate each pattern
127 
128  for (size_t k = 0; k < n; ++k)
129  {
130  double d = IndexeSample[k];
131  bool Continue = true;
132  size_t Id = 0;
133 
134  while (Continue)
135  {
136  if ((CumulWeights[Id] >= d) || (Id == nbPDF - 1))
137  Continue = false;
138  else
139  ++Id;
140  }
141 
142  Pop[Id] += 1;
143  }
144 
145  // Create patterns
146 
147  size_t PatternIndex = 0;
148 
149  for (size_t p = 0; p < nbPDF; ++p)
150  {
151  int SubSamplePop = Pop[p];
152 
153  std::vector<double> SubSample(NewGaussianSample(Mx.GetMember(p).GetMean(), Mx.GetMember(p).GetDeviation(), SubSamplePop, m, reseed));
154 
155  for (int r = 0; r < SubSamplePop; ++r)
156  {
157  Patterns[PatternIndex] = SubSample[r];
158  ++PatternIndex;
159  }
160  }
161 
162  return Patterns;
163 }
double GetDeviation() const
Returns the standard deviation of a given density function.
crn::StringUTF8 Id
Definition: CRNAltoUtils.h:31
double GetWeight(size_t k) const
Returns the weight of a given density function.
UnivariateGaussianPDF GetMember(size_t k) const
Returns a given density function.
size_t GetNbMembers() const noexcept
Returns the number of density functions.
std::vector< double > NewGaussianSample(double mu=0.0, double sigma=1.0, size_t n=1, size_t m=100, bool reseed=true)
Creates a data sample following a Gaussian probability law.
double GetMean() const noexcept
Returns the mean of a given density function.
std::vector< double > NewUniformSample(size_t n=1, bool reseed=true)
Creates a data sample following an uniform probability law.
std::vector< double > NewGaussianMixtureSample(const UnivariateGaussianMixture &Mx, size_t n=1, size_t m=100, bool reseed=true)
Creates a data sample following a Gaussian probability law.