Ejemplo n.º 1
0
void pathRecovery(const std::vector<std::vector<int> > & p, int i,int k){
	if (p[i][k]!=-1) {
		pathRecovery(p,i,p[i][k]);
		std::cout<<p[i][k]+1<<" ";
		pathRecovery(p,p[i][k],k);
	}
}
Ejemplo n.º 2
0
void		_LikelihoodFunction::RunViterbi ( _Matrix & result,				    const _Parameter * patternLikelihoods, 
											  _Matrix & hmm,					_Matrix& hmf, 
											  _SimpleList * duplicateMap,       const _SimpleList* scalers, 
											  long bl )
{
	long      		   ni			= hmm.GetHDim(),
					   siteCount	= duplicateMap?duplicateMap->lLength:bl;
	
	_Matrix	  		   temp  (ni,1,false,true),
					   temp2 (ni,1,false,true);
	
	_SimpleList		   pathRecovery (siteCount * ni, 0, 0);
	
	if  ((duplicateMap?duplicateMap->lLength:bl) > 1)
		 // non-trivial case (more than one site)
	{
		for (long site = siteCount-1; site > 0; site --)
		{
			for (long parentState = 0; parentState < ni; parentState ++)
			{
				long			bestState     = 0,
								mi			  = duplicateMap?
													duplicateMap->lData[site]:
													site,
								
								currentScaler = duplicateMap?
													scalers->lData[mi]:
													((_SimpleList*)((_List*)scalers)->lData[0])->lData[site];
				
				
				_Parameter		bestValue = log(patternLikelihoods[mi]*hmm.theData[parentState*ni]) + temp.theData[0];
				mi			 += bl;
				
				if (currentScaler)
					bestValue -= currentScaler * _logLFScaler;
				
				for (long currentState = 1; currentState < ni; currentState ++, mi += bl)
				{
					currentScaler = duplicateMap?
									scalers->lData[mi]:
									((_SimpleList*)((_List*)scalers)->lData[currentState])->lData[site];
					
					_Parameter		currentValue = log(patternLikelihoods[mi]*hmm.theData[parentState*ni + currentState]) +
												   temp.theData[currentState];
					if (currentScaler)
						currentValue -= currentScaler * _logLFScaler;
					
					if (currentValue > bestValue)
					{
						bestValue = currentValue;
						bestState = currentState;
					}
				}
				temp2.theData[parentState] = bestValue;
				pathRecovery.lData[site*ni + parentState] = bestState;
				//if (parentState != bestState && parentState == 1)
				//	printf ("%d %d -> %d\n", site, parentState, bestState);
			}
			_Parameter* swap = temp.theData;
			temp.theData     = temp2.theData;
			temp2.theData    = swap;
		}	
	}
	else
	{
		for (long parentState = 0; parentState < ni; parentState ++)
		{
			temp.theData[parentState] = log(patternLikelihoods[parentState]) + 
				(duplicateMap?scalers->lData[parentState]:((_SimpleList*)((_List*)scalers)->lData[parentState])->lData[0])*_logLFScaler;
		}
	}

	long			mi		  = duplicateMap?duplicateMap->lData[0]:0,
					bestState = 0;

	_Parameter		bestValue	  = log(patternLikelihoods [mi]*hmf.theData[0]) + temp.theData[0] + 
					(duplicateMap?scalers->lData[mi]:((_SimpleList*)((_List*)scalers)->lData[0])->lData[0])*_logLFScaler;
	
	mi+=bl;
	
	
	for (long initState = 1; initState < ni; initState ++, mi += bl)
	{
		long currentScaler = duplicateMap?scalers->lData[mi]:((_SimpleList*)((_List*)scalers)->lData[initState])->lData[0];
		
		_Parameter		currentValue = log(patternLikelihoods[mi]*hmf.theData[initState]) +
									   temp.theData[initState];
		if (currentScaler)
			currentValue -= currentScaler * _logLFScaler;
		
		if (currentValue > bestValue)
		{
			bestValue = currentValue;
			bestState = initState;
		}		
	}
	
	result.theData[0] = bestState;
	
	for (long site = 1; site < siteCount; site++)
	{
		result.theData[site] = pathRecovery.lData[site*ni + (long)result.theData[site-1]];
		//printf ("%d: (%g) 0-%ld 1-%ld\n",  site,result.theData[site-1], pathRecovery.lData[site*ni], pathRecovery.lData[site*ni+1]);
	}
	
}