queso-0.57.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
QUESO::FiniteDistribution Class Reference

A templated class for a finite distribution. More...

#include <FiniteDistribution.h>

Public Member Functions

Constructor/Destructor methods
 FiniteDistribution (const BaseEnvironment &env, const char *prefix, const std::vector< double > &inpWeights)
 Constructor. More...
 
virtual ~FiniteDistribution ()
 Virtual destructor. More...
 
Misc methods
const BaseEnvironmentenv () const
 Environment; access to protected attribute m_env. More...
 
Statistical methods
const std::vector< double > & weights () const
 Weights. More...
 
unsigned int sample () const
 Samples. More...
 

Protected Attributes

const BaseEnvironmentm_env
 
std::string m_prefix
 
std::vector< double > m_weights
 
std::map< double, unsigned int > m_map
 

Detailed Description

A templated class for a finite distribution.

Unordered, discrete distribution, whose weights must be nonnegative, and are treated as unnormalized probabilities.
TODO: Describe me better!

Definition at line 45 of file FiniteDistribution.h.

Constructor & Destructor Documentation

QUESO::FiniteDistribution::FiniteDistribution ( const BaseEnvironment env,
const char *  prefix,
const std::vector< double > &  inpWeights 
)

Constructor.

Definition at line 31 of file FiniteDistribution.C.

References QUESO::BaseEnvironment::displayVerbosity(), m_env, m_map, m_prefix, m_weights, QUESO::queso_require_equal_to_msg, and QUESO::BaseEnvironment::subDisplayFile().

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 }
std::map< double, unsigned int > m_map
const BaseEnvironment & env() const
Environment; access to protected attribute m_env.
std::vector< double > m_weights
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
Definition: Environment.C:450
const BaseEnvironment & m_env
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
Definition: Environment.C:320
QUESO::FiniteDistribution::~FiniteDistribution ( )
virtual

Virtual destructor.

Definition at line 116 of file FiniteDistribution.C.

References m_map, and m_weights.

117 {
118  queso_deprecated();
119 
120  m_map.empty();
121  m_weights.clear();
122 }
std::map< double, unsigned int > m_map
std::vector< double > m_weights

Member Function Documentation

const BaseEnvironment & QUESO::FiniteDistribution::env ( ) const

Environment; access to protected attribute m_env.

Definition at line 125 of file FiniteDistribution.C.

References m_env.

126 {
127  queso_deprecated();
128 
129  return m_env;
130 }
const BaseEnvironment & m_env
unsigned int QUESO::FiniteDistribution::sample ( ) const

Samples.

Definition at line 141 of file FiniteDistribution.C.

References m_env, m_map, QUESO::BaseEnvironment::rngObject(), and QUESO::RngBase::uniformSample().

Referenced by QUESO::MLSampling< P_V, P_M >::sampleIndexes_proc0().

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 }
std::map< double, unsigned int > m_map
const RngBase * rngObject() const
Access to the RNG object.
Definition: Environment.C:471
const BaseEnvironment & m_env
virtual double uniformSample() const =0
Samples a value from a uniform distribution.
const std::vector< double > & QUESO::FiniteDistribution::weights ( ) const

Weights.

Definition at line 133 of file FiniteDistribution.C.

References m_weights.

134 {
135  queso_deprecated();
136 
137  return m_weights;
138 }
std::vector< double > m_weights

Member Data Documentation

const BaseEnvironment& QUESO::FiniteDistribution::m_env
protected

Definition at line 73 of file FiniteDistribution.h.

Referenced by env(), FiniteDistribution(), and sample().

std::map<double,unsigned int> QUESO::FiniteDistribution::m_map
protected

Definition at line 77 of file FiniteDistribution.h.

Referenced by FiniteDistribution(), sample(), and ~FiniteDistribution().

std::string QUESO::FiniteDistribution::m_prefix
protected

Definition at line 74 of file FiniteDistribution.h.

Referenced by FiniteDistribution().

std::vector<double> QUESO::FiniteDistribution::m_weights
protected

Definition at line 75 of file FiniteDistribution.h.

Referenced by FiniteDistribution(), weights(), and ~FiniteDistribution().


The documentation for this class was generated from the following files:

Generated on Tue Jun 5 2018 19:49:23 for queso-0.57.1 by  doxygen 1.8.5