queso-0.50.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 
142  const BaseVectorRV<P_V,P_M>& sourceRv,
143  const P_V& initialPosition,
144  const P_M* inputProposalCovMatrix);
145 
149 
151 
152 
172  void generateSequence (BaseVectorSequence<P_V,P_M>& workingChain,
173  ScalarSequence<double>* workingLogLikelihoodValues,
174  ScalarSequence<double>* workingLogTargetValues);
175 
177  void getRawChainInfo (MHRawChainInfoStruct& info) const;
178 
180 
182 
183 
185  void print (std::ostream& os) const;
186  friend std::ostream& operator<<(std::ostream& os,
188  {
189  obj.print(os);
190 
191  return os;
192  }
194 
195 private:
197 
200  void commonConstructor ();
201 
203 
209  void generateFullChain (const P_V& valuesOf1stPosition,
210  unsigned int chainSize,
211  BaseVectorSequence<P_V,P_M>& workingChain,
212  ScalarSequence<double>* workingLogLikelihoodValues,
213  ScalarSequence<double>* workingLogTargetValues);
214 
216  void readFullChain (const std::string& inputFileName,
217  const std::string& inputFileType,
218  unsigned int chainSize,
219  BaseVectorSequence<P_V,P_M>& workingChain);
220 
222 
225  unsigned int idOfFirstPositionInSubChain,
226  double& lastChainSize,
227  P_V& lastMean,
228  P_M& lastAdaptedCovMatrix);
229 
231 
233  double alpha (const MarkovChainPositionData<P_V>& x,
235  unsigned int xStageId,
236  unsigned int yStageId,
237  double* alphaQuotientPtr = NULL);
238 
240 
241  double alpha (const std::vector<MarkovChainPositionData<P_V>*>& inputPositions,
242  const std::vector<unsigned int >& inputTKStageIds);
243 
245 
246  bool acceptAlpha (double alpha);
247 
249 
251  int writeInfo (const BaseVectorSequence<P_V,P_M>& workingChain,
252  std::ofstream& ofsvar) const;
253 
260  unsigned int m_numDisabledParameters; // gpmsa2
261  std::vector<bool> m_parameterEnabledStatus; // gpmsa2
263 
266  unsigned int m_stageIdForDebugging;
267  std::vector<unsigned int> m_idsOfUniquePositions;
268  std::vector<double> m_logTargets;
269  std::vector<double> m_alphaQuotients;
274 
276 
279 };
280 
281 } // End namespace QUESO
282 
283 #endif // UQ_MH_SG_H
void print(std::ostream &os) const
TODO: Prints the sequence.
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.
std::vector< double > m_logTargets
const VectorSpace< P_V, P_M > & m_vectorSpace
A templated class that represents a Markov Chain.
int writeInfo(const BaseVectorSequence< P_V, P_M > &workingChain, std::ofstream &ofsvar) const
Writes information about the Markov chain in a file.
MHRawChainInfoStruct & operator+=(const MHRawChainInfoStruct &rhs)
Addition assignment operator.
const ScalarFunctionSynchronizer< P_V, P_M > * m_targetPdfSynchronizer
void mpiSum(const MpiComm &comm, MHRawChainInfoStruct &sumInfo) const
Calculates the MPI sum of this.
void generateSequence(BaseVectorSequence< P_V, P_M > &workingChain, ScalarSequence< double > *workingLogLikelihoodValues, ScalarSequence< double > *workingLogTargetValues)
Method to generate the chain.
This (virtual) class sets up the environment underlying the use of the QUESO library by an executable...
Definition: Environment.h:187
void reset()
Resets Metropolis-Hastings chain info.
A templated class that represents a Metropolis-Hastings generator of samples.
MetropolisHastingsSG(const char *prefix, const MhOptionsValues *alternativeOptionsValues, const BaseVectorRV< P_V, P_M > &sourceRv, const P_V &initialPosition, const P_M *inputProposalCovMatrix)
Constructor.
MHRawChainInfoStruct m_rawChainInfo
A struct that represents a Metropolis-Hastings sample.
const BaseJointPdf< P_V, P_M > & m_targetPdf
void copy(const MHRawChainInfoStruct &src)
Copies Metropolis-Hastings chain info from src to this.
void getRawChainInfo(MHRawChainInfoStruct &info) const
Gets information from the raw chain.
BaseTKGroup< P_V, P_M > * m_tk
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.
MhOptionsValues m_alternativeOptionsValues
void commonConstructor()
Reads the options values from the options input file.
MHRawChainInfoStruct & operator=(const MHRawChainInfoStruct &rhs)
Assignment operator.
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.
double alpha(const MarkovChainPositionData< P_V > &x, const MarkovChainPositionData< P_V > &y, unsigned int xStageId, unsigned int yStageId, double *alphaQuotientPtr=NULL)
Calculates acceptance ration.
std::vector< unsigned int > m_idsOfUniquePositions
This class provides options for each level of the Multilevel sequence generator if no input file is a...
friend std::ostream & operator<<(std::ostream &os, const MetropolisHastingsSG< P_V, P_M > &obj)
This class provides options for the Metropolis-Hastings generator of samples if no input file is avai...
MetropolisHastingsSGOptions * m_optionsObj
std::vector< bool > m_parameterEnabledStatus
const BaseEnvironment & m_env
The QUESO MPI Communicator Class.
Definition: MpiComm.h:75
std::vector< double > m_alphaQuotients
bool acceptAlpha(double alpha)
Decides whether or not to accept alpha.
This class reads the options for the Metropolis-Hastings generator of samples from an input file...

Generated on Thu Apr 23 2015 19:18:34 for queso-0.50.1 by  doxygen 1.8.5