35 #ifndef MIXTURE_OF_PPCA_D_H 36 #define MIXTURE_OF_PPCA_D_H 62 double * PpcaAnalyserElement::_exp = 0 ;
65 _useOnlyCorrIndiv( useOnlyCorrIndiv ), _significantNumberOfVp( significantNumberOfVp ), _computed( false ), _energy(0.), _valid(true)
68 _exp =
new double[75010] ;
69 for(
int i = 0 ; i < 7510 ; ++i )
70 _exp[i] = exp( -i/100. ) ;
80 AimsData<T> indivMatrix(
max(
int(selectedIndividuals.size()), 1), individuals.
dimY() ) ;
81 list<int>::const_iterator iter( selectedIndividuals.begin() ),
82 last( selectedIndividuals.end() ) ;
85 while( iter != last ){
86 for(
int t = 0 ; t < individuals.
dimY() ; ++t )
87 indivMatrix( i, t ) = individuals( *iter, t ) ;
93 doIt( indivMatrix, individuals.
dimX(), noiseRef ) ;
99 PpcaAnalyserElement::doIt(
const AimsData<T>& indivMatrix,
int totalNbOfIndividuals,
double noiseRef )
102 int nbInd = indivMatrix.
dimX() ;
103 int nbFrame = indivMatrix.
dimY() ;
108 for(
int ind = 0 ; ind < nbInd ; ++ind )
109 for(
int t = 0 ; t < nbFrame ; ++t )
110 _mean[t] += indivMatrix( ind, t ) ;
111 for(
int t = 0 ; t < nbFrame ; ++t ){
112 _mean[t] /= centeredIndivMatrix.
dimX() ;
113 for(
int ind = 0 ; ind < centeredIndivMatrix.
dimX() ; ++ind )
114 centeredIndivMatrix( ind, t ) = indivMatrix( ind, t ) - _mean[t] ;
122 for(
int k = 0 ; k < centeredIndivMatrix.
dimX() ; ++k )
123 matVarCov(x, y) += centeredIndivMatrix(k, x) * centeredIndivMatrix(k, y) ;
124 matVarCov(x, y) /= centeredIndivMatrix.
dimX() - 1 ;
131 svd.
sort( matVarCov, eigenValues ) ;
136 for(
int i = _significantNumberOfVp ; i < nbFrame ; ++i )
137 _sigma2 += eigenValues[i] ;
138 _sigma2 /= (double)( nbFrame - _significantNumberOfVp ) ;
139 cout << endl <<
"sigma2 = " << setprecision(3) << _sigma2 << endl ;
142 for(
int i = 0 ; i < _significantNumberOfVp ; ++i ){
143 for(
int t = 0 ; t < nbFrame ; ++t ){
144 selectedEigenVectors(t, i) = matVarCov( t, i ) ;
152 for(
int i = 0 ; i < _significantNumberOfVp ; ++i )
153 tempWi(i, i) = sqrt( eigenValues[i] - _sigma2 ) ;
154 _Wi = selectedEigenVectors.
cross( tempWi ) ;
158 for(
int i = 0 ; i < nbFrame ; ++i )
159 Ci(i, i) += _sigma2 ;
162 double s = 0.000001 ;
173 for ( i = 0 ; i < n; i++ ){
174 if ( w( i, i ) < 0. ){
175 cout << endl <<
"pour i = " << i <<
", valeur w = " << w( i, i ) <<
" ! VALEUR NEGATIVE CHANGEE !" << endl ;
176 w( i, i ) = - w( i, i ) ;
178 detCi *= w( i, i ) / _sigma2 ;
179 if ( w( i, i ) > ts ) w( i, i ) = 1.0f / w( i, i );
180 else w( i, i ) = 0.0f;
188 _normFactor = 1 / ( pow( detCi, 0.5 ) * pow( _sigma2 / noiseRef, nbFrame / 2. ) ) ;
190 cout <<
"norm factor avec noiseRef = " << _normFactor << endl ;
194 for(
int i = 0 ; i < _significantNumberOfVp ; ++i )
195 Mi(i, i) += _sigma2 ;
200 w = svd3.doit( u, &v );
204 for ( i = 0 ; i < n; i++ ){
205 if ( w( i, i ) > ts ) w( i, i ) = 1.0f / w( i, i );
206 else w( i, i ) = 0.0f;
219 int nbInd = indivMatrix.
dimX() ;
220 int nbFrame = indivMatrix.
dimY() ;
231 double sumInd, sumMean, indMean, meanMean, corr = 0. ;
239 for(
int n = 0 ; n < nbInd ; ++n ){
246 for(
int t = 0 ; t < nbFrame ; ++t ){
247 indivn[t] = indivMatrix(n, t) ;
248 centeredIndivn[t] = indivMatrix(n, t) - _mean[t] ;
249 sumInd += indivn[t] ;
250 sumMean += _mean[t] ;
253 if( useOnlyCorrIndiv == 1 ){
254 indMean = sumInd / (double)nbFrame ;
255 meanMean = sumMean / (double)nbFrame ;
256 for(
int t = 0 ; t < nbFrame ; ++t )
257 corr += ( indivn[t] - indMean ) * ( _mean[t] - meanMean ) ;
272 if( _dist[n] > 1500 )
275 _pTni[n] = _normFactor * _exp[ int(0.5 * _dist[n] * 100. + 0.000001 ) ] ;
280 if( _dist[n] > 1500 )
283 _pTni[n] = _normFactor * _exp[ int(0.5 * _dist[n] * 100. + 0.000001) ] ;
307 if( nbIndTaken < indivMatrix.
dimY() * 2 )
320 bool explicitComputing = true ;
322 int nbInd = indivMatrix.
dimX() ;
323 int nbFrame = indivMatrix.
dimY() ;
337 for(
int n = 0 ; n < nbInd ; ++n ){
338 previousRn = _Rn[n] ;
340 _Rn[n] = _pTni[n] * _Pi / pTn[n] ;
355 cout <<
"critere pour la classe = " << setprecision(9) << _sumDiff2Rni <<
" - " ;
362 for(
int n = 0 ; n < nbInd ; ++n )
365 cout <<
"Pi = " << setprecision(3) << _Pi << endl ;
367 double nbIndClass = _Pi * nbIndTemp ;
368 cout <<
"'nb d'individus appartenant' a la classe = " << nbIndClass << endl ;
376 int nullRniNb = 0, nearNullRni = 0 ;
377 for(
int n = 0 ; n < nbInd ; ++n ){
381 if( _Rn[n] < 10e-30 )
383 for(
int t = 0 ; t < nbFrame ; ++t )
384 sumRnTn[t] += _Rn[n] * indivMatrix(n, t) ;
387 cout <<
"Nombre de RNi nulls : " << nullRniNb <<
"et de Rni presque nulls" << nearNullRni << endl ;
388 for(
int t = 0 ; t < nbFrame ; ++t )
389 _mean[t] = sumRnTn[t] / sumRn ;
404 for(
int n = 0 ; n < nbInd ; ++n )
406 for(
int t = 0 ; t < nbFrame ; ++t )
407 centeredIndiv(t) = indivMatrix(n, t) - _mean[t] ;
409 Si(x, y) += _Rn[n] * centeredIndiv(x) * centeredIndiv(y) ;
412 Si(x, y) /= _Pi * nbIndTemp ;
419 if( explicitComputing ){
426 svd.
sort( Si, eigenValues ) ;
432 for(
int i = _significantNumberOfVp ; i < nbFrame ; ++i )
433 _sigma2 += eigenValues[i] ;
434 _sigma2 /= (double)( nbFrame - _significantNumberOfVp ) ;
435 cout <<
"sigma2 = " << _sigma2 <<
" (explicite)" <<
" - " ;
438 for(
int i = 0 ; i < _significantNumberOfVp ; ++i )
439 for(
int t = 0 ; t < nbFrame ; ++t )
440 selectedEigenVectors(t, i) = Si( t, i ) ;
445 for(
int i = 0 ; i < _significantNumberOfVp ; ++i )
446 tempWi(i, i) = sqrt( eigenValues[i] - _sigma2 ) ;
447 _Wi = selectedEigenVectors.
cross( tempWi ) ;
451 for(
int i = 0 ; i < nbFrame ; ++i )
452 Ci(i, i) += _sigma2 ;
455 double s = 0.000001 ;
465 for ( i = 0 ; i < n; i++ ){
466 if ( w( i, i ) < 0. ){
467 cout << endl <<
"pour i = " << i <<
",valeur w = " << w( i, i ) <<
"! VALEUR NEGATIVE CHANGEE!" << endl ;
468 w( i, i ) = - w( i, i ) ;
470 detCi *= w( i, i ) / _sigma2 ;
471 if ( w( i, i ) > ts ) w( i, i ) = 1.0f / w( i, i );
472 else w( i, i ) = 0.0f;
486 for(
int i = 0 ; i < newWi.
dimX() ; ++i )
487 newWi(i, i) += _sigma2 ;
489 double s = 0.000001 ;
498 for ( i = 0 ; i < n ; i++ ){
499 if ( w( i, i ) < 0. ){
500 cout << endl <<
"pour i = " << i <<
", w = " << w( i, i ) <<
" ! VALEUR NEGATIVE CHANGEE !" << endl ;
501 w( i, i ) = - w( i, i ) ;
503 if ( w( i, i ) > ts ) w( i, i ) = 1.0f / w( i, i );
504 else w( i, i ) = 0.0f;
544 for(
int i = 0 ; i < temp.
dimX() ; ++i )
545 _sigma2 += ( Si(i, i) - temp(i, i) ) ;
547 cout <<
"sigma2 = " << _sigma2 <<
" - " ;
549 cout <<
"VALEUR NEGATIVE CHANGEE ! " << endl ;
550 _sigma2 = - _sigma2 ;
561 for(
int i = 0 ; i < Mi.
dimX() ; ++i )
562 Mi(i, i) += _sigma2 ;
571 for( i = 0 ; i < n ; i++ ){
572 if ( w2( i, i ) < 0. ){
573 cout << endl <<
"pour i = " << i <<
", w2 = " << w2( i, i ) <<
" ! VALEUR NEGATIVE CHANGEE !" << endl ;
574 w2( i, i ) = - w2( i, i ) ;
576 if ( w2( i, i ) > ts ) w2( i, i ) = 1.0f / w2( i, i ) ;
577 else w2( i, i ) = 0.0f;
586 for(
int i = 0 ; i < nbFrame ; ++i )
587 Ci(i, i) += _sigma2 ;
591 for( i = 0 ; i < Ci.
dimX() ; i++ ){
593 cout <<
"pour i = " << i <<
", Ci = " << Ci( i, i ) <<
" ! VALEUR NEGATIVE CHANGEE !" << endl ;
594 Ci( i, i ) = - Ci( i, i ) ;
596 for(
int j = 0 ; j < Ci.
dimY() ; j++ ){
597 if( Ci(i,j) != Ci(j,i) ){
599 cout <<
"pour i,j = " << i <<
"," << j <<
" Ci(i,j) = " << Ci(i,j) <<
",Ci(j,i) = " << Ci(j,i) << endl ;
603 if( sym ==
false ) cout <<
"--> Ci pas symetrique !" << endl ;
613 bool saveCi = false ;
616 for( i = 0 ; i < n ; i++ ){
618 if ( w3( i, i ) < 0. ){
620 cout << endl <<
"pour i = " << i <<
", valeur w3 = " << w3( i, i ) <<
" ! VALEUR NEGATIVE CHANGEE !" << endl ;
621 w3( i, i ) = - w3( i, i ) ;
624 detCi *= w3( i, i ) ;
626 detCi *= w3( i, i ) / _sigma2 ;
627 if ( w3( i, i ) > ts ) w3( i, i ) = 1.0f / w3( i, i );
628 else w3( i, i ) = 0.0f;
671 cout <<
"energy = " << _energy ;
674 _normFactor = 1 / pow( detCi, 0.5 ) ;
676 _normFactor = 1 / ( pow( detCi, 0.5 ) * pow( _sigma2 / noiseRef, nbFrame / 2. ) ) ;
677 cout <<
" - detCi = " << detCi
678 <<
" - normFactor = " << _normFactor << endl ;
687 int nbInd = _individuals.dimX() ;
689 double sumPtn = 0., prodPtn = 0., meanPtn = 0. , varPtn = 0. ;
693 _nullPtnIndiv.clear() ;
697 for(
int n = 0 ; n < nbInd ; ++n ){
698 for(
int c = 0 ; c < _nbOfClasses ; ++c ){
699 Pi = _elements[c].getPi() ;
700 pTni = _elements[c].getPtni() ;
701 _pTn[n] += pTni[n] * Pi ;
706 _nullPtnIndiv.push_back( n ) ;
709 _nbOfRejected = nbInd - nbIndTemp ;
711 cout <<
"nb d'individus pris en compte = " << nbIndTemp <<
" - nb d'individus rejetes = " 712 << nbInd - nbIndTemp << endl ;
715 double oldSumLnPTn = _logLikelihood ;
716 _logLikelihood = 0. ;
717 for(
int n = 0 ; n < nbInd ; ++n ){
719 _logLikelihood += log( _pTn[n] ) ;
721 _logLikelihood += -100000000. ;
723 prodPtn += _pTn[n] * _pTn[n] ;
725 meanPtn = sumPtn / nbIndTemp ;
726 varPtn = prodPtn / nbIndTemp ;
727 cout << endl <<
"CRITERIUM TO MAXIMIZE : LOG LIKELIHOOD = " << _logLikelihood <<
" and difference = " 728 << (_logLikelihood - oldSumLnPTn)/oldSumLnPTn <<
" for " 729 << nbInd - nbIndTemp <<
" rejections" << endl ;
730 cout <<
"moyenne et variance des p(tn) = " << meanPtn <<
" - " << varPtn << endl ;
732 return (_logLikelihood - oldSumLnPTn)/oldSumLnPTn ;
746 double max = 0., global_dist = 0. ;
748 for(
int c = 0 ; c < _nbOfClasses ; ++c ){
749 Rn = _elements[c].getRn() ;
750 for(
int ind = 0 ; ind < _individuals.dimX() ; ++ind )
751 Rni(ind, c) = Rn[ind] ;
754 for(
int ind = 0 ; ind < _individuals.dimX() ; ++ind ){
755 max = Rni( ind, 0 ) ;
757 for(
int c = 1 ; c < _nbOfClasses ; ++c ){
758 if( Rni(ind, c) > max ){
759 max = Rni( ind, c ) ;
760 results[ind] = c + 1 ;
763 mean = _elements[results[ind]-1].getMean() ;
764 invCi = _elements[results[ind]-1].getInvCi() ;
765 for(
int t = 0 ; t < _individuals.dimY() ; ++t )
766 centeredIndivn[t] = _individuals(ind, t) -
mean[t] ;
767 dist = ( centeredIndivn.clone().transpose() ).
cross( invCi.cross( centeredIndivn ) ) ;
768 global_dist += dist(0,0) ;
785 vector< list< int > > tempList( _nbOfClasses ) ;
798 for(
int ind = 0 ; ind < _individuals.dimX() ; ++ind ){
801 for(
int c = 0 ; c < _nbOfClasses ; ++c ){
802 if( Rni(ind, c) >
max ){
803 max = Rni( ind, c ) ;
804 results[ind] = c + 1 ;
807 if( results[ind] > 0 )
808 _finalClasses[results[ind] - 1].push_back( ind ) ;
815 if( nbOfIterations% 30 == 0 || theEnd ){
819 rejected.
setSizeXYZT( 0.858086, 0.858086, 2.425 ) ;
821 rniImage.
setSizeXYZT( 0.858086, 0.858086, 2.425 ) ;
823 resultImage.
setSizeXYZT( 0.858086, 0.858086, 2.425 ) ;
827 for(
short c = 0 ; c < _nbOfClasses ; ++c ){
828 list<int>::const_iterator iter( _finalClasses[c].begin() ),
829 last( _finalClasses[c].end() ) ;
830 while( iter != last ){
831 theVoxel = _indPosVector[*iter] ;
832 resultImage( theVoxel ) = c + 1 ;
833 rniImage(theVoxel[0], theVoxel[1], theVoxel[2], c) = Rni( *iter, c ) ;
838 list<int>::const_iterator it( _nullPtnIndiv.begin() ),
839 lt( _nullPtnIndiv.end() ) ;
841 theVoxel = _indPosVector[*it] ;
842 rejected( theVoxel ) = 1 ;
848 writer.
write( resultImage ) ;
851 writer3.
write( rejected ) ;
854 writer2.
write( rniImage ) ;
857 cout << endl <<
"Nb d'individus a l'iteration " << nbOfIterations <<
" ds chaque classe = " ;
858 for(
int i = 0 ; i < _nbOfClasses ; ++i )
859 cout << _finalClasses[i].size() <<
" " ;
868 string fformat = f.
format() ;
869 if( !writerMatrix.
write( _individuals,
false, &fformat ) )
876 for(
int c = 0 ; c < _nbOfClasses ; ++c ){
878 list<int>::const_iterator iter( _finalClasses[c].begin() ),
879 last( _finalClasses[c].end() ) ;
880 while( iter != last ){
884 item.
value() = *iter ;
892 writerClasses.
write( bClasses,
true, &fformat ) ;
896 vector<double> classesNorm( _nbOfClasses, 0. ) ;
897 multimap<double, Point2d> fusion ;
898 multimap<double, int>
split ;
900 for(
int i = 0 ; i < _nbOfClasses ; ++i )
901 for(
int n = 0 ; n < _individuals.dimX() ; ++n ) {
902 for(
int j = i ; j < _nbOfClasses ; ++j )
903 classCorrelationMatrix(i, j) += Rni( n, i ) * Rni(n, j ) ;
905 classesNorm[i] += Rni( n, i ) * Rni( n, i ) ;
908 for(
int i = 0 ; i < _nbOfClasses ; ++i ){
909 classesNorm[i] = sqrt( classesNorm[i] ) ;
910 double meanNorm = 0. ;
911 for(
int t = 0 ; t < _elements[i].getMean().dimX() ; ++t )
912 meanNorm += _elements[i].
getMean()[t] ;
914 split.insert( pair<double, int>( _elements[i].
getSigma2() / meanNorm, i) ) ;
916 for(
int i = 0 ; i < _nbOfClasses ; ++i )
917 for(
int j = i ; j < _nbOfClasses ; ++j ){
918 if ( classesNorm[i] * classesNorm[j] > 0 )
919 classCorrelationMatrix(i, j) /= classesNorm[i] * classesNorm[j] ;
921 classCorrelationMatrix(i, j) = 0. ;
924 classCorrelationMatrix(j, i) = classCorrelationMatrix(i, j) ;
926 fusion.insert( pair<double, Point2d> ( classCorrelationMatrix(i, j),
Point2d(i, j) ) ) ;
929 cout <<
"Classes � fusionner : " << endl ;
932 for( multimap<double, Point2d>::reverse_iterator rit = fusion.rbegin() ; rit != fusion.rend() && count < 5 ; ++rit, ++count )
933 cout << (rit->second)[0] <<
" avec " << (rit->second)[1] <<
" : " << rit->first << endl ;
935 cout <<
"Classe � s�parer : " << endl ;
937 for( multimap<double, int>::reverse_iterator rit = split.rbegin() ; rit != split.rend() && count < 5 ; ++rit, ++count )
938 cout << rit->second <<
" : " << rit->first << endl ;
952 multimap< float, Point2d > fScore ;
953 multimap< float, Point2d > meanFScore ;
955 for(
int fromC = 0 ; fromC < _nbOfClasses ; ++fromC ){
956 for(
int toC = 0 ; toC < _nbOfClasses ; ++toC ){
959 list<int>::const_iterator iter( _finalClasses[fromC].begin() ),
960 last( _finalClasses[fromC].end() ) ;
961 while( iter != last ){
962 sumDist += _distToClasses( *iter, toC ) ;
966 meanDistMatrix( fromC, toC ) = sumDist / count ;
973 for(
int i = 0 ; i < _nbOfClasses ; ++i )
974 for(
int j = 0 ; j < _nbOfClasses ; ++j )
975 fScore.insert( pair< float, Point2d >( meanDistMatrix(i, j),
Point2d(i, j) ) ) ;
978 for(
int i = 0 ; i < _nbOfClasses ; ++i )
979 for(
int j = i+1 ; j < _nbOfClasses ; ++j )
980 meanFScore.insert( pair< float, Point2d >( ( meanDistMatrix(i, j) + meanDistMatrix(j, i) )/2.,
983 cout << endl <<
"elements de la map des scores = " << endl;
984 multimap< float, Point2d >::iterator iter( fScore.begin() ), last( fScore.end() ) ;
985 while( iter != last ){
986 if( (*iter).first > 1.5 && (*iter).first < 100. ){
987 cout <<
" [" << (*iter).first <<
", " << (*iter).second <<
"]" ;
988 cout <<
" sigmas = " << _sigma2Matrix[ (*iter).second[0] ] <<
" et " 989 << _sigma2Matrix[ (*iter).second[1] ] << endl;
994 cout << endl <<
"elements de la map des scores moyens 2 a 2 = " << endl;
995 multimap< float, Point2d >::iterator iterM( meanFScore.begin() ), lastM( meanFScore.end() ) ;
996 while( iterM != lastM ){
997 if( exp( -0.5 * (*iterM).first ) > 0.01 ){
998 cout <<
" [" << (*iterM).first <<
", " << (*iterM).second <<
"]" ;
999 cout <<
" exp(-score/2) = " << exp( -0.5 * (*iterM).first ) << endl ;
1011 int maxNbOfIterations,
1012 const AimsData<T>& individuals,
const std::vector< Point3d > indPosVector,
1013 const std::vector< std::list <int> >& initialClasses,
1014 const std::string & fileOut,
int runNb,
int iterationToUseOnlyCorrelatedIndiv ) :
1015 _nbOfClasses( nbOfClasses ), _valid( true ), _significantNumberOfEigenValues( significantNumberOfVp ), _maxNbOfIterations( maxNbOfIterations ), _individuals( individuals ), _indPosVector( indPosVector ), _fileOut( fileOut ), _runNb( runNb ), _itToUseOnlyCorrelatedIndiv(iterationToUseOnlyCorrelatedIndiv)
1017 cout <<
"Entering MixtureOfPPCA Constructor" << endl ;
1019 double Pi_init = 1. / (double)_nbOfClasses ;
1020 _elements.reserve( _nbOfClasses ) ;
1024 _sigma2init = vector<double>( _nbOfClasses ) ;
1027 std::vector< std::list<int> > initClasses( initialClasses ) ;
1029 if( initClasses.size() == 0 ){
1030 initClasses = std::vector< std::list<int> >( _nbOfClasses ) ;
1031 for(
int i = 0 ; i < _individuals.dimX() ; ++i ){
1033 int c = int( aux * _nbOfClasses ) ;
1034 initClasses[c].push_back( i ) ;
1036 std::cout << std::endl ;
1039 for(
int c = 0 ; c < _nbOfClasses ; ++c ){
1040 std::cout <<
"Class " << c <<
" : ";
1041 for( std::list<int>::iterator iter = initClasses[c].begin() ; iter < initClasses[c].end() ; ++iter )
1042 std::cout <<
"\t" << (*iter) ;
1043 std::cout << std::endl ;
1046 for(
int c = 0 ; c < _nbOfClasses ; ++c ){
1047 if(
int( initClasses[c].size() ) > _individuals.dimY() ) {
1049 cout << endl <<
"Initialization class " << c <<
" ..." ;
1063 el.
init( initClasses[c], Pi_init, individuals, _noiseRef ) ;
1065 cout <<
"Noise Ref = " << setprecision(3) << _noiseRef << endl ;
1067 el.
init( initClasses[c], Pi_init, individuals, _noiseRef ) ;
1068 cout <<
"Class " << c <<
" initialized !" << endl ;
1069 _elements.push_back( el ) ;
1072 cerr <<
"Not enough data for element " << c << endl ;
1083 int nbOfIterations = 0 ;
1087 _finalClasses = vector< list< int > >( _nbOfClasses ) ;
1089 double sumOfEnergies, criteria ;
1091 AimsData<double> imageOfMean( _nbOfClasses, _maxNbOfIterations, _individuals.dimY() ) ;
1100 int oldNbOfRejected = 0 ;
1101 bool notYetTheEnd = true ;
1104 for(
int i = 0 ; i < _nbOfClasses ; ++i )
1105 _finalClasses[i].clear() ;
1106 cout << endl <<
"Iteration n�: " << nbOfIterations ;
1109 cout << endl <<
"Calcul des p(tn|i) pour chaque classe... " << endl ;
1110 for(
int i = 0 ; i < _nbOfClasses ; ++i ){
1111 cerr <<
"Class " << i <<
": " ;
1112 if( _elements[i].isValid() )
1113 _elements[i].newStep1( _individuals, 0 ) ;
1116 oldNbOfRejected = _nbOfRejected ;
1119 cout << endl <<
"Calcul des p(tn)..." ;
1120 imageOfLogLikelihoodProgression(nbOfIterations) =
pTnComputation() ;
1121 imageOfLogLikelihood(nbOfIterations) = _logLikelihood ;
1124 cout << endl <<
"Calcul des autres parametres pour chaque classe..." ;
1128 double oldSumOfEnergies = sumOfEnergies ;
1129 sumOfEnergies = 0. ;
1130 for(
int i = 0 ; i < _nbOfClasses ; ++i ){
1131 cout <<
"Class " << i <<
": " ;
1132 if( _elements[i].isValid() ){
1133 _elements[i].newStep2( _pTn, _individuals, 1000000. ) ;
1136 sumOfEnergies += _elements[i].getEnergy() ;
1137 criteria += _elements[i].getSumDiff2Rni() ;
1140 for(
int t = 0 ; t < _individuals.dimY() ; ++t )
1141 imageOfMean( i, nbOfIterations, t ) = ( _elements[i].getMean() )[t] ;
1142 imageOfSigma2( i, nbOfIterations ) = _elements[i].getSigma2() ;
1143 imageOfPi( i, nbOfIterations ) = _elements[i].getPi() ;
1144 imageOfRniDiff( i, nbOfIterations ) = _elements[i].getSumDiff2Rni() ;
1148 cout << endl <<
"Critere d'arret = " << criteria <<
" or " << (oldSumOfEnergies - sumOfEnergies)/_elements.size() << endl ;
1149 imageOfEnergy( nbOfIterations ) = sumOfEnergies ;
1166 notYetTheEnd = (nbOfIterations < _maxNbOfIterations) &&
1167 ( (nbOfIterations < 10 ) ||
1168 !( (oldNbOfRejected == _nbOfRejected) && (fabs( imageOfLogLikelihoodProgression(nbOfIterations) ) < 0.0000001) ) ) ;
1174 }
while( notYetTheEnd ) ;
1200 for(
int c = 0 ; c < _nbOfClasses ; ++c ){
1201 Rn = _elements[c].getRn() ;
1202 for(
int ind = 0 ; ind < _individuals.dimX() ; ++ind )
1203 RnAll(ind, c) = Rn[ind] ;
const AimsData< double > & getMean()
AimsData< T > & transpose()
float max(float x, float y)
void sort(AimsData< T > &, AimsData< T > &, AimsData< T > *v=NULL)
sort the U and V matrices and the W vector in decreasing order
std::string format() const
AimsData< T > doit(AimsData< T > &, AimsData< T > *v=NULL)
Singular Value Decomposition.
double newStep2(AimsData< double > pTn, const AimsData< T > &indivMatrix, double noiseRef=1.)
#define ForEach2d(thing, x, y)
bool distMatrixComputation()
void push_back(const AimsBucketItem< T > &item)
void setReturnType(SVDReturnType rt)
Point3df cross(const Point3df &A, const Point3df &B)
void init(const std::list< int > &selectedIndividuals, double initialPi, const AimsData< T > &individuals, double noiseRef=1.)
bool newStep1(const AimsData< T > &indivMatrix, bool useOnlyCorrIndiv)
double UniformRandom()
Uniform distribution between 0.0 and 1.0.
const AimsVector< short, 3 > & location() const
AimsData< double > getRni()
const std::vector< double > & mean() const
PpcaAnalyserElement(int significantNumberOfVp, bool useOnlyCorrIndiv=false)
virtual bool write(const T &obj, bool ascii=false, const std::string *format=0)
AimsData< T > clone() const
bool classesVisualisation(int nbOfIterations, const std::string &fileOut, bool theEnd)
AimsData< T > cross(const AimsData< T > &other)
std::vector< std::string > split(const std::string &text, const std::string &sep)
void setSizeXYZT(float sizex=1.0f, float sizey=1.0f, float sizez=1.0f, float sizet=1.0f)
std::string toString(const T &object)
MixtureOfPPCA(int nbOfClasses, int significantNumberOfVp, int maxNbOfIterations, const AimsData< T > &individuals, const std::vector< Point3d > indPosVector, const std::vector< std::list< int > > &initialClasses, const std::string &fileOut, int runNb, int iterationToUseOnlyCorrelatedIndiv=false)