25 #include <queso/asserts.h>
26 #include <queso/MetropolisHastingsSG.h>
27 #include <queso/GslVector.h>
28 #include <queso/GslMatrix.h>
30 #include <queso/HessianCovMatricesTKGroup.h>
31 #include <queso/ScaledCovMatrixTKGroup.h>
32 #include <queso/TransformedScaledCovMatrixTKGroup.h>
34 #include <queso/InvLogitGaussianJointPdf.h>
36 #include <queso/GaussianJointPdf.h>
38 #include <queso/TKFactoryInitializer.h>
39 #include <queso/TransitionKernelFactory.h>
40 #include <queso/AlgorithmFactoryInitializer.h>
41 #include <queso/AlgorithmFactory.h>
129 "MHRawChainInfoStruct::mpiSum()",
130 "failed MPI.Allreduce() for sum of doubles");
133 "MHRawChainInfoStruct::mpiSum()",
134 "failed MPI.Allreduce() for sum of unsigned ints");
140 template<
class P_V,
class P_M>
148 m_env (sourceRv.env()),
149 m_vectorSpace (sourceRv.imageSet().vectorSpace()),
150 m_targetPdf (sourceRv.pdf()),
151 m_initialPosition (initialPosition),
152 m_initialProposalCovMatrix (m_vectorSpace.zeroVector()),
153 m_nullInputProposalCovMatrix(inputProposalCovMatrix == NULL),
154 m_numDisabledParameters (0),
155 m_parameterEnabledStatus (m_vectorSpace.dimLocal(),
true),
159 m_positionIdForDebugging (0),
160 m_stageIdForDebugging (0),
161 m_idsOfUniquePositions (0),
163 m_alphaQuotients (0),
166 m_lastAdaptedCovMatrix (),
167 m_numPositionsNotSubWritten (0),
169 m_computeInitialPriorAndLikelihoodValues(
true),
170 m_initialLogPriorValue (0.),
171 m_initialLogLikelihoodValue (0.),
172 m_userDidNotProvideOptions(
false),
173 m_latestDirtyCovMatrixIteration(0)
175 if (inputProposalCovMatrix != NULL) {
176 m_initialProposalCovMatrix = *inputProposalCovMatrix;
180 if (alternativeOptionsValues != NULL) {
188 if (m_optionsObj->m_help !=
"") {
189 if (m_env.subDisplayFile() && !m_optionsObj->m_totallyMute) {
190 *m_env.subDisplayFile() << (*m_optionsObj) << std::endl;
194 if ((m_env.subDisplayFile() ) &&
195 (m_optionsObj->m_totallyMute ==
false)) {
196 *m_env.subDisplayFile() <<
"Entering MetropolisHastingsSG<P_V,P_M>::constructor(1)"
197 <<
": prefix = " << prefix
198 <<
", alternativeOptionsValues = " << alternativeOptionsValues
199 <<
", m_env.optionsInputFileName() = " << m_env.optionsInputFileName()
200 <<
", m_initialProposalCovMatrix = " << m_initialProposalCovMatrix
204 queso_require_equal_to_msg(sourceRv.imageSet().vectorSpace().dimLocal(), initialPosition.sizeLocal(),
"'sourceRv' and 'initialPosition' should have equal dimensions");
206 if (inputProposalCovMatrix) {
207 queso_require_equal_to_msg(sourceRv.imageSet().vectorSpace().dimLocal(), inputProposalCovMatrix->numRowsLocal(),
"'sourceRv' and 'inputProposalCovMatrix' should have equal dimensions");
208 queso_require_equal_to_msg(inputProposalCovMatrix->numCols(), inputProposalCovMatrix->numRowsGlobal(),
"'inputProposalCovMatrix' should be a square matrix");
213 if ((m_env.subDisplayFile() ) &&
214 (m_optionsObj->m_totallyMute ==
false)) {
215 *m_env.subDisplayFile() <<
"Leaving MetropolisHastingsSG<P_V,P_M>::constructor(1)"
220 template<
class P_V,
class P_M>
230 m_env (sourceRv.env()),
231 m_vectorSpace (sourceRv.imageSet().vectorSpace()),
232 m_targetPdf (sourceRv.pdf()),
233 m_initialPosition (initialPosition),
234 m_initialProposalCovMatrix (m_vectorSpace.zeroVector()),
235 m_nullInputProposalCovMatrix(inputProposalCovMatrix == NULL),
236 m_numDisabledParameters (0),
237 m_parameterEnabledStatus (m_vectorSpace.dimLocal(),
true),
241 m_positionIdForDebugging (0),
242 m_stageIdForDebugging (0),
243 m_idsOfUniquePositions (0),
245 m_alphaQuotients (0),
248 m_lastAdaptedCovMatrix (),
249 m_numPositionsNotSubWritten (0),
251 m_computeInitialPriorAndLikelihoodValues(
false),
252 m_initialLogPriorValue (initialLogPrior),
253 m_initialLogLikelihoodValue (initialLogLikelihood),
254 m_userDidNotProvideOptions(
false),
255 m_latestDirtyCovMatrixIteration(0)
257 if (inputProposalCovMatrix != NULL) {
258 m_initialProposalCovMatrix = *inputProposalCovMatrix;
262 if (alternativeOptionsValues != NULL) {
270 if (m_optionsObj->m_help !=
"") {
271 if (m_env.subDisplayFile() && !m_optionsObj->m_totallyMute) {
272 *m_env.subDisplayFile() << (*m_optionsObj) << std::endl;
276 if ((m_env.subDisplayFile() ) &&
277 (m_optionsObj->m_totallyMute ==
false)) {
278 *m_env.subDisplayFile() <<
"Entering MetropolisHastingsSG<P_V,P_M>::constructor(2)"
279 <<
": prefix = " << prefix
280 <<
", alternativeOptionsValues = " << alternativeOptionsValues
281 <<
", m_env.optionsInputFileName() = " << m_env.optionsInputFileName()
282 <<
", m_initialProposalCovMatrix = " << m_initialProposalCovMatrix
286 queso_require_equal_to_msg(sourceRv.imageSet().vectorSpace().dimLocal(), initialPosition.sizeLocal(),
"'sourceRv' and 'initialPosition' should have equal dimensions");
288 if (inputProposalCovMatrix) {
289 queso_require_equal_to_msg(sourceRv.imageSet().vectorSpace().dimLocal(), inputProposalCovMatrix->numRowsLocal(),
"'sourceRv' and 'inputProposalCovMatrix' should have equal dimensions");
290 queso_require_equal_to_msg(inputProposalCovMatrix->numCols(), inputProposalCovMatrix->numRowsGlobal(),
"'inputProposalCovMatrix' should be a square matrix");
295 if ((m_env.subDisplayFile() ) &&
296 (m_optionsObj->m_totallyMute ==
false)) {
297 *m_env.subDisplayFile() <<
"Leaving MetropolisHastingsSG<P_V,P_M>::constructor(2)"
302 template<
class P_V,
class P_M>
306 const P_V& initialPosition,
307 const P_M* inputProposalCovMatrix)
309 m_env (sourceRv.env()),
310 m_vectorSpace (sourceRv.imageSet().vectorSpace()),
311 m_targetPdf (sourceRv.pdf()),
312 m_initialPosition (initialPosition),
313 m_initialProposalCovMatrix (m_vectorSpace.zeroVector()),
314 m_nullInputProposalCovMatrix(inputProposalCovMatrix == NULL),
315 m_numDisabledParameters (0),
316 m_parameterEnabledStatus (m_vectorSpace.dimLocal(),true),
320 m_positionIdForDebugging (0),
321 m_stageIdForDebugging (0),
322 m_idsOfUniquePositions (0),
324 m_alphaQuotients (0),
327 m_lastAdaptedCovMatrix (),
328 m_computeInitialPriorAndLikelihoodValues(true),
329 m_initialLogPriorValue (0.),
330 m_initialLogLikelihoodValue (0.),
331 m_userDidNotProvideOptions(true),
332 m_latestDirtyCovMatrixIteration(0)
344 if (inputProposalCovMatrix != NULL) {
368 template<
class P_V,
class P_M>
372 const P_V& initialPosition,
373 double initialLogPrior,
374 double initialLogLikelihood,
375 const P_M* inputProposalCovMatrix)
377 m_env (sourceRv.env()),
378 m_vectorSpace (sourceRv.imageSet().vectorSpace()),
379 m_targetPdf (sourceRv.pdf()),
380 m_initialPosition (initialPosition),
381 m_initialProposalCovMatrix (m_vectorSpace.zeroVector()),
382 m_nullInputProposalCovMatrix(inputProposalCovMatrix == NULL),
383 m_numDisabledParameters (0),
384 m_parameterEnabledStatus (m_vectorSpace.dimLocal(),true),
388 m_positionIdForDebugging (0),
389 m_stageIdForDebugging (0),
390 m_idsOfUniquePositions (0),
392 m_alphaQuotients (0),
395 m_lastAdaptedCovMatrix (),
396 m_computeInitialPriorAndLikelihoodValues(false),
397 m_initialLogPriorValue (initialLogPrior),
398 m_initialLogLikelihoodValue (initialLogLikelihood),
399 m_userDidNotProvideOptions(true),
400 m_latestDirtyCovMatrixIteration(0)
412 if (inputProposalCovMatrix != NULL) {
436 template<
class P_V,
class P_M>
445 m_rawChainInfo.reset();
446 m_alphaQuotients.clear();
447 m_logTargets.clear();
448 m_numDisabledParameters = 0;
449 m_parameterEnabledStatus.clear();
450 m_positionIdForDebugging = 0;
451 m_stageIdForDebugging = 0;
452 m_idsOfUniquePositions.clear();
461 template<
class P_V,
class P_M>
468 if ((m_env.subDisplayFile() ) &&
469 (m_optionsObj->m_totallyMute ==
false)) {
470 *m_env.subDisplayFile() <<
"Entering MetropolisHastingsSG<P_V,P_M>::commonConstructor()"
474 if (m_optionsObj->m_initialPositionDataInputFileName !=
".") {
475 std::set<unsigned int> tmpSet;
476 tmpSet.insert(m_env.subId());
477 m_initialPosition.subReadContents((m_optionsObj->m_initialPositionDataInputFileName+
"_sub"+m_env.subIdString()),
478 m_optionsObj->m_initialPositionDataInputFileType,
480 if ((m_env.subDisplayFile() ) &&
481 (m_optionsObj->m_totallyMute ==
false)) {
482 *m_env.subDisplayFile() <<
"In MetropolisHastingsSG<P_V,P_M>::commonConstructor()"
483 <<
": just read initial position contents = " << m_initialPosition
488 if (m_optionsObj->m_parameterDisabledSet.size() > 0) {
489 for (std::set<unsigned int>::iterator setIt = m_optionsObj->m_parameterDisabledSet.begin(); setIt != m_optionsObj->m_parameterDisabledSet.end(); ++setIt) {
490 unsigned int paramId = *setIt;
491 if (paramId < m_vectorSpace.dimLocal()) {
492 m_numDisabledParameters++;
493 m_parameterEnabledStatus[paramId] =
false;
498 std::vector<double> drScalesAll(m_optionsObj->m_drScalesForExtraStages.size()+1,1.);
499 for (
unsigned int i = 1; i < (m_optionsObj->m_drScalesForExtraStages.size()+1); ++i) {
500 drScalesAll[i] = m_optionsObj->m_drScalesForExtraStages[i-1];
504 if (m_optionsObj->m_doLogitTransform != UQ_MH_SG_DO_LOGIT_TRANSFORM) {
506 msg =
"The doLogitTransform option is deprecated. ";
507 msg +=
"Use both ip_mh_algorithm and ip_mh_tk instead.";
508 queso_warning(msg.c_str());
511 if (m_optionsObj->m_initialProposalCovMatrixDataInputFileName !=
".") {
512 std::set<unsigned int> tmpSet;
513 tmpSet.insert(m_env.subId());
514 m_initialProposalCovMatrix.subReadContents((m_optionsObj->m_initialProposalCovMatrixDataInputFileName+
"_sub"+m_env.subIdString()),
515 m_optionsObj->m_initialProposalCovMatrixDataInputFileType,
517 if ((m_env.subDisplayFile() ) &&
518 (m_optionsObj->m_totallyMute ==
false)) {
519 *m_env.subDisplayFile() <<
"In MetropolisHastingsSG<P_V,P_M>::commonConstructor()"
520 <<
": just read initial proposal cov matrix contents = " << m_initialProposalCovMatrix
525 queso_require_msg(!(m_nullInputProposalCovMatrix),
"proposal cov matrix should have been passed by user, since, according to the input algorithm options, local Hessians will not be used in the proposal");
531 if ((m_optionsObj->m_algorithm ==
"logit_random_walk") &&
532 (m_optionsObj->m_tk ==
"logit_random_walk") &&
533 m_optionsObj->m_doLogitTransform) {
535 transformInitialCovMatrixToGaussianSpace(
559 template<
class P_V,
class P_M>
563 const std::vector<unsigned int >& inputTKStageIds)
571 unsigned int inputSize = inputPositionsData.size();
572 if ((m_env.subDisplayFile() ) &&
573 (m_env.displayVerbosity() >= 10 ) &&
574 (m_optionsObj->m_totallyMute ==
false)) {
575 *m_env.subDisplayFile() <<
"Entering MetropolisHastingsSG<P_V,P_M>::alpha(vec)"
576 <<
", inputSize = " << inputSize
579 queso_require_greater_equal_msg(inputSize, 2,
"inputPositionsData has size < 2");
580 queso_require_equal_to_msg(inputSize, inputPositionsData.size(),
"inputPositionsData and inputTKStageIds have different lengths");
583 if (inputPositionsData[0 ]->outOfTargetSupport())
return 0.;
584 if (inputPositionsData[inputSize-1]->outOfTargetSupport())
return 0.;
586 if ((inputPositionsData[0]->logTarget() == -INFINITY ) ||
587 (inputPositionsData[0]->logTarget() == INFINITY ) ||
588 (
queso_isnan(inputPositionsData[0]->logTarget()) )) {
589 std::cerr <<
"WARNING In MetropolisHastingsSG<P_V,P_M>::alpha(vec)"
591 <<
", fullRank " << m_env.fullRank()
592 <<
", subEnvironment " << m_env.subId()
593 <<
", subRank " << m_env.subRank()
594 <<
", inter0Rank " << m_env.inter0Rank()
595 <<
", positionId = " << m_positionIdForDebugging
596 <<
", stageId = " << m_stageIdForDebugging
597 <<
": inputSize = " << inputSize
598 <<
", inputPositionsData[0]->logTarget() = " << inputPositionsData[0]->logTarget()
599 <<
", [0]->values() = " << inputPositionsData[0]->vecValues()
600 <<
", [inputSize - 1]->values() = " << inputPositionsData[inputSize-1]->vecValues()
604 else if ((inputPositionsData[inputSize - 1]->logTarget() == -INFINITY ) ||
605 (inputPositionsData[inputSize - 1]->logTarget() == INFINITY ) ||
606 (
queso_isnan(inputPositionsData[inputSize - 1]->logTarget()) )) {
607 std::cerr <<
"WARNING In MetropolisHastingsSG<P_V,P_M>::alpha(vec)"
608 <<
", worldRank " << m_env.worldRank()
609 <<
", fullRank " << m_env.fullRank()
610 <<
", subEnvironment " << m_env.subId()
611 <<
", subRank " << m_env.subRank()
612 <<
", inter0Rank " << m_env.inter0Rank()
613 <<
", positionId = " << m_positionIdForDebugging
614 <<
", stageId = " << m_stageIdForDebugging
615 <<
": inputSize = " << inputSize
616 <<
", inputPositionsData[inputSize - 1]->logTarget() = " << inputPositionsData[inputSize-1]->logTarget()
617 <<
", [0]->values() = " << inputPositionsData[0]->vecValues()
618 <<
", [inputSize - 1]->values() = " << inputPositionsData[inputSize-1]->vecValues()
624 if (inputSize == 2) {
625 const P_V & tk_pos_x = m_tk->preComputingPosition(inputTKStageIds[inputSize-1]);
626 const P_V & tk_pos_y = m_tk->preComputingPosition(inputTKStageIds[0]);
627 return this->m_algorithm->acceptance_ratio(
628 *(inputPositionsData[0]),
629 *(inputPositionsData[inputSize - 1]),
635 std::vector<MarkovChainPositionData<P_V>*> positionsData (inputSize,NULL);
636 std::vector<MarkovChainPositionData<P_V>*> backwardPositionsData (inputSize,NULL);
638 std::vector<unsigned int > tkStageIds (inputSize,0);
639 std::vector<unsigned int > backwardTKStageIds (inputSize,0);
641 std::vector<unsigned int > tkStageIdsLess1(inputSize,0);
642 std::vector<unsigned int > backwardTKStageIdsLess1(inputSize,0);
644 for (
unsigned int i = 0; i < inputSize; ++i) {
645 positionsData [i] = inputPositionsData[i];
646 backwardPositionsData [i] = inputPositionsData[inputSize-i-1];
648 tkStageIds [i] = inputTKStageIds [i];
649 backwardTKStageIds [i] = inputTKStageIds [inputSize-i-1];
651 tkStageIdsLess1[i] = inputTKStageIds [i];
652 backwardTKStageIdsLess1[i] = inputTKStageIds [inputSize-i-1];
655 tkStageIdsLess1.pop_back();
656 backwardTKStageIdsLess1.pop_back();
659 double logNumerator = 0.;
660 double logDenominator = 0.;
661 double alphasNumerator = 1.;
662 double alphasDenominator = 1.;
665 const P_V& _lastTKPosition = m_tk->preComputingPosition( tkStageIds[inputSize-1]);
666 const P_V& _lastBackwardTKPosition = m_tk->preComputingPosition(backwardTKStageIds[inputSize-1]);
668 double numContrib = m_tk->rv(backwardTKStageIdsLess1).pdf().lnValue(_lastBackwardTKPosition);
670 double denContrib = m_tk->rv(tkStageIdsLess1).pdf().lnValue(_lastTKPosition);
671 if ((m_env.subDisplayFile() ) &&
672 (m_env.displayVerbosity() >= 10 ) &&
673 (m_optionsObj->m_totallyMute ==
false)) {
674 *m_env.subDisplayFile() <<
"In MetropolisHastingsSG<P_V,P_M>::alpha(vec)"
675 <<
", inputSize = " << inputSize
677 <<
": numContrib = " << numContrib
678 <<
", denContrib = " << denContrib
682 logNumerator += numContrib;
683 logDenominator += denContrib;
685 for (
unsigned int i = 0; i < (inputSize-2); ++i) {
686 positionsData.pop_back();
687 backwardPositionsData.pop_back();
689 const P_V& lastTKPosition = m_tk->preComputingPosition( tkStageIds[inputSize-2-i]);
690 const P_V& lastBackwardTKPosition = m_tk->preComputingPosition(backwardTKStageIds[inputSize-2-i]);
692 tkStageIds.pop_back();
693 backwardTKStageIds.pop_back();
695 tkStageIdsLess1.pop_back();
696 backwardTKStageIdsLess1.pop_back();
698 numContrib = m_tk->rv(backwardTKStageIdsLess1).pdf().lnValue(lastBackwardTKPosition);
700 denContrib = m_tk->rv(tkStageIdsLess1).pdf().lnValue(lastTKPosition);
701 if ((m_env.subDisplayFile() ) &&
702 (m_env.displayVerbosity() >= 10 ) &&
703 (m_optionsObj->m_totallyMute ==
false)) {
704 *m_env.subDisplayFile() <<
"In MetropolisHastingsSG<P_V,P_M>::alpha(vec)"
705 <<
", inputSize = " << inputSize
706 <<
", in loop, i = " << i
707 <<
": numContrib = " << numContrib
708 <<
", denContrib = " << denContrib
712 logNumerator += numContrib;
713 logDenominator += denContrib;
715 alphasNumerator *= (1. - this->alpha(backwardPositionsData,backwardTKStageIds));
716 alphasDenominator *= (1. - this->alpha( positionsData, tkStageIds));
719 double numeratorLogTargetToUse = backwardPositionsData[0]->logTarget();
720 numContrib = numeratorLogTargetToUse;
721 denContrib = positionsData[0]->logTarget();
722 if ((m_env.subDisplayFile() ) &&
723 (m_env.displayVerbosity() >= 10 ) &&
724 (m_optionsObj->m_totallyMute ==
false)) {
725 *m_env.subDisplayFile() <<
"In MetropolisHastingsSG<P_V,P_M>::alpha(vec)"
726 <<
", inputSize = " << inputSize
728 <<
": numContrib = " << numContrib
729 <<
", denContrib = " << denContrib
732 logNumerator += numContrib;
733 logDenominator += denContrib;
735 if ((m_env.subDisplayFile() ) &&
736 (m_env.displayVerbosity() >= 10 ) &&
737 (m_optionsObj->m_totallyMute ==
false)) {
738 *m_env.subDisplayFile() <<
"Leaving MetropolisHastingsSG<P_V,P_M>::alpha(vec)"
739 <<
", inputSize = " << inputSize
740 <<
": alphasNumerator = " << alphasNumerator
741 <<
", alphasDenominator = " << alphasDenominator
742 <<
", logNumerator = " << logNumerator
743 <<
", logDenominator = " << logDenominator
748 return std::min(1.,(alphasNumerator/alphasDenominator)*std::exp(logNumerator-logDenominator));
751 template<
class P_V,
class P_M>
757 if (alpha <= 0. ) result =
false;
758 else if (alpha >= 1. ) result =
true;
759 else if (alpha >= m_env.rngObject()->uniformSample()) result =
true;
765 template<
class P_V,
class P_M>
769 std::ofstream& ofsvar)
const
771 if ((m_env.subDisplayFile() ) &&
772 (m_optionsObj->m_totallyMute ==
false)) {
773 *m_env.subDisplayFile() <<
"\n"
774 <<
"\n-----------------------------------------------------"
775 <<
"\n Writing more information about the Markov chain " << workingChain.
name() <<
" to output file ..."
776 <<
"\n-----------------------------------------------------"
783 if (m_optionsObj->m_rawChainGenerateExtra) {
785 ofsvar << m_optionsObj->m_prefix <<
"logTargets_sub" << m_env.subIdString() <<
" = zeros(" << m_logTargets.size()
789 ofsvar << m_optionsObj->m_prefix <<
"logTargets_sub" << m_env.subIdString() <<
" = [";
790 for (
unsigned int i = 0; i < m_logTargets.size(); ++i) {
791 ofsvar << m_logTargets[i]
797 ofsvar << m_optionsObj->m_prefix <<
"alphaQuotients_sub" << m_env.subIdString() <<
" = zeros(" << m_alphaQuotients.size()
801 ofsvar << m_optionsObj->m_prefix <<
"alphaQuotients_sub" << m_env.subIdString() <<
" = [";
802 for (
unsigned int i = 0; i < m_alphaQuotients.size(); ++i) {
803 ofsvar << m_alphaQuotients[i]
810 ofsvar << m_optionsObj->m_prefix <<
"rejected = " << (double) m_rawChainInfo.numRejections/(
double) (workingChain.
subSequenceSize()-1)
816 ofsvar << m_optionsObj->m_prefix <<
"componentNames = {";
817 m_vectorSpace.printComponentsNames(ofsvar,
false);
821 ofsvar << m_optionsObj->m_prefix <<
"outTargetSupport = " << (double) m_rawChainInfo.numOutOfTargetSupport/(
double) (workingChain.
subSequenceSize()-1)
826 ofsvar << m_optionsObj->m_prefix <<
"runTime = " << m_rawChainInfo.runTime
831 if ((m_env.subDisplayFile() ) &&
832 (m_optionsObj->m_totallyMute ==
false)) {
833 *m_env.subDisplayFile() <<
"\n-----------------------------------------------------"
834 <<
"\n Finished writing more information about the Markov chain " << workingChain.
name()
835 <<
"\n-----------------------------------------------------"
843 template <
class P_V,
class P_M>
857 template <
class P_V,
class P_M>
864 if ((m_env.subDisplayFile() ) &&
865 (m_env.displayVerbosity() >= 5 ) &&
866 (m_optionsObj->m_totallyMute ==
false)) {
867 *m_env.subDisplayFile() <<
"Entering MetropolisHastingsSG<P_V,P_M>::generateSequence()..."
872 std::cerr <<
"'m_vectorSpace' and 'workingChain' are related to vector"
873 <<
"spaces of different dimensions"
879 bool writeLogLikelihood;
880 if ((workingLogLikelihoodValues != NULL) &&
881 (m_optionsObj->m_outputLogLikelihood)) {
882 writeLogLikelihood =
true;
885 writeLogLikelihood =
false;
890 if ((workingLogTargetValues != NULL) &&
891 (m_optionsObj->m_outputLogTarget)) {
892 writeLogTarget =
true;
895 writeLogTarget =
false;
898 MiscCheckTheParallelEnvironment<P_V,P_V>(m_initialPosition,
901 P_V valuesOf1stPosition(m_initialPosition);
904 workingChain.
setName(m_optionsObj->m_prefix +
"rawChain");
909 if (m_optionsObj->m_rawChainDataInputFileName == UQ_MH_SG_FILENAME_FOR_NO_FILE) {
910 generateFullChain(valuesOf1stPosition,
911 m_optionsObj->m_rawChainSize,
913 workingLogLikelihoodValues,
914 workingLogTargetValues);
917 readFullChain(m_optionsObj->m_rawChainDataInputFileName,
918 m_optionsObj->m_rawChainDataInputFileType,
919 m_optionsObj->m_rawChainSize,
926 if ((m_env.subDisplayFile() ) &&
927 (m_optionsObj->m_totallyMute ==
false)) {
928 *m_env.subDisplayFile() <<
"In MetropolisHastingsSG<P_V,P_M>::generateSequence()"
929 <<
", prefix = " << m_optionsObj->m_prefix
930 <<
", chain name = " << workingChain.
name()
931 <<
": about to try to open generic output file '" << m_optionsObj->m_dataOutputFileName
932 <<
"." << UQ_FILE_EXTENSION_FOR_MATLAB_FORMAT
933 <<
"', subId = " << m_env.subId()
934 <<
", subenv is allowed to write (1/true or 0/false) = " << (m_optionsObj->m_dataOutputAllowedSet.find(m_env.subId()) != m_optionsObj->m_dataOutputAllowedSet.end())
940 m_env.openOutputFile(m_optionsObj->m_dataOutputFileName,
941 UQ_FILE_EXTENSION_FOR_MATLAB_FORMAT,
942 m_optionsObj->m_dataOutputAllowedSet,
946 if ((m_env.subDisplayFile() ) &&
947 (m_optionsObj->m_totallyMute ==
false)) {
948 *m_env.subDisplayFile() <<
"In MetropolisHastingsSG<P_V,P_M>::generateSequence()"
949 <<
", prefix = " << m_optionsObj->m_prefix
950 <<
", raw chain name = " << workingChain.
name()
951 <<
": returned from opening generic output file '" << m_optionsObj->m_dataOutputFileName
952 <<
"." << UQ_FILE_EXTENSION_FOR_MATLAB_FORMAT
953 <<
"', subId = " << m_env.subId()
962 if ((m_optionsObj->m_rawChainDataOutputFileName != UQ_MH_SG_FILENAME_FOR_NO_FILE) &&
963 (m_optionsObj->m_totallyMute ==
false )) {
966 if ((m_env.subDisplayFile() ) &&
967 (m_optionsObj->m_totallyMute ==
false)) {
968 *m_env.subDisplayFile() <<
"In MetropolisHastingsSG<P_V,P_M>::generateSequence()"
969 <<
", prefix = " << m_optionsObj->m_prefix
970 <<
", raw chain name = " << workingChain.
name()
971 <<
": about to try to write raw sub chain output file '" << m_optionsObj->m_rawChainDataOutputFileName
972 <<
"." << m_optionsObj->m_rawChainDataOutputFileType
973 <<
"', subId = " << m_env.subId()
974 <<
", subenv is allowed to write 1/true or 0/false) = " << (m_optionsObj->m_rawChainDataOutputAllowedSet.find(m_env.subId()) != m_optionsObj->m_rawChainDataOutputAllowedSet.end())
979 if ((m_numPositionsNotSubWritten > 0 ) &&
980 (m_optionsObj->m_rawChainDataOutputFileName !=
".")) {
981 workingChain.
subWriteContents(m_optionsObj->m_rawChainSize - m_numPositionsNotSubWritten,
982 m_numPositionsNotSubWritten,
983 m_optionsObj->m_rawChainDataOutputFileName,
984 m_optionsObj->m_rawChainDataOutputFileType,
985 m_optionsObj->m_rawChainDataOutputAllowedSet);
986 if ((m_env.subDisplayFile() ) &&
987 (m_optionsObj->m_totallyMute ==
false)) {
988 *m_env.subDisplayFile() <<
"In MetropolisHastingsSG<P_V,P_M>::generateSequence()"
989 <<
": just wrote (per period request) remaining " << m_numPositionsNotSubWritten <<
" chain positions "
990 <<
", " << m_optionsObj->m_rawChainSize - m_numPositionsNotSubWritten <<
" <= pos <= " << m_optionsObj->m_rawChainSize - 1
994 if (writeLogLikelihood) {
995 workingLogLikelihoodValues->
subWriteContents(m_optionsObj->m_rawChainSize - m_numPositionsNotSubWritten,
996 m_numPositionsNotSubWritten,
997 m_optionsObj->m_rawChainDataOutputFileName +
"_loglikelihood",
998 m_optionsObj->m_rawChainDataOutputFileType,
999 m_optionsObj->m_rawChainDataOutputAllowedSet);
1002 if (writeLogTarget) {
1003 workingLogTargetValues->
subWriteContents(m_optionsObj->m_rawChainSize - m_numPositionsNotSubWritten,
1004 m_numPositionsNotSubWritten,
1005 m_optionsObj->m_rawChainDataOutputFileName +
"_logtarget",
1006 m_optionsObj->m_rawChainDataOutputFileType,
1007 m_optionsObj->m_rawChainDataOutputAllowedSet);
1010 m_numPositionsNotSubWritten = 0;
1014 if (workingLogLikelihoodValues) {
1017 rawSubMLEpositions);
1020 if ((m_env.subDisplayFile() ) &&
1021 (m_optionsObj->m_totallyMute ==
false)) {
1022 P_V tmpVec(m_vectorSpace.zeroVector());
1024 *m_env.subDisplayFile() <<
"In MetropolisHastingsSG<P_V,P_M>::generateSequence()"
1025 <<
": just computed MLE"
1026 <<
", rawSubMLEvalue = " << rawSubMLEvalue
1027 <<
", rawSubMLEpositions.subSequenceSize() = " << rawSubMLEpositions.
subSequenceSize()
1028 <<
", rawSubMLEpositions[0] = " << tmpVec
1034 if (workingLogTargetValues) {
1037 rawSubMAPpositions);
1040 if ((m_env.subDisplayFile() ) &&
1041 (m_optionsObj->m_totallyMute ==
false)) {
1042 P_V tmpVec(m_vectorSpace.zeroVector());
1044 *m_env.subDisplayFile() <<
"In MetropolisHastingsSG<P_V,P_M>::generateSequence()"
1045 <<
": just computed MAP"
1046 <<
", rawSubMAPvalue = " << rawSubMAPvalue
1047 <<
", rawSubMAPpositions.subSequenceSize() = " << rawSubMAPpositions.
subSequenceSize()
1048 <<
", rawSubMAPpositions[0] = " << tmpVec
1053 if ((m_env.subDisplayFile() ) &&
1054 (m_optionsObj->m_totallyMute ==
false)) {
1055 *m_env.subDisplayFile() <<
"In MetropolisHastingsSG<P_V,P_M>::generateSequence()"
1056 <<
", prefix = " << m_optionsObj->m_prefix
1057 <<
", raw chain name = " << workingChain.
name()
1058 <<
": returned from writing raw sub chain output file '" << m_optionsObj->m_rawChainDataOutputFileName
1059 <<
"." << m_optionsObj->m_rawChainDataOutputFileType
1060 <<
"', subId = " << m_env.subId()
1065 if ((m_env.subDisplayFile() ) &&
1066 (m_optionsObj->m_totallyMute ==
false)) {
1067 *m_env.subDisplayFile() <<
"In MetropolisHastingsSG<P_V,P_M>::generateSequence()"
1068 <<
", prefix = " << m_optionsObj->m_prefix
1069 <<
", raw chain name = " << workingChain.
name()
1070 <<
": about to try to write raw unified chain output file '" << m_optionsObj->m_rawChainDataOutputFileName
1071 <<
"." << m_optionsObj->m_rawChainDataOutputFileType
1072 <<
"', subId = " << m_env.subId()
1078 m_optionsObj->m_rawChainDataOutputFileType);
1079 if ((m_env.subDisplayFile() ) &&
1080 (m_optionsObj->m_totallyMute ==
false)) {
1081 *m_env.subDisplayFile() <<
"In MetropolisHastingsSG<P_V,P_M>::generateSequence()"
1082 <<
", prefix = " << m_optionsObj->m_prefix
1083 <<
", raw chain name = " << workingChain.
name()
1084 <<
": returned from writing raw unified chain output file '" << m_optionsObj->m_rawChainDataOutputFileName
1085 <<
"." << m_optionsObj->m_rawChainDataOutputFileType
1086 <<
"', subId = " << m_env.subId()
1090 if (writeLogLikelihood) {
1091 workingLogLikelihoodValues->
unifiedWriteContents(m_optionsObj->m_rawChainDataOutputFileName +
"_loglikelihood",
1092 m_optionsObj->m_rawChainDataOutputFileType);
1095 if (writeLogTarget) {
1096 workingLogTargetValues->
unifiedWriteContents(m_optionsObj->m_rawChainDataOutputFileName +
"_logtarget",
1097 m_optionsObj->m_rawChainDataOutputFileType);
1101 if (workingLogLikelihoodValues && (m_env.subRank() == 0)) {
1105 rawUnifiedMLEpositions);
1107 if ((m_env.subDisplayFile() ) &&
1108 (m_optionsObj->m_totallyMute ==
false)) {
1109 P_V tmpVec(m_vectorSpace.zeroVector());
1115 *m_env.subDisplayFile() <<
"In MetropolisHastingsSG<P_V,P_M>::generateSequence()"
1116 <<
": just computed MLE"
1117 <<
", rawUnifiedMLEvalue = " << rawUnifiedMLEvalue
1118 <<
", rawUnifiedMLEpositions.subSequenceSize() = " << rawUnifiedMLEpositions.
subSequenceSize()
1119 <<
", rawUnifiedMLEpositions[0] = " << tmpVec
1126 if (workingLogTargetValues && (m_env.subRank() == 0)) {
1129 rawUnifiedMAPpositions);
1131 if ((m_env.subDisplayFile() ) &&
1132 (m_optionsObj->m_totallyMute ==
false)) {
1133 P_V tmpVec(m_vectorSpace.zeroVector());
1139 *m_env.subDisplayFile() <<
"In MetropolisHastingsSG<P_V,P_M>::generateSequence()"
1140 <<
": just computed MAP"
1141 <<
", rawUnifiedMAPvalue = " << rawUnifiedMAPvalue
1142 <<
", rawUnifiedMAPpositions.subSequenceSize() = " << rawUnifiedMAPpositions.
subSequenceSize()
1143 <<
", rawUnifiedMAPpositions[0] = " << tmpVec
1151 if ((genericFilePtrSet.
ofsVar ) &&
1152 (m_optionsObj->m_totallyMute ==
false)) {
1154 iRC = writeInfo(workingChain,
1155 *genericFilePtrSet.
ofsVar);
1156 queso_require_msg(!(iRC),
"improper writeInfo() return");
1159 #ifdef QUESO_USES_SEQUENCE_STATISTICAL_OPTIONS
1160 if (m_optionsObj->m_rawChainComputeStats) {
1162 genericFilePtrSet.
ofsVar);
1172 if (m_optionsObj->m_filteredChainGenerate) {
1174 unsigned int filterInitialPos = (
unsigned int) (m_optionsObj->m_filteredChainDiscardedPortion * (
double) workingChain.
subSequenceSize());
1175 unsigned int filterSpacing = m_optionsObj->m_filteredChainLag;
1176 if (filterSpacing == 0) {
1183 workingChain.
filter(filterInitialPos,
1185 workingChain.
setName(m_optionsObj->m_prefix +
"filtChain");
1187 if (workingLogLikelihoodValues) workingLogLikelihoodValues->
filter(filterInitialPos,
1190 if (workingLogTargetValues) workingLogTargetValues->
filter(filterInitialPos,
1194 if ((m_env.subDisplayFile() ) &&
1195 (m_optionsObj->m_totallyMute ==
false)) {
1196 *m_env.subDisplayFile() <<
"In MetropolisHastingsSG<P_V,P_M>::generateSequence()"
1197 <<
", prefix = " << m_optionsObj->m_prefix
1198 <<
": checking necessity of opening output files for filtered chain " << workingChain.
name()
1204 if ((m_optionsObj->m_filteredChainDataOutputFileName != UQ_MH_SG_FILENAME_FOR_NO_FILE) &&
1205 (m_optionsObj->m_totallyMute ==
false )) {
1208 m_optionsObj->m_filteredChainDataOutputFileName,
1209 m_optionsObj->m_filteredChainDataOutputFileType,
1210 m_optionsObj->m_filteredChainDataOutputAllowedSet);
1211 if ((m_env.subDisplayFile() ) &&
1212 (m_optionsObj->m_totallyMute ==
false)) {
1213 *m_env.subDisplayFile() <<
"In MetropolisHastingsSG<P_V,P_M>::generateSequence()"
1214 <<
", prefix = " << m_optionsObj->m_prefix
1215 <<
": closed sub output file '" << m_optionsObj->m_filteredChainDataOutputFileName
1216 <<
"' for filtered chain " << workingChain.
name()
1220 if (writeLogLikelihood) {
1223 m_optionsObj->m_filteredChainDataOutputFileName +
"_loglikelihood",
1224 m_optionsObj->m_filteredChainDataOutputFileType,
1225 m_optionsObj->m_filteredChainDataOutputAllowedSet);
1228 if (writeLogTarget) {
1231 m_optionsObj->m_filteredChainDataOutputFileName +
"_logtarget",
1232 m_optionsObj->m_filteredChainDataOutputFileType,
1233 m_optionsObj->m_filteredChainDataOutputAllowedSet);
1240 if ((m_optionsObj->m_filteredChainDataOutputFileName != UQ_MH_SG_FILENAME_FOR_NO_FILE) &&
1241 (m_optionsObj->m_totallyMute == false )) {
1243 m_optionsObj->m_filteredChainDataOutputFileType);
1244 if ((m_env.subDisplayFile() ) &&
1245 (m_optionsObj->m_totallyMute ==
false)) {
1246 *m_env.subDisplayFile() <<
"In MetropolisHastingsSG<P_V,P_M>::generateSequence()"
1247 <<
", prefix = " << m_optionsObj->m_prefix
1248 <<
": closed unified output file '" << m_optionsObj->m_filteredChainDataOutputFileName
1249 <<
"' for filtered chain " << workingChain.
name()
1253 if (writeLogLikelihood) {
1254 workingLogLikelihoodValues->
unifiedWriteContents(m_optionsObj->m_filteredChainDataOutputFileName +
"_loglikelihood",
1255 m_optionsObj->m_filteredChainDataOutputFileType);
1258 if (writeLogTarget) {
1259 workingLogTargetValues->
unifiedWriteContents(m_optionsObj->m_filteredChainDataOutputFileName +
"_logtarget",
1260 m_optionsObj->m_filteredChainDataOutputFileType);
1267 #ifdef QUESO_USES_SEQUENCE_STATISTICAL_OPTIONS
1268 if (m_optionsObj->m_filteredChainComputeStats) {
1269 workingChain.
computeStatistics(*m_optionsObj->m_filteredChainStatisticalOptionsObj,
1270 genericFilePtrSet.
ofsVar);
1278 if (genericFilePtrSet.
ofsVar) {
1280 delete genericFilePtrSet.
ofsVar;
1281 if ((m_env.subDisplayFile() ) &&
1282 (m_optionsObj->m_totallyMute ==
false)) {
1283 *m_env.subDisplayFile() <<
"In MetropolisHastingsSG<P_V,P_M>::generateSequence()"
1284 <<
", prefix = " << m_optionsObj->m_prefix
1285 <<
": closed generic output file '" << m_optionsObj->m_dataOutputFileName
1286 <<
"' (chain name is " << workingChain.
name()
1292 if ((m_env.subDisplayFile() ) &&
1293 (m_optionsObj->m_totallyMute ==
false)) {
1294 *m_env.subDisplayFile() << std::endl;
1299 if ((m_env.subDisplayFile() ) &&
1300 (m_env.displayVerbosity() >= 5 ) &&
1301 (m_optionsObj->m_totallyMute ==
false)) {
1302 *m_env.subDisplayFile() <<
"Leaving MetropolisHastingsSG<P_V,P_M>::generateSequence()"
1310 template<
class P_V,
class P_M>
1314 info = m_rawChainInfo;
1318 template <
class P_V,
class P_M>
1321 const std::string& inputFileName,
1322 const std::string& inputFileType,
1323 unsigned int chainSize,
1330 template <
class P_V,
class P_M>
1333 const P_V& valuesOf1stPosition,
1334 unsigned int chainSize,
1341 if ((m_env.subDisplayFile() ) &&
1342 (m_optionsObj->m_totallyMute ==
false)) {
1343 *m_env.subDisplayFile() <<
"Starting the generation of Markov chain " << workingChain.
name()
1344 <<
", with " << chainSize
1350 struct timeval timevalChain;
1351 struct timeval timevalCandidate;
1352 struct timeval timevalTarget;
1353 struct timeval timevalMhAlpha;
1355 m_positionIdForDebugging = 0;
1356 m_stageIdForDebugging = 0;
1358 m_rawChainInfo.reset();
1360 iRC = gettimeofday(&timevalChain, NULL);
1363 if ((m_env.subDisplayFile() ) &&
1364 (m_optionsObj->m_totallyMute ==
false)) {
1365 *m_env.subDisplayFile() <<
"\nIn MetropolisHastingsSG<P_V,P_M>::generateFullChain()"
1366 <<
": contents of initial position are:";
1367 *m_env.subDisplayFile() << valuesOf1stPosition;
1368 *m_env.subDisplayFile() <<
"\nIn MetropolisHastingsSG<P_V,P_M>::generateFullChain()"
1369 <<
": targetPdf.domaintSet() info is:"
1370 << m_targetPdf.domainSet();
1371 *m_env.subDisplayFile() << std::endl;
1375 bool writeLogLikelihood;
1376 if ((workingLogLikelihoodValues != NULL) &&
1377 (m_optionsObj->m_outputLogLikelihood)) {
1378 writeLogLikelihood =
true;
1381 writeLogLikelihood =
false;
1385 bool writeLogTarget;
1386 if ((workingLogTargetValues != NULL) &&
1387 (m_optionsObj->m_outputLogTarget)) {
1388 writeLogTarget =
true;
1391 writeLogTarget =
false;
1394 bool outOfTargetSupport = !m_targetPdf.domainSet().contains(valuesOf1stPosition);
1395 if ((m_env.subDisplayFile()) &&
1396 (outOfTargetSupport )) {
1397 *m_env.subDisplayFile() <<
"ERROR: In MetropolisHastingsSG<P_V,P_M>::generateFullChain()"
1398 <<
": contents of initial position are:\n";
1399 *m_env.subDisplayFile() << valuesOf1stPosition;
1400 *m_env.subDisplayFile() <<
"\nERROR: In MetropolisHastingsSG<P_V,P_M>::generateFullChain()"
1401 <<
": targetPdf.domaintSet() info is:\n"
1402 << m_targetPdf.domainSet();
1403 *m_env.subDisplayFile() << std::endl;
1405 queso_require_msg(!(outOfTargetSupport),
"initial position should not be out of target pdf support");
1407 double logPrior = 0.;
1408 double logLikelihood = 0.;
1409 double logTarget = 0.;
1410 if (m_computeInitialPriorAndLikelihoodValues) {
1411 if (m_optionsObj->m_rawChainMeasureRunTimes) {
1412 iRC = gettimeofday(&timevalTarget, NULL);
1415 logTarget = m_targetPdfSynchronizer->callFunction(&valuesOf1stPosition,&logPrior,&logLikelihood);
1416 if (m_optionsObj->m_rawChainMeasureRunTimes) {
1419 m_rawChainInfo.numTargetCalls++;
1420 if ((m_env.subDisplayFile() ) &&
1421 (m_env.displayVerbosity() >= 3 ) &&
1422 (m_optionsObj->m_totallyMute ==
false)) {
1423 *m_env.subDisplayFile() <<
"In MetropolisHastingsSG<P_V,P_M>::generateFullChain()"
1424 <<
": just returned from likelihood() for initial chain position"
1425 <<
", m_rawChainInfo.numTargetCalls = " << m_rawChainInfo.numTargetCalls
1426 <<
", logPrior = " << logPrior
1427 <<
", logLikelihood = " << logLikelihood
1428 <<
", logTarget = " << logTarget
1433 logPrior = m_initialLogPriorValue;
1434 logLikelihood = m_initialLogLikelihoodValue;
1435 logTarget = logPrior + logLikelihood;
1436 if ((m_env.subDisplayFile() ) &&
1437 (m_env.displayVerbosity() >= 3 ) &&
1438 (m_optionsObj->m_totallyMute ==
false)) {
1439 *m_env.subDisplayFile() <<
"In MetropolisHastingsSG<P_V,P_M>::generateFullChain()"
1440 <<
": used input prior and likelihood for initial chain position"
1441 <<
", m_rawChainInfo.numTargetCalls = " << m_rawChainInfo.numTargetCalls
1442 <<
", logPrior = " << logPrior
1443 <<
", logLikelihood = " << logLikelihood
1444 <<
", logTarget = " << logTarget
1451 valuesOf1stPosition,
1456 P_V gaussianVector(m_vectorSpace.zeroVector());
1457 P_V tmpVecValues(m_vectorSpace.zeroVector());
1464 m_numPositionsNotSubWritten = 0;
1465 if (workingLogLikelihoodValues) workingLogLikelihoodValues->
resizeSequence(chainSize);
1466 if (workingLogTargetValues ) workingLogTargetValues->
resizeSequence (chainSize);
1467 if (
true) m_idsOfUniquePositions.resize(chainSize,0);
1468 if (m_optionsObj->m_rawChainGenerateExtra) {
1469 m_logTargets.resize (chainSize,0.);
1470 m_alphaQuotients.resize(chainSize,0.);
1473 unsigned int uniquePos = 0;
1475 m_numPositionsNotSubWritten++;
1476 if ((m_optionsObj->m_rawChainDataOutputPeriod > 0 ) &&
1477 (((0+1) % m_optionsObj->m_rawChainDataOutputPeriod) == 0 ) &&
1478 (m_optionsObj->m_rawChainDataOutputFileName !=
".")) {
1479 workingChain.
subWriteContents(0 + 1 - m_optionsObj->m_rawChainDataOutputPeriod,
1480 m_optionsObj->m_rawChainDataOutputPeriod,
1481 m_optionsObj->m_rawChainDataOutputFileName,
1482 m_optionsObj->m_rawChainDataOutputFileType,
1483 m_optionsObj->m_rawChainDataOutputAllowedSet);
1484 if ((m_env.subDisplayFile() ) &&
1485 (m_optionsObj->m_totallyMute ==
false)) {
1486 *m_env.subDisplayFile() <<
"In MetropolisHastingsSG<P_V,P_M>::generateFullChain()"
1487 <<
": just wrote (per period request) " << m_numPositionsNotSubWritten <<
" chain positions "
1488 <<
", " << 0 + 1 - m_optionsObj->m_rawChainDataOutputPeriod <<
" <= pos <= " << 0
1492 if (writeLogLikelihood) {
1493 workingLogLikelihoodValues->
subWriteContents(0 + 1 - m_optionsObj->m_rawChainDataOutputPeriod,
1494 m_optionsObj->m_rawChainDataOutputPeriod,
1495 m_optionsObj->m_rawChainDataOutputFileName +
"_loglikelihood",
1496 m_optionsObj->m_rawChainDataOutputFileType,
1497 m_optionsObj->m_rawChainDataOutputAllowedSet);
1500 if (writeLogTarget) {
1501 workingLogTargetValues->
subWriteContents(0 + 1 - m_optionsObj->m_rawChainDataOutputPeriod,
1502 m_optionsObj->m_rawChainDataOutputPeriod,
1503 m_optionsObj->m_rawChainDataOutputFileName +
"_logtarget",
1504 m_optionsObj->m_rawChainDataOutputFileType,
1505 m_optionsObj->m_rawChainDataOutputAllowedSet);
1508 m_numPositionsNotSubWritten = 0;
1511 if (workingLogLikelihoodValues) (*workingLogLikelihoodValues)[0] = currentPositionData.
logLikelihood();
1512 if (workingLogTargetValues ) (*workingLogTargetValues )[0] = currentPositionData.
logTarget();
1513 if (
true) m_idsOfUniquePositions[uniquePos++] = 0;
1514 if (m_optionsObj->m_rawChainGenerateExtra) {
1515 m_logTargets [0] = currentPositionData.
logTarget();
1516 m_alphaQuotients[0] = 1.;
1520 if ((m_env.subDisplayFile() ) &&
1521 (m_env.displayVerbosity() >= 10 ) &&
1522 (m_optionsObj->m_totallyMute ==
false)) {
1523 *m_env.subDisplayFile() <<
"\n"
1524 <<
"\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
1534 if ((m_env.numSubEnvironments() < (
unsigned int) m_env.fullComm().NumProc()) &&
1535 (m_initialPosition.numOfProcsForStorage() == 1 ) &&
1536 (m_env.subRank() != 0 )) {
1539 aux = m_targetPdfSynchronizer->callFunction(NULL,
1543 for (
unsigned int positionId = 1; positionId < workingChain.
subSequenceSize(); ++positionId) {
1547 m_rawChainInfo.numRejections++;
1550 else for (
unsigned int positionId = 1; positionId < workingChain.
subSequenceSize(); ++positionId) {
1555 m_positionIdForDebugging = positionId;
1556 if ((m_env.subDisplayFile() ) &&
1557 (m_env.displayVerbosity() >= 3 ) &&
1558 (m_optionsObj->m_totallyMute ==
false)) {
1559 *m_env.subDisplayFile() <<
"In MetropolisHastingsSG<P_V,P_M>::generateFullChain()"
1560 <<
": beginning chain position of id = " << positionId
1561 <<
", m_optionsObj->m_drMaxNumExtraStages = " << m_optionsObj->m_drMaxNumExtraStages
1564 unsigned int stageId = 0;
1565 m_stageIdForDebugging = stageId;
1567 m_tk->clearPreComputingPositions();
1569 if ((m_env.subDisplayFile() ) &&
1570 (m_env.displayVerbosity() >= 5 ) &&
1571 (m_optionsObj->m_totallyMute ==
false)) {
1572 *m_env.subDisplayFile() <<
"In MetropolisHastingsSG<P_V,P_M>::generateFullChain()"
1573 <<
": about to set TK pre computing position of local id " << 0
1574 <<
", values = " << currentPositionData.
vecValues()
1577 bool validPreComputingPosition = m_tk->setPreComputingPosition(currentPositionData.
vecValues(),0);
1578 if ((m_env.subDisplayFile() ) &&
1579 (m_env.displayVerbosity() >= 5 ) &&
1580 (m_optionsObj->m_totallyMute ==
false)) {
1581 *m_env.subDisplayFile() <<
"In MetropolisHastingsSG<P_V,P_M>::generateFullChain()"
1582 <<
": returned from setting TK pre computing position of local id " << 0
1583 <<
", values = " << currentPositionData.
vecValues()
1584 <<
", valid = " << validPreComputingPosition
1587 queso_require_msg(validPreComputingPosition,
"initial position should not be an invalid pre computing position");
1593 bool keepGeneratingCandidates =
true;
1594 while (keepGeneratingCandidates) {
1595 if (m_optionsObj->m_rawChainMeasureRunTimes) {
1596 iRC = gettimeofday(&timevalCandidate, NULL);
1600 m_tk->rv(currentPositionData.
vecValues()).realizer().realization(tmpVecValues);
1602 if (m_numDisabledParameters > 0) {
1603 for (
unsigned int paramId = 0; paramId < m_vectorSpace.dimLocal(); ++paramId) {
1604 if (m_parameterEnabledStatus[paramId] ==
false) {
1605 tmpVecValues[paramId] = m_initialPosition[paramId];
1609 if (m_optionsObj->m_rawChainMeasureRunTimes) m_rawChainInfo.candidateRunTime +=
MiscGetEllapsedSeconds(&timevalCandidate);
1611 outOfTargetSupport = !m_targetPdf.domainSet().contains(tmpVecValues);
1613 bool displayDetail = (m_env.displayVerbosity() >= 10) || m_optionsObj->m_displayCandidates;
1614 if ((m_env.subDisplayFile() ) &&
1616 (m_optionsObj->m_totallyMute ==
false)) {
1617 *m_env.subDisplayFile() <<
"In MetropolisHastingsSG<P_V,P_M>::generateFullChain()"
1618 <<
": for chain position of id = " << positionId
1619 <<
", candidate = " << tmpVecValues
1620 <<
", outOfTargetSupport = " << outOfTargetSupport
1624 if (m_optionsObj->m_putOutOfBoundsInChain) keepGeneratingCandidates =
false;
1625 else keepGeneratingCandidates = outOfTargetSupport;
1628 if ((m_env.subDisplayFile() ) &&
1629 (m_env.displayVerbosity() >= 5 ) &&
1630 (m_optionsObj->m_totallyMute ==
false)) {
1631 *m_env.subDisplayFile() <<
"In MetropolisHastingsSG<P_V,P_M>::generateFullChain()"
1632 <<
": about to set TK pre computing position of local id " << stageId+1
1633 <<
", values = " << tmpVecValues
1636 validPreComputingPosition = m_tk->setPreComputingPosition(tmpVecValues,stageId+1);
1637 if ((m_env.subDisplayFile() ) &&
1638 (m_env.displayVerbosity() >= 5 ) &&
1639 (m_optionsObj->m_totallyMute ==
false)) {
1640 *m_env.subDisplayFile() <<
"In MetropolisHastingsSG<P_V,P_M>::generateFullChain()"
1641 <<
": returned from setting TK pre computing position of local id " << stageId+1
1642 <<
", values = " << tmpVecValues
1643 <<
", valid = " << validPreComputingPosition
1647 if (outOfTargetSupport) {
1648 m_rawChainInfo.numOutOfTargetSupport++;
1649 logPrior = -INFINITY;
1650 logLikelihood = -INFINITY;
1651 logTarget = -INFINITY;
1654 if (m_optionsObj->m_rawChainMeasureRunTimes) {
1655 iRC = gettimeofday(&timevalTarget, NULL);
1658 logTarget = m_targetPdfSynchronizer->callFunction(&tmpVecValues,&logPrior,&logLikelihood);
1659 if (m_optionsObj->m_rawChainMeasureRunTimes) m_rawChainInfo.targetRunTime +=
MiscGetEllapsedSeconds(&timevalTarget);
1660 m_rawChainInfo.numTargetCalls++;
1661 if ((m_env.subDisplayFile() ) &&
1662 (m_env.displayVerbosity() >= 3 ) &&
1663 (m_optionsObj->m_totallyMute ==
false)) {
1664 *m_env.subDisplayFile() <<
"In MetropolisHastingsSG<P_V,P_M>::generateFullChain()"
1665 <<
": just returned from likelihood() for chain position of id " << positionId
1666 <<
", m_rawChainInfo.numTargetCalls = " << m_rawChainInfo.numTargetCalls
1667 <<
", logPrior = " << logPrior
1668 <<
", logLikelihood = " << logLikelihood
1669 <<
", logTarget = " << logTarget
1673 currentCandidateData.set(tmpVecValues,
1678 if ((m_env.subDisplayFile() ) &&
1679 (m_env.displayVerbosity() >= 10 ) &&
1680 (m_optionsObj->m_totallyMute ==
false)) {
1681 *m_env.subDisplayFile() <<
"\n"
1682 <<
"\n-----------------------------------------------------------\n"
1686 bool accept =
false;
1687 double alphaFirstCandidate = 0.;
1688 if (outOfTargetSupport) {
1689 if (m_optionsObj->m_rawChainGenerateExtra) {
1690 m_alphaQuotients[positionId] = 0.;
1694 if (m_optionsObj->m_rawChainMeasureRunTimes) {
1695 iRC = gettimeofday(&timevalMhAlpha, NULL);
1698 if (m_optionsObj->m_rawChainGenerateExtra) {
1699 alphaFirstCandidate = m_algorithm->acceptance_ratio(
1700 currentPositionData,
1701 currentCandidateData,
1706 alphaFirstCandidate = m_algorithm->acceptance_ratio(
1707 currentPositionData,
1708 currentCandidateData,
1712 if (m_optionsObj->m_rawChainMeasureRunTimes) m_rawChainInfo.mhAlphaRunTime +=
MiscGetEllapsedSeconds(&timevalMhAlpha);
1713 if ((m_env.subDisplayFile() ) &&
1714 (m_env.displayVerbosity() >= 10 ) &&
1715 (m_optionsObj->m_totallyMute ==
false)) {
1716 *m_env.subDisplayFile() <<
"In MetropolisHastingsSG<P_V,P_M>::generateFullChain()"
1717 <<
": for chain position of id = " << positionId
1720 accept = acceptAlpha(alphaFirstCandidate);
1723 bool displayDetail = (m_env.displayVerbosity() >= 10) || m_optionsObj->m_displayCandidates;
1724 if ((m_env.subDisplayFile() ) &&
1726 (m_optionsObj->m_totallyMute ==
false)) {
1727 *m_env.subDisplayFile() <<
"In MetropolisHastingsSG<P_V,P_M>::generateFullChain()"
1728 <<
": for chain position of id = " << positionId
1729 <<
", outOfTargetSupport = " << outOfTargetSupport
1730 <<
", alpha = " << alphaFirstCandidate
1731 <<
", accept = " << accept
1732 <<
", currentCandidateData.vecValues() = ";
1733 *m_env.subDisplayFile() << currentCandidateData.vecValues();
1734 *m_env.subDisplayFile() <<
"\n"
1735 <<
"\n curLogTarget = " << currentPositionData.
logTarget()
1737 <<
"\n canLogTarget = " << currentCandidateData.logTarget()
1741 if ((m_env.subDisplayFile() ) &&
1742 (m_env.displayVerbosity() >= 10 ) &&
1743 (m_optionsObj->m_totallyMute ==
false)) {
1744 *m_env.subDisplayFile() <<
"\n"
1745 <<
"\n-----------------------------------------------------------\n"
1754 if ((accept ==
false) &&
1755 (outOfTargetSupport ==
false) &&
1756 (m_optionsObj->m_drMaxNumExtraStages > 0)) {
1757 accept = delayedRejection(positionId,
1758 currentPositionData,
1759 currentCandidateData);
1768 if (
true) m_idsOfUniquePositions[uniquePos++] = positionId;
1769 currentPositionData = currentCandidateData;
1773 m_rawChainInfo.numRejections++;
1775 m_numPositionsNotSubWritten++;
1776 if ((m_optionsObj->m_rawChainDataOutputPeriod > 0 ) &&
1777 (((positionId+1) % m_optionsObj->m_rawChainDataOutputPeriod) == 0 ) &&
1778 (m_optionsObj->m_rawChainDataOutputFileName !=
".")) {
1779 if ((m_env.subDisplayFile() ) &&
1780 (m_env.displayVerbosity() >= 10 ) &&
1781 (m_optionsObj->m_totallyMute ==
false)) {
1782 *m_env.subDisplayFile() <<
"In MetropolisHastingsSG<P_V,P_M>::generateFullChain()"
1783 <<
", for chain position of id = " << positionId
1784 <<
": about to write (per period request) " << m_numPositionsNotSubWritten <<
" chain positions "
1785 <<
", " << positionId + 1 - m_optionsObj->m_rawChainDataOutputPeriod <<
" <= pos <= " << positionId
1788 workingChain.
subWriteContents(positionId + 1 - m_optionsObj->m_rawChainDataOutputPeriod,
1789 m_optionsObj->m_rawChainDataOutputPeriod,
1790 m_optionsObj->m_rawChainDataOutputFileName,
1791 m_optionsObj->m_rawChainDataOutputFileType,
1792 m_optionsObj->m_rawChainDataOutputAllowedSet);
1793 if ((m_env.subDisplayFile() ) &&
1794 (m_optionsObj->m_totallyMute ==
false)) {
1795 *m_env.subDisplayFile() <<
"In MetropolisHastingsSG<P_V,P_M>::generateFullChain()"
1796 <<
", for chain position of id = " << positionId
1797 <<
": just wrote (per period request) " << m_numPositionsNotSubWritten <<
" chain positions "
1798 <<
", " << positionId + 1 - m_optionsObj->m_rawChainDataOutputPeriod <<
" <= pos <= " << positionId
1802 if (writeLogLikelihood) {
1803 workingLogLikelihoodValues->
subWriteContents(0 + 1 - m_optionsObj->m_rawChainDataOutputPeriod,
1804 m_optionsObj->m_rawChainDataOutputPeriod,
1805 m_optionsObj->m_rawChainDataOutputFileName +
"_loglikelihood",
1806 m_optionsObj->m_rawChainDataOutputFileType,
1807 m_optionsObj->m_rawChainDataOutputAllowedSet);
1810 if (writeLogTarget) {
1811 workingLogTargetValues->
subWriteContents(0 + 1 - m_optionsObj->m_rawChainDataOutputPeriod,
1812 m_optionsObj->m_rawChainDataOutputPeriod,
1813 m_optionsObj->m_rawChainDataOutputFileName +
"_logtarget",
1814 m_optionsObj->m_rawChainDataOutputFileType,
1815 m_optionsObj->m_rawChainDataOutputAllowedSet);
1818 m_numPositionsNotSubWritten = 0;
1822 if (workingLogLikelihoodValues) (*workingLogLikelihoodValues)[positionId] = currentPositionData.
logLikelihood();
1823 if (workingLogTargetValues ) (*workingLogTargetValues )[positionId] = currentPositionData.
logTarget();
1825 if (m_optionsObj->m_rawChainGenerateExtra) {
1826 m_logTargets[positionId] = currentPositionData.
logTarget();
1829 if (m_optionsObj->m_enableBrooksGelmanConvMonitor > 0) {
1830 if (positionId % m_optionsObj->m_enableBrooksGelmanConvMonitor == 0 &&
1831 positionId > m_optionsObj->m_BrooksGelmanLag + 1) {
1834 m_optionsObj->m_BrooksGelmanLag,
1835 positionId - m_optionsObj->m_BrooksGelmanLag);
1837 if (m_env.subDisplayFile()) {
1838 *m_env.subDisplayFile() <<
"positionId = " << positionId
1839 <<
", conv_est = " << conv_est
1841 (*m_env.subDisplayFile()).flush();
1847 if (positionId % m_optionsObj->m_updateInterval == 0) {
1853 if (m_tk->covMatrixIsDirty()) {
1854 m_latestDirtyCovMatrixIteration = positionId;
1858 m_tk->cleanCovMatrix();
1865 this->adapt(positionId, workingChain);
1871 if ((m_env.subDisplayFile() ) &&
1872 (m_env.displayVerbosity() >= 3 ) &&
1873 (m_optionsObj->m_totallyMute ==
false)) {
1874 *m_env.subDisplayFile() <<
"In MetropolisHastingsSG<P_V,P_M>::generateFullChain()"
1875 <<
": finishing chain position of id = " << positionId
1876 <<
", accept = " << accept
1877 <<
", curLogTarget = " << currentPositionData.
logTarget()
1878 <<
", canLogTarget = " << currentCandidateData.logTarget()
1882 if ((m_optionsObj->m_rawChainDisplayPeriod > 0) &&
1883 (((positionId+1) % m_optionsObj->m_rawChainDisplayPeriod) == 0)) {
1884 if ((m_env.subDisplayFile() ) &&
1885 (m_optionsObj->m_totallyMute ==
false)) {
1886 *m_env.subDisplayFile() <<
"Finished generating " << positionId+1
1888 <<
", current rejection percentage = " << (100. * ((double) m_rawChainInfo.numRejections)/((double) (positionId+1)))
1894 if ((m_env.subDisplayFile() ) &&
1895 (m_env.displayVerbosity() >= 10 ) &&
1896 (m_optionsObj->m_totallyMute ==
false)) {
1897 *m_env.subDisplayFile() <<
"\n"
1898 <<
"\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
1904 if ((m_env.numSubEnvironments() < (
unsigned int) m_env.fullComm().NumProc()) &&
1905 (m_initialPosition.numOfProcsForStorage() == 1 ) &&
1906 (m_env.subRank() == 0 )) {
1909 aux = m_targetPdfSynchronizer->callFunction(NULL,
1919 if ((m_env.subDisplayFile() ) &&
1920 (m_optionsObj->m_totallyMute ==
false)) {
1921 *m_env.subDisplayFile() <<
"Finished the generation of Markov chain " << workingChain.
name()
1924 *m_env.subDisplayFile() <<
"\nSome information about this chain:"
1925 <<
"\n Chain run time = " << m_rawChainInfo.runTime
1927 if (m_optionsObj->m_rawChainMeasureRunTimes) {
1928 *m_env.subDisplayFile() <<
"\n\n Breaking of the chain run time:\n";
1929 *m_env.subDisplayFile() <<
"\n Candidate run time = " << m_rawChainInfo.candidateRunTime
1930 <<
" seconds (" << 100.*m_rawChainInfo.candidateRunTime/m_rawChainInfo.runTime
1932 *m_env.subDisplayFile() <<
"\n Num target calls = " << m_rawChainInfo.numTargetCalls;
1933 *m_env.subDisplayFile() <<
"\n Target d. run time = " << m_rawChainInfo.targetRunTime
1934 <<
" seconds (" << 100.*m_rawChainInfo.targetRunTime/m_rawChainInfo.runTime
1936 *m_env.subDisplayFile() <<
"\n Avg target run time = " << m_rawChainInfo.targetRunTime/((double) m_rawChainInfo.numTargetCalls)
1938 *m_env.subDisplayFile() <<
"\n Mh alpha run time = " << m_rawChainInfo.mhAlphaRunTime
1939 <<
" seconds (" << 100.*m_rawChainInfo.mhAlphaRunTime/m_rawChainInfo.runTime
1941 *m_env.subDisplayFile() <<
"\n Dr alpha run time = " << m_rawChainInfo.drAlphaRunTime
1942 <<
" seconds (" << 100.*m_rawChainInfo.drAlphaRunTime/m_rawChainInfo.runTime
1944 *m_env.subDisplayFile() <<
"\n---------------------- --------------";
1945 double sumRunTime = m_rawChainInfo.candidateRunTime + m_rawChainInfo.targetRunTime + m_rawChainInfo.mhAlphaRunTime + m_rawChainInfo.drAlphaRunTime;
1946 *m_env.subDisplayFile() <<
"\n Sum = " << sumRunTime
1947 <<
" seconds (" << 100.*sumRunTime/m_rawChainInfo.runTime
1949 *m_env.subDisplayFile() <<
"\n\n Other run times:";
1950 *m_env.subDisplayFile() <<
"\n DR run time = " << m_rawChainInfo.drRunTime
1951 <<
" seconds (" << 100.*m_rawChainInfo.drRunTime/m_rawChainInfo.runTime
1953 *m_env.subDisplayFile() <<
"\n AM run time = " << m_rawChainInfo.amRunTime
1954 <<
" seconds (" << 100.*m_rawChainInfo.amRunTime/m_rawChainInfo.runTime
1957 *m_env.subDisplayFile() <<
"\n Number of DRs = " << m_rawChainInfo.numDRs <<
"(num_DRs/chain_size = " << (double) m_rawChainInfo.numDRs/(
double) workingChain.
subSequenceSize()
1959 *m_env.subDisplayFile() <<
"\n Out of target support in DR = " << m_rawChainInfo.numOutOfTargetSupportInDR;
1960 *m_env.subDisplayFile() <<
"\n Rejection percentage = " << 100. * (double) m_rawChainInfo.numRejections/(
double) workingChain.
subSequenceSize()
1962 *m_env.subDisplayFile() <<
"\n Out of target support percentage = " << 100. * (double) m_rawChainInfo.numOutOfTargetSupport/(
double) workingChain.
subSequenceSize()
1964 *m_env.subDisplayFile() << std::endl;
1975 template <
class P_V,
class P_M>
1981 struct timeval timevalAM;
1984 if ((m_optionsObj->m_tkUseLocalHessian ==
true) ||
1985 (m_optionsObj->m_amInitialNonAdaptInterval == 0) ||
1986 (m_optionsObj->m_amAdaptInterval == 0)) {
1991 if (m_optionsObj->m_rawChainMeasureRunTimes) {
1992 iRC = gettimeofday(&timevalAM, NULL);
1996 unsigned int idOfFirstPositionInSubChain = 0;
2000 bool printAdaptedMatrix =
false;
2001 if (positionId < m_optionsObj->m_amInitialNonAdaptInterval) {
2004 else if (positionId == m_optionsObj->m_amInitialNonAdaptInterval) {
2005 idOfFirstPositionInSubChain = 0;
2006 partialChain.
resizeSequence(m_optionsObj->m_amInitialNonAdaptInterval+1);
2007 m_lastMean.reset(m_vectorSpace.newVector());
2008 m_lastAdaptedCovMatrix.reset(m_vectorSpace.newMatrix());
2009 printAdaptedMatrix =
true;
2012 unsigned int interval = positionId - m_optionsObj->m_amInitialNonAdaptInterval;
2013 if ((interval % m_optionsObj->m_amAdaptInterval) == 0) {
2019 if (m_latestDirtyCovMatrixIteration > 0) {
2020 m_lastMean->cwSet(0.0);
2021 m_lastAdaptedCovMatrix->cwSet(0.0);
2025 unsigned int iter_diff = positionId - m_latestDirtyCovMatrixIteration;
2026 idOfFirstPositionInSubChain = iter_diff;
2031 m_latestDirtyCovMatrixIteration = 0;
2034 idOfFirstPositionInSubChain = positionId - m_optionsObj->m_amAdaptInterval;
2038 if (m_optionsObj->m_amAdaptedMatricesDataOutputPeriod > 0) {
2039 if ((interval % m_optionsObj->m_amAdaptedMatricesDataOutputPeriod) == 0) {
2040 printAdaptedMatrix =
true;
2049 if (m_optionsObj->m_rawChainMeasureRunTimes) {
2057 P_V transporterVec(m_vectorSpace.zeroVector());
2063 if (this->m_optionsObj->m_tk ==
"logit_random_walk") {
2067 P_V transformedTransporterVec(m_vectorSpace.zeroVector());
2069 m_tk.get())->transformToGaussianSpace(transporterVec,
2070 transformedTransporterVec);
2078 updateAdaptedCovMatrix(partialChain,
2079 idOfFirstPositionInSubChain,
2082 *m_lastAdaptedCovMatrix);
2085 if ((printAdaptedMatrix ==
true) &&
2086 (m_optionsObj->m_amAdaptedMatricesDataOutputFileName !=
"." )) {
2087 char varNamePrefix[64];
2088 sprintf(varNamePrefix,
"mat_am%d",positionId);
2091 sprintf(tmpChar,
"_am%d",positionId);
2093 std::set<unsigned int> tmpSet;
2094 tmpSet.insert(m_env.subId());
2096 m_lastAdaptedCovMatrix->subWriteContents(varNamePrefix,
2097 (m_optionsObj->m_amAdaptedMatricesDataOutputFileName+tmpChar),
2098 m_optionsObj->m_amAdaptedMatricesDataOutputFileType,
2100 if ((m_env.subDisplayFile() ) &&
2101 (m_optionsObj->m_totallyMute ==
false)) {
2102 *m_env.subDisplayFile() <<
"In MetropolisHastingsSG<P_V,P_M>::generateFullChain()"
2103 <<
": just wrote last adapted proposal cov matrix contents = " << *m_lastAdaptedCovMatrix
2109 bool tmpCholIsPositiveDefinite =
false;
2110 P_M tmpChol(*m_lastAdaptedCovMatrix);
2111 P_M attemptedMatrix(tmpChol);
2112 if ((m_env.subDisplayFile() ) &&
2113 (m_env.displayVerbosity() >= 10)) {
2115 *m_env.subDisplayFile() <<
"In MetropolisHastingsSG<P_V,P_M>::generateFullChain()"
2116 <<
", positionId = " << positionId
2117 <<
": 'am' calling first tmpChol.chol()"
2120 iRC = tmpChol.chol();
2122 std::string err1 =
"In MetropolisHastingsSG<P_V,P_M>::adapt(): first ";
2123 err1 +=
"Cholesky factorisation of proposal covariance matrix ";
2124 err1 +=
"failed. QUESO will now attempt to regularise the ";
2125 err1 +=
"matrix before proceeding. This is not a fatal error.";
2126 std::cerr << err1 << std::endl;
2130 if ((m_env.subDisplayFile() ) &&
2131 (m_env.displayVerbosity() >= 10)) {
2133 *m_env.subDisplayFile() <<
"In MetropolisHastingsSG<P_V,P_M>::generateFullChain()"
2134 <<
", positionId = " << positionId
2135 <<
": 'am' got first tmpChol.chol() with iRC = " << iRC
2138 double diagMult = 1.;
2139 for (
unsigned int j = 0; j < tmpChol.numRowsLocal(); ++j) {
2140 diagMult *= tmpChol(j,j);
2142 *m_env.subDisplayFile() <<
"diagMult = " << diagMult
2147 #if 0 // tentative logic
2149 double diagMult = 1.;
2150 for (
unsigned int j = 0; j < tmpChol.numRowsLocal(); ++j) {
2151 diagMult *= tmpChol(j,j);
2153 if (diagMult < 1.e-40) {
2164 P_M* tmpDiag = m_vectorSpace.newDiagMatrix(m_optionsObj->m_amEpsilon);
2165 if (m_numDisabledParameters > 0) {
2166 for (
unsigned int paramId = 0; paramId < m_vectorSpace.dimLocal(); ++paramId) {
2167 if (m_parameterEnabledStatus[paramId] ==
false) {
2168 (*tmpDiag)(paramId,paramId) = 0.;
2172 tmpChol = *m_lastAdaptedCovMatrix + *tmpDiag;
2173 attemptedMatrix = tmpChol;
2176 if ((m_env.subDisplayFile() ) &&
2177 (m_env.displayVerbosity() >= 10)) {
2178 *m_env.subDisplayFile() <<
"In MetropolisHastingsSG<P_V,P_M>::generateFullChain()"
2179 <<
", positionId = " << positionId
2180 <<
": 'am' calling second tmpChol.chol()"
2185 iRC = tmpChol.chol();
2187 std::string err2 =
"In MetropolisHastingsSG<P_V,P_M>::adapt(): second ";
2188 err2 +=
"Cholesky factorisation of (regularised) proposal ";
2189 err2 +=
"covariance matrix failed. QUESO is falling back to ";
2190 err2 +=
"the last factorisable proposal covariance matrix. ";
2191 err2 +=
"This is not a fatal error.";
2192 std::cerr << err2 << std::endl;
2196 if ((m_env.subDisplayFile() ) &&
2197 (m_env.displayVerbosity() >= 10)) {
2198 *m_env.subDisplayFile() <<
"In MetropolisHastingsSG<P_V,P_M>::generateFullChain()"
2199 <<
", positionId = " << positionId
2200 <<
": 'am' got second tmpChol.chol() with iRC = " << iRC
2203 double diagMult = 1.;
2204 for (
unsigned int j = 0; j < tmpChol.numRowsLocal(); ++j) {
2205 diagMult *= tmpChol(j,j);
2207 *m_env.subDisplayFile() <<
"diagMult = " << diagMult
2211 *m_env.subDisplayFile() <<
"attemptedMatrix = " << attemptedMatrix
2222 tmpCholIsPositiveDefinite =
true;
2226 tmpCholIsPositiveDefinite =
true;
2230 if (tmpCholIsPositiveDefinite) {
2231 P_M tmpMatrix(m_optionsObj->m_amEta*attemptedMatrix);
2232 if (m_numDisabledParameters > 0) {
2233 for (
unsigned int paramId = 0; paramId < m_vectorSpace.dimLocal(); ++paramId) {
2234 if (m_parameterEnabledStatus[paramId] ==
false) {
2235 for (
unsigned int i = 0; i < m_vectorSpace.dimLocal(); ++i) {
2236 tmpMatrix(i,paramId) = 0.;
2238 for (
unsigned int j = 0; j < m_vectorSpace.dimLocal(); ++j) {
2239 tmpMatrix(paramId,j) = 0.;
2241 tmpMatrix(paramId,paramId) = 1.;
2250 if (this->m_optionsObj->m_tk ==
"logit_random_walk") {
2252 ->updateLawCovMatrix(tmpMatrix);
2258 ->updateLawCovMatrix(tmpMatrix);
2261 #ifdef UQ_DRAM_MCG_REQUIRES_INVERTED_COV_MATRICES
2272 if (m_optionsObj->m_rawChainMeasureRunTimes) {
2277 template <
class P_V,
class P_M>
2283 if ((m_optionsObj->m_drDuringAmNonAdaptiveInt ==
false ) &&
2284 (m_optionsObj->m_tkUseLocalHessian ==
false ) &&
2285 (m_optionsObj->m_amInitialNonAdaptInterval > 0 ) &&
2286 (m_optionsObj->m_amAdaptInterval > 0 ) &&
2287 (positionId <= m_optionsObj->m_amInitialNonAdaptInterval)) {
2291 unsigned int stageId = 0;
2293 bool validPreComputingPosition;
2295 m_tk->clearPreComputingPositions();
2297 validPreComputingPosition = m_tk->setPreComputingPosition(
2300 validPreComputingPosition = m_tk->setPreComputingPosition(
2301 currentCandidateData.
vecValues(), stageId + 1);
2303 std::vector<MarkovChainPositionData<P_V>*> drPositionsData(stageId+2,NULL);
2304 std::vector<unsigned int> tkStageIds (stageId+2,0);
2307 struct timeval timevalDR;
2308 struct timeval timevalDrAlpha;
2309 struct timeval timevalCandidate;
2310 struct timeval timevalTarget;
2312 if (m_optionsObj->m_rawChainMeasureRunTimes) {
2313 iRC = gettimeofday(&timevalDR, NULL);
2323 bool accept =
false;
2324 while ((validPreComputingPosition ==
true ) &&
2325 (accept == false ) &&
2326 (stageId < m_optionsObj->m_drMaxNumExtraStages)) {
2327 if ((m_env.subDisplayFile() ) &&
2328 (m_env.displayVerbosity() >= 10 ) &&
2329 (m_optionsObj->m_totallyMute ==
false)) {
2330 *m_env.subDisplayFile() <<
"\n"
2331 <<
"\n+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-"
2335 m_rawChainInfo.numDRs++;
2337 m_stageIdForDebugging = stageId;
2338 if ((m_env.subDisplayFile() ) &&
2339 (m_env.displayVerbosity() >= 10 ) &&
2340 (m_optionsObj->m_totallyMute ==
false)) {
2341 *m_env.subDisplayFile() <<
"In MetropolisHastingsSG<P_V,P_M>::generateFullChain()"
2342 <<
": for chain position of id = " << positionId
2343 <<
", beginning stageId = " << stageId
2347 P_V tmpVecValues(currentCandidateData.
vecValues());
2348 bool keepGeneratingCandidates =
true;
2349 bool outOfTargetSupport =
false;
2350 while (keepGeneratingCandidates) {
2351 if (m_optionsObj->m_rawChainMeasureRunTimes) {
2352 iRC = gettimeofday(&timevalCandidate, NULL);
2355 m_tk->rv(tkStageIds).realizer().realization(tmpVecValues);
2356 if (m_numDisabledParameters > 0) {
2357 for (
unsigned int paramId = 0; paramId < m_vectorSpace.dimLocal(); ++paramId) {
2358 if (m_parameterEnabledStatus[paramId] ==
false) {
2359 tmpVecValues[paramId] = m_initialPosition[paramId];
2363 if (m_optionsObj->m_rawChainMeasureRunTimes) m_rawChainInfo.candidateRunTime +=
MiscGetEllapsedSeconds(&timevalCandidate);
2365 outOfTargetSupport = !m_targetPdf.domainSet().contains(tmpVecValues);
2367 if (m_optionsObj->m_putOutOfBoundsInChain) keepGeneratingCandidates =
false;
2368 else keepGeneratingCandidates = outOfTargetSupport;
2371 if ((m_env.subDisplayFile() ) &&
2372 (m_env.displayVerbosity() >= 5 ) &&
2373 (m_optionsObj->m_totallyMute ==
false)) {
2374 *m_env.subDisplayFile() <<
"In MetropolisHastingsSG<P_V,P_M>::generateFullChain()"
2375 <<
": about to set TK pre computing position of local id " << stageId+1
2376 <<
", values = " << tmpVecValues
2379 validPreComputingPosition = m_tk->setPreComputingPosition(tmpVecValues,stageId+1);
2380 if ((m_env.subDisplayFile() ) &&
2381 (m_env.displayVerbosity() >= 5 ) &&
2382 (m_optionsObj->m_totallyMute ==
false)) {
2383 *m_env.subDisplayFile() <<
"In MetropolisHastingsSG<P_V,P_M>::generateFullChain()"
2384 <<
": returned from setting TK pre computing position of local id " << stageId+1
2385 <<
", values = " << tmpVecValues
2386 <<
", valid = " << validPreComputingPosition
2391 double logLikelihood;
2393 if (outOfTargetSupport) {
2394 m_rawChainInfo.numOutOfTargetSupportInDR++;
2395 logPrior = -INFINITY;
2396 logLikelihood = -INFINITY;
2397 logTarget = -INFINITY;
2400 if (m_optionsObj->m_rawChainMeasureRunTimes) {
2401 iRC = gettimeofday(&timevalTarget, NULL);
2404 logTarget = m_targetPdfSynchronizer->callFunction(&tmpVecValues,&logPrior,&logLikelihood);
2405 if (m_optionsObj->m_rawChainMeasureRunTimes) m_rawChainInfo.targetRunTime +=
MiscGetEllapsedSeconds(&timevalTarget);
2406 m_rawChainInfo.numTargetCalls++;
2407 if ((m_env.subDisplayFile() ) &&
2408 (m_env.displayVerbosity() >= 3 ) &&
2409 (m_optionsObj->m_totallyMute ==
false)) {
2410 *m_env.subDisplayFile() <<
"In MetropolisHastingsSG<P_V,P_M>::generateFullChain()"
2411 <<
": just returned from likelihood() for chain position of id " << positionId
2412 <<
", m_rawChainInfo.numTargetCalls = " << m_rawChainInfo.numTargetCalls
2413 <<
", stageId = " << stageId
2414 <<
", logPrior = " << logPrior
2415 <<
", logLikelihood = " << logLikelihood
2416 <<
", logTarget = " << logTarget
2420 currentCandidateData.
set(tmpVecValues,
2428 tkStageIds.push_back (stageId+1);
2430 double alphaDR = 0.;
2431 if (outOfTargetSupport ==
false) {
2432 if (m_optionsObj->m_rawChainMeasureRunTimes) {
2433 iRC = gettimeofday(&timevalDrAlpha, NULL);
2436 alphaDR = this->alpha(drPositionsData,tkStageIds);
2437 if (m_optionsObj->m_rawChainMeasureRunTimes) m_rawChainInfo.drAlphaRunTime +=
MiscGetEllapsedSeconds(&timevalDrAlpha);
2438 accept = acceptAlpha(alphaDR);
2441 bool displayDetail = (m_env.displayVerbosity() >= 10) || m_optionsObj->m_displayCandidates;
2442 if ((m_env.subDisplayFile() ) &&
2444 (m_optionsObj->m_totallyMute ==
false)) {
2445 *m_env.subDisplayFile() <<
"In MetropolisHastingsSG<P_V,P_M>::generateFullChain()"
2446 <<
": for chain position of id = " << positionId
2447 <<
" and stageId = " << stageId
2448 <<
", outOfTargetSupport = " << outOfTargetSupport
2449 <<
", alpha = " << alphaDR
2450 <<
", accept = " << accept
2451 <<
", currentCandidateData.vecValues() = ";
2452 *m_env.subDisplayFile() << currentCandidateData.
vecValues();
2453 *m_env.subDisplayFile() << std::endl;
2457 if (m_optionsObj->m_rawChainMeasureRunTimes) m_rawChainInfo.drRunTime +=
MiscGetEllapsedSeconds(&timevalDR);
2459 for (
unsigned int i = 0; i < drPositionsData.size(); ++i) {
2460 if (drPositionsData[i])
delete drPositionsData[i];
2467 template <
class P_V,
class P_M>
2471 unsigned int idOfFirstPositionInSubChain,
2472 double& lastChainSize,
2474 P_M& lastAdaptedCovMatrix)
2477 if (lastChainSize == 0) {
2478 queso_require_greater_equal_msg(partialChain.
subSequenceSize(), 2,
"'partialChain.subSequenceSize()' should be >= 2");
2480 #if 1 // prudenci-2012-07-06
2486 P_V tmpVec(m_vectorSpace.zeroVector());
2487 lastAdaptedCovMatrix = -doubleSubChainSize *
matrixProduct(lastMean,lastMean);
2492 lastAdaptedCovMatrix /= (doubleSubChainSize - 1.);
2495 queso_require_greater_equal_msg(partialChain.
subSequenceSize(), 1,
"'partialChain.subSequenceSize()' should be >= 1");
2496 queso_require_greater_equal_msg(idOfFirstPositionInSubChain, 1,
"'idOfFirstPositionInSubChain' should be >= 1");
2498 P_V tmpVec (m_vectorSpace.zeroVector());
2499 P_V diffVec(m_vectorSpace.zeroVector());
2501 double doubleCurrentId = (double) (idOfFirstPositionInSubChain+i);
2503 diffVec = tmpVec - lastMean;
2505 double ratio1 = (1. - 1./doubleCurrentId);
2506 double ratio2 = (1./(1.+doubleCurrentId));
2507 lastAdaptedCovMatrix = ratio1 * lastAdaptedCovMatrix + ratio2 *
matrixProduct(diffVec,diffVec);
2508 lastMean += ratio2 * diffVec;
2511 lastChainSize += doubleSubChainSize;
2513 if (m_numDisabledParameters > 0) {
2514 for (
unsigned int paramId = 0; paramId < m_vectorSpace.dimLocal(); ++paramId) {
2515 if (m_parameterEnabledStatus[paramId] ==
false) {
2516 for (
unsigned int i = 0; i < m_vectorSpace.dimLocal(); ++i) {
2517 lastAdaptedCovMatrix(i,paramId) = 0.;
2519 for (
unsigned int j = 0; j < m_vectorSpace.dimLocal(); ++j) {
2520 lastAdaptedCovMatrix(paramId,j) = 0.;
2522 lastAdaptedCovMatrix(paramId,paramId) = 1.;
2530 template <
class P_V,
class P_M>
2535 P_V min_domain_bounds(boxSubset.
minValues());
2536 P_V max_domain_bounds(boxSubset.
maxValues());
2538 for (
unsigned int i = 0; i < min_domain_bounds.sizeLocal(); i++) {
2539 double min_val = min_domain_bounds[i];
2540 double max_val = max_domain_bounds[i];
2543 if (m_initialProposalCovMatrix(i, i) >= max_val - min_val) {
2546 std::cerr <<
"Proposal variance element "
2549 << m_initialProposalCovMatrix(i, i)
2550 <<
" but domain is of size "
2551 << max_val - min_val
2553 std::cerr <<
"QUESO does not support uniform-like proposal "
2554 <<
"distributions. Try making the proposal variance smaller"
2559 double transformJacobian = 4.0 / (max_val - min_val);
2563 for (
unsigned int j = 0; j < min_domain_bounds.sizeLocal(); j++) {
2565 m_initialProposalCovMatrix(j, i) *= transformJacobian;
2567 for (
unsigned int j = 0; j < min_domain_bounds.sizeLocal(); j++) {
2569 m_initialProposalCovMatrix(i, j) *= transformJacobian;
virtual void subWriteContents(unsigned int initialPos, unsigned int numPos, const std::string &fileName, const std::string &fileType, const std::set< unsigned int > &allowedSubEnvIds) const =0
Writes info of the sub-sequence to a file. See template specialization.
P_M m_initialProposalCovMatrix
void transformInitialCovMatrixToGaussianSpace(const BoxSubset< P_V, P_M > &boxSubset)
virtual void subMeanExtra(unsigned int initialPos, unsigned int numPos, V &meanVec) const =0
Finds the mean value of the sub-sequence, considering numPos positions starting at position initialPo...
virtual unsigned int subSequenceSize() const =0
Size of the sub-sequence of vectors. See template specialization.
A templated class for synchronizing the calls of scalar functions (BaseScalarFunction and derived cla...
void resizeSequence(unsigned int newSequenceSize)
Resizes the size of the sequence of scalars.
void getRawChainInfo(MHRawChainInfoStruct &info) const
Gets information from the raw chain.
ScopedPtr< MetropolisHastingsSGOptions >::Type m_oldOptions
bool queso_isfinite(T arg)
void setName(const std::string &newName)
Changes the name of the sequence of vectors.
const V & subMeanPlain() const
Finds the mean value of the sub-sequence.
void reset()
Resets Metropolis-Hastings chain info.
static void set_tk(const BaseTKGroup< GslVector, GslMatrix > &tk)
const BaseEnvironment & m_env
A struct that represents a Metropolis-Hastings sample.
void computeStatistics(const SequenceStatisticalOptions &statisticalOptions, std::ofstream *passedOfs)
void adapt(unsigned int positionId, BaseVectorSequence< P_V, P_M > &workingChain)
Adaptive Metropolis method that deals with adapting the proposal covariance matrix.
This class reads the options for the Metropolis-Hastings generator of samples from an input file...
MonteCarloSGOptions::MonteCarloSGOptions(const BaseEnvironment &env, const char *prefix queso_require_not_equal_to_msg)(m_env.optionsInputFileName(), std::string(""), std::string("this constructor is incompatible with the absence of an options input file"))
void unifiedWriteContents(const std::string &fileName, const std::string &fileType) const
Writes the unified sequence to a file.
static void set_pdf_synchronizer(const ScalarFunctionSynchronizer< GslVector, GslMatrix > &synchronizer)
const BaseTKGroup< P_V, P_M > & transitionKernel() const
Returns the underlying transition kernel for this sequence generator.
bool delayedRejection(unsigned int positionId, const MarkovChainPositionData< P_V > ¤tPositionData, MarkovChainPositionData< P_V > ¤tCandidateData)
Does delayed rejection.
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.
static void set_vectorspace(const VectorSpace< GslVector, GslMatrix > &v)
int writeInfo(const BaseVectorSequence< P_V, P_M > &workingChain, std::ofstream &ofsvar) const
Writes information about the Markov chain in a file.
unsigned int subSequenceSize() const
Size of the sub-sequence of vectors.
void computeFilterParams(std::ofstream *passedOfs, unsigned int &initialPos, unsigned int &spacing)
Computes the filtering parameters spacing for the sequence of vectors.
const int UQ_MATRIX_IS_NOT_POS_DEFINITE_RC
static void set_target_pdf(const BaseJointPdf< GslVector, GslMatrix > &target_pdf)
double subPositionsOfMaximum(const ScalarSequence< double > &subCorrespondingScalarValues, BaseVectorSequence< V, M > &subPositionsOfMaximum)
Finds the positions where the maximum element occurs in the sub-sequence.
void setPositionValues(unsigned int posId, const V &vec)
Set the values in vec at position posId of the sequence.
static void set_options(const MhOptionsValues &options)
const V & vecValues() const
Values of the chain (vector); access to private attribute m_vecValues.
int worldRank() const
Returns the same thing as fullRank()
This class provides options for the Metropolis-Hastings generator of samples if no input file is avai...
MHRawChainInfoStruct()
Constructor.
A templated class that represents a Metropolis-Hastings generator of samples.
ScopedPtr< const MhOptionsValues >::Type m_optionsObj
This class represents a transition kernel with a scaled covariance matrix on hybrid bounded/unbounded...
unsigned int numTargetCalls
void generateSequence(BaseVectorSequence< P_V, P_M > &workingChain, ScalarSequence< double > *workingLogLikelihoodValues, ScalarSequence< double > *workingLogTargetValues)
Method to generate the chain.
virtual void unifiedWriteContents(const std::string &fileName, const std::string &fileType) const =0
Writes info of the unified sequence to a file. See template specialization.
unsigned int numOutOfTargetSupportInDR
const V & minValues() const
Vector of the minimum values of the box subset.
Class for handling vector samples (sequence of vectors).
const std::string & name() const
Access to protected attribute m_name: name of the sequence of vectors.
Class representing a subset of a vector space shaped like a hypercube.
const int UQ_INCOMPLETE_IMPLEMENTATION_RC
Base class for handling vector and array samples (sequence of vectors or arrays). ...
virtual void getPositionValues(unsigned int posId, V &vec) const =0
Gets the values of the sequence at position posId and stores them at vec. See template specialization...
void filter(unsigned int initialPos, unsigned int spacing)
Filters positions in the sequence of vectors.
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 logTarget() const
Logarithm of the value of the target; access to private attribute m_logTarget.
void commonConstructor()
Reads the options values from the options input file.
virtual void resizeSequence(unsigned int newSubSequenceSize)=0
Resize the sequence. See template specialization.
void set(const V &vecValues, bool outOfTargetSupport, double logLikelihood, double logTarget)
Sets the values of the chain.
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 & operator=(const MHRawChainInfoStruct &rhs)
Assignment operator.
static SharedPtr< BaseTKGroup< GslVector, GslMatrix > >::Type build(const std::string &name)
void Allreduce(void *sendbuf, void *recvbuf, int count, RawType_MPI_Datatype datatype, RawType_MPI_Op op, const char *whereMsg, const char *whatMsg) const
Combines values from all processes and distributes the result back to all processes.
virtual void unifiedReadContents(const std::string &fileName, const std::string &fileType, const unsigned int subSequenceSize)=0
Reads info of the unified sequence from a file. See template specialization.
This class provides options for each level of the Multilevel sequence generator if no input file is a...
This base class allows the representation of a transition kernel.
void getPositionValues(unsigned int posId, V &vec) const
Gets the values of the sequence at position posId and stores them at vec.
GslMatrix matrixProduct(const GslVector &v1, const GslVector &v2)
McOptionsValues::McOptionsValues(#ifdef QUESO_USES_SEQUENCE_STATISTICAL_OPTIONS const SsOptionsValues *alternativePSsOptionsValues, const SsOptionsValues *alternativeQSsOptionsValues#endif if)(m_alternativeQSsOptionsValues=*alternativeQSsOptionsValues)
MonteCarloSGOptions::MonteCarloSGOptions(const BaseEnvironment &env, const char *prefix, const McOptionsValues &alternativeOptionsValues queso_require_equal_to_msg)(m_env.optionsInputFileName(), std::string(""), std::string("this constructor is incompatible with the existence of an options input file"))
A templated class that represents a Markov Chain.
~MHRawChainInfoStruct()
Destructor.
static void set_environment(const BaseEnvironment &env)
virtual double estimateConvBrooksGelman(unsigned int initialPos, unsigned int numPos) const =0
Estimates convergence rate using Brooks & Gelman method. See template specialization.
The QUESO MPI Communicator Class.
virtual void filter(unsigned int initialPos, unsigned int spacing)=0
Filters positions in the sequence of vectors, starting at initialPos, and with spacing given by spaci...
static void set_dr_scales(const std::vector< double > &scales)
void subWriteContents(unsigned int initialPos, unsigned int numPos, const std::string &fileName, const std::string &fileType, const std::set< unsigned int > &allowedSubEnvIds) const
Writes the sub-sequence to a file.
virtual void setPositionValues(unsigned int posId, const V &vec)=0
Set the values in vec at position posId of the sequence. See template specialization.
void resizeSequence(unsigned int newSubSequenceSize)
Resizes the sequence.
unsigned int numOutOfTargetSupport
MetropolisHastingsSG(const char *prefix, const MhOptionsValues *alternativeOptionsValues, const BaseVectorRV< P_V, P_M > &sourceRv, const P_V &initialPosition, const P_M *inputProposalCovMatrix)
Constructor.
std::ofstream * ofsVar
Provides a stream interface to write data to files.
~MetropolisHastingsSG()
Destructor.
double logLikelihood() const
Logarithm of the value of the likelihood; access to private attribute m_logLikelihood.
unsigned int vectorSizeLocal() const
Local dimension (size) of the vector space.
Struct for handling data input and output from files.
void mpiSum(const MpiComm &comm, MHRawChainInfoStruct &sumInfo)
Calculates the MPI sum of this.
static void set_initial_cov_matrix(GslMatrix &cov_matrix)
double MiscGetEllapsedSeconds(struct timeval *timeval0)
void copy(const MHRawChainInfoStruct &src)
Copies Metropolis-Hastings chain info from src to this.
This class allows the representation of a transition kernel with a scaled covariance matrix...
const V & maxValues() const
Vector of the maximum values of the box subset.
double unifiedPositionsOfMaximum(const ScalarSequence< double > &subCorrespondingScalarValues, BaseVectorSequence< V, M > &unifiedPositionsOfMaximum)
Finds the positions where the maximum element occurs in the unified sequence.
MHRawChainInfoStruct & operator+=(const MHRawChainInfoStruct &rhs)
Addition assignment operator.
unsigned int numRejections
double alpha(const std::vector< MarkovChainPositionData< P_V > * > &inputPositions, const std::vector< unsigned int > &inputTKStageIds)
Calculates acceptance ratio.
bool acceptAlpha(double alpha)
Decides whether or not to accept alpha.
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).