queso-0.57.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
QUESO::BaseScalarFunction< V, M > Class Template Referenceabstract

A templated (base) class for handling scalar functions. More...

#include <ScalarFunction.h>

Inheritance diagram for QUESO::BaseScalarFunction< V, M >:
QUESO::BaseJointPdf< P_V, P_M > QUESO::BaseJointPdf< Q_V, Q_M > QUESO::BaseJointPdf< QUESO::GslVector, QUESO::GslMatrix > QUESO::BaseJointPdf< V, M > QUESO::ConstantScalarFunction< V, M > QUESO::GenericScalarFunction< V, M > QUESO::GPMSAEmulator< V, M > QUESO::LikelihoodBase< V, M >

Public Member Functions

void setFiniteDifferenceStepSize (double fdStepSize)
 Sets the step size for finite differencing gradients. More...
 
void setFiniteDifferenceStepSize (unsigned int i, double fdStepSize)
 
Constructor/Destructor methods.
 BaseScalarFunction (const char *prefix, const VectorSet< V, M > &domainSet)
 Default constructor. More...
 
virtual ~BaseScalarFunction ()
 Destructor. More...
 
Mathematical methods.
const VectorSet< V, M > & domainSet () const
 Access to the protected attribute m_domainSet: domain set of the scalar function. More...
 
Evaluation methods
virtual double lnValue (const V &domainVector, const V *domainDirection, V *gradVector, M *hessianMatrix, V *hessianEffect) const
 Logarithm of the value of the scalar function. Deprecated. More...
 
virtual double lnValue (const V &domainVector) const
 Returns the logarithm of the function at domainVector. More...
 
virtual double lnValue (const V &domainVector, V &gradVector) const
 Returns the logarithm of the function and its gradient at domainVector. More...
 
virtual double lnValue (const V &domainVector, V &gradVector, const V &domainDirection, V &hessianEffect) const
 
virtual double actualValue (const V &domainVector, const V *domainDirection, V *gradVector, M *hessianMatrix, V *hessianEffect) const =0
 Actual value of the scalar function. More...
 

Protected Attributes

const BaseEnvironmentm_env
 
std::string m_prefix
 
const VectorSet< V, M > & m_domainSet
 Domain set of the scalar function. More...
 

Private Attributes

ScopedPtr
< BoostInputOptionsParser >
::Type 
m_parser
 Input parser. More...
 
std::vector< double > m_fdStepSize
 Finite different step size. More...
 

Detailed Description

template<class V = GslVector, class M = GslMatrix>
class QUESO::BaseScalarFunction< V, M >

A templated (base) class for handling scalar functions.

This class allows the mathematical definition of a scalar function such as: \( f: B \subset R \rightarrow R \). A function of one or more variables has always one-dimensional range. PDFs (marginal, joint) and CDFs are examples of scalar functions.

Definition at line 52 of file ScalarFunction.h.

Constructor & Destructor Documentation

template<class V, class M>
QUESO::BaseScalarFunction< V, M >::BaseScalarFunction ( const char *  prefix,
const VectorSet< V, M > &  domainSet 
)

Default constructor.

Instantiates an object of the class, i.e. a scalar function, given a prefix and its domain.

Definition at line 46 of file ScalarFunction.C.

References dim, QUESO::BaseEnvironment::input(), QUESO::BaseScalarFunction< V, M >::m_domainSet, QUESO::BaseScalarFunction< V, M >::m_env, QUESO::BaseScalarFunction< V, M >::m_fdStepSize, QUESO::BaseScalarFunction< V, M >::m_parser, QUESO::BaseScalarFunction< V, M >::m_prefix, and QUESO::size.

48  : m_env(domainSet.env()),
49  m_prefix((std::string)(prefix) + "func_"),
51 #ifndef QUESO_DISABLE_BOOST_PROGRAM_OPTIONS
52  m_parser(new BoostInputOptionsParser(m_env.optionsInputFileName())),
53 #endif
54  m_fdStepSize(1, std::atof(QUESO_BASESCALARFN_FD_STEPSIZE_ODV))
55 {
56  unsigned int dim = m_domainSet.vectorSpace().dimLocal();
57 
58  // Snarf fd step size from input file.
59 #ifndef QUESO_DISABLE_BOOST_PROGRAM_OPTIONS
60  m_parser->registerOption<std::string>(m_prefix + "fdStepSize",
61  QUESO_BASESCALARFN_FD_STEPSIZE_ODV,
62  "step size for finite difference");
63  m_parser->scanInputFile();
64  m_parser->getOption<std::vector<double> >(m_prefix + "fdStepSize",
65  m_fdStepSize);
66 
67  // Check size of finite difference vector the user provided.
68  queso_require_msg((m_fdStepSize.size() == 1) || (m_fdStepSize.size() == dim),
69  "Finite difference vector is not the correct size");
70 
71  // If the user provided a scalar for a multi-dimensional function...
72  if (dim > 1 && m_fdStepSize.size() == 1) {
73  // ...get the only element
74  double stepSize = m_fdStepSize[0];
75 
76  // and use it to fill a vector of length dim
77  m_fdStepSize.resize(dim, stepSize);
78  }
79 #else
80  unsigned int size = m_env.input().vector_variable_size(m_prefix + "fdStepSize");
81 
82  if (size == 0) {
83  m_fdStepSize.resize(dim, std::atof(QUESO_BASESCALARFN_FD_STEPSIZE_ODV));
84  }
85  else if (size == 1) {
86  double value = m_env.input()(m_prefix + "fdStepSize",
87  std::atof(QUESO_BASESCALARFN_FD_STEPSIZE_ODV),
88  0);
89 
90  m_fdStepSize.resize(dim, value);
91  }
92  else if (size == dim) {
93  for (unsigned int i = 0; i < size; i++) {
94  m_fdStepSize[i] = m_env.input()(m_prefix + "fdStepSize",
95  std::atof(QUESO_BASESCALARFN_FD_STEPSIZE_ODV),
96  i);
97  }
98  }
99  else {
100  // Either the user provides nothing, a scalar, or the whole vector.
101  // Any other possiblities are not allowed so we error in this case.
102  queso_error_msg("Finite difference vector must be a scalar or a vector of length parameter dimension");
103  }
104 #endif
105 
106  // Check all the elements of the finite difference vector are positive
107  for (unsigned int i = 0; i < dim; i++) {
108  queso_require_greater_msg(m_fdStepSize[i],
109  0.0,
110  "Finite difference step sizes must be positive");
111  }
112 }
const GetPot & input() const
The GetPot input file parser.
Definition: Environment.C:1149
std::string optionsInputFileName() const
Access to the attribute m_optionsInputFileName, which stores the name of the input file passed by the...
Definition: Environment.C:354
ScopedPtr< BoostInputOptionsParser >::Type m_parser
Input parser.
const VectorSet< V, M > & domainSet() const
Access to the protected attribute m_domainSet: domain set of the scalar function. ...
const BaseEnvironment & m_env
std::vector< double > m_fdStepSize
Finite different step size.
int dim
Definition: ann_test.cpp:472
const VectorSet< V, M > & m_domainSet
Domain set of the scalar function.
template<class V , class M >
QUESO::BaseScalarFunction< V, M >::~BaseScalarFunction ( )
virtual

Destructor.

Definition at line 116 of file ScalarFunction.C.

117 {
118 }

Member Function Documentation

template<class V , class M >
const VectorSet< V, M > & QUESO::BaseScalarFunction< V, M >::domainSet ( ) const

Access to the protected attribute m_domainSet: domain set of the scalar function.

Definition at line 122 of file ScalarFunction.C.

Referenced by QUESO::StatisticalInverseProblem< P_V, P_M >::StatisticalInverseProblem().

123 {
124  return m_domainSet;
125 }
const VectorSet< V, M > & m_domainSet
Domain set of the scalar function.
template<class V, class M>
double QUESO::BaseScalarFunction< V, M >::lnValue ( const V &  domainVector,
const V *  domainDirection,
V *  gradVector,
M *  hessianMatrix,
V *  hessianEffect 
) const
virtual

Logarithm of the value of the scalar function. Deprecated.

Pointers will be NULL if derivative information is not required by QUESO.

Default implementation throws an exception.

Reimplemented in QUESO::InvLogitGaussianJointPdf< V, M >, QUESO::GammaJointPdf< V, M >, QUESO::GaussianJointPdf< V, M >, QUESO::ConcatenatedJointPdf< V, M >, QUESO::LogNormalJointPdf< V, M >, QUESO::PoweredJointPdf< V, M >, QUESO::JeffreysJointPdf< V, M >, QUESO::GPMSAEmulator< V, M >, QUESO::BetaJointPdf< V, M >, QUESO::InverseGammaJointPdf< V, M >, QUESO::WignerJointPdf< V, M >, QUESO::GenericScalarFunction< V, M >, QUESO::GenericJointPdf< V, M >, QUESO::UniformJointPdf< V, M >, and QUESO::ConstantScalarFunction< V, M >.

Definition at line 129 of file ScalarFunction.C.

Referenced by QUESO::LikelihoodBase< V, M >::actualValue().

134 {
135  std::string msg;
136 
137  msg += "Implementation of all lnValue methods is missing. Please implement";
138  msg += " at least lnValue(const V &).";
139 
140  queso_error_msg(msg);
141 }
template<class V, class M>
double QUESO::BaseScalarFunction< V, M >::lnValue ( const V &  domainVector) const
virtual

Returns the logarithm of the function at domainVector.

Default implementation calls the deprecated method.

QUESO calls this method when it needs to evaluate the function but doesn't need derivative information about that function.

Reimplemented in QUESO::GaussianLikelihoodBlockDiagonalCovarianceRandomCoefficients< V, M >, QUESO::BayesianJointPdf< V, M >, QUESO::GaussianLikelihoodBlockDiagonalCovariance< V, M >, QUESO::GaussianLikelihoodFullCovarianceRandomCoefficient< V, M >, QUESO::GaussianLikelihoodFullCovariance< V, M >, QUESO::GaussianLikelihoodDiagonalCovariance< V, M >, and QUESO::GaussianLikelihoodScalarCovariance< V, M >.

Definition at line 145 of file ScalarFunction.C.

146 {
147  return this->lnValue(domainVector, NULL, NULL, NULL, NULL);
148 }
virtual double lnValue(const V &domainVector, const V *domainDirection, V *gradVector, M *hessianMatrix, V *hessianEffect) const
Logarithm of the value of the scalar function. Deprecated.
template<class V, class M>
double QUESO::BaseScalarFunction< V, M >::lnValue ( const V &  domainVector,
V &  gradVector 
) const
virtual

Returns the logarithm of the function and its gradient at domainVector.

Default implementation calls above method successively to fill up gradVector with a finite difference approximation.

Note, gradVector should be filled with the gradient of the logarithm of the function, not the gradient of the function.

QUESO calls this method when it needs to evaluate the function, needs first order derivative information, but doesn't need second order derivative information.

Reimplemented in QUESO::BayesianJointPdf< V, M >.

Definition at line 152 of file ScalarFunction.C.

153 {
154  double value = this->lnValue(domainVector);
155 
156  // Create perturbed version of domainVector to use in finite difference
157  V perturbedVector(domainVector);
158 
159  // Fill up gradVector with a finite difference approximation
160  for (unsigned int i = 0; i < domainVector.sizeLocal(); ++i) {
161  // Store the old value of the perturbed element so we can undo it later
162  double tmp = perturbedVector[i];
163 
164  perturbedVector[i] += m_fdStepSize[i];
165  gradVector[i] = (this->lnValue(perturbedVector) - value) / m_fdStepSize[i];
166 
167  // Restore the old value of the perturbedVector element
168  perturbedVector[i] = tmp;
169  }
170 
171  return value;
172 }
virtual double lnValue(const V &domainVector, const V *domainDirection, V *gradVector, M *hessianMatrix, V *hessianEffect) const
Logarithm of the value of the scalar function. Deprecated.
std::vector< double > m_fdStepSize
Finite different step size.
template<class V, class M>
double QUESO::BaseScalarFunction< V, M >::lnValue ( const V &  domainVector,
V &  gradVector,
const V &  domainDirection,
V &  hessianEffect 
) const
virtual

Returns the logarithm of the function domainVector, the gradient of the logarithm at domainVector, and the effect of the hessian of the logarithm in the direction at domainDirection

Default implementation throws an exception because we don't expect the user to tolerate a finite difference approximation of the hessian.

Note, gradVector should be filled with the gradient of the logarithm of the function, not the gradient of the function.

The 'hessian' referred to here should be the hessian of the logarithm of the function at domainVector.

QUESO calls this method when it needs to evaluate the function, needs first order derivative information, and also needs second-order deriative information

Definition at line 176 of file ScalarFunction.C.

180 {
181  std::string msg;
182 
183  msg += "QUESO asked for Hessian information from an lnValue method, but the";
184  msg += " implementation of is missing. Please implement";
185  msg += " lnValue(const V &, V &, const V &, V &).";
186 
187  queso_error_msg(msg);
188 }
template<class V , class M >
void QUESO::BaseScalarFunction< V, M >::setFiniteDifferenceStepSize ( double  fdStepSize)

Sets the step size for finite differencing gradients.

If the function is multi-dimensional then the same finite difference step size is used in every direction.

Definition at line 192 of file ScalarFunction.C.

193 {
194  queso_require_greater_msg(fdStepSize, 0.0,
195  "Must provide a finite difference step > 0");
196 
197  for (unsigned int i = 0; i < this->m_fdStepSize.size(); i++) {
198  this->m_fdStepSize[i] = fdStepSize;
199  }
200 }
std::vector< double > m_fdStepSize
Finite different step size.
template<class V , class M >
void QUESO::BaseScalarFunction< V, M >::setFiniteDifferenceStepSize ( unsigned int  i,
double  fdStepSize 
)

Sets the step size for the i-th component of the finite differencing vector

i is a zero-based index.

If the function is one-dimensional, the only allowed value for i is 0.

Definition at line 204 of file ScalarFunction.C.

References QUESO::size.

206 {
207  queso_require_greater_msg(fdStepSize, 0.0,
208  "Must provide a finite difference step > 0");
209 
210  queso_require_greater_equal_msg(i, 0, "Must provide a nonnegative index");
211 
212  unsigned int size = this->m_fdStepSize.size();
213  queso_require_less_msg(i, size, "Must provide an index less than size of parameter dimension");
214 
215  this->m_fdStepSize[i] = fdStepSize;
216 }
std::vector< double > m_fdStepSize
Finite different step size.

Member Data Documentation

template<class V = GslVector, class M = GslMatrix>
const VectorSet<V, M>& QUESO::BaseScalarFunction< V, M >::m_domainSet
protected

Domain set of the scalar function.

Definition at line 158 of file ScalarFunction.h.

Referenced by QUESO::BaseScalarFunction< V, M >::BaseScalarFunction().

template<class V = GslVector, class M = GslMatrix>
std::vector<double> QUESO::BaseScalarFunction< V, M >::m_fdStepSize
private

Finite different step size.

Definition at line 167 of file ScalarFunction.h.

Referenced by QUESO::BaseScalarFunction< V, M >::BaseScalarFunction().

template<class V = GslVector, class M = GslMatrix>
ScopedPtr<BoostInputOptionsParser>::Type QUESO::BaseScalarFunction< V, M >::m_parser
private

Input parser.

Definition at line 163 of file ScalarFunction.h.

Referenced by QUESO::BaseScalarFunction< V, M >::BaseScalarFunction().


The documentation for this class was generated from the following files:

Generated on Tue Jun 5 2018 19:49:00 for queso-0.57.1 by  doxygen 1.8.5