Stats.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include "Stats.h"
  2. //-----------------------------------------------------------------------------
  3. double chooseK ( int n, int k )
  4. {
  5. if(k > (n - k)) k = n - k;
  6. double c = 1;
  7. for(int i = 0; i < k; i++)
  8. {
  9. double t = double(n-i) / double(i+1);
  10. c *= t;
  11. }
  12. return c;
  13. }
  14. double chooseUpToK ( int n, int k )
  15. {
  16. double c = 0;
  17. for(int i = 1; i <= k; i++)
  18. {
  19. c += chooseK(n,i);
  20. }
  21. return c;
  22. }
  23. //-----------------------------------------------------------------------------
  24. // Distribution "score"
  25. // TODO - big writeup of what this score means
  26. // Basically, we're computing a constant that says "The test distribution is as
  27. // uniform, RMS-wise, as a random distribution restricted to (1-X)*100 percent of
  28. // the bins. This makes for a nice uniform way to rate a distribution that isn't
  29. // dependent on the number of bins or the number of keys
  30. // (as long as # keys > # bins * 3 or so, otherwise random fluctuations show up
  31. // as distribution weaknesses)
  32. double calcScore ( const int * bins, const int bincount, const int keycount )
  33. {
  34. double n = bincount;
  35. double k = keycount;
  36. // compute rms value
  37. double r = 0;
  38. for(int i = 0; i < bincount; i++)
  39. {
  40. double b = bins[i];
  41. r += b*b;
  42. }
  43. r = sqrt(r / n);
  44. // compute fill factor
  45. double f = (k*k - 1) / (n*r*r - k);
  46. // rescale to (0,1) with 0 = good, 1 = bad
  47. return 1 - (f / n);
  48. }
  49. //----------------------------------------------------------------------------
  50. void plot ( double n )
  51. {
  52. double n2 = n * 1;
  53. if(n2 < 0) n2 = 0;
  54. n2 *= 100;
  55. if(n2 > 64) n2 = 64;
  56. int n3 = (int)n2;
  57. if(n3 == 0)
  58. printf(".");
  59. else
  60. {
  61. char x = '0' + char(n3);
  62. if(x > '9') x = 'X';
  63. printf("%c",x);
  64. }
  65. }
  66. //-----------------------------------------------------------------------------