Created
April 3, 2015 14:17
-
-
Save davidfoerster/079b6d8c92fb702b89aa to your computer and use it in GitHub Desktop.
Perform a simple statistical analysis of arbitrary byte streams
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdint.h> | |
#include <stdio.h> | |
#include <ctype.h> | |
#include <limits.h> | |
#include <math.h> | |
#include <sysexits.h> | |
inline static double square(double x) | |
{ | |
return x * x; | |
} | |
int main() | |
{ | |
static const unsigned distribution_size = 1 << CHAR_BIT; | |
int rv = EX_OK; | |
uintmax_t *distribution = calloc(distribution_size, sizeof(*distribution)); | |
{ | |
int c; | |
while ((c = getchar()) != EOF) | |
distribution[c]++; | |
if (ferror(stdin)) { | |
perror("I/O error on standard input"); | |
rv = EX_IOERR; | |
} | |
} | |
uintmax_t sum = 0; | |
for (unsigned i = 0; i != distribution_size; i++) | |
sum += distribution[i]; | |
double avg = (double) sum / distribution_size; | |
double var_accum = 0.0; | |
for (unsigned i = 0; i != distribution_size; i++) | |
{ | |
const uintmax_t x = distribution[i]; | |
printf("'%c' (%02X): %20ju", isprint((int) i) ? i : ' ', i, x); | |
if (x != 0) { | |
var_accum += square((double) x - avg); | |
printf(" (%+.2e %%)\n", ((double) x / avg - 1.0) * 100.0); | |
} else { | |
var_accum += square(avg); | |
putchar('\n'); | |
} | |
} | |
double stdev = sqrt(var_accum / distribution_size); | |
double varcoeff = stdev / avg; | |
printf( | |
"total: %ju\n" | |
"average: %e\n" | |
"standard deviation: %e\n" | |
"variation coefficient: %e\n", | |
sum, avg, stdev, varcoeff); | |
free(distribution); | |
return rv; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment