queso-0.57.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-2017 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 
156  MetropolisHastingsSG(const char* prefix,
157  const MhOptionsValues* alternativeOptionsValues, // dakota
158  const BaseVectorRV<P_V,P_M>& sourceRv,
159  const P_V& initialPosition,
160  const P_M* inputProposalCovMatrix);
161 
163  MetropolisHastingsSG(const char* prefix,
164  const MhOptionsValues* alternativeOptionsValues, // dakota
165  const BaseVectorRV<P_V,P_M>& sourceRv,
166  const P_V& initialPosition,
167  double initialLogPrior,
168  double initialLogLikelihood,
169  const P_M* inputProposalCovMatrix);
170 
173  const BaseVectorRV<P_V,P_M>& sourceRv,
174  const P_V& initialPosition,
175  const P_M* inputProposalCovMatrix);
176 
179  const BaseVectorRV<P_V,P_M>& sourceRv,
180  const P_V& initialPosition,
181  double initialLogPrior,
182  double initialLogLikelihood,
183  const P_M* inputProposalCovMatrix);
184 
188 
190 
191 
211  void generateSequence (BaseVectorSequence<P_V,P_M>& workingChain,
212  ScalarSequence<double>* workingLogLikelihoodValues,
213  ScalarSequence<double>* workingLogTargetValues);
214 
216  void getRawChainInfo (MHRawChainInfoStruct& info) const;
217 
219 
221  const BaseTKGroup<P_V, P_M> & transitionKernel() const;
222 
224 
225 
227  void print (std::ostream& os) const;
228  friend std::ostream& operator<<(std::ostream& os,
230  {
231  obj.print(os);
232 
233  return os;
234  }
236 
237 private:
239 
242  void commonConstructor ();
243 
245 
251  void generateFullChain (const P_V& valuesOf1stPosition,
252  unsigned int chainSize,
253  BaseVectorSequence<P_V,P_M>& workingChain,
254  ScalarSequence<double>* workingLogLikelihoodValues,
255  ScalarSequence<double>* workingLogTargetValues);
256 
258  void adapt(unsigned int positionId,
259  BaseVectorSequence<P_V, P_M> & workingChain);
260 
262 
279  bool delayedRejection(unsigned int positionId,
280  const MarkovChainPositionData<P_V> & currentPositionData,
281  MarkovChainPositionData<P_V> & currentCandidateData);
282 
284  void readFullChain (const std::string& inputFileName,
285  const std::string& inputFileType,
286  unsigned int chainSize,
287  BaseVectorSequence<P_V,P_M>& workingChain);
288 
290 
293  unsigned int idOfFirstPositionInSubChain,
294  double& lastChainSize,
295  P_V& lastMean,
296  P_M& lastAdaptedCovMatrix);
297 
299 
300  double alpha (const std::vector<MarkovChainPositionData<P_V>*>& inputPositions,
301  const std::vector<unsigned int >& inputTKStageIds);
302 
304 
305  bool acceptAlpha (double alpha);
306 
308 
310  int writeInfo (const BaseVectorSequence<P_V,P_M>& workingChain,
311  std::ofstream& ofsvar) const;
312 
319  unsigned int m_numDisabledParameters; // gpmsa2
320  std::vector<bool> m_parameterEnabledStatus; // gpmsa2
322 
326  unsigned int m_stageIdForDebugging;
327  std::vector<unsigned int> m_idsOfUniquePositions;
328  std::vector<double> m_logTargets;
329  std::vector<double> m_alphaQuotients;
334 
336 
339 
343 
345  boxSubset);
346 
348 
350 };
351 
352 } // End namespace QUESO
353 
354 #endif // UQ_MH_SG_H
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 commonConstructor()
Reads the options values from the options input file.
const BaseEnvironment & m_env
void transformInitialCovMatrixToGaussianSpace(const BoxSubset< P_V, P_M > &boxSubset)
void adapt(unsigned int positionId, BaseVectorSequence< P_V, P_M > &workingChain)
Adaptive Metropolis method that deals with adapting the proposal covariance matrix.
void getRawChainInfo(MHRawChainInfoStruct &info) const
Gets information from the raw chain.
Class representing a subset of a vector space shaped like a hypercube.
Definition: BoxSubset.h:44
The QUESO MPI Communicator Class.
Definition: MpiComm.h:203
void mpiSum(const MpiComm &comm, MHRawChainInfoStruct &sumInfo)
Calculates the MPI sum of this.
ScopedPtr< const ScalarFunctionSynchronizer< P_V, P_M > >::Type m_targetPdfSynchronizer
A templated class that represents a Metropolis-Hastings generator of samples.
void print(std::ostream &os) const
TODO: Prints the sequence.
std::unique_ptr< T > Type
Definition: ScopedPtr.h:76
std::vector< bool > m_parameterEnabledStatus
A struct that represents a Metropolis-Hastings sample.
ScopedPtr< P_M >::Type m_lastAdaptedCovMatrix
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.
int writeInfo(const BaseVectorSequence< P_V, P_M > &workingChain, std::ofstream &ofsvar) const
Writes information about the Markov chain in a file.
A templated class that represents a Markov Chain.
bool acceptAlpha(double alpha)
Decides whether or not to accept alpha.
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.
double alpha(const std::vector< MarkovChainPositionData< P_V > * > &inputPositions, const std::vector< unsigned int > &inputTKStageIds)
Calculates acceptance ratio.
friend std::ostream & operator<<(std::ostream &os, const MetropolisHastingsSG< P_V, P_M > &obj)
This base class allows the representation of a transition kernel.
Definition: Algorithm.h:32
ScopedPtr< P_V >::Type m_lastMean
void copy(const MHRawChainInfoStruct &src)
Copies Metropolis-Hastings chain info from src to this.
const BaseJointPdf< P_V, P_M > & m_targetPdf
MHRawChainInfoStruct & operator+=(const MHRawChainInfoStruct &rhs)
Addition assignment operator.
std::vector< double > m_logTargets
const VectorSpace< P_V, P_M > & m_vectorSpace
SharedPtr< Algorithm< P_V, P_M > >::Type m_algorithm
Base class for handling vector and array samples (sequence of vectors or arrays). ...
This class provides options for the Metropolis-Hastings generator of samples if no input file is avai...
MetropolisHastingsSG(const char *prefix, const MhOptionsValues *alternativeOptionsValues, const BaseVectorRV< P_V, P_M > &sourceRv, const P_V &initialPosition, const P_M *inputProposalCovMatrix)
Constructor.
bool delayedRejection(unsigned int positionId, const MarkovChainPositionData< P_V > &currentPositionData, MarkovChainPositionData< P_V > &currentCandidateData)
Does delayed rejection.
void reset()
Resets Metropolis-Hastings chain info.
const BaseTKGroup< P_V, P_M > & transitionKernel() const
Returns the underlying transition kernel for this sequence generator.
MHRawChainInfoStruct m_rawChainInfo
SharedPtr< BaseTKGroup< P_V, P_M > >::Type m_tk
ScopedPtr< const MhOptionsValues >::Type m_optionsObj
std::vector< double > m_alphaQuotients
MHRawChainInfoStruct & operator=(const MHRawChainInfoStruct &rhs)
Assignment operator.
This class provides options for each level of the Multilevel sequence generator if no input file is a...
This (virtual) class sets up the environment underlying the use of the QUESO library by an executable...
Definition: Environment.h:198
std::vector< unsigned int > m_idsOfUniquePositions
void generateSequence(BaseVectorSequence< P_V, P_M > &workingChain, ScalarSequence< double > *workingLogLikelihoodValues, ScalarSequence< double > *workingLogTargetValues)
Method to generate the chain.
ScopedPtr< MetropolisHastingsSGOptions >::Type m_oldOptions

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