queso-0.51.1
Protected Attributes | List of all members
QUESO::SampledScalarCdf< T > Class Template Reference

A class for handling sampled CDFs. More...

#include <SampledScalarCdf.h>

Inheritance diagram for QUESO::SampledScalarCdf< T >:
Inheritance graph
[legend]
Collaboration diagram for QUESO::SampledScalarCdf< T >:
Collaboration graph
[legend]

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 std::string & prefix() const
Access to private attribute m_prefix.
Definition: ScalarCdf.C:67
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
Definition: Environment.C:305
const BaseOneDGrid< T > & m_cdfGrid
const BaseEnvironment & env() const
Environment. Access to private attribute m_env.
Definition: ScalarCdf.C:60
const std::vector< double > & m_cdfValues
const BaseEnvironment & m_env
Definition: ScalarCdf.h:106
unsigned int displayVerbosity() const
Definition: Environment.C:436
std::string m_prefix
Definition: ScalarCdf.h:107
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 165 of file SampledScalarCdf.C.

References UQ_FATAL_TEST_MACRO.

166 {
167  if ((m_minHorizontal == -INFINITY) ||
168  (m_maxHorizontal == INFINITY)) {
169  UQ_FATAL_TEST_MACRO((m_minHorizontal != -INFINITY) || (m_maxHorizontal != INFINITY),
170  m_env.worldRank(),
171  "SampledScalarCdf<T>::getSupport()",
172  "unexpected values of m_minHorizontal and/or m_maxHorizontal");
173 
174  unsigned int iMax = m_cdfGrid.size();
175 
176  for (unsigned int i = 0; i < iMax; ++i) {
177  if (m_cdfValues[i] > 0.) {
178  if (i > 0) --i;
180  break;
181  }
182  }
183 
185  m_env.worldRank(),
186  "SampledScalarCdf<T>::getSupport()",
187  "unexpected value for m_minHorizontal");
188 
189  if (iMax == 1) {
190  UQ_FATAL_TEST_MACRO(m_cdfValues[iMax - 1] != 1.,
191  m_env.worldRank(),
192  "SampledScalarCdf<T>::getSupport()",
193  "unexpected value for case 'iMax = 1'");
194  m_maxHorizontal = m_cdfGrid[iMax-1];
195  }
196  else for (unsigned int i = 0; i < iMax; ++i) {
197  if (m_cdfValues[iMax-1-i] < 1.) {
198  if (i > 0) --i;
199  m_maxHorizontal = m_cdfGrid[iMax-1-i];
200  break;
201  }
202  }
203 
205  m_env.worldRank(),
206  "SampledScalarCdf<T>::getSupport()",
207  "unexpected value for m_maxHorizontal");
208  }
209 
210  minHorizontal = m_minHorizontal;
211  maxHorizontal = m_maxHorizontal;
212 
213  return;
214 }
int worldRank() const
Returns the process world rank.
Definition: Environment.C:235
const BaseOneDGrid< T > & m_cdfGrid
const std::vector< double > & m_cdfValues
const BaseEnvironment & m_env
Definition: ScalarCdf.h:106
#define UQ_FATAL_TEST_MACRO(test, givenRank, where, what)
Definition: Defines.h:223
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 108 of file SampledScalarCdf.C.

References UQ_FATAL_TEST_MACRO.

109 {
110  //*m_env.subDisplayFile() << "In SampledScalarCdf::inverse(): cdfValue = " << cdfValue
111  // << std::endl;
112  UQ_FATAL_TEST_MACRO((cdfValue < 0.) || (1. < cdfValue),
113  m_env.worldRank(),
114  "SampledScalarCdf<T>::inverse()",
115  "invalid cdfValue");
116  double result = 0.;
117  unsigned int i = 0;
118  unsigned int j = m_cdfValues.size()-1;
119  bool searchPosition = true;
120  do {
121  if (cdfValue == m_cdfValues[i]) {
122  while ((0 < i) && (cdfValue == m_cdfValues[i-1])) --i;
123  result = m_cdfGrid[i];
124  searchPosition = false;
125  }
126 
127  if (cdfValue == m_cdfValues[j]) {
128  while ((0 < j) && (cdfValue == m_cdfValues[j-1])) --j;
129  result = m_cdfGrid[j];
130  searchPosition = false;
131  }
132 
133  if ((j-i) <= 0) {
134  UQ_FATAL_TEST_MACRO(true,
135  m_env.worldRank(),
136  "SampledScalarCdf<T>::inverse()",
137  "invalid pair of values 'i' and 'j'");
138  }
139  else if ((j-i) == 1) {
140  double ratio = (cdfValue-m_cdfValues[i])/(m_cdfValues[j]-m_cdfValues[i]);
141  result = (1.-ratio)*m_cdfGrid[i] + ratio*m_cdfGrid[j];
142  searchPosition = false;
143  }
144  else {
145  unsigned int k= (unsigned int) ((i+j)*.5);
146  if (cdfValue < m_cdfValues[k]) {
147  j = k;
148  }
149  else if (cdfValue == m_cdfValues[k]) {
150  while ((0 < k) && (cdfValue == m_cdfValues[k-1])) --k;
151  result = m_cdfGrid[k];
152  searchPosition = false;
153  }
154  else {
155  i = k;
156  }
157  }
158  } while (searchPosition);
159 
160  return result;
161 }
int worldRank() const
Returns the process world rank.
Definition: Environment.C:235
const BaseOneDGrid< T > & m_cdfGrid
const std::vector< double > & m_cdfValues
const BaseEnvironment & m_env
Definition: ScalarCdf.h:106
#define UQ_FATAL_TEST_MACRO(test, givenRank, where, what)
Definition: Defines.h:223
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 218 of file SampledScalarCdf.C.

219 {
220  // Print values *of* grid points
221  os << m_cdfGrid;
222 
223  // Print *cdf* values *at* grid points
224  os << m_prefix << "values_sub" << m_env.subIdString() << " = zeros(" << m_cdfValues.size()
225  << "," << 1
226  << ");"
227  << std::endl;
228  os << m_prefix << "values_sub" << m_env.subIdString() << " = [";
229  for (unsigned int j = 0; j < m_cdfValues.size(); ++j) {
230  os << m_cdfValues[j] << " ";
231  }
232  os << "];"
233  << std::endl;
234 
235  return;
236 }
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:335
const BaseOneDGrid< T > & m_cdfGrid
const std::vector< double > & m_cdfValues
const BaseEnvironment & m_env
Definition: ScalarCdf.h:106
std::string m_prefix
Definition: ScalarCdf.h:107
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 240 of file SampledScalarCdf.C.

References QUESO::FilePtrSetStruct::ofsVar, and UQ_FATAL_TEST_MACRO.

245 {
247  m_env.worldRank(),
248  "SampledScalarCdf<T>::subWriteContents()",
249  "unexpected subRank");
250 
251  FilePtrSetStruct filePtrSet;
252  if (m_env.openOutputFile(fileName,
253  fileType, // "m or hdf"
254  allowedSubEnvIds,
255  false,
256  filePtrSet)) {
257 
258  // Grid
259  *filePtrSet.ofsVar << varNamePrefix << "grid_sub" << m_env.subIdString() << " = zeros(" << m_cdfGrid.size()
260  << "," << 1
261  << ");"
262  << std::endl;
263  *filePtrSet.ofsVar << varNamePrefix << "grid_sub" << m_env.subIdString() << " = [";
264 
265  unsigned int savedPrecision = filePtrSet.ofsVar->precision();
266  filePtrSet.ofsVar->precision(16);
267  for (unsigned int j = 0; j < m_cdfGrid.size(); ++j) {
268  *filePtrSet.ofsVar << m_cdfGrid[j] << " ";
269  }
270  filePtrSet.ofsVar->precision(savedPrecision);
271 
272  *filePtrSet.ofsVar << "];\n";
273 
274  // Values
275  *filePtrSet.ofsVar << varNamePrefix << "values_sub" << m_env.subIdString() << " = zeros(" << m_cdfValues.size()
276  << "," << 1
277  << ");"
278  << std::endl;
279  *filePtrSet.ofsVar << varNamePrefix << "values_sub" << m_env.subIdString() << " = [";
280 
281  savedPrecision = filePtrSet.ofsVar->precision();
282  filePtrSet.ofsVar->precision(16);
283  for (unsigned int j = 0; j < m_cdfValues.size(); ++j) {
284  *filePtrSet.ofsVar << m_cdfValues[j] << " ";
285  }
286  filePtrSet.ofsVar->precision(savedPrecision);
287 
288  *filePtrSet.ofsVar << "];\n";
289 
290  // Close file
291  m_env.closeFile(filePtrSet,fileType);
292  }
293 
294  return;
295 }
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:335
int subRank() const
Access function for sub-rank.
Definition: Environment.C:263
int worldRank() const
Returns the process world rank.
Definition: Environment.C:235
const BaseOneDGrid< T > & m_cdfGrid
void closeFile(FilePtrSetStruct &filePtrSet, const std::string &fileType) const
Closes the file.
Definition: Environment.C:1117
const std::vector< double > & m_cdfValues
const BaseEnvironment & m_env
Definition: ScalarCdf.h:106
#define UQ_FATAL_TEST_MACRO(test, givenRank, where, what)
Definition: Defines.h:223
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:516
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.

References UQ_FATAL_TEST_MACRO.

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  UQ_FATAL_TEST_MACRO(intervalId >= (m_cdfGrid.size()-1),
78  m_env.worldRank(),
79  "SampledScalarCdf<T>::value()",
80  "invalid intervalId");
81 
82  double intervalLen = m_cdfGrid[intervalId+1] - m_cdfGrid[intervalId];
83  double ratio = (paramValue - m_cdfGrid[intervalId])/intervalLen;
84 #if 0
85  *m_env.subDisplayFile() << "In SampledScalarCdf::value()"
86  << ": paramValue = " << paramValue
87  << ", intervalId = " << intervalId
88  << ", cdfGrid.size() = " << m_cdfGrid.size()
89  << ", m_cdfGrid[intervalId] = " << m_cdfGrid[intervalId]
90  << ", m_cdfGrid[intervalId+1] = " << m_cdfGrid[intervalId+1]
91  << ", intervalLen = " << intervalLen
92  << ", ratio = " << ratio
93  << std::endl;
94 #endif
95  UQ_FATAL_TEST_MACRO(ratio < 0.,
96  m_env.worldRank(),
97  "SampledScalarCdf<T>::value()",
98  "invalid ratio");
99 
100  result = (1.-ratio)*m_cdfValues[intervalId] + ratio*m_cdfValues[intervalId+1];
101  }
102 
103  return result;
104 }
int worldRank() const
Returns the process world rank.
Definition: Environment.C:235
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
Definition: Environment.C:305
const BaseOneDGrid< T > & m_cdfGrid
const std::vector< double > & m_cdfValues
const BaseEnvironment & m_env
Definition: ScalarCdf.h:106
#define UQ_FATAL_TEST_MACRO(test, givenRank, where, what)
Definition: Defines.h:223

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 Thu Apr 23 2015 19:26:18 for queso-0.51.1 by  doxygen 1.8.5