queso-0.56.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-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 <queso/SharedPtr.h>
39 
40 namespace QUESO {
41 
42 class GslVector;
43 class GslMatrix;
44 
45 template <class P_V, class P_M>
46 class Algorithm;
47 
48 //--------------------------------------------------
49 // MHRawChainInfoStruct --------------------------
50 //--------------------------------------------------
62 {
64 
67 
70 
74 
76 
79 
83 
85 
86  void copy (const MHRawChainInfoStruct& src);
88 
90  void reset ();
91 
93  void mpiSum(const MpiComm& comm, MHRawChainInfoStruct& sumInfo);
95 
96  double runTime;
98  double targetRunTime;
101  double drRunTime;
102  double amRunTime;
103 
104  unsigned int numTargetCalls;
105  unsigned int numDRs;
106  unsigned int numOutOfTargetSupport;
108  unsigned int numRejections;
109 
110 };
111 
112 //--------------------------------------------------
113 // MetropolisHastingsSG----------------------
114 //--------------------------------------------------
115 
125 template <class P_V = GslVector, class P_M = GslMatrix>
127 {
128 public:
130 
131 
140  MetropolisHastingsSG(const char* prefix,
141  const MhOptionsValues* alternativeOptionsValues, // dakota
142  const BaseVectorRV<P_V,P_M>& sourceRv,
143  const P_V& initialPosition,
144  const P_M* inputProposalCovMatrix);
145 
147  MetropolisHastingsSG(const char* prefix,
148  const MhOptionsValues* alternativeOptionsValues, // dakota
149  const BaseVectorRV<P_V,P_M>& sourceRv,
150  const P_V& initialPosition,
151  double initialLogPrior,
152  double initialLogLikelihood,
153  const P_M* inputProposalCovMatrix);
154 
157  const BaseVectorRV<P_V,P_M>& sourceRv,
158  const P_V& initialPosition,
159  const P_M* inputProposalCovMatrix);
160 
163  const BaseVectorRV<P_V,P_M>& sourceRv,
164  const P_V& initialPosition,
165  double initialLogPrior,
166  double initialLogLikelihood,
167  const P_M* inputProposalCovMatrix);
168 
172 
174 
175 
195  void generateSequence (BaseVectorSequence<P_V,P_M>& workingChain,
196  ScalarSequence<double>* workingLogLikelihoodValues,
197  ScalarSequence<double>* workingLogTargetValues);
198 
200  void getRawChainInfo (MHRawChainInfoStruct& info) const;
201 
203 
205  const BaseTKGroup<P_V, P_M> & transitionKernel() const;
206 
208 
209 
211  void print (std::ostream& os) const;
212  friend std::ostream& operator<<(std::ostream& os,
214  {
215  obj.print(os);
216 
217  return os;
218  }
220 
221 private:
223 
226  void commonConstructor ();
227 
229 
235  void generateFullChain (const P_V& valuesOf1stPosition,
236  unsigned int chainSize,
237  BaseVectorSequence<P_V,P_M>& workingChain,
238  ScalarSequence<double>* workingLogLikelihoodValues,
239  ScalarSequence<double>* workingLogTargetValues);
240 
242  void adapt(unsigned int positionId,
243  BaseVectorSequence<P_V, P_M> & workingChain);
244 
246 
263  bool delayedRejection(unsigned int positionId,
264  const MarkovChainPositionData<P_V> & currentPositionData,
265  MarkovChainPositionData<P_V> & currentCandidateData);
266 
268  void readFullChain (const std::string& inputFileName,
269  const std::string& inputFileType,
270  unsigned int chainSize,
271  BaseVectorSequence<P_V,P_M>& workingChain);
272 
274 
277  unsigned int idOfFirstPositionInSubChain,
278  double& lastChainSize,
279  P_V& lastMean,
280  P_M& lastAdaptedCovMatrix);
281 
283 
284  double alpha (const std::vector<MarkovChainPositionData<P_V>*>& inputPositions,
285  const std::vector<unsigned int >& inputTKStageIds);
286 
288 
289  bool acceptAlpha (double alpha);
290 
292 
294  int writeInfo (const BaseVectorSequence<P_V,P_M>& workingChain,
295  std::ofstream& ofsvar) const;
296 
303  unsigned int m_numDisabledParameters; // gpmsa2
304  std::vector<bool> m_parameterEnabledStatus; // gpmsa2
306 
307  typename SharedPtr<BaseTKGroup<P_V,P_M> >::Type m_tk;
308  typename SharedPtr<Algorithm<P_V, P_M> >::Type m_algorithm;
310  unsigned int m_stageIdForDebugging;
311  std::vector<unsigned int> m_idsOfUniquePositions;
312  std::vector<double> m_logTargets;
313  std::vector<double> m_alphaQuotients;
315  P_V * m_lastMean;
318 
320 
323 
327 
329  boxSubset);
330 
332 };
333 
334 } // End namespace QUESO
335 
336 #endif // UQ_MH_SG_H
bool acceptAlpha(double alpha)
Decides whether or not to accept alpha.
bool delayedRejection(unsigned int positionId, const MarkovChainPositionData< P_V > &currentPositionData, MarkovChainPositionData< P_V > &currentCandidateData)
Does delayed rejection.
void commonConstructor()
Reads the options values from the options input file.
This class reads the options for the Metropolis-Hastings generator of samples from an input file...
This (virtual) class sets up the environment underlying the use of the QUESO library by an executable...
Definition: Environment.h:197
MHRawChainInfoStruct & operator=(const MHRawChainInfoStruct &rhs)
Assignment operator.
void getRawChainInfo(MHRawChainInfoStruct &info) const
Gets information from the raw chain.
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.
void copy(const MHRawChainInfoStruct &src)
Copies Metropolis-Hastings chain info from src to this.
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.
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.
void adapt(unsigned int positionId, BaseVectorSequence< P_V, P_M > &workingChain)
Adaptive Metropolis method that deals with adapting the proposal covariance matrix.
const BaseJointPdf< P_V, P_M > & m_targetPdf
void print(std::ostream &os) const
TODO: Prints the sequence.
A templated class that represents a Metropolis-Hastings generator of samples.
std::vector< bool > m_parameterEnabledStatus
MetropolisHastingsSGOptions * m_oldOptions
std::vector< unsigned int > m_idsOfUniquePositions
void reset()
Resets Metropolis-Hastings chain info.
const MhOptionsValues * m_optionsObj
This class provides options for each level of the Multilevel sequence generator if no input file is a...
void mpiSum(const MpiComm &comm, MHRawChainInfoStruct &sumInfo)
Calculates the MPI sum of this.
SharedPtr< Algorithm< P_V, P_M > >::Type m_algorithm
void transformInitialCovMatrixToGaussianSpace(const BoxSubset< P_V, P_M > &boxSubset)
A struct that represents a Metropolis-Hastings sample.
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
friend std::ostream & operator<<(std::ostream &os, const MetropolisHastingsSG< P_V, P_M > &obj)
The QUESO MPI Communicator Class.
Definition: MpiComm.h:203
This base class allows the representation of a transition kernel.
Definition: Algorithm.h:32
double alpha(const std::vector< MarkovChainPositionData< P_V > * > &inputPositions, const std::vector< unsigned int > &inputTKStageIds)
Calculates acceptance ratio.
const ScalarFunctionSynchronizer< P_V, P_M > * m_targetPdfSynchronizer
std::vector< double > m_alphaQuotients
const VectorSpace< P_V, P_M > & m_vectorSpace
This class provides options for the Metropolis-Hastings generator of samples if no input file is avai...
std::vector< double > m_logTargets
void generateSequence(BaseVectorSequence< P_V, P_M > &workingChain, ScalarSequence< double > *workingLogLikelihoodValues, ScalarSequence< double > *workingLogTargetValues)
Method to generate the chain.
const BaseEnvironment & m_env
SharedPtr< BaseTKGroup< P_V, P_M > >::Type m_tk
int writeInfo(const BaseVectorSequence< P_V, P_M > &workingChain, std::ofstream &ofsvar) const
Writes information about the Markov chain in a file.
const BaseTKGroup< P_V, P_M > & transitionKernel() const
Returns the underlying transition kernel for this sequence generator.
A templated class that represents a Markov Chain.

Generated on Thu Dec 15 2016 13:23:11 for queso-0.56.1 by  doxygen 1.8.5