queso-0.51.1
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,2009,2010,2011,2012,2013 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 {
39  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
40  *m_env.subDisplayFile() << "Entering FiniteDistribution::constructor()"
41  << ": prefix = " << m_prefix
42  << ", inpWeights.size() = " << inpWeights.size()
43  << std::endl;
44  }
45 
46  unsigned int numOfZeroWeights = 0;
47  unsigned int numRareCases = 0;
48  double sumCheck = 0.;
49  unsigned int j = 0;
50  m_map.empty(); // prudenci 2010-08-11
51  for (unsigned int i = 0; i < inpWeights.size(); ++i) {
52  double previousSum = sumCheck;
53  sumCheck += inpWeights[i];
54  if (sumCheck == previousSum) {
55  numOfZeroWeights++;
56  }
57  else {
58  if ((sumCheck - 1) > 1.e-8) {
59  std::cerr << "In FiniteDistribution::constructor()"
60  << ": sumCheck - 1 = " << sumCheck - 1.
61  << std::endl;
62  }
63  UQ_FATAL_TEST_MACRO((sumCheck - 1) > 1.e-8,
64  m_env.worldRank(),
65  "FiniteDistribution::constructor()",
66  "weights sum is too bigger than 1.");
67 
68  if (sumCheck > 1.) sumCheck = 1.;
69  m_weights[j] = inpWeights[i];
70  std::pair<std::map<double,unsigned int>::iterator,bool> ret;
71  ret = m_map.insert(std::map<double,unsigned int>::value_type(sumCheck,i));
72  if (ret.second == true) {
73  j++;
74  }
75  else {
76  numRareCases++;
77  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 2)) {
78  *m_env.subDisplayFile() << "In FiniteDistribution::constructor()"
79  << ": WARNING, map insertion failed"
80  << std::endl;
81  }
82  }
83  }
84  }
85  m_weights.resize(j,0.);
86 
87  if ((1 - sumCheck) > 1.e-8) {
88  std::cerr << "In FiniteDistribution::constructor()"
89  << ": 1 - sumCheck = " << 1. - sumCheck
90  << std::endl;
91  }
92  UQ_FATAL_TEST_MACRO((1 - sumCheck) > 1.e-8,
93  m_env.worldRank(),
94  "FiniteDistribution::constructor()",
95  "weights sum is too smaller than 1.");
96 
97 
98  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 3)) {
99  *m_env.subDisplayFile() << "In FiniteDistribution::constructor()"
100  << ": inpWeights.size() = " << inpWeights.size()
101  << ", numOfZeroWeights = " << numOfZeroWeights
102  << ", numRareCases = " << numRareCases
103  << ", m_map.size() = " << m_map.size()
104  << ", m_weights.size() = " << m_weights.size()
105  << std::endl;
106  }
107 
108  UQ_FATAL_TEST_MACRO((inpWeights.size() != (m_weights.size()+numOfZeroWeights+numRareCases)),
109  m_env.worldRank(),
110  "FiniteDistribution::constructor()",
111  "number of input weights was not conserved");
112 
113  UQ_FATAL_TEST_MACRO((m_map.size() != m_weights.size()),
114  m_env.worldRank(),
115  "FiniteDistribution::constructor()",
116  "map and inpWeights have different sizes");
117 
118  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
119  *m_env.subDisplayFile() << "Leaving FiniteDistribution::constructor()"
120  << ": prefix = " << m_prefix
121  << std::endl;
122  }
123 }
124 // Destructor ---------------------------------------
126 {
127  m_map.empty();
128  m_weights.clear();
129 }
130 // Misc methods--------------------------------------
131 const BaseEnvironment&
133 {
134  return m_env;
135 }
136 // Stats methods-------------------------------------
137 const std::vector<double>&
139 {
140  return m_weights;
141 }
142 //---------------------------------------------------
143 unsigned int
145 {
146  unsigned int result = 0;
147 
148  double aux = m_env.rngObject()->uniformSample();
149  UQ_FATAL_TEST_MACRO((aux < 0) || (aux > 1.),
150  m_env.worldRank(),
151  "FiniteDistribution::sample()",
152  "invalid uniform");
153 
154  if (aux == 0.) {
155  result = 0;
156  }
157  else if (aux == 1.) {
158  result = m_map.find(aux)->second;
159  }
160  else {
161  result = m_map.upper_bound(aux)->second;
162  //if (m_map.upper_bound(aux)->second == 0) {
163  // result = 0;
164  //}
165  //else {
166  // result = m_map.upper_bound(aux)->second-1;
167  //}
168  }
169 #if 0 // WE insert 'i' in map, not 'j'. So, the tests below don't make sense
170  if (result >= m_map.size()) {
171  std::cerr << "In FiniteDistribution::sample()"
172  << ": aux = " << aux
173  << ", m_map.size() = " << m_map.size()
174  << ", result = " << result
175  << std::endl;
176  }
177  UQ_FATAL_TEST_MACRO((result >= m_map.size()),
178  m_env.worldRank(),
179  "FiniteDistribution::sample()",
180  "invalid result");
181 #endif
182 
183  return result;
184 }
185 
186 } // End namespace QUESO
const BaseEnvironment & env() const
Environment; access to protected attribute m_env.
const BaseEnvironment & m_env
const RngBase * rngObject() const
Access to the RNG object.
Definition: Environment.C:466
int worldRank() const
Returns the process world rank.
Definition: Environment.C:235
unsigned int sample() const
Samples.
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
Definition: Environment.C:305
FiniteDistribution(const BaseEnvironment &env, const char *prefix, const std::vector< double > &inpWeights)
Constructor.
This (virtual) class sets up the environment underlying the use of the QUESO library by an executable...
Definition: Environment.h:187
const std::vector< double > & weights() const
Weights.
std::map< double, unsigned int > m_map
virtual ~FiniteDistribution()
Virtual destructor.
std::vector< double > m_weights
virtual double uniformSample() const =0
Samples a value from a uniform distribution.
unsigned int displayVerbosity() const
Definition: Environment.C:436
#define UQ_FATAL_TEST_MACRO(test, givenRank, where, what)
Definition: Defines.h:223

Generated on Thu Apr 23 2015 19:26:15 for queso-0.51.1 by  doxygen 1.8.5