lib_confidence.c 809 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #define _LIB /* bench.h needs this */
  2. #include "bench.h"
  3. #include <math.h>
  4. #include <stdio.h>
  5. /**
  6. * 95% confidence interval is [y - ci_width, y + ci_width]
  7. */
  8. double ci_width(double stddev, int n)
  9. {
  10. double c = 0;
  11. assert(n > 0);
  12. if (n >= 120)
  13. c = 1.96;
  14. else if (n >= 90)
  15. c = 1.987;
  16. else if (n >= 60)
  17. c = 2.0;
  18. else if (n >= 40)
  19. c = 2.021;
  20. else if (n >= 30)
  21. c = 2.042;
  22. else if (n >= 25)
  23. c = 2.06;
  24. else if (n >= 20)
  25. c = 2.086;
  26. else if (n >= 15)
  27. c = 2.131;
  28. else if (n >= 10)
  29. c = 2.228;
  30. else if (n >= 5)
  31. c = 2.571;
  32. else if (n == 4)
  33. c = 2.776;
  34. else if (n == 3)
  35. c = 3.182;
  36. else if (n == 2)
  37. c = 4.303;
  38. else if (n == 1)
  39. c = 12.706;
  40. else {
  41. fprintf(stderr,"ERROR: n < 1. cannot calculate confidence interval.");
  42. return 0;
  43. }
  44. return (c * stddev/sqrt((double)n));
  45. }