queso-0.53.0
Protected Attributes | List of all members
QUESO::FiniteDistribution Class Reference

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

#include <FiniteDistribution.h>

Collaboration diagram for QUESO::FiniteDistribution:
Collaboration graph
[legend]

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 30 of file FiniteDistribution.C.

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

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 }
unsigned int displayVerbosity() const
Definition: Environment.C:396
std::map< double, unsigned int > m_map
const BaseEnvironment & m_env
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
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
Definition: Environment.C:274
#define queso_deprecated()
Definition: Defines.h:120
QUESO::FiniteDistribution::~FiniteDistribution ( )
virtual

Virtual destructor.

Definition at line 115 of file FiniteDistribution.C.

References m_map, m_weights, and queso_deprecated.

116 {
118 
119  m_map.empty();
120  m_weights.clear();
121 }
std::map< double, unsigned int > m_map
std::vector< double > m_weights
#define queso_deprecated()
Definition: Defines.h:120

Member Function Documentation

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

Environment; access to protected attribute m_env.

Definition at line 124 of file FiniteDistribution.C.

References m_env, and queso_deprecated.

125 {
127 
128  return m_env;
129 }
const BaseEnvironment & m_env
#define queso_deprecated()
Definition: Defines.h:120
unsigned int QUESO::FiniteDistribution::sample ( ) const

Samples.

Definition at line 140 of file FiniteDistribution.C.

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

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

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 }
std::map< double, unsigned int > m_map
const BaseEnvironment & m_env
#define queso_require_msg(asserted, msg)
Definition: asserts.h:69
#define queso_require_less_msg(expr1, expr2, msg)
Definition: asserts.h:87
#define queso_deprecated()
Definition: Defines.h:120
const RngBase * rngObject() const
Access to the RNG object.
Definition: Environment.C:417
virtual double uniformSample() const =0
Samples a value from a uniform distribution.
const std::vector< double > & QUESO::FiniteDistribution::weights ( ) const

Weights.

Definition at line 132 of file FiniteDistribution.C.

References m_weights, and queso_deprecated.

133 {
135 
136  return m_weights;
137 }
std::vector< double > m_weights
#define queso_deprecated()
Definition: Defines.h:120

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 Thu Jun 11 2015 13:52:35 for queso-0.53.0 by  doxygen 1.8.5