/**
 * Set the file extension.
 */
void AbstractFileMonitor::addFileExtension(const std::string &s, bool dir)
{
    
    // compute the working filename
    if ( dir == false )
    {
        RbFileManager fm = RbFileManager(filename);
        working_file_name = fm.getFilePath() + fm.getPathSeparator() + fm.getFileNameWithoutExtension() + s + "." + fm.getFileExtension();
    }
    else
    {
        RbFileManager fm = RbFileManager(filename);
        working_file_name = fm.getFilePath() + fm.getPathSeparator() + s + fm.getPathSeparator() + fm.getFileName();
    }
    
}
/**
 * Should we stop now?
 * Yes, if the minimum ESS is larger than the provided threshold.
 */
bool GewekeStoppingRule::stop( size_t g )
{
    
    bool passed = true;
    
    for ( size_t i = 1; i <= numReplicates; ++i)
    {
        std::string fn = filename;
        if ( numReplicates > 1 )
        {
            RbFileManager fm = RbFileManager(filename);
            fn = fm.getFilePath() + fm.getPathSeparator() + fm.getFileNameWithoutExtension() + "_run_" + StringUtilities::to_string(i) + "." + fm.getFileExtension();
        }
        
        TraceContinuousReader reader = TraceContinuousReader( fn );
        
        // get the vector of traces from the reader
        std::vector<Trace> &data = reader.getTraces();
        
        size_t maxBurnin = 0;
        
        for ( size_t j = 0; j < data.size(); ++j)
        {
            Trace &t = data[j];
            const std::vector<double> &v = t.getValues();
            size_t b = burninEst->estimateBurnin( v );
            if ( maxBurnin < b )
            {
                maxBurnin = b;
            }
        }
        
        GewekeTest gTest = GewekeTest( alpha, frac1, frac2 );
        
        for ( size_t j = 0; j < data.size(); ++j)
        {
            RevBayesCore::Trace &t = data[j];
            const std::vector<double> &v = t.getValues();
            t.setBurnin( maxBurnin );
            t.computeStatistics();
            
            passed &= gTest.assessConvergenceSingleChain( v, maxBurnin );
        }
        
    }
    
    
    return passed;
}
Beispiel #3
0
void RevBayesCore::PosteriorPredictiveSimulation::run( int thinning )
{
    
    // some general constant variables
    RbFileManager fm = RbFileManager( directory );
    const std::string path_separator = fm.getPathSeparator();
    
    // this is where we need to implement the posterior predictive simulation
    
    size_t n_samples = traces[0].size();
    size_t n_traces = traces.size();
    
    std::vector<DagNode*> nodes = model.getDagNodes();
    
    size_t sim_pid_start = size_t(floor( (double(pid) / num_processes * (n_samples/double(thinning) ) ) ) );
    size_t sim_pid_end   = std::max( int(sim_pid_start), int(floor( (double(pid+1) / num_processes * (n_samples/double(thinning) ) ) ) - 1) );
    
    for (size_t i=sim_pid_start; i<=sim_pid_end; ++i)
    {
        
        // create a new directory name for this simulation
        std::stringstream s;
        s << directory << path_separator << "posterior_predictive_sim_" << (i+1);
        std::string sim_directory_name = s.str();
        
        // now for the numerical parameters
        for ( size_t j=0; j<n_traces; ++j )
        {
            std::string parameter_name = traces[j].getParameterName();
            
            // iterate over all DAG nodes (variables)
            for ( std::vector<DagNode*>::iterator it = nodes.begin(); it!=nodes.end(); ++it )
            {
                DagNode *the_node = *it;
                
                if ( the_node->getName() == parameter_name )
                {
                    // set the value for the variable with the i-th sample
                    the_node->setValueFromString( traces[j].objectAt( i ) );
                }
            
            }
        
        }

        // next we need to simulate the data and store it
        // iterate over all DAG nodes (variables)
        for ( std::vector<DagNode*>::iterator it = nodes.begin(); it!=nodes.end(); ++it )
        {
            DagNode *the_node = *it;
            
            if ( the_node->isClamped() == true )
            {
                // redraw new values
                the_node->redraw();
                
                // we need to store the new simulated data
                the_node->writeToFile(sim_directory_name);
                
            }
            
        }
        
    } // end for over all samples
    
}
/**
 * Combine output for the monitor.
 * Overwrite this method for specialized behavior.
 */
void AbstractFileMonitor::combineReplicates( size_t n_reps )
{
    
    if ( enabled == true )
    {
        
        std::fstream combined_output_stream;
        
        int sample_number = 0;
        
        // open the stream to the file
        combined_output_stream.open( filename.c_str(), std::fstream::out);
        combined_output_stream.close();
        combined_output_stream.open( filename.c_str(), std::fstream::in | std::fstream::out);
        
        for (size_t i=0; i<n_reps; ++i)
        {
            std::stringstream ss;
            ss << "_run_" << (i+1);
            std::string s = ss.str();
            RbFileManager fm = RbFileManager(filename);
            std::string current_file_name = fm.getFilePath() + fm.getPathSeparator() + fm.getFileNameWithoutExtension() + s + "." + fm.getFileExtension();
            
            RbFileManager current_fm = RbFileManager(current_file_name);
            std::ifstream current_input_stream;
            
            if ( current_fm.openFile(current_input_stream) == false )
            {
                throw RbException( "Could not open file '" + current_file_name + "'." );
            }
            
            std::string read_line = "";
            size_t lines_skipped = 0;
            size_t lines_to_skip = 1;
            while (std::getline(current_input_stream,read_line))
            {
                ++lines_skipped;
                if ( lines_skipped <= lines_to_skip)
                {
                    if ( i == 0 )
                    {
                        // write output
                        combined_output_stream << read_line;

                        // add a new line
                        combined_output_stream << std::endl;
                    }
                    continue;
                }
                
                std::vector<std::string> fields;
                StringUtilities::stringSplit(read_line, separator, fields);
                
                // add the current sample number
                combined_output_stream << sample_number;
                ++sample_number;
                for (size_t j=1; j<fields.size(); ++j)
                {
                    // add a separator before every new element
                    combined_output_stream << separator;

                    // write output
                    combined_output_stream << fields[j];
                }
                // add a new line
                combined_output_stream << std::endl;
                
            };
            
            fm.closeFile( current_input_stream );

        }
        
        combined_output_stream.close();

    }

}