25 #include <queso/FiniteDistribution.h>
26 #include <queso/RngBase.h>
34 const std::vector<double>& inpWeights)
37 m_prefix ((std::string)(prefix)+
"fd_"),
38 m_weights(inpWeights.
size(),0.)
45 <<
", inpWeights.size() = " << inpWeights.size()
49 unsigned int numOfZeroWeights = 0;
50 unsigned int numRareCases = 0;
54 for (
unsigned int i = 0; i < inpWeights.size(); ++i) {
55 double previousSum = sumCheck;
56 sumCheck += inpWeights[i];
57 if (sumCheck == previousSum) {
61 if ((sumCheck - 1) > 1.e-8) {
62 std::cerr <<
"In FiniteDistribution::constructor()"
63 <<
": sumCheck - 1 = " << sumCheck - 1.
66 queso_require_less_equal_msg((sumCheck - 1), 1.e-8,
"weights sum is too bigger than 1.");
68 if (sumCheck > 1.) sumCheck = 1.;
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) {
79 <<
": WARNING, map insertion failed"
87 if ((1 - sumCheck) > 1.e-8) {
88 std::cerr <<
"In FiniteDistribution::constructor()"
89 <<
": 1 - sumCheck = " << 1. - sumCheck
92 queso_require_less_equal_msg((1 - sumCheck), 1.e-8,
"weights sum is too smaller than 1.");
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()
132 const std::vector<double>&
145 unsigned int result = 0;
148 queso_require_msg(!((aux < 0) || (aux > 1.)),
"invalid uniform");
153 else if (aux == 1.) {
154 result =
m_map.find(aux)->second;
157 result =
m_map.upper_bound(aux)->second;
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()"
169 <<
", m_map.size() = " <<
m_map.size()
170 <<
", result = " << result
173 queso_require_less_msg(result,
m_map.size(),
"invalid result");
FiniteDistribution(const BaseEnvironment &env, const char *prefix, const std::vector< double > &inpWeights)
Constructor.
const std::vector< double > & weights() const
Weights.
std::map< double, unsigned int > m_map
This (virtual) class sets up the environment underlying the use of the QUESO library by an executable...
const BaseEnvironment & env() const
Environment; access to protected attribute m_env.
std::vector< double > m_weights
const RngBase * rngObject() const
Access to the RNG object.
unsigned int sample() const
Samples.
virtual ~FiniteDistribution()
Virtual destructor.
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"))
unsigned int displayVerbosity() const
const BaseEnvironment & m_env
virtual double uniformSample() const =0
Samples a value from a uniform distribution.
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).