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);
77 queso_require_less_msg(intervalId, (m_cdfGrid.size()-1),
"invalid intervalId");
79 double intervalLen = m_cdfGrid[intervalId+1] - m_cdfGrid[intervalId];
80 double ratio = (paramValue - m_cdfGrid[intervalId])/intervalLen;
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
92 queso_require_greater_equal_msg(ratio, 0.,
"invalid ratio");
94 result = (1.-ratio)*m_cdfValues[intervalId] + ratio*m_cdfValues[intervalId+1];
106 queso_require_msg(!((cdfValue < 0.) || (1. < cdfValue)),
"invalid cdfValue");
109 unsigned int j = m_cdfValues.size()-1;
110 bool searchPosition =
true;
112 if (cdfValue == m_cdfValues[i]) {
113 while ((0 < i) && (cdfValue == m_cdfValues[i-1])) --i;
114 result = m_cdfGrid[i];
115 searchPosition =
false;
118 if (cdfValue == m_cdfValues[j]) {
119 while ((0 < j) && (cdfValue == m_cdfValues[j-1])) --j;
120 result = m_cdfGrid[j];
121 searchPosition =
false;
125 queso_error_msg(
"invalid pair of values 'i' and 'j'");
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;
133 unsigned int k= (
unsigned int) ((i+j)*.5);
134 if (cdfValue < m_cdfValues[k]) {
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;
146 }
while (searchPosition);
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");
159 unsigned int iMax = m_cdfGrid.size();
161 for (
unsigned int i = 0; i < iMax; ++i) {
162 if (m_cdfValues[i] > 0.) {
164 m_minHorizontal = m_cdfGrid[i];
173 m_maxHorizontal = m_cdfGrid[iMax-1];
175 else for (
unsigned int i = 0; i < iMax; ++i) {
176 if (m_cdfValues[iMax-1-i] < 1.) {
178 m_maxHorizontal = m_cdfGrid[iMax-1-i];
186 minHorizontal = m_minHorizontal;
187 maxHorizontal = m_maxHorizontal;
200 os << m_prefix <<
"values_sub" << m_env.subIdString() <<
" = zeros(" << m_cdfValues.size()
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] <<
" ";
217 const std::string& varNamePrefix,
218 const std::string& fileName,
219 const std::string& fileType,
220 const std::set<unsigned int>& allowedSubEnvIds)
const
222 queso_require_greater_equal_msg(m_env.subRank(), 0,
"unexpected subRank");
225 if (m_env.openOutputFile(fileName,
232 *filePtrSet.
ofsVar << varNamePrefix <<
"grid_sub" << m_env.subIdString() <<
" = zeros(" << m_cdfGrid.size()
236 *filePtrSet.ofsVar << varNamePrefix <<
"grid_sub" << m_env.subIdString() <<
" = [";
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] <<
" ";
243 filePtrSet.ofsVar->precision(savedPrecision);
245 *filePtrSet.ofsVar <<
"];\n";
248 *filePtrSet.ofsVar << varNamePrefix <<
"values_sub" << m_env.subIdString() <<
" = zeros(" << m_cdfValues.size()
252 *filePtrSet.ofsVar << varNamePrefix <<
"values_sub" << m_env.subIdString() <<
" = [";
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] <<
" ";
259 filePtrSet.ofsVar->precision(savedPrecision);
261 *filePtrSet.ofsVar <<
"];\n";
264 m_env.closeFile(filePtrSet,fileType);
This (virtual) class sets up the environment underlying the use of the QUESO library by an executable...
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"))
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.
const BaseEnvironment & m_env
void print(std::ostream &os) const
Prints the CDF (values of the grid points and of the CDF at such grid points).
Base class for accommodating one-dimensional grids.
A templated (base) class for handling CDFs.
double value(T paramValue) const
Returns the value of the CDF at paramValue.
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"))
void getSupport(T &minHorizontal, T &maxHorizontal) const
Returns the support (image) of the CDF between two horizontal values (domain).
T inverse(double cdfValue) const
Returns the position of a given value of CDF.
SampledScalarCdf(const BaseEnvironment &env, const char *prefix, const BaseOneDGrid< T > &cdfGrid, const std::vector< double > &cdfValues)
Default constructor.
unsigned int displayVerbosity() const
std::ofstream * ofsVar
Provides a stream interface to write data to files.
Struct for handling data input and output from files.
~SampledScalarCdf()
Destructor.
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).