25 #include <queso/SampledScalarCdf.h>
35 const std::vector<double>& cdfValues)
39 m_cdfValues (cdfValues)
69 if (paramValue <= m_cdfGrid[0]) {
72 else if (m_cdfGrid[m_cdfGrid.size()-1] <= paramValue) {
76 unsigned int intervalId = m_cdfGrid.findIntervalId(paramValue);
79 "SampledScalarCdf<T>::value()",
80 "invalid intervalId");
82 double intervalLen = m_cdfGrid[intervalId+1] - m_cdfGrid[intervalId];
83 double ratio = (paramValue - m_cdfGrid[intervalId])/intervalLen;
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
97 "SampledScalarCdf<T>::value()",
100 result = (1.-ratio)*m_cdfValues[intervalId] + ratio*m_cdfValues[intervalId+1];
114 "SampledScalarCdf<T>::inverse()",
118 unsigned int j = m_cdfValues.size()-1;
119 bool searchPosition =
true;
121 if (cdfValue == m_cdfValues[i]) {
122 while ((0 < i) && (cdfValue == m_cdfValues[i-1])) --i;
123 result = m_cdfGrid[i];
124 searchPosition =
false;
127 if (cdfValue == m_cdfValues[j]) {
128 while ((0 < j) && (cdfValue == m_cdfValues[j-1])) --j;
129 result = m_cdfGrid[j];
130 searchPosition =
false;
136 "SampledScalarCdf<T>::inverse()",
137 "invalid pair of values 'i' and 'j'");
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;
145 unsigned int k= (
unsigned int) ((i+j)*.5);
146 if (cdfValue < m_cdfValues[k]) {
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;
158 }
while (searchPosition);
167 if ((m_minHorizontal == -INFINITY) ||
168 (m_maxHorizontal == INFINITY)) {
171 "SampledScalarCdf<T>::getSupport()",
172 "unexpected values of m_minHorizontal and/or m_maxHorizontal");
174 unsigned int iMax = m_cdfGrid.size();
176 for (
unsigned int i = 0; i < iMax; ++i) {
177 if (m_cdfValues[i] > 0.) {
179 m_minHorizontal = m_cdfGrid[i];
186 "SampledScalarCdf<T>::getSupport()",
187 "unexpected value for m_minHorizontal");
192 "SampledScalarCdf<T>::getSupport()",
193 "unexpected value for case 'iMax = 1'");
194 m_maxHorizontal = m_cdfGrid[iMax-1];
196 else for (
unsigned int i = 0; i < iMax; ++i) {
197 if (m_cdfValues[iMax-1-i] < 1.) {
199 m_maxHorizontal = m_cdfGrid[iMax-1-i];
206 "SampledScalarCdf<T>::getSupport()",
207 "unexpected value for m_maxHorizontal");
210 minHorizontal = m_minHorizontal;
211 maxHorizontal = m_maxHorizontal;
224 os << m_prefix <<
"values_sub" << m_env.subIdString() <<
" = zeros(" << m_cdfValues.size()
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] <<
" ";
241 const std::string& varNamePrefix,
242 const std::string& fileName,
243 const std::string& fileType,
244 const std::set<unsigned int>& allowedSubEnvIds)
const
248 "SampledScalarCdf<T>::subWriteContents()",
249 "unexpected subRank");
252 if (m_env.openOutputFile(fileName,
259 *filePtrSet.
ofsVar << varNamePrefix <<
"grid_sub" << m_env.subIdString() <<
" = zeros(" << m_cdfGrid.size()
263 *filePtrSet.ofsVar << varNamePrefix <<
"grid_sub" << m_env.subIdString() <<
" = [";
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] <<
" ";
270 filePtrSet.ofsVar->precision(savedPrecision);
272 *filePtrSet.ofsVar <<
"];\n";
275 *filePtrSet.ofsVar << varNamePrefix <<
"values_sub" << m_env.subIdString() <<
" = zeros(" << m_cdfValues.size()
279 *filePtrSet.ofsVar << varNamePrefix <<
"values_sub" << m_env.subIdString() <<
" = [";
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] <<
" ";
286 filePtrSet.ofsVar->precision(savedPrecision);
288 *filePtrSet.ofsVar <<
"];\n";
291 m_env.closeFile(filePtrSet,fileType);
A templated (base) class for handling CDFs.
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.
void getSupport(T &minHorizontal, T &maxHorizontal) const
Returns the support (image) of the CDF between two horizontal values (domain).
std::ofstream * ofsVar
Provides a stream interface to write data to files.
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
~SampledScalarCdf()
Destructor.
Base class for accommodating one-dimensional grids.
T inverse(double cdfValue) const
Returns the position of a given value of CDF.
This (virtual) class sets up the environment underlying the use of the QUESO library by an executable...
double value(T paramValue) const
Returns the value of the CDF at paramValue.
#define UQ_FATAL_TEST_MACRO(test, givenRank, where, what)
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.
Struct for handling data input and output from files.
unsigned int displayVerbosity() const
const BaseEnvironment & m_env