#include "MeanEstimate.h" #include #include #include #include #include CMeanEstimate::CMeanEstimate() { } CMeanEstimate::~CMeanEstimate(void) { } void CMeanEstimate::RemoveValues(std::vector< double > *A, double value) { A->erase( remove_if( A->begin(), A->end(), bind2nd( std::equal_to< double >(), value ) ), A->end() ); /* double x; int size = (int)A->size(); int count=0; //number removed; for(int i=0; iGetAt(i); if( x == value ) { A->RemoveAt(i); count++; i--; size--; } } */ } struct is_not_a_number : public std::unary_function< double, bool > { bool operator() ( double x ) { return !CMeanEstimate::IsANumber( x ); } }; void CMeanEstimate::RemoveNanValues(std::vector< double > *A) // Nan = Not A Number { A->erase( remove_if( A->begin(), A->end(), is_not_a_number() ), A->end() ); /* double x; int size = (int)A->GetSize(); int count=0; //number removed; for(int i=0; iGetAt(i); if( ! CMeanEstimate::IsANumber(x) ) { A->RemoveAt(i); count++; i--; size--; } } */ } template < class Op1, class Op2, class Op3 > class binary_compose : public std::unary_function< typename Op2::argument_type, typename Op1::result_type > { public: binary_compose( const Op1& o1, const Op2& o2, const Op3& o3 ) : f1( o1 ), f2( o2 ), f3( o3 ) { } typename Op1::result_type operator() ( const typename Op2::argument_type& x ) const { return f1( f2( x ), f3( x ) ); } protected: Op1 f1; Op2 f2; Op3 f3; }; template < class Op1, class Op2, class Op3 > inline binary_compose< Op1, Op2, Op3 > compose2( const Op1& f1, const Op2& f2, const Op3& f3 ) { return binary_compose< Op1, Op2, Op3 >( f1, f2, f3 ); } void CMeanEstimate::RemoveOutOfBoundsValues(std::vector< double > *A, double percent) { double mean = GetMeanEstimate(A, percent); double maxBound = mean * (1+percent); double minBound = mean * (1-percent); A->erase( remove_if( A->begin(), A->end(), compose2( std::logical_or< bool >(), std::bind2nd( std::less< double >(), minBound ), std::bind2nd( std::greater< double >(), maxBound ) ) ), A->end() ); /* double x=0; int size = (int)A->GetSize(); int count=0; //number removed; for(int i=0; iGetAt(i); if( x > maxBound || x < minBound ) { A->RemoveAt(i); count++; i--; size--; } } */ } void CMeanEstimate::FindMinMaxWithinBoundsValues(std::vector< double > *A, double percent, double &min, double &max) { std::vector< double > A2(*A); RemoveValues(&A2, -1); RemoveValues(&A2, 0); RemoveNanValues(&A2); min = std::numeric_limits::max(); max = std::numeric_limits::min(); double mean = GetMeanEstimate(&A2, percent); double maxBound = mean * (1+percent); double minBound = mean * (1-percent); double x=0; int i = 0; double value=-1; std::vector< double >::iterator it = A->begin(), ie = A->end(); while (it != ie) { x = *it; if( !CMeanEstimate::IsANumber(x) || x<=0){ }else{ if (x > maxBound || x < minBound ) { }else{ if( x > max ){ max = x; }else{ if(x < min ){ min = x; } } } } i++; ++it; } } void CMeanEstimate::FindMinMax(std::vector< double > *A, double &min, double &max) { min = std::numeric_limits::max(); max = std::numeric_limits::min(); //min = max = A->GetAt(0); double x=0; std::vector< double >::iterator it = A->begin(), ie = A->end(); while ( it != ie ) { x = *it; if( x > max ) max = x; if(x>0 && x < min ) min = x; ++it; } } double CMeanEstimate::GetMeanEstimate(std::vector< double > *A) { return GetMeanEstimate(A, VARIATION_COEF); } double CMeanEstimate::GetMeanEstimate(std::vector< double > *A, double similarityCoef) { double mean=GetMean(A); double x=0, sum = 0 ; int count=0; //double maxDeviation = mean * similarityCoef; std::vector< double >::iterator it = A->begin(), ie = A->end(); while ( it != ie ) { x = *it; //if(maxDeviation> fabs(x-mean)){ if( x*similarityCoef > fabs(x-mean) ) { sum+=x; count++; } else { int xxx=0; xxx++; } ++it; } // VERIFY(count); return sum / (double)count; } double CMeanEstimate::GetMeanEstimateFileOutput(std::vector< double > *A, double similarityCoef) { double mean=GetMean(A); double x=0, sum = 0 ; int count=0; //double maxDeviation = mean * similarityCoef; std::vector< double >::iterator it = A->begin(), ie = A->end(); while ( it != ie ) { x = *it; //if(maxDeviation> fabs(x-mean)){ if( x*similarityCoef > fabs(x-mean) ) { sum+=x; count++; } ++it; } //#define OUTPUT #ifdef OUTPUT fprintf(m_file, "%d",count); #endif //VERIFY(count); return sum / (double)count; } void CMeanEstimate::PrintMeanEstimate(std::vector< double > *A, std::vector< double > *B) { PrintMeanEstimate(A,B, VARIATION_COEF); } void CMeanEstimate::PrintMeanEstimate(std::vector< double > *A, std::vector< double > *B, double similarityCoef) { double mean =GetMean(A); double meanB=GetMean(B); double x=0; int count=0, countB=0; //fprintf(m_file, "MeanEstimate, coef:%f\n %f \t%f\n\n",similarityCoef, GetMeanEstimate(A,similarityCoef), GetMeanEstimate(B,similarityCoef)); //fprintf(m_file, "AA\t exclu=0\t\tII\t exclu=0\n"); //double maxDeviation = mean * similarityCoef; //double maxDeviationB = meanB * similarityCoef; assert( B->size() <= A->size() ); std::vector< double >::iterator ia = A->begin(), ae = A->end(), ib = B->begin(); while ( ia != ae ) { x = *ia; //if(maxDeviation > fabs(x-mean) ){ if ( x*similarityCoef > fabs(x-mean) ) { //sum+=x; //fprintf(m_file, "%f\t1\t", x); } else { count++; //fprintf(m_file, "%f\t0\t", x); } x = *ib; //fprintf(m_file, "\t"); //if(maxDeviationB> fabs(x-meanB) ){ if( x*similarityCoef > fabs(x-meanB) ) { //sumB+=x; //fprintf(m_file, "%f\t1\t", x); } else { countB++; //fprintf(m_file, "%f\t0\t", x); } //fprintf(m_file, "\n"); ++ia; ++ib; } //fprintf(m_file, "\nnombre de points exclus:\t%d\t\t\t%d\n",count, countB ); } bool CMeanEstimate::IsANumber(double x) { return ((x < (double)(std::numeric_limits::max())) && (x > (double)(std::numeric_limits::min()))); } double CMeanEstimate::GetMean(std::vector< double > *A) { double sum=0; double x=0; std::vector< double >::iterator it = A->begin(), ie = A->end(); while ( it != ie ) { x = *it; if( !IsANumber(x) ) { int xxx=0; xxx++; } else // if( x == std::numeric_limits::quiet_NaN() ){ // int xxx=0; xxx++; // } // if( abs(x) == std::numeric_limits::infinity() ){ // int xxx=0; xxx++; // } sum+= x; ++it; } return sum / (double)A->size(); } double CMeanEstimate::GetStandardDeviation(std::vector< double > *A) { return sqrt(GetVariance(A)); } double CMeanEstimate::GetVariance(std::vector< double > *A) { double mean=GetMean(A); double sum=0, diff=0; std::vector< double >::iterator it = A->begin(), ie = A->end(); while ( it != ie ) { diff = *it - mean; sum+= diff*diff; ++it; } return sum / (double)A->size(); } //void CMeanEstimate::OutputToFile(char* filename, CArray *A, CArray *B) ////implementation rapide et specifique est repectant peu le principde objet !! //{ // FILE* file = fopen(filename, "a"); // // if(file==0){ // ASSERT(file); // }else{ // fprintf(file, "Mean\n %f \t %f \n", CMeanEstimate::GetMean(A), CMeanEstimate::GetMean(B)); // fprintf(file, "StandardDeviation\n %f \t %f \n", CMeanEstimate::GetStandardDeviation(A), CMeanEstimate::GetStandardDeviation(B)); // fprintf(file, "\nValues:\nAA \tII\n"); // ASSERT(A->GetSize()==B->GetSize()); // for(int i=A->GetSize()-1; i; i--){ // fprintf(file, "%g \t %g \n", A->GetAt(i), B->GetAt(i)); // } // fprintf(file, "\n\n"); // fclose(file); // } //}