queso-0.57.0
Public Member Functions | Protected Attributes | Private Attributes | List of all members
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 >:
Inheritance graph
[legend]
Collaboration diagram for QUESO::BaseScalarFunction< V, M >:
Collaboration graph
[legend]

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 45 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.

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

Destructor.

Definition at line 115 of file ScalarFunction.C.

116 {
117 }

Member Function Documentation

template<class V = GslVector, class M = GslMatrix>
virtual double QUESO::BaseScalarFunction< V, M >::actualValue ( const V &  domainVector,
const V *  domainDirection,
V *  gradVector,
M *  hessianMatrix,
V *  hessianEffect 
) const
pure virtual
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 121 of file ScalarFunction.C.

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

122 {
123  return m_domainSet;
124 }
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 128 of file ScalarFunction.C.

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

133 {
134  std::string msg;
135 
136  msg += "Implementation of all lnValue methods is missing. Please implement";
137  msg += " at least lnValue(const V &).";
138 
139  queso_error_msg(msg);
140 }
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 144 of file ScalarFunction.C.

145 {
146  return this->lnValue(domainVector, NULL, NULL, NULL, NULL);
147 }
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 151 of file ScalarFunction.C.

152 {
153  double value = this->lnValue(domainVector);
154 
155  // Create perturbed version of domainVector to use in finite difference
156  V perturbedVector(domainVector);
157 
158  // Fill up gradVector with a finite difference approximation
159  for (unsigned int i = 0; i < domainVector.sizeLocal(); ++i) {
160  // Store the old value of the perturbed element so we can undo it later
161  double tmp = perturbedVector[i];
162 
163  perturbedVector[i] += m_fdStepSize[i];
164  gradVector[i] = (this->lnValue(perturbedVector) - value) / m_fdStepSize[i];
165 
166  // Restore the old value of the perturbedVector element
167  perturbedVector[i] = tmp;
168  }
169 
170  return value;
171 }
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 175 of file ScalarFunction.C.

179 {
180  std::string msg;
181 
182  msg += "QUESO asked for Hessian information from an lnValue method, but the";
183  msg += " implementation of is missing. Please implement";
184  msg += " lnValue(const V &, V &, const V &, V &).";
185 
186  queso_error_msg(msg);
187 }
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 191 of file ScalarFunction.C.

192 {
193  queso_require_greater_msg(fdStepSize, 0.0,
194  "Must provide a finite difference step > 0");
195 
196  for (unsigned int i = 0; i < this->m_fdStepSize.size(); i++) {
197  this->m_fdStepSize[i] = fdStepSize;
198  }
199 }
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 203 of file ScalarFunction.C.

References QUESO::size.

205 {
206  queso_require_greater_msg(fdStepSize, 0.0,
207  "Must provide a finite difference step > 0");
208 
209  queso_require_greater_equal_msg(i, 0, "Must provide a nonnegative index");
210 
211  unsigned int size = this->m_fdStepSize.size();
212  queso_require_less_msg(i, size, "Must provide an index less than size of parameter dimension");
213 
214  this->m_fdStepSize[i] = fdStepSize;
215 }
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>
const BaseEnvironment& QUESO::BaseScalarFunction< V, M >::m_env
protected
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().

template<class V = GslVector, class M = GslMatrix>
std::string QUESO::BaseScalarFunction< V, M >::m_prefix
protected

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

Generated on Sat Apr 22 2017 14:04:38 for queso-0.57.0 by  doxygen 1.8.5