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< StateFreqUpdater > SharedPtr;
StateFreqUpdater(QMatrix::SharedPtr qmatrix);
~StateFreqUpdater();
private:
void pullFromModel();
void pushToModel();
QMatrix::SharedPtr _qmatrix;
};
// member function bodies go here
}
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 << "Creating a StateFreqUpdater" << std::endl;
DirichletUpdater::clear();
_name = "State Frequencies";
assert(qmatrix);
_qmatrix = qmatrix;
}
inline StateFreqUpdater::~StateFreqUpdater() {
//std::cout << "Destroying a StateFreqUpdater" << std::endl;
}
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->getStateFreqsSharedPtr();
_curr_point.assign(freqs->begin(), freqs->end());
}
inline void StateFreqUpdater::pushToModel() {
_qmatrix->setStateFreqs(_curr_point);
}
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.