MeanEstimate.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. #include "MeanEstimate.h"
  2. #include <cassert>
  3. #include <cmath>
  4. #include <limits>
  5. #include <algorithm>
  6. #include <vector>
  7. CMeanEstimate::CMeanEstimate()
  8. {
  9. }
  10. CMeanEstimate::~CMeanEstimate(void)
  11. {
  12. }
  13. void CMeanEstimate::RemoveValues(std::vector< double > *A, double value)
  14. {
  15. A->erase( remove_if( A->begin(), A->end(), bind2nd( std::equal_to< double >(), value ) ), A->end() );
  16. /*
  17. double x;
  18. int size = (int)A->size();
  19. int count=0; //number removed;
  20. for(int i=0; i<size; i++)
  21. {
  22. x = A->GetAt(i);
  23. if( x == value )
  24. {
  25. A->RemoveAt(i);
  26. count++;
  27. i--;
  28. size--;
  29. }
  30. }
  31. */
  32. }
  33. struct is_not_a_number : public std::unary_function< double, bool >
  34. {
  35. bool operator() ( double x )
  36. {
  37. return !CMeanEstimate::IsANumber( x );
  38. }
  39. };
  40. void CMeanEstimate::RemoveNanValues(std::vector< double > *A) // Nan = Not A Number
  41. {
  42. A->erase( remove_if( A->begin(), A->end(), is_not_a_number() ), A->end() );
  43. /*
  44. double x;
  45. int size = (int)A->GetSize();
  46. int count=0; //number removed;
  47. for(int i=0; i<size; i++)
  48. {
  49. x = A->GetAt(i);
  50. if( ! CMeanEstimate::IsANumber(x) )
  51. {
  52. A->RemoveAt(i);
  53. count++;
  54. i--;
  55. size--;
  56. }
  57. }
  58. */
  59. }
  60. template < class Op1, class Op2, class Op3 >
  61. class binary_compose : public std::unary_function< typename Op2::argument_type,
  62. typename Op1::result_type >
  63. {
  64. public:
  65. binary_compose( const Op1& o1, const Op2& o2, const Op3& o3 ) : f1( o1 ),
  66. f2( o2 ),
  67. f3( o3 )
  68. {
  69. }
  70. typename Op1::result_type operator() ( const typename Op2::argument_type& x ) const
  71. {
  72. return f1( f2( x ), f3( x ) );
  73. }
  74. protected:
  75. Op1 f1;
  76. Op2 f2;
  77. Op3 f3;
  78. };
  79. template < class Op1, class Op2, class Op3 >
  80. inline binary_compose< Op1, Op2, Op3 > compose2( const Op1& f1, const Op2& f2, const Op3& f3 )
  81. {
  82. return binary_compose< Op1, Op2, Op3 >( f1, f2, f3 );
  83. }
  84. void CMeanEstimate::RemoveOutOfBoundsValues(std::vector< double > *A, double percent)
  85. {
  86. double mean = GetMeanEstimate(A, percent);
  87. double maxBound = mean * (1+percent);
  88. double minBound = mean * (1-percent);
  89. A->erase( remove_if( A->begin(), A->end(),
  90. compose2( std::logical_or< bool >(),
  91. std::bind2nd( std::less< double >(), minBound ),
  92. std::bind2nd( std::greater< double >(), maxBound ) ) ),
  93. A->end() );
  94. /*
  95. double x=0;
  96. int size = (int)A->GetSize();
  97. int count=0; //number removed;
  98. for(int i=0; i<size; i++)
  99. {
  100. x = A->GetAt(i);
  101. if( x > maxBound || x < minBound )
  102. {
  103. A->RemoveAt(i);
  104. count++;
  105. i--;
  106. size--;
  107. }
  108. }
  109. */
  110. }
  111. void CMeanEstimate::FindMinMaxWithinBoundsValues(std::vector< double > *A, double percent, double &min, double &max)
  112. {
  113. std::vector< double > A2(*A);
  114. RemoveValues(&A2, -1);
  115. RemoveValues(&A2, 0);
  116. RemoveNanValues(&A2);
  117. min = std::numeric_limits<double>::max();
  118. max = std::numeric_limits<double>::min();
  119. double mean = GetMeanEstimate(&A2, percent);
  120. double maxBound = mean * (1+percent);
  121. double minBound = mean * (1-percent);
  122. double x=0;
  123. int i = 0;
  124. double value=-1;
  125. std::vector< double >::iterator
  126. it = A->begin(),
  127. ie = A->end();
  128. while (it != ie)
  129. {
  130. x = *it;
  131. if( !CMeanEstimate::IsANumber(x)
  132. || x<=0){
  133. }else{
  134. if (x > maxBound || x < minBound )
  135. {
  136. }else{
  137. if( x > max ){
  138. max = x;
  139. }else{
  140. if(x < min ){
  141. min = x;
  142. }
  143. }
  144. }
  145. }
  146. i++;
  147. ++it;
  148. }
  149. }
  150. void CMeanEstimate::FindMinMax(std::vector< double > *A, double &min, double &max)
  151. {
  152. min = std::numeric_limits<double>::max();
  153. max = std::numeric_limits<double>::min();
  154. //min = max = A->GetAt(0);
  155. double x=0;
  156. std::vector< double >::iterator
  157. it = A->begin(),
  158. ie = A->end();
  159. while ( it != ie )
  160. {
  161. x = *it;
  162. if( x > max ) max = x;
  163. if(x>0 && x < min ) min = x;
  164. ++it;
  165. }
  166. }
  167. double CMeanEstimate::GetMeanEstimate(std::vector< double > *A)
  168. {
  169. return GetMeanEstimate(A, VARIATION_COEF);
  170. }
  171. double CMeanEstimate::GetMeanEstimate(std::vector< double > *A, double similarityCoef)
  172. {
  173. double mean=GetMean(A);
  174. double x=0, sum = 0 ;
  175. int count=0;
  176. //double maxDeviation = mean * similarityCoef;
  177. std::vector< double >::iterator
  178. it = A->begin(),
  179. ie = A->end();
  180. while ( it != ie )
  181. {
  182. x = *it;
  183. //if(maxDeviation> fabs(x-mean)){
  184. if( x*similarityCoef > fabs(x-mean) )
  185. {
  186. sum+=x;
  187. count++;
  188. }
  189. else
  190. {
  191. int xxx=0; xxx++;
  192. }
  193. ++it;
  194. }
  195. // VERIFY(count);
  196. return sum / (double)count;
  197. }
  198. double CMeanEstimate::GetMeanEstimateFileOutput(std::vector< double > *A, double similarityCoef)
  199. {
  200. double mean=GetMean(A);
  201. double x=0, sum = 0 ;
  202. int count=0;
  203. //double maxDeviation = mean * similarityCoef;
  204. std::vector< double >::iterator
  205. it = A->begin(),
  206. ie = A->end();
  207. while ( it != ie )
  208. {
  209. x = *it;
  210. //if(maxDeviation> fabs(x-mean)){
  211. if( x*similarityCoef > fabs(x-mean) )
  212. {
  213. sum+=x;
  214. count++;
  215. }
  216. ++it;
  217. }
  218. //#define OUTPUT
  219. #ifdef OUTPUT
  220. fprintf(m_file, "%d",count);
  221. #endif
  222. //VERIFY(count);
  223. return sum / (double)count;
  224. }
  225. void CMeanEstimate::PrintMeanEstimate(std::vector< double > *A, std::vector< double > *B)
  226. {
  227. PrintMeanEstimate(A,B, VARIATION_COEF);
  228. }
  229. void CMeanEstimate::PrintMeanEstimate(std::vector< double > *A, std::vector< double > *B, double similarityCoef)
  230. {
  231. double mean =GetMean(A);
  232. double meanB=GetMean(B);
  233. double x=0;
  234. int count=0, countB=0;
  235. //fprintf(m_file, "MeanEstimate, coef:%f\n %f \t%f\n\n",similarityCoef, GetMeanEstimate(A,similarityCoef), GetMeanEstimate(B,similarityCoef));
  236. //fprintf(m_file, "AA\t exclu=0\t\tII\t exclu=0\n");
  237. //double maxDeviation = mean * similarityCoef;
  238. //double maxDeviationB = meanB * similarityCoef;
  239. assert( B->size() <= A->size() );
  240. std::vector< double >::iterator
  241. ia = A->begin(),
  242. ae = A->end(),
  243. ib = B->begin();
  244. while ( ia != ae )
  245. {
  246. x = *ia;
  247. //if(maxDeviation > fabs(x-mean) ){
  248. if ( x*similarityCoef > fabs(x-mean) )
  249. {
  250. //sum+=x;
  251. //fprintf(m_file, "%f\t1\t", x);
  252. }
  253. else
  254. {
  255. count++;
  256. //fprintf(m_file, "%f\t0\t", x);
  257. }
  258. x = *ib;
  259. //fprintf(m_file, "\t");
  260. //if(maxDeviationB> fabs(x-meanB) ){
  261. if( x*similarityCoef > fabs(x-meanB) )
  262. {
  263. //sumB+=x;
  264. //fprintf(m_file, "%f\t1\t", x);
  265. }
  266. else
  267. {
  268. countB++;
  269. //fprintf(m_file, "%f\t0\t", x);
  270. }
  271. //fprintf(m_file, "\n");
  272. ++ia;
  273. ++ib;
  274. }
  275. //fprintf(m_file, "\nnombre de points exclus:\t%d\t\t\t%d\n",count, countB );
  276. }
  277. bool CMeanEstimate::IsANumber(double x)
  278. {
  279. return ((x < (double)(std::numeric_limits<double>::max())) &&
  280. (x > (double)(std::numeric_limits<double>::min())));
  281. }
  282. double CMeanEstimate::GetMean(std::vector< double > *A)
  283. {
  284. double sum=0;
  285. double x=0;
  286. std::vector< double >::iterator
  287. it = A->begin(),
  288. ie = A->end();
  289. while ( it != ie )
  290. {
  291. x = *it;
  292. if( !IsANumber(x) )
  293. {
  294. int xxx=0; xxx++;
  295. } else
  296. // if( x == std::numeric_limits<double>::quiet_NaN() ){
  297. // int xxx=0; xxx++;
  298. // }
  299. // if( abs(x) == std::numeric_limits<double>::infinity() ){
  300. // int xxx=0; xxx++;
  301. // }
  302. sum+= x;
  303. ++it;
  304. }
  305. return sum / (double)A->size();
  306. }
  307. double CMeanEstimate::GetStandardDeviation(std::vector< double > *A)
  308. {
  309. return sqrt(GetVariance(A));
  310. }
  311. double CMeanEstimate::GetVariance(std::vector< double > *A)
  312. {
  313. double mean=GetMean(A);
  314. double sum=0, diff=0;
  315. std::vector< double >::iterator
  316. it = A->begin(),
  317. ie = A->end();
  318. while ( it != ie )
  319. {
  320. diff = *it - mean;
  321. sum+= diff*diff;
  322. ++it;
  323. }
  324. return sum / (double)A->size();
  325. }
  326. //void CMeanEstimate::OutputToFile(char* filename, CArray<double> *A, CArray<double> *B)
  327. ////implementation rapide et specifique est repectant peu le principde objet !!
  328. //{
  329. // FILE* file = fopen(filename, "a");
  330. //
  331. // if(file==0){
  332. // ASSERT(file);
  333. // }else{
  334. // fprintf(file, "Mean\n %f \t %f \n", CMeanEstimate::GetMean(A), CMeanEstimate::GetMean(B));
  335. // fprintf(file, "StandardDeviation\n %f \t %f \n", CMeanEstimate::GetStandardDeviation(A), CMeanEstimate::GetStandardDeviation(B));
  336. // fprintf(file, "\nValues:\nAA \tII\n");
  337. // ASSERT(A->GetSize()==B->GetSize());
  338. // for(int i=A->GetSize()-1; i; i--){
  339. // fprintf(file, "%g \t %g \n", A->GetAt(i), B->GetAt(i));
  340. // }
  341. // fprintf(file, "\n\n");
  342. // fclose(file);
  343. // }
  344. //}