queso-0.53.0
FiniteDistribution.C
Go to the documentation of this file.
1 //-----------------------------------------------------------------------bl-
2 //--------------------------------------------------------------------------
3 //
4 // QUESO - a library to support the Quantification of Uncertainty
5 // for Estimation, Simulation and Optimization
6 //
7 // Copyright (C) 2008-2015 The PECOS Development Team
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the Version 2.1 GNU Lesser General
11 // Public License as published by the Free Software Foundation.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc. 51 Franklin Street, Fifth Floor,
21 // Boston, MA 02110-1301 USA
22 //
23 //-----------------------------------------------------------------------el-
24 
25 #include <queso/FiniteDistribution.h>
26 
27 namespace QUESO {
28 
29 // Default constructor -----------------------------
31  const BaseEnvironment& env,
32  const char* prefix,
33  const std::vector<double>& inpWeights)
34  :
35  m_env (env),
36  m_prefix ((std::string)(prefix)+"fd_"),
37  m_weights(inpWeights.size(),0.)
38 {
40 
41  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
42  *m_env.subDisplayFile() << "Entering FiniteDistribution::constructor()"
43  << ": prefix = " << m_prefix
44  << ", inpWeights.size() = " << inpWeights.size()
45  << std::endl;
46  }
47 
48  unsigned int numOfZeroWeights = 0;
49  unsigned int numRareCases = 0;
50  double sumCheck = 0.;
51  unsigned int j = 0;
52  m_map.empty(); // prudenci 2010-08-11
53  for (unsigned int i = 0; i < inpWeights.size(); ++i) {
54  double previousSum = sumCheck;
55  sumCheck += inpWeights[i];
56  if (sumCheck == previousSum) {
57  numOfZeroWeights++;
58  }
59  else {
60  if ((sumCheck - 1) > 1.e-8) {
61  std::cerr << "In FiniteDistribution::constructor()"
62  << ": sumCheck - 1 = " << sumCheck - 1.
63  << std::endl;
64  }
65  queso_require_less_equal_msg((sumCheck - 1), 1.e-8, "weights sum is too bigger than 1.");
66 
67  if (sumCheck > 1.) sumCheck = 1.;
68  m_weights[j] = inpWeights[i];
69  std::pair<std::map<double,unsigned int>::iterator,bool> ret;
70  ret = m_map.insert(std::map<double,unsigned int>::value_type(sumCheck,i));
71  if (ret.second == true) {
72  j++;
73  }
74  else {
75  numRareCases++;
76  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 2)) {
77  *m_env.subDisplayFile() << "In FiniteDistribution::constructor()"
78  << ": WARNING, map insertion failed"
79  << std::endl;
80  }
81  }
82  }
83  }
84  m_weights.resize(j,0.);
85 
86  if ((1 - sumCheck) > 1.e-8) {
87  std::cerr << "In FiniteDistribution::constructor()"
88  << ": 1 - sumCheck = " << 1. - sumCheck
89  << std::endl;
90  }
91  queso_require_less_equal_msg((1 - sumCheck), 1.e-8, "weights sum is too smaller than 1.");
92 
93 
94  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 3)) {
95  *m_env.subDisplayFile() << "In FiniteDistribution::constructor()"
96  << ": inpWeights.size() = " << inpWeights.size()
97  << ", numOfZeroWeights = " << numOfZeroWeights
98  << ", numRareCases = " << numRareCases
99  << ", m_map.size() = " << m_map.size()
100  << ", m_weights.size() = " << m_weights.size()
101  << std::endl;
102  }
103 
104  queso_require_equal_to_msg(inpWeights.size(), (m_weights.size()+numOfZeroWeights+numRareCases), "number of input weights was not conserved");
105 
106  queso_require_equal_to_msg(m_map.size(), m_weights.size(), "map and inpWeights have different sizes");
107 
108  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
109  *m_env.subDisplayFile() << "Leaving FiniteDistribution::constructor()"
110  << ": prefix = " << m_prefix
111  << std::endl;
112  }
113 }
114 // Destructor ---------------------------------------
116 {
118 
119  m_map.empty();
120  m_weights.clear();
121 }
122 // Misc methods--------------------------------------
123 const BaseEnvironment&
125 {
127 
128  return m_env;
129 }
130 // Stats methods-------------------------------------
131 const std::vector<double>&
133 {
135 
136  return m_weights;
137 }
138 //---------------------------------------------------
139 unsigned int
141 {
143 
144  unsigned int result = 0;
145 
146  double aux = m_env.rngObject()->uniformSample();
147  queso_require_msg(!((aux < 0) || (aux > 1.)), "invalid uniform");
148 
149  if (aux == 0.) {
150  result = 0;
151  }
152  else if (aux == 1.) {
153  result = m_map.find(aux)->second;
154  }
155  else {
156  result = m_map.upper_bound(aux)->second;
157  //if (m_map.upper_bound(aux)->second == 0) {
158  // result = 0;
159  //}
160  //else {
161  // result = m_map.upper_bound(aux)->second-1;
162  //}
163  }
164 #if 0 // WE insert 'i' in map, not 'j'. So, the tests below don't make sense
165  if (result >= m_map.size()) {
166  std::cerr << "In FiniteDistribution::sample()"
167  << ": aux = " << aux
168  << ", m_map.size() = " << m_map.size()
169  << ", result = " << result
170  << std::endl;
171  }
172  queso_require_less_msg(result, m_map.size(), "invalid result");
173 #endif
174 
175  return result;
176 }
177 
178 } // End namespace QUESO
unsigned int displayVerbosity() const
Definition: Environment.C:396
const std::vector< double > & weights() const
Weights.
std::map< double, unsigned int > m_map
const BaseEnvironment & m_env
unsigned int sample() const
Samples.
const BaseEnvironment & env() const
Environment; access to protected attribute m_env.
#define queso_require_less_equal_msg(expr1, expr2, msg)
Definition: asserts.h:89
#define queso_require_equal_to_msg(expr1, expr2, msg)
Definition: asserts.h:85
std::vector< double > m_weights
#define queso_require_msg(asserted, msg)
Definition: asserts.h:69
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
Definition: Environment.C:274
#define queso_require_less_msg(expr1, expr2, msg)
Definition: asserts.h:87
This (virtual) class sets up the environment underlying the use of the QUESO library by an executable...
Definition: Environment.h:193
virtual ~FiniteDistribution()
Virtual destructor.
#define queso_deprecated()
Definition: Defines.h:120
const RngBase * rngObject() const
Access to the RNG object.
Definition: Environment.C:417
FiniteDistribution(const BaseEnvironment &env, const char *prefix, const std::vector< double > &inpWeights)
Constructor.
virtual double uniformSample() const =0
Samples a value from a uniform distribution.

Generated on Thu Jun 11 2015 13:52:32 for queso-0.53.0 by  doxygen 1.8.5