queso-0.53.0
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-2015 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  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 }
99 //---------------------------------------------------
100 template<class T>
101 T
102 SampledScalarCdf<T>::inverse(double cdfValue) const
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 }
150 //---------------------------------------------------
151 template<class T>
152 void
153 SampledScalarCdf<T>::getSupport(T& minHorizontal, T& maxHorizontal) const
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;
164  m_minHorizontal = m_cdfGrid[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 }
191 // I/O methods---------------------------------------
192 template <class T>
193 void
194 SampledScalarCdf<T>::print(std::ostream& os) const
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 }
213 //---------------------------------------------------
214 template<class T>
215 void
217  const std::string& varNamePrefix,
218  const std::string& fileName,
219  const std::string& fileType,
220  const std::set<unsigned int>& allowedSubEnvIds) const
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 }
269 
270 } // End namespace QUESO
unsigned int displayVerbosity() const
Definition: Environment.C:396
A templated (base) class for handling CDFs.
Definition: ScalarCdf.h:49
const BaseEnvironment & m_env
Definition: ScalarCdf.h:99
std::ofstream * ofsVar
Provides a stream interface to write data to files.
Definition: Environment.h:81
#define queso_require_not_equal_to_msg(expr1, expr2, msg)
Definition: asserts.h:86
Struct for handling data input and output from files.
Definition: Environment.h:72
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.
#define queso_error_msg(msg)
Definition: asserts.h:47
std::string m_prefix
Definition: ScalarCdf.h:100
double value(T paramValue) const
Returns the value of the CDF at paramValue.
#define queso_require_equal_to_msg(expr1, expr2, msg)
Definition: asserts.h:85
#define queso_require_msg(asserted, msg)
Definition: asserts.h:69
SampledScalarCdf(const BaseEnvironment &env, const char *prefix, const BaseOneDGrid< T > &cdfGrid, const std::vector< double > &cdfValues)
Default constructor.
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
Definition: Environment.C:274
#define queso_require_less_msg(expr1, expr2, msg)
Definition: asserts.h:87
This (virtual) class sets up the environment underlying the use of the QUESO library by an executable...
Definition: Environment.h:193
void getSupport(T &minHorizontal, T &maxHorizontal) const
Returns the support (image) of the CDF between two horizontal values (domain).
Base class for accommodating one-dimensional grids.
Definition: OneDGrid.h:46
#define queso_require_greater_equal_msg(expr1, expr2, msg)
Definition: asserts.h:90
T inverse(double cdfValue) const
Returns the position of a given value of CDF.
~SampledScalarCdf()
Destructor.
void print(std::ostream &os) const
Prints the CDF (values of the grid points and of the CDF at such grid points).
int k
Definition: ann_sample.cpp:53

Generated on Thu Jun 11 2015 13:52:32 for queso-0.53.0 by  doxygen 1.8.5