20 std::shared_ptr<const TransitionModelInterface> model,
21 std::shared_ptr<const DiscreteDistributionInterface> rDist)
25 nbStates_ = model->getNumberOfStates();
26 nbClasses_ = rDist->getNumberOfCategories();
27 pxy_.resize(nbClasses_);
28 for (
size_t i = 0; i < nbClasses_; i++)
30 pxy_[i].resize(nbStates_);
31 for (
size_t j = 0; j < nbStates_; j++)
33 pxy_[i][j].resize(nbStates_);
41 double l = getParameterValue(
"BrLen");
44 for (
size_t c = 0; c < nbClasses_; c++)
48 for (
size_t x = 0; x < nbStates_; x++)
50 Vdouble* pxy__c_x = &(*pxy__c)[x];
51 for (
size_t y = 0; y < nbStates_; y++)
53 (*pxy__c_x)[y] = Q(x, y);
64 vector<double> la(array1_->size());
65 for (
size_t i = 0; i < array1_->size(); i++)
68 for (
size_t c = 0; c < nbClasses_; c++)
70 double rc = rDist_->getProbability(c);
71 for (
size_t x = 0; x < nbStates_; x++)
73 for (
size_t y = 0; y < nbStates_; y++)
75 Li += rc * (*array1_)[i][c][x] * pxy_[c][x][y] * (*array2_)[i][c][y];
79 la[i] = weights_[i] *
log(Li);
82 sort(la.begin(), la.end());
83 for (
size_t i = array1_->size(); i > 0; i--)
93 std::shared_ptr<TransitionModelInterface> model,
94 std::shared_ptr<DiscreteDistributionInterface> rDist,
115 std::shared_ptr<TransitionModelInterface> model,
116 std::shared_ptr<DiscreteDistributionInterface> rDist,
169 const Node* son =
tree_->getNode(nodeId);
171 throw NodePException(
"DRHomogeneousTreeLikelihood::testNNI(). Node 'son' must not be the root node.", son);
174 throw NodePException(
"DRHomogeneousTreeLikelihood::testNNI(). Node 'parent' must not be the root node.", parent);
181 const Node* uncle = grandFather->
getSon(parentPosition > 1 ? 0 : 1 - parentPosition);
187 size_t nbParentNeighbors = parentNeighbors.size();
188 vector<const VVVdouble*> parentArrays(nbParentNeighbors);
189 vector<const VVVdouble*> parentTProbs(nbParentNeighbors);
190 for (
size_t k = 0; k < nbParentNeighbors; k++)
192 const Node* n = parentNeighbors[k];
202 size_t nbGrandFatherNeighbors = grandFatherNeighbors.size();
203 vector<const VVVdouble*> grandFatherArrays;
204 vector<const VVVdouble*> grandFatherTProbs;
205 for (
size_t k = 0; k < nbGrandFatherNeighbors; k++)
207 const Node* n = grandFatherNeighbors[k];
211 grandFatherTProbs.push_back(&
pxy_[n->
getId()]);
218 grandFatherArrays.push_back(sonArray);
219 grandFatherTProbs.push_back(&
pxy_[son->
getId()]);
244 parentArrays.push_back(uncleArray);
245 parentTProbs.push_back(&
pxy_[uncle->
getId()]);
256 throw Exception(
"NNIHomogeneousTreeLikelihood::testNNI. Invalid node id.");
283 throw NodePException(
"DRHomogeneousTreeLikelihood::testNNI(). Node 'son' must not be the root node.", son);
286 throw NodePException(
"DRHomogeneousTreeLikelihood::testNNI(). Node 'parent' must not be the root node.", parent);
292 Node* uncle = grandFather->
getSon(parentPosition > 1 ? 0 : 1 - parentPosition);
302 throw Exception(
"NNIHomogeneousTreeLikelihood::doNNI. Invalid node id.");
313 cerr <<
"ERROR, branch not found: " << nodeId << endl;
321 cerr <<
"DEBUG:" << endl;
323 cerr <<
"DEBUG:" << name << endl;
std::shared_ptr< DiscreteDistributionInterface > rateDistribution_
static void resetLikelihoodArray(VVVdouble &likelihoodArray)
Set all conditional likelihoods to 1.
std::vector< Node * > nodes_
Pointer toward all nodes in the tree.
std::shared_ptr< TransitionModelInterface > model_
ParameterList brLenParameters_
std::vector< double > rootFreqs_
std::map< int, VVVdouble > pxy_
const Parameter & parameter(const std::string &name) const override
Parameter & getParameter_(const std::string &name)
std::shared_ptr< TreeTemplate< Node > > tree_
static std::string CONSTRAINTS_AUTO
void initModel(std::shared_ptr< const TransitionModelInterface > model, std::shared_ptr< const DiscreteDistributionInterface > rDist)
void computeLogLikelihood()
void computeAllTransitionProbabilities()
DRASDRTreeLikelihoodNodeData & getNodeData(int nodeId)
Likelihood data structure for a node.
VVVdouble & getLikelihoodArrayForNeighbor(int neighborId)
This class implements the likelihood computation for a tree using the double-recursive algorithm.
static void computeLikelihoodFromArrays(const std::vector< const VVVdouble * > &iLik, const std::vector< const VVVdouble * > &tProb, VVVdouble &oLik, size_t nbNodes, size_t nbDistinctSites, size_t nbClasses, size_t nbStates, bool reset=true)
Compute conditional likelihoods.
double getValue() const override
Function and NNISearchable interface.
DRHomogeneousTreeLikelihood & operator=(const DRHomogeneousTreeLikelihood &lik)
DRASDRTreeLikelihoodData & likelihoodData() override
This class adds support for NNI topology estimation to the DRHomogeneousTreeLikelihood class.
NNIHomogeneousTreeLikelihood(const Tree &tree, std::shared_ptr< TransitionModelInterface > model, std::shared_ptr< DiscreteDistributionInterface > rDist, bool checkRooted=true, bool verbose=true)
Build a new NNIHomogeneousTreeLikelihood object.
NNIHomogeneousTreeLikelihood & operator=(const NNIHomogeneousTreeLikelihood &lik)
std::shared_ptr< BranchLikelihood > brLikFunction_
std::map< int, double > brLenNNIValues_
Hash used for backing up branch lengths when testing NNIs.
virtual ~NNIHomogeneousTreeLikelihood()
std::unique_ptr< BrentOneDimension > brentOptimizer_
Optimizer used for testing NNI.
void doNNI(int nodeId) override
Perform a NNI movement.
double testNNI(int nodeId) const override
Send the score of a NNI movement, without performing it.
ParameterList brLenNNIParams_
General exception thrown when something is wrong with a particular node.
The phylogenetic node class.
virtual void setDistanceToFather(double distance)
Set or update the distance toward the father node.
virtual Node * removeSon(size_t pos)
virtual int getId() const
Get the node's id.
virtual void addSon(size_t pos, Node *node)
virtual const Node * getSon(size_t pos) const
virtual const Node * getFather() const
Get the father of this node is there is one.
virtual size_t getSonPosition(const Node *son) const
virtual bool hasFather() const
Tell if this node has a father node.
virtual void setParameterValue(const std::string &name, double value)
virtual void addParameter(const Parameter ¶m)
virtual const Parameter & parameter(const std::string &name) const
virtual void printParameters(OutputStream &out) const
virtual void setValue(double value)
virtual double getValue() const
virtual void setName(const std::string &name)
Interface for phylogenetic tree objects.
std::string toString(T t)
Defines the basic types of data flow nodes.
double log(const ExtendedFloat &ef)
std::vector< double > Vdouble
std::vector< VVdouble > VVVdouble
std::vector< Vdouble > VVdouble