queso-0.57.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
QUESO::SampledScalarCdf< T > Class Template Reference

A class for handling sampled CDFs. More...

#include <SampledScalarCdf.h>

Inheritance diagram for QUESO::SampledScalarCdf< T >:
QUESO::BaseScalarCdf< T >

Public Member Functions

Constructor/Destructor methods
 SampledScalarCdf (const BaseEnvironment &env, const char *prefix, const BaseOneDGrid< T > &cdfGrid, const std::vector< double > &cdfValues)
 Default constructor. More...
 
 ~SampledScalarCdf ()
 Destructor. More...
 
Mathematical methods
double value (T paramValue) const
 Returns the value of the CDF at paramValue. More...
 
inverse (double cdfValue) const
 Returns the position of a given value of CDF. More...
 
void getSupport (T &minHorizontal, T &maxHorizontal) const
 Returns the support (image) of the CDF between two horizontal values (domain). More...
 
I/O methods
void print (std::ostream &os) const
 Prints the CDF (values of the grid points and of the CDF at such grid points). More...
 
void subWriteContents (const std::string &varNamePrefix, const std::string &fileName, const std::string &fileType, const std::set< unsigned int > &allowedSubEnvIds) const
 Writes the CDF of an allowed sub-environment to a file. More...
 
- Public Member Functions inherited from QUESO::BaseScalarCdf< T >
 BaseScalarCdf (const BaseEnvironment &env, const char *prefix)
 Default constructor. More...
 
virtual ~BaseScalarCdf ()
 Virtual destructor. More...
 
const BaseEnvironmentenv () const
 Environment. Access to private attribute m_env. More...
 
const std::string & prefix () const
 Access to private attribute m_prefix. More...
 

Protected Attributes

const BaseOneDGrid< T > & m_cdfGrid
 
const std::vector< double > & m_cdfValues
 
- Protected Attributes inherited from QUESO::BaseScalarCdf< T >
const BaseEnvironmentm_env
 
std::string m_prefix
 
m_minHorizontal
 
m_maxHorizontal
 

Detailed Description

template<class T>
class QUESO::SampledScalarCdf< T >

A class for handling sampled CDFs.

This class implements a sampled cumulative distribution function (CDF), given the grid points where it will be sampled and its resulting values.

Definition at line 46 of file SampledScalarCdf.h.

Constructor & Destructor Documentation

template<class T >
QUESO::SampledScalarCdf< T >::SampledScalarCdf ( const BaseEnvironment env,
const char *  prefix,
const BaseOneDGrid< T > &  cdfGrid,
const std::vector< double > &  cdfValues 
)

Default constructor.

Instantiates an object of the class given a prefix, the environment, the grid points where it will be sampled/evaluated and its resulting values.

Definition at line 31 of file SampledScalarCdf.C.

References QUESO::BaseEnvironment::displayVerbosity(), QUESO::BaseScalarCdf< T >::m_env, QUESO::BaseScalarCdf< T >::m_prefix, and QUESO::BaseEnvironment::subDisplayFile().

36  :
37  BaseScalarCdf<T>(env,((std::string)(prefix)+"").c_str()),
38  m_cdfGrid (cdfGrid ),
39  m_cdfValues (cdfValues)
40 {
41  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
42  *m_env.subDisplayFile() << "Entering SampledScalarCdf<T>::constructor()"
43  << ": prefix = " << m_prefix
44  << std::endl;
45  }
46 
47  //for (unsigned int i = 0; i < m_cdfValues.size(); ++i) {
48  // m_sortedCdfValues[i] = m_cdfValues[i];
49  //}
50  //std::sort(m_sortedCdfValues.begin(), m_sortedCdfValues.end());
51 
52  if ((m_env.subDisplayFile()) && (m_env.displayVerbosity() >= 5)) {
53  *m_env.subDisplayFile() << "Leaving SampledScalarCdf<T>::constructor()"
54  << ": prefix = " << m_prefix
55  << std::endl;
56  }
57 }
const BaseEnvironment & env() const
Environment. Access to private attribute m_env.
Definition: ScalarCdf.C:60
const BaseEnvironment & m_env
Definition: ScalarCdf.h:99
const BaseOneDGrid< T > & m_cdfGrid
std::string m_prefix
Definition: ScalarCdf.h:100
const std::vector< double > & m_cdfValues
const std::string & prefix() const
Access to private attribute m_prefix.
Definition: ScalarCdf.C:67
unsigned int displayVerbosity() const
Definition: Environment.C:450
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
Definition: Environment.C:320
template<class T >
QUESO::SampledScalarCdf< T >::~SampledScalarCdf ( )

Destructor.

Definition at line 60 of file SampledScalarCdf.C.

61 {
62 }

Member Function Documentation

template<class T >
void QUESO::SampledScalarCdf< T >::getSupport ( T &  minHorizontal,
T &  maxHorizontal 
) const
virtual

Returns the support (image) of the CDF between two horizontal values (domain).

Implements QUESO::BaseScalarCdf< T >.

Definition at line 153 of file SampledScalarCdf.C.

References QUESO::queso_require_equal_to_msg, and QUESO::queso_require_not_equal_to_msg.

154 {
155  if ((m_minHorizontal == -INFINITY) ||
156  (m_maxHorizontal == INFINITY)) {
157  queso_require_msg(!((m_minHorizontal != -INFINITY) || (m_maxHorizontal != INFINITY)), "unexpected values of m_minHorizontal and/or m_maxHorizontal");
158 
159  unsigned int iMax = m_cdfGrid.size();
160 
161  for (unsigned int i = 0; i < iMax; ++i) {
162  if (m_cdfValues[i] > 0.) {
163  if (i > 0) --i;
165  break;
166  }
167  }
168 
169  queso_require_not_equal_to_msg(m_minHorizontal, -INFINITY, "unexpected value for m_minHorizontal");
170 
171  if (iMax == 1) {
172  queso_require_equal_to_msg(m_cdfValues[iMax - 1], 1., "unexpected value for case 'iMax = 1'");
173  m_maxHorizontal = m_cdfGrid[iMax-1];
174  }
175  else for (unsigned int i = 0; i < iMax; ++i) {
176  if (m_cdfValues[iMax-1-i] < 1.) {
177  if (i > 0) --i;
178  m_maxHorizontal = m_cdfGrid[iMax-1-i];
179  break;
180  }
181  }
182 
183  queso_require_not_equal_to_msg(m_maxHorizontal, INFINITY, "unexpected value for m_maxHorizontal");
184  }
185 
186  minHorizontal = m_minHorizontal;
187  maxHorizontal = m_maxHorizontal;
188 
189  return;
190 }
MonteCarloSGOptions::MonteCarloSGOptions(const BaseEnvironment &env, const char *prefix queso_require_not_equal_to_msg)(m_env.optionsInputFileName(), std::string(""), std::string("this constructor is incompatible with the absence of an options input file"))
const BaseOneDGrid< T > & m_cdfGrid
const std::vector< double > & m_cdfValues
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"))
template<class T >
T QUESO::SampledScalarCdf< T >::inverse ( double  cdfValue) const
virtual

Returns the position of a given value of CDF.

Implements QUESO::BaseScalarCdf< T >.

Definition at line 102 of file SampledScalarCdf.C.

103 {
104  //*m_env.subDisplayFile() << "In SampledScalarCdf::inverse(): cdfValue = " << cdfValue
105  // << std::endl;
106  queso_require_msg(!((cdfValue < 0.) || (1. < cdfValue)), "invalid cdfValue");
107  double result = 0.;
108  unsigned int i = 0;
109  unsigned int j = m_cdfValues.size()-1;
110  bool searchPosition = true;
111  do {
112  if (cdfValue == m_cdfValues[i]) {
113  while ((0 < i) && (cdfValue == m_cdfValues[i-1])) --i;
114  result = m_cdfGrid[i];
115  searchPosition = false;
116  }
117 
118  if (cdfValue == m_cdfValues[j]) {
119  while ((0 < j) && (cdfValue == m_cdfValues[j-1])) --j;
120  result = m_cdfGrid[j];
121  searchPosition = false;
122  }
123 
124  if ((j-i) <= 0) {
125  queso_error_msg("invalid pair of values 'i' and 'j'");
126  }
127  else if ((j-i) == 1) {
128  double ratio = (cdfValue-m_cdfValues[i])/(m_cdfValues[j]-m_cdfValues[i]);
129  result = (1.-ratio)*m_cdfGrid[i] + ratio*m_cdfGrid[j];
130  searchPosition = false;
131  }
132  else {
133  unsigned int k= (unsigned int) ((i+j)*.5);
134  if (cdfValue < m_cdfValues[k]) {
135  j = k;
136  }
137  else if (cdfValue == m_cdfValues[k]) {
138  while ((0 < k) && (cdfValue == m_cdfValues[k-1])) --k;
139  result = m_cdfGrid[k];
140  searchPosition = false;
141  }
142  else {
143  i = k;
144  }
145  }
146  } while (searchPosition);
147 
148  return result;
149 }
const BaseOneDGrid< T > & m_cdfGrid
const std::vector< double > & m_cdfValues
template<class T >
void QUESO::SampledScalarCdf< T >::print ( std::ostream &  os) const
virtual

Prints the CDF (values of the grid points and of the CDF at such grid points).

Implements QUESO::BaseScalarCdf< T >.

Definition at line 194 of file SampledScalarCdf.C.

195 {
196  // Print values *of* grid points
197  os << m_cdfGrid;
198 
199  // Print *cdf* values *at* grid points
200  os << m_prefix << "values_sub" << m_env.subIdString() << " = zeros(" << m_cdfValues.size()
201  << "," << 1
202  << ");"
203  << std::endl;
204  os << m_prefix << "values_sub" << m_env.subIdString() << " = [";
205  for (unsigned int j = 0; j < m_cdfValues.size(); ++j) {
206  os << m_cdfValues[j] << " ";
207  }
208  os << "];"
209  << std::endl;
210 
211  return;
212 }
const BaseEnvironment & m_env
Definition: ScalarCdf.h:99
const BaseOneDGrid< T > & m_cdfGrid
std::string m_prefix
Definition: ScalarCdf.h:100
const std::vector< double > & m_cdfValues
const std::string & subIdString() const
Access to the attribute m_subIdString; which stores the string for the sub-environment, and it will be used, for instance, to create the output files for each sub-environment.
Definition: Environment.C:348
template<class T >
void QUESO::SampledScalarCdf< T >::subWriteContents ( const std::string &  varNamePrefix,
const std::string &  fileName,
const std::string &  fileType,
const std::set< unsigned int > &  allowedSubEnvIds 
) const
virtual

Writes the CDF of an allowed sub-environment to a file.

It will write the data in Octave/Matlab compatible format.

Reimplemented from QUESO::BaseScalarCdf< T >.

Definition at line 216 of file SampledScalarCdf.C.

References QUESO::FilePtrSetStruct::ofsVar.

221 {
222  queso_require_greater_equal_msg(m_env.subRank(), 0, "unexpected subRank");
223 
224  FilePtrSetStruct filePtrSet;
225  if (m_env.openOutputFile(fileName,
226  fileType, // "m or hdf"
227  allowedSubEnvIds,
228  false,
229  filePtrSet)) {
230 
231  // Grid
232  *filePtrSet.ofsVar << varNamePrefix << "grid_sub" << m_env.subIdString() << " = zeros(" << m_cdfGrid.size()
233  << "," << 1
234  << ");"
235  << std::endl;
236  *filePtrSet.ofsVar << varNamePrefix << "grid_sub" << m_env.subIdString() << " = [";
237 
238  unsigned int savedPrecision = filePtrSet.ofsVar->precision();
239  filePtrSet.ofsVar->precision(16);
240  for (unsigned int j = 0; j < m_cdfGrid.size(); ++j) {
241  *filePtrSet.ofsVar << m_cdfGrid[j] << " ";
242  }
243  filePtrSet.ofsVar->precision(savedPrecision);
244 
245  *filePtrSet.ofsVar << "];\n";
246 
247  // Values
248  *filePtrSet.ofsVar << varNamePrefix << "values_sub" << m_env.subIdString() << " = zeros(" << m_cdfValues.size()
249  << "," << 1
250  << ");"
251  << std::endl;
252  *filePtrSet.ofsVar << varNamePrefix << "values_sub" << m_env.subIdString() << " = [";
253 
254  savedPrecision = filePtrSet.ofsVar->precision();
255  filePtrSet.ofsVar->precision(16);
256  for (unsigned int j = 0; j < m_cdfValues.size(); ++j) {
257  *filePtrSet.ofsVar << m_cdfValues[j] << " ";
258  }
259  filePtrSet.ofsVar->precision(savedPrecision);
260 
261  *filePtrSet.ofsVar << "];\n";
262 
263  // Close file
264  m_env.closeFile(filePtrSet,fileType);
265  }
266 
267  return;
268 }
void closeFile(FilePtrSetStruct &filePtrSet, const std::string &fileType) const
Closes the file.
Definition: Environment.C:1084
const BaseEnvironment & m_env
Definition: ScalarCdf.h:99
int subRank() const
Returns the rank of the MPI process in the sub-communicator subComm()
Definition: Environment.C:287
const BaseOneDGrid< T > & m_cdfGrid
const std::vector< double > & m_cdfValues
const std::string & subIdString() const
Access to the attribute m_subIdString; which stores the string for the sub-environment, and it will be used, for instance, to create the output files for each sub-environment.
Definition: Environment.C:348
bool openOutputFile(const std::string &fileName, const std::string &fileType, const std::set< unsigned int > &allowedSubEnvIds, bool writeOver, FilePtrSetStruct &filePtrSet) const
Opens an output file for each sub-environment that was chosen to send data to the file...
Definition: Environment.C:521
template<class T >
double QUESO::SampledScalarCdf< T >::value ( paramValue) const
virtual

Returns the value of the CDF at paramValue.

Implements QUESO::BaseScalarCdf< T >.

Definition at line 66 of file SampledScalarCdf.C.

67 {
68  double result = 0.;
69  if (paramValue <= m_cdfGrid[0]) {
70  result = 0.;
71  }
72  else if (m_cdfGrid[m_cdfGrid.size()-1] <= paramValue) {
73  result = 1.;
74  }
75  else {
76  unsigned int intervalId = m_cdfGrid.findIntervalId(paramValue);
77  queso_require_less_msg(intervalId, (m_cdfGrid.size()-1), "invalid intervalId");
78 
79  double intervalLen = m_cdfGrid[intervalId+1] - m_cdfGrid[intervalId];
80  double ratio = (paramValue - m_cdfGrid[intervalId])/intervalLen;
81 #if 0
82  *m_env.subDisplayFile() << "In SampledScalarCdf::value()"
83  << ": paramValue = " << paramValue
84  << ", intervalId = " << intervalId
85  << ", cdfGrid.size() = " << m_cdfGrid.size()
86  << ", m_cdfGrid[intervalId] = " << m_cdfGrid[intervalId]
87  << ", m_cdfGrid[intervalId+1] = " << m_cdfGrid[intervalId+1]
88  << ", intervalLen = " << intervalLen
89  << ", ratio = " << ratio
90  << std::endl;
91 #endif
92  queso_require_greater_equal_msg(ratio, 0., "invalid ratio");
93 
94  result = (1.-ratio)*m_cdfValues[intervalId] + ratio*m_cdfValues[intervalId+1];
95  }
96 
97  return result;
98 }
const BaseEnvironment & m_env
Definition: ScalarCdf.h:99
const BaseOneDGrid< T > & m_cdfGrid
const std::vector< double > & m_cdfValues
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
Definition: Environment.C:320

Member Data Documentation

template<class T>
const BaseOneDGrid<T>& QUESO::SampledScalarCdf< T >::m_cdfGrid
protected

Definition at line 92 of file SampledScalarCdf.h.

template<class T>
const std::vector<double>& QUESO::SampledScalarCdf< T >::m_cdfValues
protected

Definition at line 93 of file SampledScalarCdf.h.


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

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