queso-0.57.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-2017 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 #include <queso/RngBase.h>
27 
28 namespace QUESO {
29 
30 // Default constructor -----------------------------
32  const BaseEnvironment& env,
33  const char* prefix,
34  const std::vector<double>& inpWeights)
35  :
36  m_env (env),
37  m_prefix ((std::string)(prefix)+"fd_"),
38  m_weights(inpWeights.size(),0.)
39 {
40  queso_deprecated();
41 
42  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
43  *m_env.subDisplayFile() << "Entering FiniteDistribution::constructor()"
44  << ": prefix = " << m_prefix
45  << ", inpWeights.size() = " << inpWeights.size()
46  << std::endl;
47  }
48 
49  unsigned int numOfZeroWeights = 0;
50  unsigned int numRareCases = 0;
51  double sumCheck = 0.;
52  unsigned int j = 0;
53  m_map.empty(); // prudenci 2010-08-11
54  for (unsigned int i = 0; i < inpWeights.size(); ++i) {
55  double previousSum = sumCheck;
56  sumCheck += inpWeights[i];
57  if (sumCheck == previousSum) {
58  numOfZeroWeights++;
59  }
60  else {
61  if ((sumCheck - 1) > 1.e-8) {
62  std::cerr << "In FiniteDistribution::constructor()"
63  << ": sumCheck - 1 = " << sumCheck - 1.
64  << std::endl;
65  }
66  queso_require_less_equal_msg((sumCheck - 1), 1.e-8, "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  queso_require_less_equal_msg((1 - sumCheck), 1.e-8, "weights sum is too smaller than 1.");
93 
94 
95  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 3)) {
96  *m_env.subDisplayFile() << "In FiniteDistribution::constructor()"
97  << ": inpWeights.size() = " << inpWeights.size()
98  << ", numOfZeroWeights = " << numOfZeroWeights
99  << ", numRareCases = " << numRareCases
100  << ", m_map.size() = " << m_map.size()
101  << ", m_weights.size() = " << m_weights.size()
102  << std::endl;
103  }
104 
105  queso_require_equal_to_msg(inpWeights.size(), (m_weights.size()+numOfZeroWeights+numRareCases), "number of input weights was not conserved");
106 
107  queso_require_equal_to_msg(m_map.size(), m_weights.size(), "map and inpWeights have different sizes");
108 
109  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
110  *m_env.subDisplayFile() << "Leaving FiniteDistribution::constructor()"
111  << ": prefix = " << m_prefix
112  << std::endl;
113  }
114 }
115 // Destructor ---------------------------------------
117 {
118  queso_deprecated();
119 
120  m_map.empty();
121  m_weights.clear();
122 }
123 // Misc methods--------------------------------------
124 const BaseEnvironment&
126 {
127  queso_deprecated();
128 
129  return m_env;
130 }
131 // Stats methods-------------------------------------
132 const std::vector<double>&
134 {
135  queso_deprecated();
136 
137  return m_weights;
138 }
139 //---------------------------------------------------
140 unsigned int
142 {
143  queso_deprecated();
144 
145  unsigned int result = 0;
146 
147  double aux = m_env.rngObject()->uniformSample();
148  queso_require_msg(!((aux < 0) || (aux > 1.)), "invalid uniform");
149 
150  if (aux == 0.) {
151  result = 0;
152  }
153  else if (aux == 1.) {
154  result = m_map.find(aux)->second;
155  }
156  else {
157  result = m_map.upper_bound(aux)->second;
158  //if (m_map.upper_bound(aux)->second == 0) {
159  // result = 0;
160  //}
161  //else {
162  // result = m_map.upper_bound(aux)->second-1;
163  //}
164  }
165 #if 0 // WE insert 'i' in map, not 'j'. So, the tests below don't make sense
166  if (result >= m_map.size()) {
167  std::cerr << "In FiniteDistribution::sample()"
168  << ": aux = " << aux
169  << ", m_map.size() = " << m_map.size()
170  << ", result = " << result
171  << std::endl;
172  }
173  queso_require_less_msg(result, m_map.size(), "invalid result");
174 #endif
175 
176  return result;
177 }
178 
179 } // End namespace QUESO
const std::vector< double > & weights() const
Weights.
std::map< double, unsigned int > m_map
virtual ~FiniteDistribution()
Virtual destructor.
const RngBase * rngObject() const
Access to the RNG object.
Definition: Environment.C:471
unsigned int sample() const
Samples.
const BaseEnvironment & env() const
Environment; access to protected attribute m_env.
const BaseEnvironment & m_env
std::vector< double > m_weights
virtual double uniformSample() const =0
Samples a value from a uniform distribution.
MonteCarloSGOptions::MonteCarloSGOptions(const BaseEnvironment &env, const char *prefix, const McOptionsValues &alternativeOptionsValues queso_require_equal_to_msg)(m_env.optionsInputFileName(), std::string(""), std::string("this constructor is incompatible with the existence of an options input file"))
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
Definition: Environment.C:320
unsigned int displayVerbosity() const
Definition: Environment.C:450
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:198

Generated on Sat Apr 22 2017 14:04:36 for queso-0.57.0 by  doxygen 1.8.5