File: bestParamUSSRV.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 (474 lines) | stat: -rwxr-xr-x 16,451 bytes parent folder | download | duplicates (5)
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
// 	$Id: bestParamUSSRV.cpp 4951 2008-09-24 11:16:58Z osnatz $	
#include "bestParamUSSRV.h"

/* structure of this method:
(1) checks of the number of parameters to optimize, and decide how many parameters optimizations iteration,
and how many parameters+bbl iterations will be done.
(2) A loop over the parameters+bbl iterations
	(2.1) A loop over the parameters optimization iterations
		(2.1.1) Optimize alpha
		(2.1.2) Optimize nu
		(2.1.3) Optimize f
		if the likelihood wasn't changed during this loop --> parameters converged --> break
	(2.2) BBL
	if the likelihood wasn't changed during this loop --> parameters+bbl converged --> break
(3) return likelihood
*/

// ***************
// *    USSRV    *
// ***************

MDOUBLE bestParamUSSRV::operator() (tree& et,
									const sequenceContainer& sc,
									const sequenceContainer& baseSc,
									ussrvModel& model,
									const Vdouble * weights /* =NULL */,
									const MDOUBLE AlphaUpperBound /* = 15 */, 
									const MDOUBLE NuUpperBound /* = 15 */, 
									const MDOUBLE FUpperBound /* = 1 */, 
									const MDOUBLE epsilonParamOptimization /* = 0.01 */,
									const MDOUBLE epsilonLikelihoodImprovment /* = 0.01 */,
									const int maxIterations /* = 50 */,
									const int maxOfParametersAndBblIterations /* = 40 */)
{
	_bestL = VERYSMALL;
	MDOUBLE newL = VERYSMALL;	

	bestAlphaFixedTreeUSSRV alphaOptimization;
	bestNuFixedTreeUSSRV nuOptimization;
	bestFFixedTreeUSSRV fOptimization;
	
	int it, bblIt;
	int numberOfIterations(maxIterations);
	int numberOfParametersAndBblIterations(maxOfParametersAndBblIterations);
	
	// if only one parameter is optimize (only Alpha or only Nu or only F) then we need only one iteration.
	// if we only do bbl, without any optimization of the parameters, then we don't need iterations at all.
	int countParameters2Optimize(0);
	if (_AlphaOptimizationFlag) countParameters2Optimize++;
	if (_NuOptimizationFlag) countParameters2Optimize++;
	if (_FOptimizationFlag) countParameters2Optimize++;

	if (countParameters2Optimize==0)
	{
		numberOfIterations=0;
		numberOfParametersAndBblIterations=1;
	}
	else if (countParameters2Optimize==1)
		numberOfIterations=1;
	
	if (_bblOptimizationFlag == false)
		numberOfParametersAndBblIterations = 1;
	
	_bestAlpha = model.getAlpha();
	_bestNu = model.getNu();
	_bestF = model.getF();

	bool changes(false);
	bool bblChanges(false);
	for (bblIt=0; bblIt < numberOfParametersAndBblIterations; ++bblIt)
	{
		LOG(8,<<"bestParamUSSRV, params+bbl, iteration: " << bblIt << endl);
		bblChanges = false;
		// parameters optimizations (without bbl)
		// in each iteration : optimization of Alpha and then optimization of Nu, and then of F.
		for (it=0; it < numberOfIterations; ++it)
		{
			changes = false;	
			// Alpha optimization
			if (_AlphaOptimizationFlag)
			{
				LOGDO(5,printTime(myLog::LogFile()));
				newL = alphaOptimization(et,sc,baseSc,model,weights,AlphaUpperBound,epsilonParamOptimization);

				//the improvement in Likelihood is smaller than epsilon
				if (newL < _bestL)
				{				
					LOG(5,<<"likelihood went down in LS! (Alpha optimization)"<<endl<<"oldL = "<<_bestL<<" newL= "<<newL<<endl);
					//go back to previous alpha
					alphaOptimization.setAlpha(_bestAlpha,model);
					alphaOptimization.setBestL(_bestL); // @@@@ maybe this is unnecessary 
					//break;
				}
				else 
				{// update of likelihood and model.
					if (newL > _bestL+epsilonLikelihoodImprovment) 
					{
						changes = true;
						bblChanges = true;
					}
					LOG(9,<<"newL = " << newL << " _bestL = " << _bestL << " epsilonLikelihoodImprovment = " << epsilonLikelihoodImprovment << endl);
					_bestL = newL;
					_bestAlpha = alphaOptimization.getBestAlpha();
					LOG(5,<<"new L = " << _bestL<<"  new Alpha = " << _bestAlpha<<endl);		
				}
			}
		
			// Nu optimization
			if (_NuOptimizationFlag)
			{
				LOGDO(5,printTime(myLog::LogFile()));
				newL = nuOptimization(et,sc,baseSc,model,weights,NuUpperBound,epsilonParamOptimization);
			
				//the improvement in Likelihood is smaller than epsilon
				if (newL < _bestL)
				{
					LOG(5,<<"likelihood went down in LS! (Nu optimization)"<<endl<<"oldL = "<<_bestL<<" newL= "<<newL<<endl);
					//go back to previous Nu
					nuOptimization.setNu(_bestNu,model);
					nuOptimization.setBestL(_bestL); // @@@@ maybe this is unnecessary 
					//break;
				}
				else
				{// update of likelihood and model.
					if (newL > _bestL+epsilonLikelihoodImprovment) 
					{
						changes = true;
						bblChanges = true;
					}
					LOG(9,<<"newL = " << newL << " _bestL = " << _bestL << " epsilonLikelihoodImprovment = " << epsilonLikelihoodImprovment << endl);
					_bestL = newL;
					_bestNu = nuOptimization.getBestNu();
					LOG(5,<<"new L = " << _bestL<<"  new Nu = " << _bestNu<<endl);		
				}
			}

			// F optimization
			if (_FOptimizationFlag)
			{
				LOGDO(5,printTime(myLog::LogFile()));
				newL = fOptimization(et,sc,baseSc,model,weights,FUpperBound,epsilonParamOptimization);

				//the improvement in Likelihood is smaller than epsilon
				if (newL < _bestL)
				{
					LOG(5,<<"likelihood went down in LS! (F optimization)"<<endl<<"oldL = "<<_bestL<<" newL= "<<newL<<endl);
					//go back to previous F
					fOptimization.setF(_bestF,model);
					fOptimization.setBestL(_bestL); // @@@@ maybe this is unnecessary 
					//break;
				}
				else 
				{// update of likelihood and model.
					if (newL > _bestL+epsilonLikelihoodImprovment ) 
					{
						changes = true;
						bblChanges = true;
					}
					LOG(9,<<"newL = " << newL << " _bestL = " << _bestL << " epsilonLikelihoodImprovment = " << epsilonLikelihoodImprovment << endl);
					_bestL = newL;
					_bestF = fOptimization.getBestF();
					LOG(5,<<"new L = " << _bestL<<"  new F = " << _bestF<<endl);						
				}
			}
			if (changes == false)
			{
				LOG(5,<<"bestParamUSSRV parameters alpha,nu,f converged!"<<endl);
				break;
			}
		}

		if (changes == true)
			LOG(5,<<"bestParamUSSRV parameters alpha, nu, f, did not converge after " << numberOfIterations << " iterations"<<endl);


		// BBL
		if (_bblOptimizationFlag == true)
		{
			LOGDO(5,printTime(myLog::LogFile()));
			bblEM2USSRV bbl(et,sc,baseSc,model,weights,maxIterations);
			newL = bbl.getTreeLikelihood();
			LOG(5,<<"current best L= "<<_bestL<<endl);
			LOG(5,<<"new L After BBL = " << newL<< " = "<< bbl.getTreeLikelihood() <<endl);
			LOG(5,<<"The new tree is: " << endl);
			if (5 <= myLog::LogLevel()) 
				et.output(myLog::LogFile());
			LOG(5,<<endl);
			if (newL > _bestL+epsilonLikelihoodImprovment)
				bblChanges = true;
			if (newL < _bestL){
				LOG(5,<<"likelihood went down in LS! (BBL)"<<endl<<"oldL = "<<_bestL);
				LOG(5,<<" newL= "<<newL<<endl) ;
			}
			else
				_bestL = newL;
		}

		if (bblChanges == false)
		{
			LOG(5,<<"bestParamUSSRV bbl and parameters converged!"<<endl);
			break;
		}
	}

	if (bblIt == numberOfParametersAndBblIterations)
		LOG(5,<<"bestParamUSSRV bbl and parameters alpha did not converge after " << numberOfParametersAndBblIterations << "iterations"<<endl);	

	LOGDO(5,printTime(myLog::LogFile()));
	return _bestL;
}



// ***************
// *    SSRV    *
// ***************

MDOUBLE bestParamSSRV::operator() (tree& et,
								   const sequenceContainer& sc,
								   stochasticProcessSSRV& ssrvSp,
								   const Vdouble * weights /* =NULL */,
								   const MDOUBLE AlphaUpperBound /* = 15 */, 
								   const MDOUBLE NuUpperBound /* = 15 */, 
								   const MDOUBLE TrTvUpperBound /* = 10 */,
								   const MDOUBLE epsilonParamOptimization /* = 0.01 */,
								   const MDOUBLE epsilonLikelihoodImprovment /* = 0.01 */,
								   const MDOUBLE epsilonBbl /*= 0.05 */,
								   const int maxIterations /* = 50 */,
								   const int maxOfParametersAndBblIterations /* = 40 */)
{
	_bestL = VERYSMALL;
	MDOUBLE newL = VERYSMALL;	

	bestAlphaFixedTreeSSRV alphaOptimization;
	bestNuFixedTreeSSRV nuOptimization;
	bestTamura92ParamFixedTreeSSRV tamura92Optimization;
	
	int it, bblIt;
	int numberOfIterations(maxIterations);
	int numberOfParametersAndBblIterations(maxOfParametersAndBblIterations);

	// if only one parameter is optimize (only Alpha or only Nu or only tamura92) then we need only one iteration.
	// if we only do bbl, without any optimization of the parameters, then we don't need iterations at all.
	int countParameters2Optimize(0);
	if (_AlphaOptimizationFlag) countParameters2Optimize++;
	if (_NuOptimizationFlag) countParameters2Optimize++;
	if (_tamura92OptimizationFlag) countParameters2Optimize++;


	if (countParameters2Optimize==0)
	{
		numberOfIterations=0;
		numberOfParametersAndBblIterations=1;
	}
	else if (countParameters2Optimize==1)
		numberOfIterations=1;

	if (_bblOptimizationFlag == false)
		numberOfParametersAndBblIterations = 1;

	replacementModelSSRV* pMulRM = static_cast<replacementModelSSRV*>(ssrvSp.getPijAccelerator()->getReplacementModel());
	gammaDistribution* gammaDist = static_cast<gammaDistribution*>(pMulRM->getDistribution()); 
	_bestAlpha = gammaDist->getAlpha();
	_bestNu = pMulRM->getRateOfRate();


	bool changes(false);
	bool bblChanges(false);

	for (bblIt=0; bblIt < numberOfParametersAndBblIterations; ++bblIt)
	{
		bblChanges = false;

		// Set initial values of lower/upper bounds for params
		MDOUBLE AlphaLowerBoundCur = 0.0;
		MDOUBLE AlphaUpperBoundCur = AlphaUpperBound;
		MDOUBLE NuLowerBoundCur = 0.0;
		MDOUBLE NuUpperBoundCur = NuUpperBound;
		MDOUBLE TrTvLowerBoundCur = 0.0;
		MDOUBLE TrTvUpperBoundCur = TrTvUpperBound;
		MDOUBLE ThetaLowerBoundCur = 0.0;
		MDOUBLE ThetaUpperBoundCur = 1.0;
		// And for epsilon
		MDOUBLE epsilonParamOptimizationCur = epsilonParamOptimization;

		// parameters optimizations (without bbl)
		// in each iteration : optimization of Alpha and then optimization of Nu, and then of F.
		for (it=0; it < numberOfIterations; ++it)
		{
			LOG(8,<<"bestParamUSSRV, params+bbl, iteration: " << bblIt << endl);
			changes = false;	
			// Alpha optimization
			if (_AlphaOptimizationFlag)
			{
				LOGDO(5,printTime(myLog::LogFile()));
				newL = alphaOptimization(et,sc,ssrvSp,weights,AlphaLowerBoundCur,AlphaUpperBoundCur,epsilonParamOptimizationCur);

				//the improvement in Likelihood is smaller than epsilon
				if (newL < _bestL)
				{				
					LOG(5,<<"likelihood went down in LS! (Alpha optimization)"<<endl<<"oldL = "<<_bestL<<" newL= "<<newL<<endl);
					//go back to previous alpha
					alphaOptimization.setAlpha(_bestAlpha,ssrvSp);
					alphaOptimization.setBestL(_bestL); // @@@@ maybe this is unnecessary 
					//break;
				}
				else 
				{// update of likelihood and model.
					if (newL > _bestL+epsilonLikelihoodImprovment) 
					{
						changes = true;
						bblChanges = true;
					}
					LOG(9,<<"newL = " << newL << " _bestL = " << _bestL << " epsilonLikelihoodImprovment = " << epsilonLikelihoodImprovment << endl);
					_bestL = newL;
					_bestAlpha = alphaOptimization.getBestAlpha();
					LOG(5,<<"new L = " << _bestL<<"  new Alpha = " << _bestAlpha<<endl);		
				}

				// Narrow search range between lower/upper bounds
				AlphaLowerBoundCur = (AlphaLowerBoundCur + 2*_bestAlpha) / 3;
				AlphaUpperBoundCur = (AlphaUpperBoundCur + 2*_bestAlpha) / 3;
			}

			// Nu optimization
			if (_NuOptimizationFlag)
			{
				LOGDO(5,printTime(myLog::LogFile()));
				newL = nuOptimization(et,sc,ssrvSp,weights,NuLowerBoundCur,NuUpperBoundCur,epsilonParamOptimizationCur);

				//the improvement in Likelihood is smaller than epsilon
				if (newL < _bestL)
				{
					LOG(5,<<"likelihood went down in LS! (Nu optimization)"<<endl<<"oldL = "<<_bestL<<" newL= "<<newL<<endl);
					//go back to previous Nu
					nuOptimization.setNu(_bestNu,ssrvSp);
					nuOptimization.setBestL(_bestL); // @@@@ maybe this is unnecessary 
					//break;
				}
				else
				{// update of likelihood and model.
					if (newL > _bestL+epsilonLikelihoodImprovment) 
					{
						changes = true;
						bblChanges = true;
					}
					LOG(9,<<"newL = " << newL << " _bestL = " << _bestL << " epsilonLikelihoodImprovment = " << epsilonLikelihoodImprovment << endl);
					_bestL = newL;
					_bestNu = nuOptimization.getBestNu();
					LOG(5,<<"new L = " << _bestL<<"  new Nu = " << _bestNu<<endl);		
				}

				// Narrow search range between lower/upper bounds
				NuLowerBoundCur = (NuLowerBoundCur + 2*_bestNu) / 3;
				NuUpperBoundCur = (NuUpperBoundCur + 2*_bestNu) / 3;
			}

			// tamura92 optimization
			if (_tamura92OptimizationFlag)
			{
				LOGDO(5,printTime(myLog::LogFile()));
				newL = tamura92Optimization(
					et,sc,ssrvSp,weights,5,epsilonLikelihoodImprovment,
					TrTvLowerBoundCur,TrTvUpperBoundCur,ThetaLowerBoundCur,ThetaUpperBoundCur,
					epsilonParamOptimizationCur,epsilonParamOptimizationCur);
				MDOUBLE bestTrTv = tamura92Optimization.getBestTrTv();
				MDOUBLE bestTheta = tamura92Optimization.getBestTheta();

				//the improvement in Likelihood is smaller than epsilon
				if (newL < _bestL)
				{
					LOG(5,<<"likelihood went down in LS! (tamura92 optimization)"<<endl<<"oldL = "<<_bestL<<" newL= "<<newL<<endl);
				}
				else
				{// update of likelihood and model.
					if (newL > _bestL+epsilonLikelihoodImprovment) 
					{
						changes = true;
						bblChanges = true;
					}
					LOG(9,<<"newL = " << newL << " _bestL = " << _bestL << " epsilonLikelihoodImprovment = " << epsilonLikelihoodImprovment << endl);
					_bestL = newL;
					LOG(5,<<"new L = " << _bestL
						<<"  new TrTv = " << bestTrTv
						<<"  new Theta = " << bestTheta <<endl);
				}

				// Narrow search range between lower/upper bounds
				TrTvLowerBoundCur = (TrTvLowerBoundCur + 2*bestTrTv) / 3;
				TrTvUpperBoundCur = (TrTvUpperBoundCur + 2*bestTrTv) / 3;

				ThetaLowerBoundCur = (ThetaLowerBoundCur + 2*bestTheta) / 3;
				ThetaUpperBoundCur = (ThetaUpperBoundCur + 2*bestTheta) / 3;
			}

			if (changes == false)
			{
				LOG(5,<<"bestParamSSRV parameters alpha,nu, and tamura92 params converged!"<<endl);
				break;
			}

			// Reduce epsilonParamOptimizationCur
			epsilonParamOptimizationCur /= 2;
		}

		if (changes == true)
			LOG(5,<<"bestParamSSRV parameters alpha, nu, and tamura92 params did not converge after " << numberOfIterations << " iterations"<<endl);


		// BBL
		if (_bblOptimizationFlag == true)
		{
			LOGDO(5,printTime(myLog::LogFile()));
			bblEM bbl(et,sc,ssrvSp,weights,maxIterations,epsilonBbl);
			newL = bbl.getTreeLikelihood();
			LOG(5,<<" current best L= "<<_bestL<<endl);
			LOG(5,<<"new L After BBL = " << newL<< " = "<< bbl.getTreeLikelihood() <<endl);
			LOG(5,<<"The new tree is: " << endl);
			if (5 <= myLog::LogLevel()) 
				et.output(myLog::LogFile());
			LOG(5,<<endl);
			if (newL > _bestL+epsilonLikelihoodImprovment)
				bblChanges = true;
			if (newL < _bestL){
				LOG(5,<<"likelihood went down in LS! (BBL)"<<endl<<"oldL = "<<_bestL);
				LOG(5,<<" newL= "<<newL<<endl) ;
			}
			else
				_bestL = newL;
		}

		if (bblChanges == false)
		{
			LOG(5,<<"bestParamSSRV bbl and parameters converged!"<<endl);
			break;
		}
	}

	if (bblIt == numberOfParametersAndBblIterations)
		LOG(5,<<"bestParamSSRV bbl and parameters alpha did not converge after " << numberOfParametersAndBblIterations << "iterations"<<endl);	

	LOGDO(5,printTime(myLog::LogFile()));
	return _bestL;
}



// Variant that can work on a const tree - only if we're not doing BBL
// WARNING: Running this with bblOptimization==true will give a fatal error
MDOUBLE bestParamSSRV::operator() (const tree& et,
								   const sequenceContainer& sc,
								   stochasticProcessSSRV& ssrvSp,
								   const Vdouble * weights /* =NULL */,
								   const MDOUBLE AlphaUpperBound /* = 15 */, 
								   const MDOUBLE NuUpperBound /* = 15 */, 
								   const MDOUBLE TrTvUpperBound /* = 10 */,
								   const MDOUBLE epsilonParamOptimization /* = 0.01 */,
								   const MDOUBLE epsilonLikelihoodImprovment /* = 0.01 */,
								   const MDOUBLE epsilonBbl /*= 0.05 */,
								   const int maxIterations /* = 50 */,
								   const int maxOfParametersAndBblIterations /* = 40 */)
{
	if (_bblOptimizationFlag == true)
		errorMsg::reportError("bestParamSSRV::operator(): Can't work on const tree if bblOptimization was requested");

	tree etNotConst(et);
	return operator()(etNotConst, sc, ssrvSp, weights,
					  AlphaUpperBound, NuUpperBound, 
					  epsilonParamOptimization, epsilonLikelihoodImprovment,
					  epsilonBbl, maxIterations,
					  maxOfParametersAndBblIterations);
}