#include "GamMixtureOptimizer.h" #include "someUtil.h" #include "optGammaMixtureEM.h" #include "optGammaMixtureLS.h" #include #include #include using namespace std; GamMixtureOptimizer::GamMixtureOptimizer(stochasticProcess* pSp, const sequenceContainer& sc, const tree& inTree, unObservableData* unObservableData_p) { _pSc = ≻ _pTree = &inTree; _pSp = pSp; _unObservableData_p = unObservableData_p; _tolOptSpecific = 0.001; } GamMixtureOptimizer::~GamMixtureOptimizer() { } /////////////////////////////////////////////////////////////////////////////////////////////////////////// //findBestParamManyStarts: Finds the best gammaMixture from many starting points. //The function starts form few starting points. //For each point it tries to optimize the likellihood doing only a small number of iterations. //It then picks the best points (highest likelihood) and continue the maximization for these points only. //This can be repeated a number of times, each cycle with a different optimization algorithm. //The best gammaMixture is stored in _sp and the best likelihood is returned. //input Parameters: //pointsNum: a vector with the number of points to peformed the current cycle of optimization. //iterNum: the number of iterations to perform in each cycle. //OptAlgs: the optimization algorithm to be performed in each cycle. //tol = for determining convergence in the maximization process. MDOUBLE GamMixtureOptimizer::findBestParamManyStarts(const Vint pointsNum, const Vint iterNum, const vector OptAlgs, const Vdouble tols, const Vdouble * pWeights, ofstream* pOutF/*= NULL*/) { //make sure that the number of points in each cycle is not bigger than the previous cycle. int i; for (i = 0; i < pointsNum.size()-1; ++i) { if (pointsNum[i] < pointsNum[i+1]) errorMsg::reportError("input error in GamMixtureOptimizer::findBestParamManyStarts()"); } //create starting distributions vector distVec; const mixtureDistribution * pMixture = getMixtureDist(); for (i = 0; i < pointsNum[0]; ++i) { //the first distribution will be the current one if (i == 0) distVec.push_back(new mixtureDistribution(*pMixture)); else distVec.push_back(new mixtureDistribution(pMixture->getComponentsNum(), pMixture->categoriesForOneComponent(), LAGUERRE, 15, 15)); } //make a small number of iterations for all random starts int numOfOptCycles = pointsNum.size(); Vdouble likelihoodVec; for (i = 0; i < numOfOptCycles; ++i) { if (i != 0) { vector tmpDistVec(0); //sort results and continue optimization only with the best (pointsNum[i]) points Vdouble sortedL = likelihoodVec; sort(sortedL.begin(),sortedL.end()); MDOUBLE threshold = sortedL[sortedL.size()- pointsNum[i]]; for (int j = 0; j < likelihoodVec.size(); ++j) { if (likelihoodVec[j] >= threshold) tmpDistVec.push_back(distVec[j]); else delete distVec[j]; } distVec.clear(); distVec = tmpDistVec; } likelihoodVec.clear(); likelihoodVec.resize(pointsNum[i]); int c; for (c = 0; c < pointsNum[i]; ++c) { cerr <<"optimizing point " <