void PowerPosteriorAnalysis::summarizeStones( void ) { // create the directory if necessary RbFileManager f = RbFileManager(filename); f.createDirectoryForFile(); std::ofstream outStream; outStream.open( filename.c_str(), std::fstream::out); outStream << "state\t" << "power\t" << "likelihood" << std::endl; /* Append each stone */ for (size_t idx = 0; idx < powers.size(); ++idx) { RbFileManager fm = RbFileManager(filename); std::string stoneFileName = fm.getFileNameWithoutExtension() + "_stone_" + idx + "." + fm.getFileExtension(); RbFileManager f = RbFileManager(fm.getFilePath(), stoneFileName); // read the i-th stone std::ifstream inStream; inStream.open( f.getFullFileName().c_str(), std::fstream::in); if (inStream.is_open()) { bool header = true; std::string line = ""; while ( std::getline (inStream,line) ) { // we need to skip the header line if ( header == true ) { header = false; } else { outStream << line << std::endl; } } inStream.close(); } else { std::cerr << "Problem reading stone " << idx+1 << " from file " << stoneFileName << "." << std::endl; } } }
void PowerPosteriorAnalysis::runStone(size_t idx, size_t gen) { // create the directory if necessary RbFileManager fm = RbFileManager(filename); std::string stoneFileName = fm.getFileNameWithoutExtension() + "_stone_" + idx + "." + fm.getFileExtension(); RbFileManager f = RbFileManager(fm.getFilePath(), stoneFileName); f.createDirectoryForFile(); std::fstream outStream; outStream.open( f.getFullFileName().c_str(), std::fstream::out); outStream << "state\t" << "power\t" << "likelihood" << std::endl; // reset the counters for the move schedules sampler->reset(); // if ( sampler->getCurrentGeneration() == 0 ) // { // } /* Reset the monitors */ // for (size_t i=0; i<replicates; ++i) // { // for (size_t j=0; i<runs[i].getMonitors().size(); i++) // { // runs[i].getMonitors()[j].reset( kIterations); // } // } // reset the stopping rules // for (size_t i=0; i<rules.size(); ++i) // { // rules[i].runStarted(); // } size_t burnin = size_t( ceil( 0.25*gen ) ); size_t printInterval = size_t( round( fmax(1,gen/20.0) ) ); size_t digits = size_t( ceil( log10( powers.size() ) ) ); /* Run the chain */ if ( processActive ) { std::cout << "Step "; for (size_t d = size_t( ceil( log10( idx+1.1 ) ) ); d < digits; d++ ) { std::cout << " "; } std::cout << (idx+1) << " / " << powers.size(); std::cout << "\t\t"; } // set the power of this sampler sampler->setLikelihoodHeat( powers[idx] ); sampler->setStoneIndex( idx ); // Monitor sampler->startMonitors(gen); sampler->monitor(0); double p = powers[idx]; for (size_t k=1; k<=gen; k++) { if ( processActive ) { if ( k % printInterval == 0 ) { std::cout << "**"; std::cout.flush(); } } sampler->nextCycle( true ); // Monitor sampler->monitor(k); // sample the likelihood if ( k > burnin && k % sampleFreq == 0 ) { // compute the joint likelihood double likelihood = sampler->getModelLnProbability(); outStream << k << "\t" << p << "\t" << likelihood << std::endl; } } if ( processActive ) { std::cout << std::endl; } outStream.close(); }