1 /* 2 * ---------------------------------------------------------------------------- 3 * "THE BEER-WARE LICENSE" (Revision 42): 4 * <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you 5 * can do whatever you want with this stuff. If we meet some day, and you think 6 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp 7 * ---------------------------------------------------------------------------- 8 * 9 */ 10 11 #include <sys/cdefs.h> 12 __FBSDID("$FreeBSD$"); 13 14 #include <stdio.h> 15 #include <math.h> 16 #include <err.h> 17 #include <string.h> 18 #include <stdlib.h> 19 #include <unistd.h> 20 #include <sys/queue.h> 21 22 #define NSTUDENT 100 23 #define NCONF 6 24 double const studentpct[] = { 80, 90, 95, 98, 99, 99.5 }; 25 double student [NSTUDENT + 1][NCONF] = { 26 /* inf */ { 1.282, 1.645, 1.960, 2.326, 2.576, 3.090 }, 27 /* 1. */ { 3.078, 6.314, 12.706, 31.821, 63.657, 318.313 }, 28 /* 2. */ { 1.886, 2.920, 4.303, 6.965, 9.925, 22.327 }, 29 /* 3. */ { 1.638, 2.353, 3.182, 4.541, 5.841, 10.215 }, 30 /* 4. */ { 1.533, 2.132, 2.776, 3.747, 4.604, 7.173 }, 31 /* 5. */ { 1.476, 2.015, 2.571, 3.365, 4.032, 5.893 }, 32 /* 6. */ { 1.440, 1.943, 2.447, 3.143, 3.707, 5.208 }, 33 /* 7. */ { 1.415, 1.895, 2.365, 2.998, 3.499, 4.782 }, 34 /* 8. */ { 1.397, 1.860, 2.306, 2.896, 3.355, 4.499 }, 35 /* 9. */ { 1.383, 1.833, 2.262, 2.821, 3.250, 4.296 }, 36 /* 10. */ { 1.372, 1.812, 2.228, 2.764, 3.169, 4.143 }, 37 /* 11. */ { 1.363, 1.796, 2.201, 2.718, 3.106, 4.024 }, 38 /* 12. */ { 1.356, 1.782, 2.179, 2.681, 3.055, 3.929 }, 39 /* 13. */ { 1.350, 1.771, 2.160, 2.650, 3.012, 3.852 }, 40 /* 14. */ { 1.345, 1.761, 2.145, 2.624, 2.977, 3.787 }, 41 /* 15. */ { 1.341, 1.753, 2.131, 2.602, 2.947, 3.733 }, 42 /* 16. */ { 1.337, 1.746, 2.120, 2.583, 2.921, 3.686 }, 43 /* 17. */ { 1.333, 1.740, 2.110, 2.567, 2.898, 3.646 }, 44 /* 18. */ { 1.330, 1.734, 2.101, 2.552, 2.878, 3.610 }, 45 /* 19. */ { 1.328, 1.729, 2.093, 2.539, 2.861, 3.579 }, 46 /* 20. */ { 1.325, 1.725, 2.086, 2.528, 2.845, 3.552 }, 47 /* 21. */ { 1.323, 1.721, 2.080, 2.518, 2.831, 3.527 }, 48 /* 22. */ { 1.321, 1.717, 2.074, 2.508, 2.819, 3.505 }, 49 /* 23. */ { 1.319, 1.714, 2.069, 2.500, 2.807, 3.485 }, 50 /* 24. */ { 1.318, 1.711, 2.064, 2.492, 2.797, 3.467 }, 51 /* 25. */ { 1.316, 1.708, 2.060, 2.485, 2.787, 3.450 }, 52 /* 26. */ { 1.315, 1.706, 2.056, 2.479, 2.779, 3.435 }, 53 /* 27. */ { 1.314, 1.703, 2.052, 2.473, 2.771, 3.421 }, 54 /* 28. */ { 1.313, 1.701, 2.048, 2.467, 2.763, 3.408 }, 55 /* 29. */ { 1.311, 1.699, 2.045, 2.462, 2.756, 3.396 }, 56 /* 30. */ { 1.310, 1.697, 2.042, 2.457, 2.750, 3.385 }, 57 /* 31. */ { 1.309, 1.696, 2.040, 2.453, 2.744, 3.375 }, 58 /* 32. */ { 1.309, 1.694, 2.037, 2.449, 2.738, 3.365 }, 59 /* 33. */ { 1.308, 1.692, 2.035, 2.445, 2.733, 3.356 }, 60 /* 34. */ { 1.307, 1.691, 2.032, 2.441, 2.728, 3.348 }, 61 /* 35. */ { 1.306, 1.690, 2.030, 2.438, 2.724, 3.340 }, 62 /* 36. */ { 1.306, 1.688, 2.028, 2.434, 2.719, 3.333 }, 63 /* 37. */ { 1.305, 1.687, 2.026, 2.431, 2.715, 3.326 }, 64 /* 38. */ { 1.304, 1.686, 2.024, 2.429, 2.712, 3.319 }, 65 /* 39. */ { 1.304, 1.685, 2.023, 2.426, 2.708, 3.313 }, 66 /* 40. */ { 1.303, 1.684, 2.021, 2.423, 2.704, 3.307 }, 67 /* 41. */ { 1.303, 1.683, 2.020, 2.421, 2.701, 3.301 }, 68 /* 42. */ { 1.302, 1.682, 2.018, 2.418, 2.698, 3.296 }, 69 /* 43. */ { 1.302, 1.681, 2.017, 2.416, 2.695, 3.291 }, 70 /* 44. */ { 1.301, 1.680, 2.015, 2.414, 2.692, 3.286 }, 71 /* 45. */ { 1.301, 1.679, 2.014, 2.412, 2.690, 3.281 }, 72 /* 46. */ { 1.300, 1.679, 2.013, 2.410, 2.687, 3.277 }, 73 /* 47. */ { 1.300, 1.678, 2.012, 2.408, 2.685, 3.273 }, 74 /* 48. */ { 1.299, 1.677, 2.011, 2.407, 2.682, 3.269 }, 75 /* 49. */ { 1.299, 1.677, 2.010, 2.405, 2.680, 3.265 }, 76 /* 50. */ { 1.299, 1.676, 2.009, 2.403, 2.678, 3.261 }, 77 /* 51. */ { 1.298, 1.675, 2.008, 2.402, 2.676, 3.258 }, 78 /* 52. */ { 1.298, 1.675, 2.007, 2.400, 2.674, 3.255 }, 79 /* 53. */ { 1.298, 1.674, 2.006, 2.399, 2.672, 3.251 }, 80 /* 54. */ { 1.297, 1.674, 2.005, 2.397, 2.670, 3.248 }, 81 /* 55. */ { 1.297, 1.673, 2.004, 2.396, 2.668, 3.245 }, 82 /* 56. */ { 1.297, 1.673, 2.003, 2.395, 2.667, 3.242 }, 83 /* 57. */ { 1.297, 1.672, 2.002, 2.394, 2.665, 3.239 }, 84 /* 58. */ { 1.296, 1.672, 2.002, 2.392, 2.663, 3.237 }, 85 /* 59. */ { 1.296, 1.671, 2.001, 2.391, 2.662, 3.234 }, 86 /* 60. */ { 1.296, 1.671, 2.000, 2.390, 2.660, 3.232 }, 87 /* 61. */ { 1.296, 1.670, 2.000, 2.389, 2.659, 3.229 }, 88 /* 62. */ { 1.295, 1.670, 1.999, 2.388, 2.657, 3.227 }, 89 /* 63. */ { 1.295, 1.669, 1.998, 2.387, 2.656, 3.225 }, 90 /* 64. */ { 1.295, 1.669, 1.998, 2.386, 2.655, 3.223 }, 91 /* 65. */ { 1.295, 1.669, 1.997, 2.385, 2.654, 3.220 }, 92 /* 66. */ { 1.295, 1.668, 1.997, 2.384, 2.652, 3.218 }, 93 /* 67. */ { 1.294, 1.668, 1.996, 2.383, 2.651, 3.216 }, 94 /* 68. */ { 1.294, 1.668, 1.995, 2.382, 2.650, 3.214 }, 95 /* 69. */ { 1.294, 1.667, 1.995, 2.382, 2.649, 3.213 }, 96 /* 70. */ { 1.294, 1.667, 1.994, 2.381, 2.648, 3.211 }, 97 /* 71. */ { 1.294, 1.667, 1.994, 2.380, 2.647, 3.209 }, 98 /* 72. */ { 1.293, 1.666, 1.993, 2.379, 2.646, 3.207 }, 99 /* 73. */ { 1.293, 1.666, 1.993, 2.379, 2.645, 3.206 }, 100 /* 74. */ { 1.293, 1.666, 1.993, 2.378, 2.644, 3.204 }, 101 /* 75. */ { 1.293, 1.665, 1.992, 2.377, 2.643, 3.202 }, 102 /* 76. */ { 1.293, 1.665, 1.992, 2.376, 2.642, 3.201 }, 103 /* 77. */ { 1.293, 1.665, 1.991, 2.376, 2.641, 3.199 }, 104 /* 78. */ { 1.292, 1.665, 1.991, 2.375, 2.640, 3.198 }, 105 /* 79. */ { 1.292, 1.664, 1.990, 2.374, 2.640, 3.197 }, 106 /* 80. */ { 1.292, 1.664, 1.990, 2.374, 2.639, 3.195 }, 107 /* 81. */ { 1.292, 1.664, 1.990, 2.373, 2.638, 3.194 }, 108 /* 82. */ { 1.292, 1.664, 1.989, 2.373, 2.637, 3.193 }, 109 /* 83. */ { 1.292, 1.663, 1.989, 2.372, 2.636, 3.191 }, 110 /* 84. */ { 1.292, 1.663, 1.989, 2.372, 2.636, 3.190 }, 111 /* 85. */ { 1.292, 1.663, 1.988, 2.371, 2.635, 3.189 }, 112 /* 86. */ { 1.291, 1.663, 1.988, 2.370, 2.634, 3.188 }, 113 /* 87. */ { 1.291, 1.663, 1.988, 2.370, 2.634, 3.187 }, 114 /* 88. */ { 1.291, 1.662, 1.987, 2.369, 2.633, 3.185 }, 115 /* 89. */ { 1.291, 1.662, 1.987, 2.369, 2.632, 3.184 }, 116 /* 90. */ { 1.291, 1.662, 1.987, 2.368, 2.632, 3.183 }, 117 /* 91. */ { 1.291, 1.662, 1.986, 2.368, 2.631, 3.182 }, 118 /* 92. */ { 1.291, 1.662, 1.986, 2.368, 2.630, 3.181 }, 119 /* 93. */ { 1.291, 1.661, 1.986, 2.367, 2.630, 3.180 }, 120 /* 94. */ { 1.291, 1.661, 1.986, 2.367, 2.629, 3.179 }, 121 /* 95. */ { 1.291, 1.661, 1.985, 2.366, 2.629, 3.178 }, 122 /* 96. */ { 1.290, 1.661, 1.985, 2.366, 2.628, 3.177 }, 123 /* 97. */ { 1.290, 1.661, 1.985, 2.365, 2.627, 3.176 }, 124 /* 98. */ { 1.290, 1.661, 1.984, 2.365, 2.627, 3.175 }, 125 /* 99. */ { 1.290, 1.660, 1.984, 2.365, 2.626, 3.175 }, 126 /* 100. */ { 1.290, 1.660, 1.984, 2.364, 2.626, 3.174 } 127 }; 128 129 #define MAX_DS 8 130 static char symbol[MAX_DS] = { ' ', 'x', '+', '*', '%', '#', '@', 'O' }; 131 132 TAILQ_HEAD(pointlist, point); 133 134 struct dataset { 135 struct pointlist list; 136 double sy, syy; 137 int n; 138 }; 139 140 static struct dataset * 141 NewSet(void) 142 { 143 struct dataset *ds; 144 145 ds = calloc(1, sizeof *ds); 146 TAILQ_INIT(&ds->list); 147 return(ds); 148 } 149 150 struct point { 151 TAILQ_ENTRY(point) list; 152 double val; 153 }; 154 155 static void 156 AddPoint(struct dataset *ds, double a) 157 { 158 struct point *pp, *pp2; 159 160 pp = calloc(1, sizeof *pp); 161 pp->val = a; 162 163 ds->n++; 164 ds->sy += a; 165 ds->syy += a * a; 166 if (TAILQ_EMPTY(&ds->list)) { 167 TAILQ_INSERT_HEAD(&ds->list, pp, list); 168 return; 169 } 170 TAILQ_FOREACH(pp2, &ds->list, list) { 171 if (pp->val < pp2->val) { 172 TAILQ_INSERT_BEFORE(pp2, pp, list); 173 return; 174 } 175 } 176 TAILQ_INSERT_TAIL(&ds->list, pp, list); 177 } 178 179 static double 180 Min(struct dataset *ds) 181 { 182 183 return (TAILQ_FIRST(&ds->list)->val); 184 } 185 186 static double 187 Max(struct dataset *ds) 188 { 189 190 return(TAILQ_LAST(&ds->list, pointlist)->val); 191 } 192 193 static double 194 Avg(struct dataset *ds) 195 { 196 197 return(ds->sy / ds->n); 198 } 199 200 static double 201 Median(struct dataset *ds) 202 { 203 int i; 204 struct point *pp; 205 206 i = ds->n / 2; 207 TAILQ_FOREACH(pp, &ds->list, list) { 208 if (i--) 209 continue; 210 return (pp->val); 211 } 212 } 213 214 static double 215 Var(struct dataset *ds) 216 { 217 218 return (ds->syy - ds->sy * ds->sy / ds->n) / (ds->n - 1.0); 219 } 220 221 static double 222 Stddev(struct dataset *ds) 223 { 224 225 return sqrt(Var(ds)); 226 } 227 228 static void 229 VitalsHead(void) 230 { 231 232 printf(" N Min Max Median Avg Stddev\n"); 233 } 234 235 static void 236 Vitals(struct dataset *ds, int flag) 237 { 238 double a; 239 240 printf("%c %3d %13.8g %13.8g %13.8g %13.8g %13.8g", symbol[flag], 241 ds->n, Min(ds), Max(ds), Median(ds), Avg(ds), Stddev(ds)); 242 printf("\n"); 243 } 244 245 static void 246 Relative(struct dataset *ds, struct dataset *rs, int confidx) 247 { 248 double spool, s, d, e, t; 249 int i, c; 250 251 i = ds->n + rs->n - 2; 252 if (i > NSTUDENT) 253 t = student[0][confidx]; 254 else 255 t = student[i][confidx]; 256 spool = (ds->n - 1) * Var(ds) + (rs->n - 1) * Var(rs); 257 spool /= ds->n + rs->n - 2; 258 spool = sqrt(spool); 259 s = spool * sqrt(1.0 / ds->n + 1.0 / rs->n); 260 d = Avg(ds) - Avg(rs); 261 e = t * s; 262 263 if (fabs(d) > e) { 264 265 printf("Difference at %.1f%% confidence\n", studentpct[confidx]); 266 printf(" %g +/- %g\n", d, e); 267 printf(" %g%% +/- %g%%\n", d * 100 / Avg(rs), e * 100 / Avg(rs)); 268 printf(" (Student's t, pooled s = %g)\n", spool); 269 } else { 270 printf("No difference proven at %.1f%% confidence\n", 271 studentpct[confidx]); 272 } 273 } 274 275 struct plot { 276 double min; 277 double max; 278 double span; 279 int width; 280 281 double x0, dx; 282 int height; 283 char *data; 284 char **bar; 285 int separate_bars; 286 int num_datasets; 287 }; 288 289 static struct plot plot; 290 291 static void 292 SetupPlot(int width, int separate, int num_datasets) 293 { 294 struct plot *pl; 295 296 pl = &plot; 297 pl->width = width; 298 pl->height = 0; 299 pl->data = NULL; 300 pl->bar = NULL; 301 pl->separate_bars = separate; 302 pl->num_datasets = num_datasets; 303 pl->min = 999e99; 304 pl->max = -999e99; 305 } 306 307 static void 308 AdjPlot(double a) 309 { 310 struct plot *pl; 311 312 pl = &plot; 313 if (a < pl->min) 314 pl->min = a; 315 if (a > pl->max) 316 pl->max = a; 317 pl->span = pl->max - pl->min; 318 pl->dx = pl->span / (pl->width - 1.0); 319 pl->x0 = pl->min - .5 * pl->dx; 320 } 321 322 static void 323 DimPlot(struct dataset *ds) 324 { 325 AdjPlot(Min(ds)); 326 AdjPlot(Max(ds)); 327 AdjPlot(Avg(ds) - Stddev(ds)); 328 AdjPlot(Avg(ds) + Stddev(ds)); 329 } 330 331 static void 332 PlotSet(struct dataset *ds, int val) 333 { 334 struct plot *pl; 335 struct point *pp; 336 int i, j, m, x; 337 int bar; 338 339 pl = &plot; 340 if (pl->span == 0) 341 return; 342 343 if (pl->separate_bars) 344 bar = val-1; 345 else 346 bar = 0; 347 348 if (pl->bar == NULL) { 349 pl->bar = malloc(sizeof(char *) * pl->num_datasets); 350 memset(pl->bar, 0, sizeof(char*) * pl->num_datasets); 351 } 352 if (pl->bar[bar] == NULL) { 353 pl->bar[bar] = malloc(pl->width); 354 memset(pl->bar[bar], 0, pl->width); 355 } 356 357 m = 1; 358 i = -1; 359 j = 0; 360 TAILQ_FOREACH(pp, &ds->list, list) { 361 x = (pp->val - pl->x0) / pl->dx; 362 if (x == i) { 363 j++; 364 if (j > m) 365 m = j; 366 } else { 367 j = 1; 368 i = x; 369 } 370 } 371 m += 1; 372 if (m > pl->height) { 373 pl->data = realloc(pl->data, pl->width * m); 374 memset(pl->data + pl->height * pl->width, 0, 375 (m - pl->height) * pl->width); 376 } 377 pl->height = m; 378 i = -1; 379 TAILQ_FOREACH(pp, &ds->list, list) { 380 x = (pp->val - pl->x0) / pl->dx; 381 if (x == i) { 382 j++; 383 } else { 384 j = 1; 385 i = x; 386 } 387 pl->data[j * pl->width + x] |= val; 388 } 389 x = ((Avg(ds) - Stddev(ds)) - pl->x0) / pl->dx; 390 m = ((Avg(ds) + Stddev(ds)) - pl->x0) / pl->dx; 391 pl->bar[bar][m] = '|'; 392 pl->bar[bar][x] = '|'; 393 for (i = x + 1; i < m; i++) 394 if (pl->bar[bar][i] == 0) 395 pl->bar[bar][i] = '_'; 396 x = (Median(ds) - pl->x0) / pl->dx; 397 pl->bar[bar][x] = 'M'; 398 x = (Avg(ds) - pl->x0) / pl->dx; 399 pl->bar[bar][x] = 'A'; 400 } 401 402 static void 403 DumpPlot(void) 404 { 405 struct plot *pl; 406 int i, j, k; 407 408 pl = &plot; 409 if (pl->span == 0) { 410 printf("[no plot, span is zero width]\n"); 411 return; 412 } 413 414 putchar('+'); 415 for (i = 0; i < pl->width; i++) 416 putchar('-'); 417 putchar('+'); 418 putchar('\n'); 419 for (i = 1; i < pl->height; i++) { 420 putchar('|'); 421 for (j = 0; j < pl->width; j++) { 422 k = pl->data[(pl->height - i) * pl->width + j]; 423 if (k >= 0 && k < MAX_DS) 424 putchar(symbol[k]); 425 else 426 printf("[%02x]", k); 427 } 428 putchar('|'); 429 putchar('\n'); 430 } 431 for (i = 0; i < pl->num_datasets; i++) { 432 if (pl->bar[i] == NULL) 433 continue; 434 putchar('|'); 435 for (j = 0; j < pl->width; j++) { 436 k = pl->bar[i][j]; 437 if (k == 0) 438 k = ' '; 439 putchar(k); 440 } 441 putchar('|'); 442 putchar('\n'); 443 } 444 putchar('+'); 445 for (i = 0; i < pl->width; i++) 446 putchar('-'); 447 putchar('+'); 448 putchar('\n'); 449 } 450 451 452 static struct dataset * 453 ReadSet(char *n) 454 { 455 FILE *f; 456 char buf[BUFSIZ], *p; 457 struct dataset *s; 458 double d; 459 int line; 460 461 if (n == NULL) { 462 f = stdin; 463 n = "<stdin>"; 464 } else if (!strcmp(n, "-")) { 465 f = stdin; 466 n = "<stdin>"; 467 } else { 468 f = fopen(n, "r"); 469 } 470 if (f == NULL) 471 err(1, "Cannot open %s", n); 472 s = NewSet(); 473 line = 0; 474 while (fgets(buf, sizeof buf, f) != NULL) { 475 line++; 476 p = strchr(buf, '#'); 477 if (p != NULL) 478 *p = '\0'; 479 p = buf + strlen(buf) - 1; 480 while (p >= buf && isspace(*p)) { 481 *p = '\0'; 482 p--; 483 } 484 d = strtod(buf, &p); 485 if (p != NULL && *p != '\0') 486 err(2, "Invalid data on line %d in %s\n", line, n); 487 if (*buf != '\0') 488 AddPoint(s, d); 489 } 490 fclose(f); 491 if (s->n < 3) { 492 fprintf(stderr, 493 "Dataset %s must contain at least 3 data points\n", n); 494 exit (2); 495 } 496 return (s); 497 } 498 499 static void 500 usage(char const *whine) 501 { 502 int i; 503 504 fprintf(stderr, "%s\n", whine); 505 fprintf(stderr, 506 "Usage: ministat [ -c confidence ] [-ns] [file [file ...]]\n"); 507 fprintf(stderr, "\tconfidence = {"); 508 for (i = 0; i < NCONF; i++) { 509 fprintf(stderr, "%s%g%%", 510 i ? ", " : "", 511 studentpct[i]); 512 } 513 fprintf(stderr, "}\n"); 514 fprintf(stderr, "\t-n : print summary statistics only, no graph/test\n"); 515 fprintf(stderr, "\t-s : print avg/median/stddev bars on separate lines\n"); 516 exit (2); 517 } 518 519 int 520 main(int argc, char **argv) 521 { 522 struct dataset *ds[7]; 523 int nds; 524 double a; 525 char *p; 526 int c, i, ci; 527 int flag_s = 0; 528 int flag_n = 0; 529 530 ci = -1; 531 while ((c = getopt(argc, argv, "c:sn")) != -1) 532 switch (c) { 533 case 'c': 534 a = strtod(optarg, &p); 535 if (p != NULL && *p != '\0') 536 usage("Not a floating point number"); 537 for (i = 0; i < NCONF; i++) 538 if (a == studentpct[i]) 539 ci = i; 540 if (ci == -1) 541 usage("No support for confidence level"); 542 break; 543 case 'n': 544 flag_n = 1; 545 break; 546 case 's': 547 flag_s = 1; 548 break; 549 default: 550 usage("Unknown option"); 551 break; 552 } 553 if (ci == -1) 554 ci = 2; 555 argc -= optind; 556 argv += optind; 557 558 if (argc == 0) { 559 ds[0] = ReadSet(NULL); 560 printf("x stdin\n"); 561 nds = 1; 562 } else { 563 if (argc > (MAX_DS - 1)) 564 usage("Too many datasets."); 565 nds = argc; 566 for (i = 0; i < nds; i++) { 567 ds[i] = ReadSet(argv[i]); 568 printf("%c %s\n", symbol[i+1], argv[i]); 569 } 570 } 571 572 if (!flag_n) { 573 SetupPlot(74, flag_s, nds); 574 for (i = 0; i < nds; i++) 575 DimPlot(ds[i]); 576 for (i = 0; i < nds; i++) 577 PlotSet(ds[i], i + 1); 578 DumpPlot(); 579 } 580 VitalsHead(); 581 Vitals(ds[0], 1); 582 for (i = 1; i < nds; i++) { 583 Vitals(ds[i], i + 1); 584 if (!flag_n) 585 Relative(ds[i], ds[0], ci); 586 } 587 exit(0); 588 } 589