// $Id: bestAlpha.cpp 10046 2011-12-09 15:35:00Z rubi $ #include using namespace std; #include "bestAlpha.h" #include "bblEM.h" #include "bblEMProportionalEB.h" #include "bblLSProportionalEB.h" #include "numRec.h" #include "logFile.h" #include "errorMsg.h" #ifndef VERBOS #define VERBOS #endif //void bestAlpha::checkAllocation() { // if (_pi->stocProcessFromLabel(0)->getPijAccelerator() == NULL) { // errorMsg::reportError(" error in function findBestAlpha"); // } //} // // @@@@ The method works with oldL,oldA,bestA and newL,newA. // Only when it's about to end, the members _bestAlpha and _bestL are filled. bestAlphaAndBBL::bestAlphaAndBBL(tree& et, //find Best Alpha and best BBL const sequenceContainer& sc, stochasticProcess& sp, const Vdouble * weights, const MDOUBLE initAlpha, const MDOUBLE upperBoundOnAlpha, const MDOUBLE epsilonLoglikelihoodForAlphaOptimization, const MDOUBLE epsilonLoglikelihoodForBBL, const int maxBBLIterations, const int maxTotalIterations){ // LOG(5,<<"find Best Alpha and best BBL"< oldL+epsilonLoglikelihoodForBBL) { oldL = newL; } else { oldL=newL; _bestL = oldL; _bestAlpha= oldA; (static_cast(sp.distr()))->setAlpha(bestA); break; } } if (i==maxTotalIterations) { _bestL = newL; _bestAlpha= bestA; (static_cast(sp.distr()))->setAlpha(bestA); } } bestAlphasAndBBLProportional::bestAlphasAndBBLProportional(tree& et, //find Best Alphas (per gene - local and proportional factors - global) and best BBL vector& sc, multipleStochasticProcess* msp, gammaDistribution* pProportionDist, Vdouble initLocalRateAlphas, const MDOUBLE upperBoundOnLocalRateAlpha, const MDOUBLE initGlobalRateAlpha, const MDOUBLE upperBoundOnGlobalRateAlpha, const int maxBBLIterations, const int maxTotalIterations, const bool optimizeSelectedBranches, const bool optimizeTree, const string branchLengthOptimizationMethod, const bool optimizeLocalAlpha, const bool optimizeGlobalAlpha, const Vdouble * weights, const MDOUBLE epsilonLoglikelihoodForLocalRateAlphaOptimization, const MDOUBLE epsilonLoglikelihoodForGlobalRateAlphaOptimization, const MDOUBLE epsilonLoglikelihoodForBBL){ // LOG(5,<<"find Best Alpha and best BBL"<setAlpha(_bestGlobalAlpha); _bestLocalAlphaVec = initLocalRateAlphas; for(spIndex = 0;spIndex < msp->getSPVecSize();++spIndex){ (static_cast(msp->getSp(spIndex)->distr()))->setAlpha(_bestLocalAlphaVec[spIndex]); } //First compute the likelihood _bestLvec = likelihoodComputation::getTreeLikelihoodProportionalAllPosAlphTheSame(et,sc,msp,pProportionDist,weights); if((!optimizeTree) && (!optimizeLocalAlpha) && (!optimizeGlobalAlpha)) return; MDOUBLE currentGlobalAlpha; currentGlobalAlpha = initGlobalRateAlpha; Vdouble currentLocalAlphaVec; Vdouble newLvec; //doubleRep newL;//DR MDOUBLE newL; //doubleRep oldL(VERYSMALL);//DR MDOUBLE oldL = VERYSMALL; currentLocalAlphaVec = initLocalRateAlphas; newLvec.resize(msp->getSPVecSize()); //doubleRep epsilonLoglikelihoodForGlobalRateAlphaOptimizationDR(epsilonLoglikelihoodForGlobalRateAlphaOptimization);//DR string alphas; //doubleRep minusOne(-1.0);//DR int i; MDOUBLE a_localAlpha_x = 0.0; MDOUBLE c_localAlpha_x = upperBoundOnLocalRateAlpha; for(i=0; i < maxTotalIterations; ++i) { //Find best local alphas if(optimizeLocalAlpha){ for(spIndex = 0;spIndex < msp->getSPVecSize();++spIndex){ MDOUBLE b_localAlpha_x = _bestLocalAlphaVec[spIndex]; newLvec[spIndex] = -brent(a_localAlpha_x,b_localAlpha_x,c_localAlpha_x, C_evalLocalAlpha(et,sc[spIndex],*msp->getSp(spIndex),pProportionDist,weights), epsilonLoglikelihoodForLocalRateAlphaOptimization, ¤tLocalAlphaVec[spIndex]); if (newLvec[spIndex] >= _bestLvec[spIndex]) { _bestLvec[spIndex] = newLvec[spIndex]; _bestLocalAlphaVec[spIndex] = currentLocalAlphaVec[spIndex]; } else {//likelihood went down! LOG(2,<<"likelihood went down in optimizing local alpha"<(msp->getSp(spIndex)->distr()))->setAlpha(_bestLocalAlphaVec[spIndex]); } LOGnOUT(2,<<"Done with local alpha optimization"<= sumVdouble(_bestLvec)) { //converged _bestGlobalAlpha = currentGlobalAlpha; } else {//likelihood went down! LOG(2,<<"likelihood went down in optimizing global alpha"<setAlpha(_bestGlobalAlpha); //whether or not likelihood has improved we need to update _bestLvec _bestLvec = likelihoodComputation::getTreeLikelihoodProportionalAllPosAlphTheSame(et,sc,msp,pProportionDist,weights); LOGnOUT(2,<<"Done with global alpha optimization"< oldL+epsilonLoglikelihoodForBBL) { //global and local alpha have already been updated individually oldL = sumVdouble(_bestLvec); } else { break; } LOGnOUT(2,<<"Done with optimization iteration "< oldL+epsilonLoglikelihoodForBBL) { oldL = newL; } else { oldL=newL; _bestL = oldL; _bestBeta= oldB; (static_cast(sp.distr()))->setBeta(bestB); break; } } if (i==maxTotalIterations) { _bestL = newL; _bestBeta= bestB; (static_cast(sp.distr()))->setBeta(bestB); } } bestAlphaFixedTree::bestAlphaFixedTree(const tree& et, //findBestAlphaFixedTree const sequenceContainer& sc, stochasticProcess& sp, const Vdouble * weights, const MDOUBLE upperBoundOnAlpha, const MDOUBLE epsilonLoglikelihoodForAlphaOptimization){ //LOG(5,<<"findBestAlphaFixedTree"<(sp.distr())->getAlpha(); const MDOUBLE ax=0.0; _bestL = -brent(ax,bx,cx, C_evalAlpha(et,sc,sp,weights), epsilonLoglikelihoodForAlphaOptimization, &bestA); (static_cast(sp.distr()))->setAlpha(bestA); _bestAlpha= bestA; } bestAlphaAndBetaAndBBL::bestAlphaAndBetaAndBBL(tree& et, //find Best Alpha and best BBL const sequenceContainer& sc, stochasticProcess& sp, const Vdouble * weights, const MDOUBLE initAlpha, const MDOUBLE initBeta, const MDOUBLE upperBoundOnAlpha, const MDOUBLE upperBoundOnBeta, const MDOUBLE epsilonLoglikelihoodForAlphaOptimization, const MDOUBLE epsilonLoglikelihoodForBetaOptimization, const MDOUBLE epsilonLoglikelihoodForBBL, const int maxBBLIterations, const int maxTotalIterations){ // LOG(5,<<"find Best Alpha and Beta and best BBL"< oldL+epsilonLoglikelihoodForBBL) { optimize = true; } (static_cast(sp.distr()))->setAlpha(bestA); //optimize beta newL = -brent(dx,ex,fx, C_evalBeta(et,sc,sp,weights), epsilonLoglikelihoodForBetaOptimization, &bestB); ex = bestB; #ifdef VERBOS LOG(5,<<"# bestAlphaAndBetaAndBBL::bestAlphaAndBetaAndBBL iteration " << i < oldL+epsilonLoglikelihoodForBBL) { optimize = true; } (static_cast(sp.distr()))->setBeta(bestB); //bblEM bblEM bblEM1(et,sc,sp,NULL,maxBBLIterations,epsilonLoglikelihoodForBBL);//maxIterations=1000 newL =bblEM1.getTreeLikelihood(); #ifdef VERBOS LOG(5,<<"# bestAlphaAndBetaAndBBL::bestAlphaAndBetaAndBBL iteration " << i < oldL+epsilonLoglikelihoodForBBL) { optimize = true; } if (!optimize) break; } }