queso-0.57.1
|
A templated class that represents a Metropolis-Hastings generator of samples. More...
#include <MetropolisHastingsSG.h>
Public Member Functions | |
const BaseTKGroup< P_V, P_M > & | transitionKernel () const |
Returns the underlying transition kernel for this sequence generator. More... | |
Constructor/Destructor methods | |
MetropolisHastingsSG (const char *prefix, const MhOptionsValues *alternativeOptionsValues, const BaseVectorRV< P_V, P_M > &sourceRv, const P_V &initialPosition, const P_M *inputProposalCovMatrix) | |
Constructor. More... | |
MetropolisHastingsSG (const char *prefix, const MhOptionsValues *alternativeOptionsValues, const BaseVectorRV< P_V, P_M > &sourceRv, const P_V &initialPosition, double initialLogPrior, double initialLogLikelihood, const P_M *inputProposalCovMatrix) | |
Constructor. More... | |
MetropolisHastingsSG (const MLSamplingLevelOptions &mlOptions, const BaseVectorRV< P_V, P_M > &sourceRv, const P_V &initialPosition, const P_M *inputProposalCovMatrix) | |
Constructor. More... | |
MetropolisHastingsSG (const MLSamplingLevelOptions &mlOptions, const BaseVectorRV< P_V, P_M > &sourceRv, const P_V &initialPosition, double initialLogPrior, double initialLogLikelihood, const P_M *inputProposalCovMatrix) | |
Constructor. More... | |
~MetropolisHastingsSG () | |
Destructor. More... | |
Statistical methods | |
void | generateSequence (BaseVectorSequence< P_V, P_M > &workingChain, ScalarSequence< double > *workingLogLikelihoodValues, ScalarSequence< double > *workingLogTargetValues) |
Method to generate the chain. More... | |
void | getRawChainInfo (MHRawChainInfoStruct &info) const |
Gets information from the raw chain. More... | |
Private Member Functions | |
void | commonConstructor () |
Reads the options values from the options input file. More... | |
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. More... | |
void | adapt (unsigned int positionId, BaseVectorSequence< P_V, P_M > &workingChain) |
Adaptive Metropolis method that deals with adapting the proposal covariance matrix. More... | |
bool | delayedRejection (unsigned int positionId, const MarkovChainPositionData< P_V > ¤tPositionData, MarkovChainPositionData< P_V > ¤tCandidateData) |
Does delayed rejection. More... | |
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. More... | |
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. More... | |
double | alpha (const std::vector< MarkovChainPositionData< P_V > * > &inputPositions, const std::vector< unsigned int > &inputTKStageIds) |
Calculates acceptance ratio. More... | |
bool | acceptAlpha (double alpha) |
Decides whether or not to accept alpha. More... | |
int | writeInfo (const BaseVectorSequence< P_V, P_M > &workingChain, std::ofstream &ofsvar) const |
Writes information about the Markov chain in a file. More... | |
void | transformInitialCovMatrixToGaussianSpace (const BoxSubset< P_V, P_M > &boxSubset) |
I/O methods | |
void | print (std::ostream &os) const |
TODO: Prints the sequence. More... | |
std::ostream & | operator<< (std::ostream &os, const MetropolisHastingsSG< P_V, P_M > &obj) |
A templated class that represents a Metropolis-Hastings generator of samples.
This class implements a Metropolis-Hastings generator of samples. 'SG' stands for 'Sequence Generator'. Options reading is handled by class 'MetropolisHastingsOptions'. If options request data to be written in the output file (MATLAB .m format only, for now), the user can check which MATLAB variables are defined and set by running 'grep zeros <OUTPUT file="" name>="">' after the solution procedures ends. The names of the variables are self explanatory.
Definition at line 126 of file MetropolisHastingsSG.h.
QUESO::MetropolisHastingsSG< P_V, P_M >::MetropolisHastingsSG | ( | const char * | prefix, |
const MhOptionsValues * | alternativeOptionsValues, | ||
const BaseVectorRV< P_V, P_M > & | sourceRv, | ||
const P_V & | initialPosition, | ||
const P_M * | inputProposalCovMatrix | ||
) |
Constructor.
This method reads the options from the options input file. It calls commonConstructor()
.
Requirements:
If alternativeOptionsValues
is NULL and an input file is specified, the constructor reads input options that begin with the string '<prefix>mh_'. For instance, if 'prefix' is 'pROblem_775_ip_', then the constructor will read all options that begin with 'pROblem_775_ip_mh_'.
If alternativeOptionsValues
is not NULL, the input file is ignored and construction copies the object pointed to by alternativeOptionsValues
to and stores the copy internally. Users may delete the object poined to by alternativeOptionsValues
. Users cannot change the options object after MetropolisHastingsSG has been constructed.
Options reading is handled by class 'MetropolisHastingsOptions'.
prefix | Prefix |
alternativeOptionsValues | Options (if no input file) |
sourceRv | The source RV |
initialPosition | Initial chain position |
inputProposalCovMatrix | Proposal cov. matrix |
Definition at line 141 of file MetropolisHastingsSG.C.
References QUESO::queso_require_equal_to_msg.
QUESO::MetropolisHastingsSG< P_V, P_M >::MetropolisHastingsSG | ( | const char * | prefix, |
const MhOptionsValues * | alternativeOptionsValues, | ||
const BaseVectorRV< P_V, P_M > & | sourceRv, | ||
const P_V & | initialPosition, | ||
double | initialLogPrior, | ||
double | initialLogLikelihood, | ||
const P_M * | inputProposalCovMatrix | ||
) |
Constructor.
prefix | Prefix |
alternativeOptionsValues | Options (if no input file) |
sourceRv | The source RV |
initialPosition | Initial chain position |
initialLogPrior | Initial chain prior |
initialLogLikelihood | Initial chain likelihood |
inputProposalCovMatrix | Proposal cov. matrix |
Definition at line 221 of file MetropolisHastingsSG.C.
References QUESO::queso_require_equal_to_msg.
QUESO::MetropolisHastingsSG< P_V, P_M >::MetropolisHastingsSG | ( | const MLSamplingLevelOptions & | mlOptions, |
const BaseVectorRV< P_V, P_M > & | sourceRv, | ||
const P_V & | initialPosition, | ||
const P_M * | inputProposalCovMatrix | ||
) |
Constructor.
Definition at line 303 of file MetropolisHastingsSG.C.
References QUESO::MetropolisHastingsSG< P_V, P_M >::commonConstructor(), QUESO::MetropolisHastingsSG< P_V, P_M >::m_env, QUESO::MetropolisHastingsSG< P_V, P_M >::m_initialProposalCovMatrix, QUESO::MetropolisHastingsSG< P_V, P_M >::m_oldOptions, QUESO::MetropolisHastingsSG< P_V, P_M >::m_optionsObj, and QUESO::BaseEnvironment::subDisplayFile().
QUESO::MetropolisHastingsSG< P_V, P_M >::MetropolisHastingsSG | ( | const MLSamplingLevelOptions & | mlOptions, |
const BaseVectorRV< P_V, P_M > & | sourceRv, | ||
const P_V & | initialPosition, | ||
double | initialLogPrior, | ||
double | initialLogLikelihood, | ||
const P_M * | inputProposalCovMatrix | ||
) |
Constructor.
Definition at line 369 of file MetropolisHastingsSG.C.
References QUESO::MetropolisHastingsSG< P_V, P_M >::commonConstructor(), QUESO::MetropolisHastingsSG< P_V, P_M >::m_env, QUESO::MetropolisHastingsSG< P_V, P_M >::m_initialProposalCovMatrix, QUESO::MetropolisHastingsSG< P_V, P_M >::m_oldOptions, QUESO::MetropolisHastingsSG< P_V, P_M >::m_optionsObj, and QUESO::BaseEnvironment::subDisplayFile().
QUESO::MetropolisHastingsSG< P_V, P_M >::~MetropolisHastingsSG | ( | ) |
Destructor.
Definition at line 437 of file MetropolisHastingsSG.C.
|
private |
Decides whether or not to accept alpha.
If either alpha is negative or greater than one, its value will not be accepted.
Definition at line 753 of file MetropolisHastingsSG.C.
|
private |
Adaptive Metropolis method that deals with adapting the proposal covariance matrix.
Definition at line 1977 of file MetropolisHastingsSG.C.
References QUESO::BaseVectorSequence< V, M >::getPositionValues(), QUESO::MiscGetEllapsedSeconds(), QUESO::queso_require_equal_to_msg, QUESO::SequenceOfVectors< V, M >::resizeSequence(), QUESO::SequenceOfVectors< V, M >::setPositionValues(), QUESO::SequenceOfVectors< V, M >::subSequenceSize(), QUESO::UQ_INCOMPLETE_IMPLEMENTATION_RC, QUESO::UQ_MATRIX_IS_NOT_POS_DEFINITE_RC, and QUESO::UQ_OK_RC.
|
private |
Calculates acceptance ratio.
The acceptance ratio is used to decide whether to accept or reject a candidate.
Definition at line 561 of file MetropolisHastingsSG.C.
References QUESO::queso_isnan(), QUESO::queso_require_equal_to_msg, and QUESO::BaseEnvironment::worldRank().
|
private |
Reads the options values from the options input file.
This method actually reads the options input file, such as the value for the Delayed Rejection scales, the presence of Hessian covariance matrices and reads the user-provided initial proposal covariance matrix (alternative to local Hessians).
Definition at line 463 of file MetropolisHastingsSG.C.
References QUESO::Factory< BaseTKGroup< GslVector, GslMatrix > >::build(), QUESO::Factory< Algorithm< GslVector, GslMatrix > >::build(), QUESO::TransitionKernelFactory::set_dr_scales(), QUESO::AlgorithmFactory::set_environment(), QUESO::TransitionKernelFactory::set_initial_cov_matrix(), QUESO::TransitionKernelFactory::set_options(), QUESO::TransitionKernelFactory::set_pdf_synchronizer(), QUESO::TransitionKernelFactory::set_target_pdf(), QUESO::AlgorithmFactory::set_tk(), and QUESO::TransitionKernelFactory::set_vectorspace().
Referenced by QUESO::MetropolisHastingsSG< P_V, P_M >::MetropolisHastingsSG().
|
private |
Does delayed rejection.
When faced with an imminent rejection, this method computes a series of new candidates in the same proposal direction but with smaller proposal step sizes. For each of these new candidates, we check if it will be accepted, if not we repeat the process until all the candidates have been tested.
If there is a candidate that will be accepted, this method will return true
, otherwise it returns false
.
currentCandidateData
is updated whenever a new proposal is generated throughout the delayed rejection procedure.
delayedRejection
promises not to change currentPositionData
, because changing the current position of the Markov chain would be grossly inappropriate.
Definition at line 2279 of file MetropolisHastingsSG.C.
References QUESO::if, QUESO::MiscGetEllapsedSeconds(), QUESO::queso_require_equal_to_msg, QUESO::MarkovChainPositionData< V >::set(), QUESO::UQ_OK_RC, and QUESO::MarkovChainPositionData< V >::vecValues().
|
private |
This method generates the chain.
Given the size of the chain, the values of the first position in the chain. It checks if the position is out of target pdf support, otherwise it calculates the value of both its likelihood and prior and adds the position to the chain. For the next positions, once they are generated, some tests are performed (such as unicity and the value of alpha) and the steps for the first position are repeated, including the optional Delayed Rejection and the adaptive Metropolis (adaptation of covariance matrix) steps.
Definition at line 1332 of file MetropolisHastingsSG.C.
References QUESO::BaseVectorSequence< V, M >::estimateConvBrooksGelman(), QUESO::if, QUESO::MarkovChainPositionData< V >::logLikelihood(), QUESO::MarkovChainPositionData< V >::logTarget(), QUESO::MiscGetEllapsedSeconds(), QUESO::BaseVectorSequence< V, M >::name(), QUESO::queso_require_equal_to_msg, QUESO::ScalarSequence< T >::resizeSequence(), QUESO::BaseVectorSequence< V, M >::resizeSequence(), QUESO::BaseVectorSequence< V, M >::setPositionValues(), QUESO::BaseVectorSequence< V, M >::subSequenceSize(), QUESO::BaseVectorSequence< V, M >::subWriteContents(), QUESO::ScalarSequence< T >::subWriteContents(), QUESO::UQ_OK_RC, and QUESO::MarkovChainPositionData< V >::vecValues().
void QUESO::MetropolisHastingsSG< P_V, P_M >::generateSequence | ( | BaseVectorSequence< P_V, P_M > & | workingChain, |
ScalarSequence< double > * | workingLogLikelihoodValues, | ||
ScalarSequence< double > * | workingLogTargetValues | ||
) |
Method to generate the chain.
Requirement: the vector space 'm_vectorSpace' should have dimension equal to the size of a vector in 'workingChain'. If the requirement is satisfied, this operation sets the size and the contents of 'workingChain' using the algorithm options set in the constructor. If not NULL, 'workingLogLikelihoodValues' and 'workingLogTargetValues' are set accordingly. This operation currently implements the DRAM algorithm (Heikki Haario, Marko Laine, Antonietta Mira and Eero Saksman, "DRAM: Efficient Adaptive MCMC", Statistics and Computing (2006), 16:339-354), as a translation of the core routine at the MCMC toolbox for MATLAB, available at helios.fmi.fi/~lainema/mcmc/‎ (accessed in July 3rd, 2013). Indeed, the example available in examples/statisticalInverseProblem is closely related to the 'normal example' in the toolbox. Over time, though:
Definition at line 859 of file MetropolisHastingsSG.C.
References QUESO::BaseVectorSequence< V, M >::computeFilterParams(), QUESO::BaseVectorSequence< V, M >::computeStatistics(), QUESO::BaseVectorSequence< V, M >::filter(), QUESO::ScalarSequence< T >::filter(), QUESO::SequenceOfVectors< V, M >::getPositionValues(), QUESO::BaseVectorSequence< V, M >::name(), QUESO::FilePtrSetStruct::ofsVar, QUESO::queso_require_not_equal_to_msg, QUESO::BaseVectorSequence< V, M >::setName(), QUESO::BaseVectorSequence< V, M >::subPositionsOfMaximum(), QUESO::BaseVectorSequence< V, M >::subSequenceSize(), QUESO::SequenceOfVectors< V, M >::subSequenceSize(), QUESO::BaseVectorSequence< V, M >::subWriteContents(), QUESO::ScalarSequence< T >::subWriteContents(), QUESO::BaseVectorSequence< V, M >::unifiedPositionsOfMaximum(), QUESO::BaseVectorSequence< V, M >::unifiedWriteContents(), QUESO::ScalarSequence< T >::unifiedWriteContents(), QUESO::UQ_OK_RC, and QUESO::BaseVectorSequence< V, M >::vectorSizeLocal().
Referenced by QUESO::MLSampling< P_V, P_M >::generateBalLinkedChains_all(), and QUESO::MLSampling< P_V, P_M >::generateUnbLinkedChains_all().
void QUESO::MetropolisHastingsSG< P_V, P_M >::getRawChainInfo | ( | MHRawChainInfoStruct & | info | ) | const |
Gets information from the raw chain.
Definition at line 1312 of file MetropolisHastingsSG.C.
Referenced by QUESO::MLSampling< P_V, P_M >::generateBalLinkedChains_all(), and QUESO::MLSampling< P_V, P_M >::generateUnbLinkedChains_all().
void QUESO::MetropolisHastingsSG< P_V, P_M >::print | ( | std::ostream & | os | ) | const |
TODO: Prints the sequence.
|
private |
This method reads the chain contents.
Definition at line 1320 of file MetropolisHastingsSG.C.
References QUESO::BaseVectorSequence< V, M >::unifiedReadContents().
|
private |
Definition at line 2532 of file MetropolisHastingsSG.C.
References QUESO::BoxSubset< V, M >::maxValues(), QUESO::BoxSubset< V, M >::minValues(), and QUESO::queso_isfinite().
const BaseTKGroup< P_V, P_M > & QUESO::MetropolisHastingsSG< P_V, P_M >::transitionKernel | ( | ) | const |
Returns the underlying transition kernel for this sequence generator.
Definition at line 845 of file MetropolisHastingsSG.C.
|
private |
This method updates the adapted covariance matrix.
This function is called is the option to used adaptive Metropolis was chosen by the user (via options input file). It performs an adaptation of covariance matrix.
Definition at line 2469 of file MetropolisHastingsSG.C.
References QUESO::BaseVectorSequence< V, M >::getPositionValues(), QUESO::matrixProduct(), QUESO::BaseVectorSequence< V, M >::subMeanExtra(), QUESO::BaseVectorSequence< V, M >::subMeanPlain(), and QUESO::BaseVectorSequence< V, M >::subSequenceSize().
|
private |
Writes information about the Markov chain in a file.
It writes down the alpha quotients, the number of rejected positions, number of positions out of target support, the name of the components and the chain runtime.
Definition at line 767 of file MetropolisHastingsSG.C.
References QUESO::BaseVectorSequence< V, M >::name(), QUESO::BaseVectorSequence< V, M >::subSequenceSize(), and QUESO::UQ_OK_RC.
|
friend |
Definition at line 228 of file MetropolisHastingsSG.h.
|
private |
Definition at line 324 of file MetropolisHastingsSG.h.
|
private |
Definition at line 329 of file MetropolisHastingsSG.h.
|
private |
Definition at line 340 of file MetropolisHastingsSG.h.
|
private |
Definition at line 313 of file MetropolisHastingsSG.h.
Referenced by QUESO::MetropolisHastingsSG< P_V, P_M >::MetropolisHastingsSG().
|
private |
Definition at line 327 of file MetropolisHastingsSG.h.
|
private |
Definition at line 342 of file MetropolisHastingsSG.h.
|
private |
Definition at line 341 of file MetropolisHastingsSG.h.
|
private |
Definition at line 316 of file MetropolisHastingsSG.h.
|
private |
Definition at line 317 of file MetropolisHastingsSG.h.
Referenced by QUESO::MetropolisHastingsSG< P_V, P_M >::MetropolisHastingsSG().
|
private |
Definition at line 332 of file MetropolisHastingsSG.h.
|
private |
Definition at line 330 of file MetropolisHastingsSG.h.
|
private |
Definition at line 331 of file MetropolisHastingsSG.h.
|
private |
Definition at line 349 of file MetropolisHastingsSG.h.
|
private |
Definition at line 328 of file MetropolisHastingsSG.h.
|
private |
Definition at line 318 of file MetropolisHastingsSG.h.
|
private |
Definition at line 319 of file MetropolisHastingsSG.h.
|
private |
Definition at line 333 of file MetropolisHastingsSG.h.
|
private |
Definition at line 338 of file MetropolisHastingsSG.h.
Referenced by QUESO::MetropolisHastingsSG< P_V, P_M >::MetropolisHastingsSG().
|
private |
Definition at line 337 of file MetropolisHastingsSG.h.
Referenced by QUESO::MetropolisHastingsSG< P_V, P_M >::MetropolisHastingsSG().
|
private |
Definition at line 320 of file MetropolisHastingsSG.h.
|
private |
Definition at line 325 of file MetropolisHastingsSG.h.
|
private |
Definition at line 335 of file MetropolisHastingsSG.h.
|
private |
Definition at line 326 of file MetropolisHastingsSG.h.
|
private |
Definition at line 315 of file MetropolisHastingsSG.h.
|
private |
Definition at line 321 of file MetropolisHastingsSG.h.
|
private |
Definition at line 323 of file MetropolisHastingsSG.h.
|
private |
Definition at line 347 of file MetropolisHastingsSG.h.
|
private |
Definition at line 314 of file MetropolisHastingsSG.h.