Browse code

update_constrained now correct

chakalakka authored on 02/10/2014 09:24:28
Showing 3 changed files

... ...
@@ -14,7 +14,7 @@ void R_univariate_hmm(int* O, int* T, int* N, double* size, double* prob, int* m
14 14
 // 	FILE* pFile = fopen("chromStar.log", "w");
15 15
 // 	Output2FILE::Stream() = pFile;
16 16
  	FILELog::ReportingLevel() = FILELog::FromString("ERROR");
17
-//  	FILELog::ReportingLevel() = FILELog::FromString("DEBUG1");
17
+//  	FILELog::ReportingLevel() = FILELog::FromString("DEBUG2");
18 18
 
19 19
 	// Parallelization settings
20 20
 	omp_set_num_threads(*num_threads);
... ...
@@ -337,8 +337,8 @@ void NegativeBinomial::update_constrained(double** weights, int fromState, int t
337 337
 	{
338 338
 		for (int t=0; t<this->T; t++)
339 339
 		{
340
-			numerator+=weights[i+fromState][t]*this->size*(i+1);
341
-			denominator+=weights[i+fromState][t]*(this->size*(i+1)+this->obs[t]);
340
+			numerator += weights[i+fromState][t] * this->size*(i+1);
341
+			denominator += weights[i+fromState][t] * (this->size*(i+1) + this->obs[t]);
342 342
 		}
343 343
 	}
344 344
 	this->prob = numerator/denominator; // Update of size (r) is now done with old prob
... ...
@@ -371,22 +371,22 @@ void NegativeBinomial::update_constrained(double** weights, int fromState, int t
371 371
 				{
372 372
 					if(this->obs[t]==0)
373 373
 					{
374
-						Fr+=weights[i+fromState][t]*(i+1)*logp;
374
+						Fr += weights[i+fromState][t] * (i+1) * logp;
375 375
 						//dFrdr+=0;
376 376
 					}
377 377
 					if(this->obs[t]!=0)
378 378
 					{
379
-						Fr+=weights[i+fromState][t]*(i+1)*(logp-DigammaR+DigammaRplusX[(int)obs[t]]);
380
-						dFrdr+=weights[i+fromState][t]/dr*(i+1)*(DigammaR-DigammaRplusDR+DigammaRplusDRplusX[(int)obs[t]]-DigammaRplusX[(int)obs[t]]);
379
+						Fr += weights[i+fromState][t] * (i+1) * (logp - DigammaR + DigammaRplusX[(int)obs[t]]);
380
+						dFrdr += weights[i+fromState][t] / dr * (i+1) * (DigammaR - DigammaRplusDR + DigammaRplusDRplusX[(int)obs[t]] - DigammaRplusX[(int)obs[t]]);
381 381
 					}
382 382
 				}
383 383
 				if(fabs(Fr)<eps)
384 384
 				{
385 385
 					break;
386 386
 				}
387
-				if(Fr/dFrdr<rhere) rhere=rhere-Fr/dFrdr;
388
-				if(Fr/dFrdr>rhere) rhere=rhere/2.0;
389 387
 			}
388
+			if(Fr/dFrdr<rhere) rhere=rhere-Fr/dFrdr;
389
+			if(Fr/dFrdr>rhere) rhere=rhere/2.0;
390 390
 		}
391 391
 	}
392 392
 	else
... ...
@@ -406,13 +406,13 @@ void NegativeBinomial::update_constrained(double** weights, int fromState, int t
406 406
 					DigammaRplusDRplusX = digamma((i+1)*(rhere+dr)+this->obs[t]); // boost::math::digamma<>(rhere+dr+this->obs[ti]);
407 407
 					if(this->obs[t]==0)
408 408
 					{
409
-						Fr+=weights[i+fromState][t]*(i+1)*logp;
409
+						Fr += weights[i+fromState][t] * (i+1) * logp;
410 410
 						//dFrdr+=0;
411 411
 					}
412 412
 					if(this->obs[t]!=0)
413 413
 					{
414
-						Fr+=weights[i+fromState][t]*(i+1)*(logp-DigammaR+DigammaRplusX);
415
-						dFrdr+=weights[i+fromState][t]/dr*(i+1)*(DigammaR-DigammaRplusDR+DigammaRplusDRplusX-DigammaRplusX);
414
+						Fr += weights[i+fromState][t] * (i+1) * (logp - DigammaR + DigammaRplusX);
415
+						dFrdr += weights[i+fromState][t] / dr * (i+1) * (DigammaR - DigammaRplusDR + DigammaRplusDRplusX - DigammaRplusX);
416 416
 					}
417 417
 				}
418 418
 			}
... ...
@@ -262,34 +262,7 @@ void ScaleHMM::baumWelch(int* maxiter, int* maxtime, double* eps)
262 262
 // 			this->densityFunctions[iN]->update(this->gamma[iN]);
263 263
 // 		}
264 264
 
265
-// 		// Update distribution of state 'null-mixed' and 'monosomic', set others as multiples of 'monosomic'
266
-// 		// This loop assumes that the negative binomial states come last and are consecutive
267
-// 		for (int iN=0; iN<this->N; iN++)
268
-// 		{
269
-// 			if (this->densityFunctions[iN]->get_name() == ZERO_INFLATION) {}
270
-// 			if (this->densityFunctions[iN]->get_name() == GEOMETRIC)
271
-// 			{
272
-// 				this->densityFunctions[iN]->update(this->gamma[iN]);
273
-// 			}
274
-// 			if (this->densityFunctions[iN]->get_name() == NEGATIVE_BINOMIAL)
275
-// 			{
276
-// 				FILE_LOG(logDEBUG1) << "mean(state="<<iN<<") = " << this->densityFunctions[iN]->get_mean() << ", var(state="<<iN<<") = " << this->densityFunctions[iN]->get_variance();
277
-// 				this->densityFunctions[iN]->update_constrained(this->gamma, iN, this->N);
278
-// 				double mean1 = this->densityFunctions[iN]->get_mean();
279
-// 				double variance1 = this->densityFunctions[iN]->get_variance();
280
-// 				FILE_LOG(logDEBUG1) << "mean(state="<<iN<<") = " << this->densityFunctions[iN]->get_mean() << ", var(state="<<iN<<") = " << this->densityFunctions[iN]->get_variance();
281
-// 				// Set others as multiples
282
-// 				for (int jN=iN+1; jN<this->N; jN++)
283
-// 				{
284
-// 					this->densityFunctions[jN]->set_mean(mean1 * (jN-iN+1));
285
-// 					this->densityFunctions[jN]->set_variance(variance1 * (jN-iN+1));
286
-// 					FILE_LOG(logDEBUG1) << "mean(state="<<jN<<") = " << this->densityFunctions[jN]->get_mean() << ", var(state="<<jN<<") = " << this->densityFunctions[jN]->get_variance();
287
-// 				}
288
-// 				break;
289
-// 			}
290
-// 		}
291
-			
292
-		// Update distribution of state 'null-mixed' and 'disomic', set others as multiples of 'disomic'/2
265
+		// Update distribution of state 'null-mixed' and 'monosomic', set others as multiples of 'monosomic'
293 266
 		// This loop assumes that the negative binomial states come last and are consecutive
294 267
 		int xsomy = 1;
295 268
 		for (int iN=0; iN<this->N; iN++)
... ...
@@ -301,18 +274,18 @@ void ScaleHMM::baumWelch(int* maxiter, int* maxtime, double* eps)
301 274
 			}
302 275
 			if (this->densityFunctions[iN]->get_name() == NEGATIVE_BINOMIAL)
303 276
 			{
304
-				if (xsomy==2)
277
+				if (xsomy==1)
305 278
 				{
306 279
 					FILE_LOG(logDEBUG1) << "mean(state="<<iN<<") = " << this->densityFunctions[iN]->get_mean() << ", var(state="<<iN<<") = " << this->densityFunctions[iN]->get_variance();
307
-					this->densityFunctions[iN]->update(this->gamma[iN]);
308
-					double mean1 = this->densityFunctions[iN]->get_mean() / xsomy;
309
-					double variance1 = this->densityFunctions[iN]->get_variance() / xsomy;
280
+					this->densityFunctions[iN]->update_constrained(this->gamma, iN, this->N);
281
+					double mean1 = this->densityFunctions[iN]->get_mean();
282
+					double variance1 = this->densityFunctions[iN]->get_variance();
310 283
 					FILE_LOG(logDEBUG1) << "mean(state="<<iN<<") = " << this->densityFunctions[iN]->get_mean() << ", var(state="<<iN<<") = " << this->densityFunctions[iN]->get_variance();
311 284
 					// Set others as multiples
312
-					for (int jN=2; jN<this->N; jN++)
285
+					for (int jN=iN+1; jN<this->N; jN++)
313 286
 					{
314
-						this->densityFunctions[jN]->set_mean(mean1 * (jN-iN+2));
315
-						this->densityFunctions[jN]->set_variance(variance1 * (jN-iN+2));
287
+						this->densityFunctions[jN]->set_mean(mean1 * (jN-iN+1));
288
+						this->densityFunctions[jN]->set_variance(variance1 * (jN-iN+1));
316 289
 						FILE_LOG(logDEBUG1) << "mean(state="<<jN<<") = " << this->densityFunctions[jN]->get_mean() << ", var(state="<<jN<<") = " << this->densityFunctions[jN]->get_variance();
317 290
 					}
318 291
 					break;
... ...
@@ -321,6 +294,38 @@ void ScaleHMM::baumWelch(int* maxiter, int* maxtime, double* eps)
321 294
 			}
322 295
 		}
323 296
 			
297
+// 		// Update distribution of state 'null-mixed' and 'disomic', set others as multiples of 'disomic'/2
298
+// 		// This loop assumes that the negative binomial states come last and are consecutive
299
+// 		int xsomy = 1;
300
+// 		for (int iN=0; iN<this->N; iN++)
301
+// 		{
302
+// 			if (this->densityFunctions[iN]->get_name() == ZERO_INFLATION) {}
303
+// 			if (this->densityFunctions[iN]->get_name() == GEOMETRIC)
304
+// 			{
305
+// 				this->densityFunctions[iN]->update(this->gamma[iN]);
306
+// 			}
307
+// 			if (this->densityFunctions[iN]->get_name() == NEGATIVE_BINOMIAL)
308
+// 			{
309
+// 				if (xsomy==2)
310
+// 				{
311
+// 					FILE_LOG(logDEBUG1) << "mean(state="<<iN<<") = " << this->densityFunctions[iN]->get_mean() << ", var(state="<<iN<<") = " << this->densityFunctions[iN]->get_variance();
312
+// 					this->densityFunctions[iN]->update(this->gamma[iN]);
313
+// 					double mean1 = this->densityFunctions[iN]->get_mean() / xsomy;
314
+// 					double variance1 = this->densityFunctions[iN]->get_variance() / xsomy;
315
+// 					FILE_LOG(logDEBUG1) << "mean(state="<<iN<<") = " << this->densityFunctions[iN]->get_mean() << ", var(state="<<iN<<") = " << this->densityFunctions[iN]->get_variance();
316
+// 					// Set others as multiples
317
+// 					for (int jN=2; jN<this->N; jN++)
318
+// 					{
319
+// 						this->densityFunctions[jN]->set_mean(mean1 * (jN-iN+2));
320
+// 						this->densityFunctions[jN]->set_variance(variance1 * (jN-iN+2));
321
+// 						FILE_LOG(logDEBUG1) << "mean(state="<<jN<<") = " << this->densityFunctions[jN]->get_mean() << ", var(state="<<jN<<") = " << this->densityFunctions[jN]->get_variance();
322
+// 					}
323
+// 					break;
324
+// 				}
325
+// 				xsomy++;
326
+// 			}
327
+// 		}
328
+			
324 329
 		dtime = clock() - clocktime;
325 330
 	 	FILE_LOG(logDEBUG) << "updating distributions: " << dtime << " clicks";
326 331
 		R_CheckUserInterrupt();