queso-0.51.1
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,2009,2010,2011,2012,2013 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 //--------------------------------------------------
43 // MHRawChainInfoStruct --------------------------
44 //--------------------------------------------------
56 {
58 
61 
64 
68 
70 
73 
77 
79 
80  void copy (const MHRawChainInfoStruct& src);
82 
84  void reset ();
85 
87  void mpiSum(const MpiComm& comm, MHRawChainInfoStruct& sumInfo) const;
89 
90  double runTime;
92  double targetRunTime;
95  double drRunTime;
96  double amRunTime;
97 
98  unsigned int numTargetCalls;
99  unsigned int numDRs;
100  unsigned int numOutOfTargetSupport;
102  unsigned int numRejections;
103 
104 };
105 
106 //--------------------------------------------------
107 // MetropolisHastingsSG----------------------
108 //--------------------------------------------------
109 
119 template <class P_V,class P_M>
121 {
122 public:
124 
125 
134  MetropolisHastingsSG(const char* prefix,
135  const MhOptionsValues* alternativeOptionsValues, // dakota
136  const BaseVectorRV<P_V,P_M>& sourceRv,
137  const P_V& initialPosition,
138  const P_M* inputProposalCovMatrix);
139 
141  MetropolisHastingsSG(const char* prefix,
142  const MhOptionsValues* alternativeOptionsValues, // dakota
143  const BaseVectorRV<P_V,P_M>& sourceRv,
144  const P_V& initialPosition,
145  double initialLogPrior,
146  double initialLogLikelihood,
147  const P_M* inputProposalCovMatrix);
148 
151  const BaseVectorRV<P_V,P_M>& sourceRv,
152  const P_V& initialPosition,
153  const P_M* inputProposalCovMatrix);
154 
157  const BaseVectorRV<P_V,P_M>& sourceRv,
158  const P_V& initialPosition,
159  double initialLogPrior,
160  double initialLogLikelihood,
161  const P_M* inputProposalCovMatrix);
162 
166 
168 
169 
189  void generateSequence (BaseVectorSequence<P_V,P_M>& workingChain,
190  ScalarSequence<double>* workingLogLikelihoodValues,
191  ScalarSequence<double>* workingLogTargetValues);
192 
194  void getRawChainInfo (MHRawChainInfoStruct& info) const;
195 
197 
199  const BaseTKGroup<P_V, P_M> & transitionKernel() const;
200 
202 
203 
205  void print (std::ostream& os) const;
206  friend std::ostream& operator<<(std::ostream& os,
208  {
209  obj.print(os);
210 
211  return os;
212  }
214 
215 private:
217 
220  void commonConstructor ();
221 
223 
229  void generateFullChain (const P_V& valuesOf1stPosition,
230  unsigned int chainSize,
231  BaseVectorSequence<P_V,P_M>& workingChain,
232  ScalarSequence<double>* workingLogLikelihoodValues,
233  ScalarSequence<double>* workingLogTargetValues);
234 
236  void readFullChain (const std::string& inputFileName,
237  const std::string& inputFileType,
238  unsigned int chainSize,
239  BaseVectorSequence<P_V,P_M>& workingChain);
240 
242 
245  unsigned int idOfFirstPositionInSubChain,
246  double& lastChainSize,
247  P_V& lastMean,
248  P_M& lastAdaptedCovMatrix);
249 
251 
253  double alpha (const MarkovChainPositionData<P_V>& x,
255  unsigned int xStageId,
256  unsigned int yStageId,
257  double* alphaQuotientPtr = NULL);
258 
260 
261  double alpha (const std::vector<MarkovChainPositionData<P_V>*>& inputPositions,
262  const std::vector<unsigned int >& inputTKStageIds);
263 
265 
266  bool acceptAlpha (double alpha);
267 
269 
271  int writeInfo (const BaseVectorSequence<P_V,P_M>& workingChain,
272  std::ofstream& ofsvar) const;
273 
280  unsigned int m_numDisabledParameters; // gpmsa2
281  std::vector<bool> m_parameterEnabledStatus; // gpmsa2
283 
286  unsigned int m_stageIdForDebugging;
287  std::vector<unsigned int> m_idsOfUniquePositions;
288  std::vector<double> m_logTargets;
289  std::vector<double> m_alphaQuotients;
291  P_V * m_lastMean;
294 
296 
302 
304  boxSubset);
305 };
306 
307 } // End namespace QUESO
308 
309 #endif // UQ_MH_SG_H
The QUESO MPI Communicator Class.
Definition: MpiComm.h:75
const BaseEnvironment & m_env
std::vector< bool > m_parameterEnabledStatus
const BaseJointPdf< P_V, P_M > & m_targetPdf
This class reads the options for the Metropolis-Hastings generator of samples from an input file...
void reset()
Resets Metropolis-Hastings chain info.
MHRawChainInfoStruct & operator=(const MHRawChainInfoStruct &rhs)
Assignment operator.
MetropolisHastingsSG(const char *prefix, const MhOptionsValues *alternativeOptionsValues, const BaseVectorRV< P_V, P_M > &sourceRv, const P_V &initialPosition, const P_M *inputProposalCovMatrix)
Constructor.
const ScalarFunctionSynchronizer< P_V, P_M > * m_targetPdfSynchronizer
void transformInitialCovMatrixToGaussianSpace(const BoxSubset< P_V, P_M > &boxSubset)
MhOptionsValues m_alternativeOptionsValues
const VectorSpace< P_V, P_M > & m_vectorSpace
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.
This (virtual) class sets up the environment underlying the use of the QUESO library by an executable...
Definition: Environment.h:187
MetropolisHastingsSGOptions * m_optionsObj
void commonConstructor()
Reads the options values from the options input file.
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.
friend std::ostream & operator<<(std::ostream &os, const MetropolisHastingsSG< P_V, P_M > &obj)
void mpiSum(const MpiComm &comm, MHRawChainInfoStruct &sumInfo) const
Calculates the MPI sum of this.
A templated class that represents a Metropolis-Hastings generator of samples.
int writeInfo(const BaseVectorSequence< P_V, P_M > &workingChain, std::ofstream &ofsvar) const
Writes information about the Markov chain in a file.
double alpha(const MarkovChainPositionData< P_V > &x, const MarkovChainPositionData< P_V > &y, unsigned int xStageId, unsigned int yStageId, double *alphaQuotientPtr=NULL)
Calculates acceptance ration.
A struct that represents a Metropolis-Hastings sample.
void print(std::ostream &os) const
TODO: Prints the sequence.
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.
MHRawChainInfoStruct m_rawChainInfo
MHRawChainInfoStruct & operator+=(const MHRawChainInfoStruct &rhs)
Addition assignment operator.
std::vector< unsigned int > m_idsOfUniquePositions
void getRawChainInfo(MHRawChainInfoStruct &info) const
Gets information from the raw chain.
A templated class that represents a Markov Chain.
bool acceptAlpha(double alpha)
Decides whether or not to accept alpha.
BaseTKGroup< P_V, P_M > * m_tk
This class provides options for the Metropolis-Hastings generator of samples if no input file is avai...
std::vector< double > m_logTargets
void copy(const MHRawChainInfoStruct &src)
Copies Metropolis-Hastings chain info from src to this.
std::vector< double > m_alphaQuotients
This class provides options for each level of the Multilevel sequence generator if no input file is a...

Generated on Thu Apr 23 2015 19:26:15 for queso-0.51.1 by  doxygen 1.8.5