queso-0.53.0
MetropolisHastingsSG.h
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 #ifndef UQ_MH_SG_H
26 #define UQ_MH_SG_H
27 
28 #include <queso/MetropolisHastingsSGOptions.h>
29 #include <queso/TKGroup.h>
30 #include <queso/VectorRV.h>
31 #include <queso/VectorSpace.h>
32 #include <queso/MarkovChainPositionData.h>
33 #include <queso/ScalarFunctionSynchronizer.h>
34 #include <queso/SequenceOfVectors.h>
35 #include <queso/ArrayOfSequences.h>
36 #include <sys/time.h>
37 #include <fstream>
38 #include <boost/math/special_functions.hpp> // for Boost isnan. Note parentheses are important in function call.
39 
40 namespace QUESO {
41 
42 class GslVector;
43 class GslMatrix;
44 
45 //--------------------------------------------------
46 // MHRawChainInfoStruct --------------------------
47 //--------------------------------------------------
59 {
61 
64 
67 
71 
73 
76 
80 
82 
83  void copy (const MHRawChainInfoStruct& src);
85 
87  void reset ();
88 
90  void mpiSum(const MpiComm& comm, MHRawChainInfoStruct& sumInfo) const;
92 
93  double runTime;
95  double targetRunTime;
98  double drRunTime;
99  double amRunTime;
100 
101  unsigned int numTargetCalls;
102  unsigned int numDRs;
103  unsigned int numOutOfTargetSupport;
105  unsigned int numRejections;
106 
107 };
108 
109 //--------------------------------------------------
110 // MetropolisHastingsSG----------------------
111 //--------------------------------------------------
112 
122 template <class P_V = GslVector, class P_M = GslMatrix>
124 {
125 public:
127 
128 
137  MetropolisHastingsSG(const char* prefix,
138  const MhOptionsValues* alternativeOptionsValues, // dakota
139  const BaseVectorRV<P_V,P_M>& sourceRv,
140  const P_V& initialPosition,
141  const P_M* inputProposalCovMatrix);
142 
144  MetropolisHastingsSG(const char* prefix,
145  const MhOptionsValues* alternativeOptionsValues, // dakota
146  const BaseVectorRV<P_V,P_M>& sourceRv,
147  const P_V& initialPosition,
148  double initialLogPrior,
149  double initialLogLikelihood,
150  const P_M* inputProposalCovMatrix);
151 
154  const BaseVectorRV<P_V,P_M>& sourceRv,
155  const P_V& initialPosition,
156  const P_M* inputProposalCovMatrix);
157 
160  const BaseVectorRV<P_V,P_M>& sourceRv,
161  const P_V& initialPosition,
162  double initialLogPrior,
163  double initialLogLikelihood,
164  const P_M* inputProposalCovMatrix);
165 
169 
171 
172 
192  void generateSequence (BaseVectorSequence<P_V,P_M>& workingChain,
193  ScalarSequence<double>* workingLogLikelihoodValues,
194  ScalarSequence<double>* workingLogTargetValues);
195 
197  void getRawChainInfo (MHRawChainInfoStruct& info) const;
198 
200 
202  const BaseTKGroup<P_V, P_M> & transitionKernel() const;
203 
205 
206 
208  void print (std::ostream& os) const;
209  friend std::ostream& operator<<(std::ostream& os,
211  {
212  obj.print(os);
213 
214  return os;
215  }
217 
218 private:
220 
223  void commonConstructor ();
224 
226 
232  void generateFullChain (const P_V& valuesOf1stPosition,
233  unsigned int chainSize,
234  BaseVectorSequence<P_V,P_M>& workingChain,
235  ScalarSequence<double>* workingLogLikelihoodValues,
236  ScalarSequence<double>* workingLogTargetValues);
237 
239  void adapt(unsigned int positionId,
240  BaseVectorSequence<P_V, P_M> & workingChain);
241 
243  void readFullChain (const std::string& inputFileName,
244  const std::string& inputFileType,
245  unsigned int chainSize,
246  BaseVectorSequence<P_V,P_M>& workingChain);
247 
249 
252  unsigned int idOfFirstPositionInSubChain,
253  double& lastChainSize,
254  P_V& lastMean,
255  P_M& lastAdaptedCovMatrix);
256 
258 
260  double alpha (const MarkovChainPositionData<P_V>& x,
262  unsigned int xStageId,
263  unsigned int yStageId,
264  double* alphaQuotientPtr = NULL);
265 
267 
268  double alpha (const std::vector<MarkovChainPositionData<P_V>*>& inputPositions,
269  const std::vector<unsigned int >& inputTKStageIds);
270 
272 
273  bool acceptAlpha (double alpha);
274 
276 
278  int writeInfo (const BaseVectorSequence<P_V,P_M>& workingChain,
279  std::ofstream& ofsvar) const;
280 
287  unsigned int m_numDisabledParameters; // gpmsa2
288  std::vector<bool> m_parameterEnabledStatus; // gpmsa2
290 
293  unsigned int m_stageIdForDebugging;
294  std::vector<unsigned int> m_idsOfUniquePositions;
295  std::vector<double> m_logTargets;
296  std::vector<double> m_alphaQuotients;
298  P_V * m_lastMean;
301 
303 
306 
310 
312  boxSubset);
313 
315 };
316 
317 } // End namespace QUESO
318 
319 #endif // UQ_MH_SG_H
const MhOptionsValues * m_optionsObj
const BaseJointPdf< P_V, P_M > & m_targetPdf
void transformInitialCovMatrixToGaussianSpace(const BoxSubset< P_V, P_M > &boxSubset)
std::vector< bool > m_parameterEnabledStatus
MHRawChainInfoStruct & operator=(const MHRawChainInfoStruct &rhs)
Assignment operator.
std::vector< double > m_alphaQuotients
MetropolisHastingsSG(const char *prefix, const MhOptionsValues *alternativeOptionsValues, const BaseVectorRV< P_V, P_M > &sourceRv, const P_V &initialPosition, const P_M *inputProposalCovMatrix)
Constructor.
A templated class that represents a Metropolis-Hastings generator of samples.
void generateSequence(BaseVectorSequence< P_V, P_M > &workingChain, ScalarSequence< double > *workingLogLikelihoodValues, ScalarSequence< double > *workingLogTargetValues)
Method to generate the chain.
const BaseTKGroup< P_V, P_M > & transitionKernel() const
Returns the underlying transition kernel for this sequence generator.
void generateFullChain(const P_V &valuesOf1stPosition, unsigned int chainSize, BaseVectorSequence< P_V, P_M > &workingChain, ScalarSequence< double > *workingLogLikelihoodValues, ScalarSequence< double > *workingLogTargetValues)
This method generates the chain.
MHRawChainInfoStruct & operator+=(const MHRawChainInfoStruct &rhs)
Addition assignment operator.
A struct that represents a Metropolis-Hastings sample.
const ScalarFunctionSynchronizer< P_V, P_M > * m_targetPdfSynchronizer
void updateAdaptedCovMatrix(const BaseVectorSequence< P_V, P_M > &subChain, unsigned int idOfFirstPositionInSubChain, double &lastChainSize, P_V &lastMean, P_M &lastAdaptedCovMatrix)
This method updates the adapted covariance matrix.
const VectorSpace< P_V, P_M > & m_vectorSpace
This class provides options for each level of the Multilevel sequence generator if no input file is a...
The QUESO MPI Communicator Class.
Definition: MpiComm.h:75
void commonConstructor()
Reads the options values from the options input file.
A templated class that represents a Markov Chain.
friend std::ostream & operator<<(std::ostream &os, const MetropolisHastingsSG< P_V, P_M > &obj)
void adapt(unsigned int positionId, BaseVectorSequence< P_V, P_M > &workingChain)
Adaptive Metropolis method that deals with adapting the proposal covariance matrix.
This (virtual) class sets up the environment underlying the use of the QUESO library by an executable...
Definition: Environment.h:193
void print(std::ostream &os) const
TODO: Prints the sequence.
bool acceptAlpha(double alpha)
Decides whether or not to accept alpha.
This class provides options for the Metropolis-Hastings generator of samples if no input file is avai...
void readFullChain(const std::string &inputFileName, const std::string &inputFileType, unsigned int chainSize, BaseVectorSequence< P_V, P_M > &workingChain)
This method reads the chain contents.
int writeInfo(const BaseVectorSequence< P_V, P_M > &workingChain, std::ofstream &ofsvar) const
Writes information about the Markov chain in a file.
std::vector< unsigned int > m_idsOfUniquePositions
double alpha(const MarkovChainPositionData< P_V > &x, const MarkovChainPositionData< P_V > &y, unsigned int xStageId, unsigned int yStageId, double *alphaQuotientPtr=NULL)
Calculates acceptance ration.
BaseTKGroup< P_V, P_M > * m_tk
MetropolisHastingsSGOptions * m_oldOptions
MHRawChainInfoStruct m_rawChainInfo
void getRawChainInfo(MHRawChainInfoStruct &info) const
Gets information from the raw chain.
void mpiSum(const MpiComm &comm, MHRawChainInfoStruct &sumInfo) const
Calculates the MPI sum of this.
void reset()
Resets Metropolis-Hastings chain info.
std::vector< double > m_logTargets
This class reads the options for the Metropolis-Hastings generator of samples from an input file...
const BaseEnvironment & m_env
void copy(const MHRawChainInfoStruct &src)
Copies Metropolis-Hastings chain info from src to this.

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