File: bestAlpha.cpp

package info (click to toggle)
fastml 3.11-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,772 kB
  • sloc: cpp: 48,522; perl: 3,588; ansic: 819; makefile: 386; python: 83; sh: 55
file content (444 lines) | stat: -rw-r--r-- 15,584 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
// $Id: bestAlpha.cpp 10046 2011-12-09 15:35:00Z rubi $

#include <iostream>
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"<<endl);
//	LOG(5,<<" 1. bestAlpha::findBestAlpha"<<endl);
//	brLenOpt br1(*et,*pi,weights);
	
	MDOUBLE oldL = VERYSMALL;
	MDOUBLE newL = VERYSMALL;
	const MDOUBLE bx=initAlpha;
	const MDOUBLE ax=0;
	const MDOUBLE cx=upperBoundOnAlpha;
//
	MDOUBLE bestA=0;
	MDOUBLE oldA=0;
	int i=0;
	for (i=0; i < maxTotalIterations; ++i) {
		newL = -brent(ax,bx,cx,
		C_evalAlpha(et,sc,sp,weights),
		epsilonLoglikelihoodForAlphaOptimization,
		&bestA);
 
#ifdef VERBOS
		LOG(5,<<"# bestAlphaAndBBL::bestAlphaAndBBL iteration " << i <<endl
		      <<"# old L = " << oldL << "\t"
		      <<"# new L = " << newL << endl
		      <<"# new Alpha = " << bestA << endl);
#endif
		if (newL > oldL+epsilonLoglikelihoodForBBL) {
		    oldL = newL;
		    oldA = bestA;
		} else {
		    oldL = newL;
		    oldA = bestA;

		    
		    _bestL = oldL;
		    _bestAlpha= oldA;
		    (static_cast<gammaDistribution*>(sp.distr()))->setAlpha(bestA);
		    break;
		}

		(static_cast<gammaDistribution*>(sp.distr()))->setAlpha(bestA);
		bblEM bblEM1(et,sc,sp,NULL,maxBBLIterations,epsilonLoglikelihoodForBBL);//maxIterations=1000
		newL =bblEM1.getTreeLikelihood();
#ifdef VERBOS
		LOG(5,<<"# bestAlphaAndBBL::bestAlphaAndBBL iteration " << i <<endl 
		      <<"# After BBL new L = "<<newL<<" old L = "<<oldL<<endl
		      <<"# The tree:" );
		LOGDO(5,et.output(myLog::LogFile()));
#endif

		if (newL > oldL+epsilonLoglikelihoodForBBL) {
			oldL = newL;
		}
		else {
		    oldL=newL;
		    _bestL = oldL;	
			_bestAlpha= oldA; 
			(static_cast<gammaDistribution*>(sp.distr()))->setAlpha(bestA);
			break;
		}
	}
	if (i==maxTotalIterations) {
		_bestL = newL;
		_bestAlpha= bestA;
		(static_cast<gammaDistribution*>(sp.distr()))->setAlpha(bestA);
	}
}

bestAlphasAndBBLProportional::bestAlphasAndBBLProportional(tree& et, //find Best Alphas (per gene - local and proportional factors - global) and best BBL
					   vector<sequenceContainer>& 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"<<endl);
//	LOG(5,<<" 1. bestAlpha::findBestAlpha"<<endl);
//	brLenOpt br1(*et,*pi,weights);
	
	
    if(initLocalRateAlphas.size() != sc.size()){
		MDOUBLE val = initLocalRateAlphas[0]; 
		initLocalRateAlphas.resize(sc.size(),val);
	}
	int spIndex;
	_bestGlobalAlpha = initGlobalRateAlpha;
	pProportionDist->setAlpha(_bestGlobalAlpha);
	_bestLocalAlphaVec = initLocalRateAlphas;
	for(spIndex = 0;spIndex < msp->getSPVecSize();++spIndex){
		(static_cast<gammaDistribution*>(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,
				&currentLocalAlphaVec[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"<<endl<<"oldL = "<<sumVdouble(_bestLvec));
				}
			    (static_cast<gammaDistribution*>(msp->getSp(spIndex)->distr()))->setAlpha(_bestLocalAlphaVec[spIndex]);
			}
			LOGnOUT(2,<<"Done with local alpha optimization"<<endl<<"LL:"<<sumVdouble(_bestLvec)<<endl);
			LOGnOUT(2,<<"Local Alphas:");
			for(spIndex = 0;spIndex < _bestLocalAlphaVec.size();++spIndex){
				LOGnOUT(2,<<_bestLocalAlphaVec[spIndex]<<",";);
			}
			LOGnOUT(2,<<endl);
			_bestLvec = likelihoodComputation::getTreeLikelihoodProportionalAllPosAlphTheSame(et,sc,msp,pProportionDist,weights);
		}
		//Find best global alpha
		if(optimizeGlobalAlpha){
			//doubleRep b_globalAlpha_x(_bestGlobalAlpha);//DR
            //doubleRep a_globalAlpha_x(0.0);//DR
			//doubleRep c_globalAlpha_x(upperBoundOnGlobalRateAlpha);//DR
			MDOUBLE b_globalAlpha_x = _bestGlobalAlpha;
            MDOUBLE a_globalAlpha_x = 0.0;
			MDOUBLE c_globalAlpha_x = upperBoundOnGlobalRateAlpha;

			//newL = minusOne*brentDoubleRep(a_globalAlpha_x,b_globalAlpha_x,c_globalAlpha_x,
			//C_evalGlobalAlpha(et,sc,msp,pProportionDist,weights),
			//epsilonLoglikelihoodForGlobalRateAlphaOptimizationDR,
			//&_bestGlobalAlpha);//DR

			newL = -brent(a_globalAlpha_x,b_globalAlpha_x,c_globalAlpha_x,
			C_evalGlobalAlpha(et,sc,msp,pProportionDist,weights),
			epsilonLoglikelihoodForGlobalRateAlphaOptimization,
			&currentGlobalAlpha);

			if (newL >= sumVdouble(_bestLvec)) { //converged   
				_bestGlobalAlpha = currentGlobalAlpha;
			}
			else
			{//likelihood went down!
				LOG(2,<<"likelihood went down in optimizing global alpha"<<endl<<"oldL = "<<sumVdouble(_bestLvec));
			}
            pProportionDist->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"<<endl<<"LL:"<<sumVdouble(_bestLvec)<<endl);
			LOGnOUT(2,<<"Global Alpha:"<<_bestGlobalAlpha<<endl);
		}
		
		if(optimizeTree){
			if(branchLengthOptimizationMethod == "bblLS"){
				bblLSProportionalEB bblLSPEB1(et,sc,msp,pProportionDist,_bestLvec,optimizeSelectedBranches,maxBBLIterations,epsilonLoglikelihoodForBBL);
				_bestLvec = bblLSPEB1.getTreeLikelihoodVec();
				LOGnOUT(2,<<"Done with bblLS"<<endl<<"LL:"<<sumVdouble(_bestLvec)<<endl);
			}
			else if(branchLengthOptimizationMethod == "bblEM"){
				bblEMProportionalEB bblEMPEB1(et,sc,msp,pProportionDist,optimizeSelectedBranches,NULL,maxBBLIterations,epsilonLoglikelihoodForBBL);//maxIterations=1000
				_bestLvec = bblEMPEB1.getTreeLikelihood();
				LOGnOUT(2,<<"Done with bblEM"<<endl<<"LL:"<<sumVdouble(_bestLvec)<<endl);
			}
			LOGnOUT(2,<<et.stringTreeInPhylipTreeFormat()<<endl);
		}
		if (sumVdouble(_bestLvec) > oldL+epsilonLoglikelihoodForBBL) {
			//global and local alpha have already been updated individually
			oldL = sumVdouble(_bestLvec);
		}
		else {
			break;
		}
		LOGnOUT(2,<<"Done with optimization iteration "<<i<<". LL: "<<sumVdouble(_bestLvec)<<endl);
	}
}

bestBetaAndBBL::bestBetaAndBBL(tree& et, //find Best Alpha and best BBL
					   const sequenceContainer& sc,
					   stochasticProcess& sp,
					   const Vdouble * weights,
					   const MDOUBLE initBeta,
					   const MDOUBLE upperBoundOnBeta,
					   const MDOUBLE epsilonLoglikelihoodForBetaOptimization,
					   const MDOUBLE epsilonLoglikelihoodForBBL,
					   const int maxBBLIterations,
					   const int maxTotalIterations){
//	LOG(5,<<"find Best Beta and best BBL"<<endl);
//	LOG(5,<<" 1. bestBetaa::findBestBeta"<<endl);
//	brLenOpt br1(*et,*pi,weights);
	
	MDOUBLE oldL = VERYSMALL;
	MDOUBLE newL = VERYSMALL;
	const MDOUBLE bx=initBeta;
	const MDOUBLE ax=0;
	const MDOUBLE cx=upperBoundOnBeta;
//
	MDOUBLE bestB=0;
	MDOUBLE oldB=0;
	int i=0;
	for (i=0; i < maxTotalIterations; ++i) {
		newL = -brent(ax,bx,cx,
		C_evalBeta(et,sc,sp,weights),
		epsilonLoglikelihoodForBetaOptimization,
		&bestB);
 
#ifdef VERBOS
		LOG(5,<<"# bestBetaAndBBL::bestBetaAndBBL iteration " << i <<endl
		      <<"# old L = " << oldL << "\t"
		      <<"# new L = " << newL << endl
		      <<"# new Beta = " << bestB << endl);
#endif
		if (newL > oldL+epsilonLoglikelihoodForBBL) {
		    oldL = newL;
		    oldB = bestB;
		} else {
		    oldL = newL;
		    oldB = bestB;

		    
		    _bestL = oldL;
		    _bestBeta= oldB;
		    (static_cast<gammaDistribution*>(sp.distr()))->setBeta(bestB);
		    break;
		}

		(static_cast<gammaDistribution*>(sp.distr()))->setBeta(bestB);
		bblEM bblEM1(et,sc,sp,NULL,maxBBLIterations,epsilonLoglikelihoodForBBL);//maxIterations=1000
		newL =bblEM1.getTreeLikelihood();
#ifdef VERBOS
		LOG(5,<<"# bestBetaAndBBL::bestBetaAndBBL iteration " << i <<endl 
		      <<"# After BBL new L = "<<newL<<" old L = "<<oldL<<endl
		      <<"# The tree:" );
		LOGDO(5,et.output(myLog::LogFile()));
#endif

		if (newL > oldL+epsilonLoglikelihoodForBBL) {
			oldL = newL;
		}
		else {
		    oldL=newL;
		    _bestL = oldL;	
			_bestBeta= oldB; 
			(static_cast<gammaDistribution*>(sp.distr()))->setBeta(bestB);
			break;
		}
	}
	if (i==maxTotalIterations) {
		_bestL = newL;
		_bestBeta= bestB;
		(static_cast<gammaDistribution*>(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"<<endl);
	MDOUBLE bestA=0;
	const MDOUBLE cx=upperBoundOnAlpha;// left, midle, right limit on alpha
	const MDOUBLE bx=static_cast<gammaDistribution*>(sp.distr())->getAlpha();
	const MDOUBLE ax=0.0;

	
	_bestL = -brent(ax,bx,cx,
		C_evalAlpha(et,sc,sp,weights),
		epsilonLoglikelihoodForAlphaOptimization,
		&bestA);
	(static_cast<gammaDistribution*>(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"<<endl);
//	LOG(5,<<" 1. bestAlphaAndBetaAndBBL::findBestAlphaAndBeta"<<endl);
//	brLenOpt br1(*et,*pi,weights);
	
	MDOUBLE oldL = VERYSMALL;
	MDOUBLE newL = VERYSMALL;
	MDOUBLE bx=initAlpha;
	const MDOUBLE ax=0;
	const MDOUBLE cx=upperBoundOnAlpha;
	MDOUBLE ex=initBeta;
	const MDOUBLE dx=0;
	const MDOUBLE fx=upperBoundOnBeta;
	bool optimize = false;

//
	MDOUBLE bestA=0;
	MDOUBLE oldA=0;
	MDOUBLE bestB=0;
	MDOUBLE oldB=0;
	int i=0;
	for (i=0; i < maxTotalIterations; ++i) {
//optimize alpha
		newL = -brent(ax,bx,cx,
		C_evalAlpha(et,sc,sp,weights),
		epsilonLoglikelihoodForAlphaOptimization,
		&bestA);
		bx = bestA;
 
#ifdef VERBOS
		LOG(5,<<"# bestAlphaAndBetaAndBBL::bestAlphaAndBetaAndBBL iteration " << i <<endl
		      <<"# old L = " << oldL << "\t"
		      <<"# new L = " << newL << endl
		      <<"# new Alpha = " << bestA << endl);
#endif
		if(newL < oldL)
			errorMsg::reportError("likelihood decreased in alhpa optimization step in bestAlphaAndBetaAndBBL::bestAlphaAndBetaAndBBL");
		oldL = newL;
		oldA = bestA;
		_bestL = newL;
		_bestAlpha= bestA;
		if (newL > oldL+epsilonLoglikelihoodForBBL) {
			optimize = true;
		}
		(static_cast<generalGammaDistribution*>(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 <<endl
		      <<"# old L = " << oldL << "\t"
		      <<"# new L = " << newL << endl
		      <<"# new Beta = " << bestB << endl);
#endif
		if(newL < oldL)
			errorMsg::reportError("likelihood decreased in beta optimization step in bestAlphaAndBetaAndBBL::bestAlphaAndBetaAndBBL");
	    oldL = newL;
	    oldB = bestB;
	    _bestL = oldL;
	    _bestBeta= oldB;
		if (newL > oldL+epsilonLoglikelihoodForBBL) {
			optimize = true;
		} 
		(static_cast<generalGammaDistribution*>(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 <<endl 
		      <<"# After BBL new L = "<<newL<<" old L = "<<oldL<<endl
		      <<"# The tree:" );
		LOGDO(5,et.output(myLog::LogFile()));
#endif
		if(newL < oldL)
			errorMsg::reportError("likelihood decreased in bbl optimization step in bestAlphaAndBetaAndBBL::bestAlphaAndBetaAndBBL");
		oldL = newL;
		_bestL = newL;
		if (newL > oldL+epsilonLoglikelihoodForBBL) {
			optimize = true;
		}
		if (!optimize)
			break;
	}
}