queso-0.51.1
SampledScalarCdf.C
Go to the documentation of this file.
1 //-----------------------------------------------------------------------bl-
2 //--------------------------------------------------------------------------
3 //
4 // QUESO - a library to support the Quantification of Uncertainty
5 // for Estimation, Simulation and Optimization
6 //
7 // Copyright (C) 2008,2009,2010,2011,2012,2013 The PECOS Development Team
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the Version 2.1 GNU Lesser General
11 // Public License as published by the Free Software Foundation.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc. 51 Franklin Street, Fifth Floor,
21 // Boston, MA 02110-1301 USA
22 //
23 //-----------------------------------------------------------------------el-
24 
25 #include <queso/SampledScalarCdf.h>
26 
27 namespace QUESO {
28 
29 // Default constructor -----------------------------
30 template<class T>
32  const BaseEnvironment& env,
33  const char* prefix,
34  const BaseOneDGrid<T>& cdfGrid,
35  const std::vector<double>& cdfValues)
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 }
58 // Destructor ---------------------------------------
59 template<class T>
61 {
62 }
63 // Math methods--------------------------------------
64 template<class T>
65 double
66 SampledScalarCdf<T>::value(T paramValue) const
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 }
105 //---------------------------------------------------
106 template<class T>
107 T
108 SampledScalarCdf<T>::inverse(double cdfValue) const
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 }
162 //---------------------------------------------------
163 template<class T>
164 void
165 SampledScalarCdf<T>::getSupport(T& minHorizontal, T& maxHorizontal) const
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;
179  m_minHorizontal = m_cdfGrid[i];
180  break;
181  }
182  }
183 
184  UQ_FATAL_TEST_MACRO(m_minHorizontal == -INFINITY,
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 
204  UQ_FATAL_TEST_MACRO(m_maxHorizontal == INFINITY,
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 }
215 // I/O methods---------------------------------------
216 template <class T>
217 void
218 SampledScalarCdf<T>::print(std::ostream& os) const
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 }
237 //---------------------------------------------------
238 template<class T>
239 void
241  const std::string& varNamePrefix,
242  const std::string& fileName,
243  const std::string& fileType,
244  const std::set<unsigned int>& allowedSubEnvIds) const
245 {
246  UQ_FATAL_TEST_MACRO(m_env.subRank() < 0,
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 }
296 
297 } // End namespace QUESO
std::ofstream * ofsVar
Provides a stream interface to write data to files.
Definition: Environment.h:75
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
Definition: Environment.C:305
A templated (base) class for handling CDFs.
Definition: ScalarCdf.h:56
Base class for accommodating one-dimensional grids.
Definition: OneDGrid.h:46
~SampledScalarCdf()
Destructor.
This (virtual) class sets up the environment underlying the use of the QUESO library by an executable...
Definition: Environment.h:187
T inverse(double cdfValue) const
Returns the position of a given value of CDF.
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.
Struct for handling data input and output from files.
Definition: Environment.h:66
void print(std::ostream &os) const
Prints the CDF (values of the grid points and of the CDF at such grid points).
SampledScalarCdf(const BaseEnvironment &env, const char *prefix, const BaseOneDGrid< T > &cdfGrid, const std::vector< double > &cdfValues)
Default constructor.
const BaseEnvironment & m_env
Definition: ScalarCdf.h:106
double value(T paramValue) const
Returns the value of the CDF at paramValue.
unsigned int displayVerbosity() const
Definition: Environment.C:436
#define UQ_FATAL_TEST_MACRO(test, givenRank, where, what)
Definition: Defines.h:223
void getSupport(T &minHorizontal, T &maxHorizontal) const
Returns the support (image) of the CDF between two horizontal values (domain).
std::string m_prefix
Definition: ScalarCdf.h:107

Generated on Thu Apr 23 2015 19:26:16 for queso-0.51.1 by  doxygen 1.8.5