16.2 The StateFreqUpdater Class

(Mac version)

< 16.1 | 16.2 | 16.3 >

This class is derived from DirichletUpdater, which is, in turn, derived from Updater. It thus inherits everything that both Updater and DirichletUpdater can do. Remember that DirichletUpdater filled in some of the functionality that Updater specified but didn’t implement, namely the overrides of the pure virtual functions proposeNewState and revert. All we must do in this class is fill in blanks specific to the parameter being updated: DirichletUpdater does everything that is generic to any parameter with a Dirichlet prior distribution.

Create a new header file named statefreq_updater.hpp and fill it with the following.

#pragma once    

#include "dirichlet_updater.hpp"

namespace strom {
    
    class StateFreqUpdater : public DirichletUpdater {
        public:
            typedef std::shared_ptr&lt; StateFreqUpdater &gt; SharedPtr;

                                            StateFreqUpdater(QMatrix::SharedPtr qmatrix);
                                            ~StateFreqUpdater();
        
        private:
        
            void                            pullFromModel();
            void                            pushToModel();

            QMatrix::SharedPtr              _qmatrix;
        };

    // member function bodies go here
    
}   

Constructor and destructor

The constructor first calls the clear function defined by the DirichletUpdater base class. It then sets its _name to “State Frequencies” and its _qmatrix data member to the shared pointer qmatrix supplied as an argument. This points to the QMatrix object that manages the state frequencies that this class will update. The destructor is merely a placeholder, as usual.

    inline StateFreqUpdater::StateFreqUpdater(QMatrix::SharedPtr qmatrix) {   
        //std::cout &lt;&lt; "Creating a StateFreqUpdater" &lt;&lt; std::endl;
        DirichletUpdater::clear();
        _name = "State Frequencies";
        assert(qmatrix);
        _qmatrix = qmatrix;
    }

    inline StateFreqUpdater::~StateFreqUpdater() {
        //std::cout &lt;&lt; "Destroying a StateFreqUpdater" &lt;&lt; std::endl;
    }   

The pullFromModel and pushToModel member functions

The pullFromModel function first obtains a shared pointer to the state frequencies vector of the QMatrix object pointed to by _qmatrix. It then copies those state frequencies into the _curr_point vector. The pushToModel function copies the values from _curr_point to the model via the setStateFreqs member function of QMatrix, which handles recalculation of the instantaneous rate matrix and the corresponding eigen decomposition using the new state frequencies.

    inline void StateFreqUpdater::pullFromModel() {  
        QMatrix::freq_xchg_ptr_t freqs = _qmatrix-&gt;getStateFreqsSharedPtr();
        _curr_point.assign(freqs-&gt;begin(), freqs-&gt;end());
    }
    
    inline void StateFreqUpdater::pushToModel() {
        _qmatrix-&gt;setStateFreqs(_curr_point);
    }   

That’s it!

The DirichletUpdater base class does almost all the work, so only a small amount of work in the constructor and the pullFromModel and pushToModel functions was needed to implement a state frequency updater.

< 16.1 | 16.2 | 16.3 >