/*---------------------------------------------------------------------------------------------------------------------- | Constructor initializes the conditional likelihood vectors using `nRates' and `nStates', and initializes the | `pMatrices' data member using `pMat'. The parameter `managePMatrices' determines whether to allocate space for the | transition probability matrices. */ InternalData::InternalData( PartitionModelShPtr partition, /**< is the PartitionModel object containing information about the number of states and rates for each subset */ CondLikelihoodStorageShPtr cla_storage) : state(-1), cla_pool(cla_storage) { const unsigned num_subsets = partition->getNumSubsets(); pMatrices.resize(num_subsets); for (unsigned i = 0; i < num_subsets; ++i) { const unsigned num_rates = partition->subset_num_rates[i]; const unsigned num_states = partition->subset_num_states[i]; pMatrices[i].Initialize(num_rates, num_states, num_states); } }
/*---------------------------------------------------------------------------------------------------------------------- | Calls proposeNewState(), then decides whether to accept or reject the proposed new state, calling accept() or | revert(), whichever is appropriate. | | We actually generate a new mapping all the time (in proposeNewState). Then we change the branch length. The branch length | change might be rejected. */ bool UnimapEdgeMove::update() { #if 1 || DISABLED_UNTIL_UNIMAP_WORKING_WITH_PARTITIONING // The only case in which is_fixed is true occurs when the user decides to fix the edge lengths. // A proposed UnimapEdgeMove cannot be accepted without changing edge lengths, so it is best to just bail out now. if (is_fixed) return false; // std::cerr << "****** UnimapEdgeMove::update" << std::endl; ChainManagerShPtr p = chain_mgr.lock(); PHYCAS_ASSERT(p); //likelihood->fullRemapping(tree, rng, true); ///@TEMP!!!! proposeNewState(); PartitionModelShPtr partModel = likelihood->getPartitionModel(); const unsigned nSubsets = partModel->getNumSubsets(); std::vector<double> uniformization_lambda(nSubsets); for (unsigned i = 0; i < nSubsets; ++i) { ModelShPtr subMod = partModel->getModel(i); uniformization_lambda[i] = subMod->calcUniformizationLambda(); } bool is_internal_edge = origNode->IsInternal(); double prev_ln_prior = (is_internal_edge ? p->calcInternalEdgeLenPriorUnnorm(origEdgelen) : p->calcExternalEdgeLenPriorUnnorm(origEdgelen)); double curr_edgelen = r*origEdgelen; double curr_ln_prior = (is_internal_edge ? p->calcInternalEdgeLenPriorUnnorm(curr_edgelen) : p->calcExternalEdgeLenPriorUnnorm(curr_edgelen)); double log_posterior_ratio = 0.0; const double log_prior_ratio = curr_ln_prior - prev_ln_prior; double log_likelihood_ratio = (double)mdot*log(r); const double edgeLenDiff = (curr_edgelen - origEdgelen); for (unsigned i = 0; i < nSubsets; ++i) { const unsigned numSites = partModel->getNumSites(i); const double ul = uniformization_lambda[i]; PHYCAS_ASSERT(ul > 0.0); log_likelihood_ratio -= numSites*ul*edgeLenDiff; } likelihood->incrementNumLikelihoodEvals(); if (is_standard_heating) log_posterior_ratio = heating_power*(log_likelihood_ratio + log_prior_ratio); else log_posterior_ratio = heating_power*log_likelihood_ratio + log_prior_ratio; double ln_accept_ratio = log_posterior_ratio + getLnHastingsRatio(); double lnu = std::log(rng->Uniform(FILE_AND_LINE)); /* std::cerr << " log_likelihood_ratio = " << log_likelihood_ratio << '\n'; std::cerr << " log_posterior_ratio = " << log_posterior_ratio << '\n'; std::cerr << " ln_accept_ratio = " << ln_accept_ratio << '\n'; */ if (ln_accept_ratio >= 0.0 || lnu <= ln_accept_ratio) { p->setLastLnPrior(p->getLastLnPrior() + log_prior_ratio); p->setLastLnLike(p->getLastLnLike() + log_likelihood_ratio); accept(); return true; } else { curr_ln_like = p->getLastLnLike(); curr_ln_prior = p->getLastLnPrior(); revert(); return false; } #else return true; #endif }