blob: 55e99fc29c1b569d2f846dccdeaec0cbac547cf1 [file] [log] [blame]
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +00001#include "Stats.h"
2
3//-----------------------------------------------------------------------------
4
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +00005double chooseK ( int n, int k )
6{
tanjent@gmail.com96601f22011-03-31 02:41:29 +00007 if(k > (n - k)) k = n - k;
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +00008
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +00009 double c = 1;
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +000010
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000011 for(int i = 0; i < k; i++)
12 {
13 double t = double(n-i) / double(i+1);
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +000014
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000015 c *= t;
16 }
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +000017
18 return c;
19}
20
21double chooseUpToK ( int n, int k )
22{
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000023 double c = 0;
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +000024
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000025 for(int i = 1; i <= k; i++)
26 {
27 c += chooseK(n,i);
28 }
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +000029
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000030 return c;
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +000031}
32
33//-----------------------------------------------------------------------------
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +000034// Distribution "score"
35// TODO - big writeup of what this score means
36
37// Basically, we're computing a constant that says "The test distribution is as
38// uniform, RMS-wise, as a random distribution restricted to (1-X)*100 percent of
39// the bins. This makes for a nice uniform way to rate a distribution that isn't
40// dependent on the number of bins or the number of keys
41
42// (as long as # keys > # bins * 3 or so, otherwise random fluctuations show up
43// as distribution weaknesses)
44
tanjent@gmail.comad4b3632010-11-05 01:20:58 +000045double calcScore ( const int * bins, const int bincount, const int keycount )
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +000046{
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000047 double n = bincount;
48 double k = keycount;
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +000049
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000050 // compute rms value
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +000051
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000052 double r = 0;
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +000053
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000054 for(int i = 0; i < bincount; i++)
55 {
56 double b = bins[i];
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +000057
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000058 r += b*b;
59 }
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +000060
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000061 r = sqrt(r / n);
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +000062
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000063 // compute fill factor
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +000064
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000065 double f = (k*k - 1) / (n*r*r - k);
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +000066
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000067 // rescale to (0,1) with 0 = good, 1 = bad
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +000068
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000069 return 1 - (f / n);
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +000070}
71
72
73//----------------------------------------------------------------------------
74
75void plot ( double n )
76{
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000077 double n2 = n * 1;
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +000078
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000079 if(n2 < 0) n2 = 0;
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +000080
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000081 n2 *= 100;
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +000082
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000083 if(n2 > 64) n2 = 64;
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +000084
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000085 int n3 = (int)n2;
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +000086
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000087 if(n3 == 0)
88 printf(".");
89 else
90 {
91 char x = '0' + char(n3);
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +000092
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000093 if(x > '9') x = 'X';
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +000094
tanjent@gmail.com6ffe0102011-03-19 21:28:26 +000095 printf("%c",x);
96 }
tanjent@gmail.com7e5c3632010-11-02 00:50:04 +000097}
98
99//-----------------------------------------------------------------------------