All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
cluster.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  ** Filename: cluster.c
3  ** Purpose: Routines for clustering points in N-D space
4  ** Author: Dan Johnson
5  ** History: 5/29/89, DSJ, Created.
6  **
7  ** (c) Copyright Hewlett-Packard Company, 1988.
8  ** Licensed under the Apache License, Version 2.0 (the "License");
9  ** you may not use this file except in compliance with the License.
10  ** You may obtain a copy of the License at
11  ** http://www.apache.org/licenses/LICENSE-2.0
12  ** Unless required by applicable law or agreed to in writing, software
13  ** distributed under the License is distributed on an "AS IS" BASIS,
14  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  ** See the License for the specific language governing permissions and
16  ** limitations under the License.
17  ******************************************************************************/
18 #include "const.h"
19 #include "cluster.h"
20 #include "emalloc.h"
21 #include "genericheap.h"
22 #include "helpers.h"
23 #include "kdpair.h"
24 #include "matrix.h"
25 #include "tprintf.h"
26 #include "danerror.h"
27 #include "freelist.h"
28 #include <math.h>
29 
30 #define HOTELLING 1 // If true use Hotelling's test to decide where to split.
31 #define FTABLE_X 10 // Size of FTable.
32 #define FTABLE_Y 100 // Size of FTable.
33 
34 // Table of values approximating the cumulative F-distribution for a confidence of 1%.
35 const double FTable[FTABLE_Y][FTABLE_X] = {
36  {4052.19, 4999.52, 5403.34, 5624.62, 5763.65, 5858.97, 5928.33, 5981.10, 6022.50, 6055.85,},
37  {98.502, 99.000, 99.166, 99.249, 99.300, 99.333, 99.356, 99.374, 99.388, 99.399,},
38  {34.116, 30.816, 29.457, 28.710, 28.237, 27.911, 27.672, 27.489, 27.345, 27.229,},
39  {21.198, 18.000, 16.694, 15.977, 15.522, 15.207, 14.976, 14.799, 14.659, 14.546,},
40  {16.258, 13.274, 12.060, 11.392, 10.967, 10.672, 10.456, 10.289, 10.158, 10.051,},
41  {13.745, 10.925, 9.780, 9.148, 8.746, 8.466, 8.260, 8.102, 7.976, 7.874,},
42  {12.246, 9.547, 8.451, 7.847, 7.460, 7.191, 6.993, 6.840, 6.719, 6.620,},
43  {11.259, 8.649, 7.591, 7.006, 6.632, 6.371, 6.178, 6.029, 5.911, 5.814,},
44  {10.561, 8.022, 6.992, 6.422, 6.057, 5.802, 5.613, 5.467, 5.351, 5.257,},
45  {10.044, 7.559, 6.552, 5.994, 5.636, 5.386, 5.200, 5.057, 4.942, 4.849,},
46  { 9.646, 7.206, 6.217, 5.668, 5.316, 5.069, 4.886, 4.744, 4.632, 4.539,},
47  { 9.330, 6.927, 5.953, 5.412, 5.064, 4.821, 4.640, 4.499, 4.388, 4.296,},
48  { 9.074, 6.701, 5.739, 5.205, 4.862, 4.620, 4.441, 4.302, 4.191, 4.100,},
49  { 8.862, 6.515, 5.564, 5.035, 4.695, 4.456, 4.278, 4.140, 4.030, 3.939,},
50  { 8.683, 6.359, 5.417, 4.893, 4.556, 4.318, 4.142, 4.004, 3.895, 3.805,},
51  { 8.531, 6.226, 5.292, 4.773, 4.437, 4.202, 4.026, 3.890, 3.780, 3.691,},
52  { 8.400, 6.112, 5.185, 4.669, 4.336, 4.102, 3.927, 3.791, 3.682, 3.593,},
53  { 8.285, 6.013, 5.092, 4.579, 4.248, 4.015, 3.841, 3.705, 3.597, 3.508,},
54  { 8.185, 5.926, 5.010, 4.500, 4.171, 3.939, 3.765, 3.631, 3.523, 3.434,},
55  { 8.096, 5.849, 4.938, 4.431, 4.103, 3.871, 3.699, 3.564, 3.457, 3.368,},
56  { 8.017, 5.780, 4.874, 4.369, 4.042, 3.812, 3.640, 3.506, 3.398, 3.310,},
57  { 7.945, 5.719, 4.817, 4.313, 3.988, 3.758, 3.587, 3.453, 3.346, 3.258,},
58  { 7.881, 5.664, 4.765, 4.264, 3.939, 3.710, 3.539, 3.406, 3.299, 3.211,},
59  { 7.823, 5.614, 4.718, 4.218, 3.895, 3.667, 3.496, 3.363, 3.256, 3.168,},
60  { 7.770, 5.568, 4.675, 4.177, 3.855, 3.627, 3.457, 3.324, 3.217, 3.129,},
61  { 7.721, 5.526, 4.637, 4.140, 3.818, 3.591, 3.421, 3.288, 3.182, 3.094,},
62  { 7.677, 5.488, 4.601, 4.106, 3.785, 3.558, 3.388, 3.256, 3.149, 3.062,},
63  { 7.636, 5.453, 4.568, 4.074, 3.754, 3.528, 3.358, 3.226, 3.120, 3.032,},
64  { 7.598, 5.420, 4.538, 4.045, 3.725, 3.499, 3.330, 3.198, 3.092, 3.005,},
65  { 7.562, 5.390, 4.510, 4.018, 3.699, 3.473, 3.305, 3.173, 3.067, 2.979,},
66  { 7.530, 5.362, 4.484, 3.993, 3.675, 3.449, 3.281, 3.149, 3.043, 2.955,},
67  { 7.499, 5.336, 4.459, 3.969, 3.652, 3.427, 3.258, 3.127, 3.021, 2.934,},
68  { 7.471, 5.312, 4.437, 3.948, 3.630, 3.406, 3.238, 3.106, 3.000, 2.913,},
69  { 7.444, 5.289, 4.416, 3.927, 3.611, 3.386, 3.218, 3.087, 2.981, 2.894,},
70  { 7.419, 5.268, 4.396, 3.908, 3.592, 3.368, 3.200, 3.069, 2.963, 2.876,},
71  { 7.396, 5.248, 4.377, 3.890, 3.574, 3.351, 3.183, 3.052, 2.946, 2.859,},
72  { 7.373, 5.229, 4.360, 3.873, 3.558, 3.334, 3.167, 3.036, 2.930, 2.843,},
73  { 7.353, 5.211, 4.343, 3.858, 3.542, 3.319, 3.152, 3.021, 2.915, 2.828,},
74  { 7.333, 5.194, 4.327, 3.843, 3.528, 3.305, 3.137, 3.006, 2.901, 2.814,},
75  { 7.314, 5.179, 4.313, 3.828, 3.514, 3.291, 3.124, 2.993, 2.888, 2.801,},
76  { 7.296, 5.163, 4.299, 3.815, 3.501, 3.278, 3.111, 2.980, 2.875, 2.788,},
77  { 7.280, 5.149, 4.285, 3.802, 3.488, 3.266, 3.099, 2.968, 2.863, 2.776,},
78  { 7.264, 5.136, 4.273, 3.790, 3.476, 3.254, 3.087, 2.957, 2.851, 2.764,},
79  { 7.248, 5.123, 4.261, 3.778, 3.465, 3.243, 3.076, 2.946, 2.840, 2.754,},
80  { 7.234, 5.110, 4.249, 3.767, 3.454, 3.232, 3.066, 2.935, 2.830, 2.743,},
81  { 7.220, 5.099, 4.238, 3.757, 3.444, 3.222, 3.056, 2.925, 2.820, 2.733,},
82  { 7.207, 5.087, 4.228, 3.747, 3.434, 3.213, 3.046, 2.916, 2.811, 2.724,},
83  { 7.194, 5.077, 4.218, 3.737, 3.425, 3.204, 3.037, 2.907, 2.802, 2.715,},
84  { 7.182, 5.066, 4.208, 3.728, 3.416, 3.195, 3.028, 2.898, 2.793, 2.706,},
85  { 7.171, 5.057, 4.199, 3.720, 3.408, 3.186, 3.020, 2.890, 2.785, 2.698,},
86  { 7.159, 5.047, 4.191, 3.711, 3.400, 3.178, 3.012, 2.882, 2.777, 2.690,},
87  { 7.149, 5.038, 4.182, 3.703, 3.392, 3.171, 3.005, 2.874, 2.769, 2.683,},
88  { 7.139, 5.030, 4.174, 3.695, 3.384, 3.163, 2.997, 2.867, 2.762, 2.675,},
89  { 7.129, 5.021, 4.167, 3.688, 3.377, 3.156, 2.990, 2.860, 2.755, 2.668,},
90  { 7.119, 5.013, 4.159, 3.681, 3.370, 3.149, 2.983, 2.853, 2.748, 2.662,},
91  { 7.110, 5.006, 4.152, 3.674, 3.363, 3.143, 2.977, 2.847, 2.742, 2.655,},
92  { 7.102, 4.998, 4.145, 3.667, 3.357, 3.136, 2.971, 2.841, 2.736, 2.649,},
93  { 7.093, 4.991, 4.138, 3.661, 3.351, 3.130, 2.965, 2.835, 2.730, 2.643,},
94  { 7.085, 4.984, 4.132, 3.655, 3.345, 3.124, 2.959, 2.829, 2.724, 2.637,},
95  { 7.077, 4.977, 4.126, 3.649, 3.339, 3.119, 2.953, 2.823, 2.718, 2.632,},
96  { 7.070, 4.971, 4.120, 3.643, 3.333, 3.113, 2.948, 2.818, 2.713, 2.626,},
97  { 7.062, 4.965, 4.114, 3.638, 3.328, 3.108, 2.942, 2.813, 2.708, 2.621,},
98  { 7.055, 4.959, 4.109, 3.632, 3.323, 3.103, 2.937, 2.808, 2.703, 2.616,},
99  { 7.048, 4.953, 4.103, 3.627, 3.318, 3.098, 2.932, 2.803, 2.698, 2.611,},
100  { 7.042, 4.947, 4.098, 3.622, 3.313, 3.093, 2.928, 2.798, 2.693, 2.607,},
101  { 7.035, 4.942, 4.093, 3.618, 3.308, 3.088, 2.923, 2.793, 2.689, 2.602,},
102  { 7.029, 4.937, 4.088, 3.613, 3.304, 3.084, 2.919, 2.789, 2.684, 2.598,},
103  { 7.023, 4.932, 4.083, 3.608, 3.299, 3.080, 2.914, 2.785, 2.680, 2.593,},
104  { 7.017, 4.927, 4.079, 3.604, 3.295, 3.075, 2.910, 2.781, 2.676, 2.589,},
105  { 7.011, 4.922, 4.074, 3.600, 3.291, 3.071, 2.906, 2.777, 2.672, 2.585,},
106  { 7.006, 4.917, 4.070, 3.596, 3.287, 3.067, 2.902, 2.773, 2.668, 2.581,},
107  { 7.001, 4.913, 4.066, 3.591, 3.283, 3.063, 2.898, 2.769, 2.664, 2.578,},
108  { 6.995, 4.908, 4.062, 3.588, 3.279, 3.060, 2.895, 2.765, 2.660, 2.574,},
109  { 6.990, 4.904, 4.058, 3.584, 3.275, 3.056, 2.891, 2.762, 2.657, 2.570,},
110  { 6.985, 4.900, 4.054, 3.580, 3.272, 3.052, 2.887, 2.758, 2.653, 2.567,},
111  { 6.981, 4.896, 4.050, 3.577, 3.268, 3.049, 2.884, 2.755, 2.650, 2.563,},
112  { 6.976, 4.892, 4.047, 3.573, 3.265, 3.046, 2.881, 2.751, 2.647, 2.560,},
113  { 6.971, 4.888, 4.043, 3.570, 3.261, 3.042, 2.877, 2.748, 2.644, 2.557,},
114  { 6.967, 4.884, 4.040, 3.566, 3.258, 3.039, 2.874, 2.745, 2.640, 2.554,},
115  { 6.963, 4.881, 4.036, 3.563, 3.255, 3.036, 2.871, 2.742, 2.637, 2.551,},
116  { 6.958, 4.877, 4.033, 3.560, 3.252, 3.033, 2.868, 2.739, 2.634, 2.548,},
117  { 6.954, 4.874, 4.030, 3.557, 3.249, 3.030, 2.865, 2.736, 2.632, 2.545,},
118  { 6.950, 4.870, 4.027, 3.554, 3.246, 3.027, 2.863, 2.733, 2.629, 2.542,},
119  { 6.947, 4.867, 4.024, 3.551, 3.243, 3.025, 2.860, 2.731, 2.626, 2.539,},
120  { 6.943, 4.864, 4.021, 3.548, 3.240, 3.022, 2.857, 2.728, 2.623, 2.537,},
121  { 6.939, 4.861, 4.018, 3.545, 3.238, 3.019, 2.854, 2.725, 2.621, 2.534,},
122  { 6.935, 4.858, 4.015, 3.543, 3.235, 3.017, 2.852, 2.723, 2.618, 2.532,},
123  { 6.932, 4.855, 4.012, 3.540, 3.233, 3.014, 2.849, 2.720, 2.616, 2.529,},
124  { 6.928, 4.852, 4.010, 3.538, 3.230, 3.012, 2.847, 2.718, 2.613, 2.527,},
125  { 6.925, 4.849, 4.007, 3.535, 3.228, 3.009, 2.845, 2.715, 2.611, 2.524,},
126  { 6.922, 4.846, 4.004, 3.533, 3.225, 3.007, 2.842, 2.713, 2.609, 2.522,},
127  { 6.919, 4.844, 4.002, 3.530, 3.223, 3.004, 2.840, 2.711, 2.606, 2.520,},
128  { 6.915, 4.841, 3.999, 3.528, 3.221, 3.002, 2.838, 2.709, 2.604, 2.518,},
129  { 6.912, 4.838, 3.997, 3.525, 3.218, 3.000, 2.835, 2.706, 2.602, 2.515,},
130  { 6.909, 4.836, 3.995, 3.523, 3.216, 2.998, 2.833, 2.704, 2.600, 2.513,},
131  { 6.906, 4.833, 3.992, 3.521, 3.214, 2.996, 2.831, 2.702, 2.598, 2.511,},
132  { 6.904, 4.831, 3.990, 3.519, 3.212, 2.994, 2.829, 2.700, 2.596, 2.509,},
133  { 6.901, 4.829, 3.988, 3.517, 3.210, 2.992, 2.827, 2.698, 2.594, 2.507,},
134  { 6.898, 4.826, 3.986, 3.515, 3.208, 2.990, 2.825, 2.696, 2.592, 2.505,},
135  { 6.895, 4.824, 3.984, 3.513, 3.206, 2.988, 2.823, 2.694, 2.590, 2.503}
136 };
137 
142 #define MINVARIANCE 0.0004
143 
150 #define MINSAMPLESPERBUCKET 5
151 #define MINSAMPLES (MINBUCKETS * MINSAMPLESPERBUCKET)
152 #define MINSAMPLESNEEDED 1
153 
160 #define BUCKETTABLESIZE 1024
161 #define NORMALEXTENT 3.0
162 
163 struct TEMPCLUSTER {
166 };
167 
170 
171 struct STATISTICS {
174  FLOAT32 *Min; // largest negative distance from the mean
175  FLOAT32 *Max; // largest positive distance from the mean
176 };
177 
178 struct BUCKETS {
179  DISTRIBUTION Distribution; // distribution being tested for
180  uinT32 SampleCount; // # of samples in histogram
181  FLOAT64 Confidence; // confidence level of test
182  FLOAT64 ChiSquared; // test threshold
183  uinT16 NumberOfBuckets; // number of cells in histogram
184  uinT16 Bucket[BUCKETTABLESIZE];// mapping to histogram buckets
185  uinT32 *Count; // frequency of occurence histogram
186  FLOAT32 *ExpectedCount; // expected histogram
187 };
188 
189 struct CHISTRUCT{
193 };
194 
195 // For use with KDWalk / MakePotentialClusters
197  ClusterHeap *heap; // heap used to hold temp clusters, "best" on top
198  TEMPCLUSTER *candidates; // array of potential clusters
199  KDTREE *tree; // kd-tree to be searched for neighbors
200  inT32 next; // next candidate to be used
201 };
202 
203 typedef FLOAT64 (*DENSITYFUNC) (inT32);
204 typedef FLOAT64 (*SOLVEFUNC) (CHISTRUCT *, double);
205 
206 #define Odd(N) ((N)%2)
207 #define Mirror(N,R) ((R) - (N) - 1)
208 #define Abs(N) ( ( (N) < 0 ) ? ( -(N) ) : (N) )
209 
210 //--------------Global Data Definitions and Declarations----------------------
218 #define SqrtOf2Pi 2.506628275
219 static const FLOAT64 kNormalStdDev = BUCKETTABLESIZE / (2.0 * NORMALEXTENT);
220 static const FLOAT64 kNormalVariance =
222 static const FLOAT64 kNormalMagnitude =
223  (2.0 * NORMALEXTENT) / (SqrtOf2Pi * BUCKETTABLESIZE);
224 static const FLOAT64 kNormalMean = BUCKETTABLESIZE / 2;
225 
228 #define LOOKUPTABLESIZE 8
229 #define MAXDEGREESOFFREEDOM MAXBUCKETS
230 
231 static const uinT32 kCountTable[LOOKUPTABLESIZE] = {
232  MINSAMPLES, 200, 400, 600, 800, 1000, 1500, 2000
233 }; // number of samples
234 
235 static const uinT16 kBucketsTable[LOOKUPTABLESIZE] = {
236  MINBUCKETS, 16, 20, 24, 27, 30, 35, MAXBUCKETS
237 }; // number of buckets
238 
239 /*-------------------------------------------------------------------------
240  Private Function Prototypes
241 --------------------------------------------------------------------------*/
242 void CreateClusterTree(CLUSTERER *Clusterer);
243 
244 void MakePotentialClusters(ClusteringContext *context, CLUSTER *Cluster,
245  inT32 Level);
246 
248  CLUSTER *Cluster,
249  FLOAT32 *Distance);
250 
251 CLUSTER *MakeNewCluster(CLUSTERER *Clusterer, TEMPCLUSTER *TempCluster);
252 
254 register PARAM_DESC ParamDesc[],
255 register inT32 n1,
256 register inT32 n2,
257 register FLOAT32 m[],
258 register FLOAT32 m1[], register FLOAT32 m2[]);
259 
261 
262 PROTOTYPE *MakePrototype(CLUSTERER *Clusterer,
264  CLUSTER *Cluster);
265 
267  CLUSTER *Cluster,
268  STATISTICS *Statistics,
269  PROTOSTYLE Style,
270  inT32 MinSamples);
271 
274  CLUSTER *Cluster,
275  STATISTICS *Statistics);
276 
278  CLUSTER *Cluster,
279  STATISTICS *Statistics,
280  BUCKETS *Buckets);
281 
283  CLUSTER *Cluster,
284  STATISTICS *Statistics,
285  BUCKETS *Buckets);
286 
288  CLUSTER *Cluster,
289  STATISTICS *Statistics,
290  BUCKETS *NormalBuckets,
291  FLOAT64 Confidence);
292 
293 void MakeDimRandom(uinT16 i, PROTOTYPE *Proto, PARAM_DESC *ParamDesc);
294 
295 void MakeDimUniform(uinT16 i, PROTOTYPE *Proto, STATISTICS *Statistics);
296 
298 PARAM_DESC ParamDesc[], CLUSTER * Cluster);
299 
301  CLUSTER *Cluster,
302  STATISTICS *Statistics);
303 
305  CLUSTER *Cluster,
306  STATISTICS *Statistics);
307 
308 PROTOTYPE *NewMixedProto(inT16 N, CLUSTER *Cluster, STATISTICS *Statistics);
309 
310 PROTOTYPE *NewSimpleProto(inT16 N, CLUSTER *Cluster);
311 
312 BOOL8 Independent (PARAM_DESC ParamDesc[],
313 inT16 N, FLOAT32 * CoVariance, FLOAT32 Independence);
314 
315 BUCKETS *GetBuckets(CLUSTERER* clusterer,
316  DISTRIBUTION Distribution,
317  uinT32 SampleCount,
318  FLOAT64 Confidence);
319 
320 BUCKETS *MakeBuckets(DISTRIBUTION Distribution,
321  uinT32 SampleCount,
322  FLOAT64 Confidence);
323 
325 
327 
329 
331 
333 
334 void FillBuckets(BUCKETS *Buckets,
335  CLUSTER *Cluster,
336  uinT16 Dim,
337  PARAM_DESC *ParamDesc,
338  FLOAT32 Mean,
339  FLOAT32 StdDev);
340 
341 uinT16 NormalBucket(PARAM_DESC *ParamDesc,
342  FLOAT32 x,
343  FLOAT32 Mean,
344  FLOAT32 StdDev);
345 
346 uinT16 UniformBucket(PARAM_DESC *ParamDesc,
347  FLOAT32 x,
348  FLOAT32 Mean,
349  FLOAT32 StdDev);
350 
351 BOOL8 DistributionOK(BUCKETS *Buckets);
352 
353 void FreeStatistics(STATISTICS *Statistics);
354 
355 void FreeBuckets(BUCKETS *Buckets);
356 
357 void FreeCluster(CLUSTER *Cluster);
358 
359 uinT16 DegreesOfFreedom(DISTRIBUTION Distribution, uinT16 HistogramBuckets);
360 
361 int NumBucketsMatch(void *arg1, // BUCKETS *Histogram,
362  void *arg2); // uinT16 *DesiredNumberOfBuckets);
363 
364 int ListEntryMatch(void *arg1, void *arg2);
365 
366 void AdjustBuckets(BUCKETS *Buckets, uinT32 NewSampleCount);
367 
368 void InitBuckets(BUCKETS *Buckets);
369 
370 int AlphaMatch(void *arg1, // CHISTRUCT *ChiStruct,
371  void *arg2); // CHISTRUCT *SearchKey);
372 
374 
375 FLOAT64 Solve(SOLVEFUNC Function,
376  void *FunctionParams,
377  FLOAT64 InitialGuess,
378  FLOAT64 Accuracy);
379 
380 FLOAT64 ChiArea(CHISTRUCT *ChiParams, FLOAT64 x);
381 
383  CLUSTER *Cluster,
384  FLOAT32 MaxIllegal);
385 
386 double InvertMatrix(const float* input, int size, float* inv);
387 
388 //--------------------------Public Code--------------------------------------
399 CLUSTERER *
400 MakeClusterer (inT16 SampleSize, const PARAM_DESC ParamDesc[]) {
401  CLUSTERER *Clusterer;
402  int i;
403 
404  // allocate main clusterer data structure and init simple fields
405  Clusterer = (CLUSTERER *) Emalloc (sizeof (CLUSTERER));
406  Clusterer->SampleSize = SampleSize;
407  Clusterer->NumberOfSamples = 0;
408  Clusterer->NumChar = 0;
409 
410  // init fields which will not be used initially
411  Clusterer->Root = NULL;
412  Clusterer->ProtoList = NIL_LIST;
413 
414  // maintain a copy of param descriptors in the clusterer data structure
415  Clusterer->ParamDesc =
416  (PARAM_DESC *) Emalloc (SampleSize * sizeof (PARAM_DESC));
417  for (i = 0; i < SampleSize; i++) {
418  Clusterer->ParamDesc[i].Circular = ParamDesc[i].Circular;
419  Clusterer->ParamDesc[i].NonEssential = ParamDesc[i].NonEssential;
420  Clusterer->ParamDesc[i].Min = ParamDesc[i].Min;
421  Clusterer->ParamDesc[i].Max = ParamDesc[i].Max;
422  Clusterer->ParamDesc[i].Range = ParamDesc[i].Max - ParamDesc[i].Min;
423  Clusterer->ParamDesc[i].HalfRange = Clusterer->ParamDesc[i].Range / 2;
424  Clusterer->ParamDesc[i].MidRange =
425  (ParamDesc[i].Max + ParamDesc[i].Min) / 2;
426  }
427 
428  // allocate a kd tree to hold the samples
429  Clusterer->KDTree = MakeKDTree (SampleSize, ParamDesc);
430 
431  // Initialize cache of histogram buckets to minimize recomputing them.
432  for (int d = 0; d < DISTRIBUTION_COUNT; ++d) {
433  for (int c = 0; c < MAXBUCKETS + 1 - MINBUCKETS; ++c)
434  Clusterer->bucket_cache[d][c] = NULL;
435  }
436 
437  return Clusterer;
438 } // MakeClusterer
439 
440 
457 SAMPLE* MakeSample(CLUSTERER * Clusterer, const FLOAT32* Feature,
458  inT32 CharID) {
459  SAMPLE *Sample;
460  int i;
461 
462  // see if the samples have already been clustered - if so trap an error
463  if (Clusterer->Root != NULL)
465  "Can't add samples after they have been clustered");
466 
467  // allocate the new sample and initialize it
468  Sample = (SAMPLE *) Emalloc (sizeof (SAMPLE) +
469  (Clusterer->SampleSize -
470  1) * sizeof (FLOAT32));
471  Sample->Clustered = FALSE;
472  Sample->Prototype = FALSE;
473  Sample->SampleCount = 1;
474  Sample->Left = NULL;
475  Sample->Right = NULL;
476  Sample->CharID = CharID;
477 
478  for (i = 0; i < Clusterer->SampleSize; i++)
479  Sample->Mean[i] = Feature[i];
480 
481  // add the sample to the KD tree - keep track of the total # of samples
482  Clusterer->NumberOfSamples++;
483  KDStore (Clusterer->KDTree, Sample->Mean, (char *) Sample);
484  if (CharID >= Clusterer->NumChar)
485  Clusterer->NumChar = CharID + 1;
486 
487  // execute hook for monitoring clustering operation
488  // (*SampleCreationHook)( Sample );
489 
490  return (Sample);
491 } // MakeSample
492 
493 
516  //only create cluster tree if samples have never been clustered before
517  if (Clusterer->Root == NULL)
518  CreateClusterTree(Clusterer);
519 
520  //deallocate the old prototype list if one exists
521  FreeProtoList (&Clusterer->ProtoList);
522  Clusterer->ProtoList = NIL_LIST;
523 
524  //compute prototypes starting at the root node in the tree
525  ComputePrototypes(Clusterer, Config);
526  return (Clusterer->ProtoList);
527 } // ClusterSamples
528 
529 
543 void FreeClusterer(CLUSTERER *Clusterer) {
544  if (Clusterer != NULL) {
545  memfree (Clusterer->ParamDesc);
546  if (Clusterer->KDTree != NULL)
547  FreeKDTree (Clusterer->KDTree);
548  if (Clusterer->Root != NULL)
549  FreeCluster (Clusterer->Root);
550  // Free up all used buckets structures.
551  for (int d = 0; d < DISTRIBUTION_COUNT; ++d) {
552  for (int c = 0; c < MAXBUCKETS + 1 - MINBUCKETS; ++c)
553  if (Clusterer->bucket_cache[d][c] != NULL)
554  FreeBuckets(Clusterer->bucket_cache[d][c]);
555  }
556 
557  memfree(Clusterer);
558  }
559 } // FreeClusterer
560 
561 
571 void FreeProtoList(LIST *ProtoList) {
572  destroy_nodes(*ProtoList, FreePrototype);
573 } // FreeProtoList
574 
575 
586 void FreePrototype(void *arg) { //PROTOTYPE *Prototype)
587  PROTOTYPE *Prototype = (PROTOTYPE *) arg;
588 
589  // unmark the corresponding cluster (if there is one
590  if (Prototype->Cluster != NULL)
591  Prototype->Cluster->Prototype = FALSE;
592 
593  // deallocate the prototype statistics and then the prototype itself
594  if (Prototype->Distrib != NULL)
595  memfree (Prototype->Distrib);
596  if (Prototype->Mean != NULL)
597  memfree (Prototype->Mean);
598  if (Prototype->Style != spherical) {
599  if (Prototype->Variance.Elliptical != NULL)
600  memfree (Prototype->Variance.Elliptical);
601  if (Prototype->Magnitude.Elliptical != NULL)
602  memfree (Prototype->Magnitude.Elliptical);
603  if (Prototype->Weight.Elliptical != NULL)
604  memfree (Prototype->Weight.Elliptical);
605  }
606  memfree(Prototype);
607 } // FreePrototype
608 
609 
625 CLUSTER *NextSample(LIST *SearchState) {
626  CLUSTER *Cluster;
627 
628  if (*SearchState == NIL_LIST)
629  return (NULL);
630  Cluster = (CLUSTER *) first_node (*SearchState);
631  *SearchState = pop (*SearchState);
632  while (TRUE) {
633  if (Cluster->Left == NULL)
634  return (Cluster);
635  *SearchState = push (*SearchState, Cluster->Right);
636  Cluster = Cluster->Left;
637  }
638 } // NextSample
639 
640 
650 FLOAT32 Mean(PROTOTYPE *Proto, uinT16 Dimension) {
651  return (Proto->Mean[Dimension]);
652 } // Mean
653 
654 
665  switch (Proto->Style) {
666  case spherical:
667  return ((FLOAT32) sqrt ((double) Proto->Variance.Spherical));
668  case elliptical:
669  return ((FLOAT32)
670  sqrt ((double) Proto->Variance.Elliptical[Dimension]));
671  case mixed:
672  switch (Proto->Distrib[Dimension]) {
673  case normal:
674  return ((FLOAT32)
675  sqrt ((double) Proto->Variance.Elliptical[Dimension]));
676  case uniform:
677  case D_random:
678  return (Proto->Variance.Elliptical[Dimension]);
679  case DISTRIBUTION_COUNT:
680  ASSERT_HOST(!"Distribution count not allowed!");
681  }
682  }
683  return 0.0f;
684 } // StandardDeviation
685 
686 
687 /*---------------------------------------------------------------------------
688  Private Code
689 ----------------------------------------------------------------------------*/
705 void CreateClusterTree(CLUSTERER *Clusterer) {
706  ClusteringContext context;
707  ClusterPair HeapEntry;
708  TEMPCLUSTER *PotentialCluster;
709 
710  // each sample and its nearest neighbor form a "potential" cluster
711  // save these in a heap with the "best" potential clusters on top
712  context.tree = Clusterer->KDTree;
713  context.candidates = (TEMPCLUSTER *)
714  Emalloc(Clusterer->NumberOfSamples * sizeof(TEMPCLUSTER));
715  context.next = 0;
716  context.heap = new ClusterHeap(Clusterer->NumberOfSamples);
717  KDWalk(context.tree, (void_proc)MakePotentialClusters, &context);
718 
719  // form potential clusters into actual clusters - always do "best" first
720  while (context.heap->Pop(&HeapEntry)) {
721  PotentialCluster = HeapEntry.data;
722 
723  // if main cluster of potential cluster is already in another cluster
724  // then we don't need to worry about it
725  if (PotentialCluster->Cluster->Clustered) {
726  continue;
727  }
728 
729  // if main cluster is not yet clustered, but its nearest neighbor is
730  // then we must find a new nearest neighbor
731  else if (PotentialCluster->Neighbor->Clustered) {
732  PotentialCluster->Neighbor =
733  FindNearestNeighbor(context.tree, PotentialCluster->Cluster,
734  &HeapEntry.key);
735  if (PotentialCluster->Neighbor != NULL) {
736  context.heap->Push(&HeapEntry);
737  }
738  }
739 
740  // if neither cluster is already clustered, form permanent cluster
741  else {
742  PotentialCluster->Cluster =
743  MakeNewCluster(Clusterer, PotentialCluster);
744  PotentialCluster->Neighbor =
745  FindNearestNeighbor(context.tree, PotentialCluster->Cluster,
746  &HeapEntry.key);
747  if (PotentialCluster->Neighbor != NULL) {
748  context.heap->Push(&HeapEntry);
749  }
750  }
751  }
752 
753  // the root node in the cluster tree is now the only node in the kd-tree
754  Clusterer->Root = (CLUSTER *) RootOf(Clusterer->KDTree);
755 
756  // free up the memory used by the K-D tree, heap, and temp clusters
757  FreeKDTree(context.tree);
758  Clusterer->KDTree = NULL;
759  delete context.heap;
760  memfree(context.candidates);
761 } // CreateClusterTree
762 
763 
774  CLUSTER *Cluster, inT32 Level) {
775  ClusterPair HeapEntry;
776  int next = context->next;
777  context->candidates[next].Cluster = Cluster;
778  HeapEntry.data = &(context->candidates[next]);
779  context->candidates[next].Neighbor =
780  FindNearestNeighbor(context->tree,
781  context->candidates[next].Cluster,
782  &HeapEntry.key);
783  if (context->candidates[next].Neighbor != NULL) {
784  context->heap->Push(&HeapEntry);
785  context->next++;
786  }
787 } // MakePotentialClusters
788 
789 
806 CLUSTER *
807 FindNearestNeighbor(KDTREE * Tree, CLUSTER * Cluster, FLOAT32 * Distance)
808 #define MAXNEIGHBORS 2
809 #define MAXDISTANCE MAX_FLOAT32
810 {
811  CLUSTER *Neighbor[MAXNEIGHBORS];
812  FLOAT32 Dist[MAXNEIGHBORS];
813  int NumberOfNeighbors;
814  inT32 i;
815  CLUSTER *BestNeighbor;
816 
817  // find the 2 nearest neighbors of the cluster
819  &NumberOfNeighbors, (void **)Neighbor, Dist);
820 
821  // search for the nearest neighbor that is not the cluster itself
822  *Distance = MAXDISTANCE;
823  BestNeighbor = NULL;
824  for (i = 0; i < NumberOfNeighbors; i++) {
825  if ((Dist[i] < *Distance) && (Neighbor[i] != Cluster)) {
826  *Distance = Dist[i];
827  BestNeighbor = Neighbor[i];
828  }
829  }
830  return BestNeighbor;
831 } // FindNearestNeighbor
832 
833 
846 CLUSTER *MakeNewCluster(CLUSTERER *Clusterer, TEMPCLUSTER *TempCluster) {
847  CLUSTER *Cluster;
848 
849  // allocate the new cluster and initialize it
850  Cluster = (CLUSTER *) Emalloc(
851  sizeof(CLUSTER) + (Clusterer->SampleSize - 1) * sizeof(FLOAT32));
852  Cluster->Clustered = FALSE;
853  Cluster->Prototype = FALSE;
854  Cluster->Left = TempCluster->Cluster;
855  Cluster->Right = TempCluster->Neighbor;
856  Cluster->CharID = -1;
857 
858  // mark the old clusters as "clustered" and delete them from the kd-tree
859  Cluster->Left->Clustered = TRUE;
860  Cluster->Right->Clustered = TRUE;
861  KDDelete(Clusterer->KDTree, Cluster->Left->Mean, Cluster->Left);
862  KDDelete(Clusterer->KDTree, Cluster->Right->Mean, Cluster->Right);
863 
864  // compute the mean and sample count for the new cluster
865  Cluster->SampleCount =
866  MergeClusters(Clusterer->SampleSize, Clusterer->ParamDesc,
867  Cluster->Left->SampleCount, Cluster->Right->SampleCount,
868  Cluster->Mean, Cluster->Left->Mean, Cluster->Right->Mean);
869 
870  // add the new cluster to the KD tree
871  KDStore(Clusterer->KDTree, Cluster->Mean, Cluster);
872  return Cluster;
873 } // MakeNewCluster
874 
875 
892  PARAM_DESC ParamDesc[],
893  inT32 n1,
894  inT32 n2,
895  FLOAT32 m[],
896  FLOAT32 m1[], FLOAT32 m2[]) {
897  inT32 i, n;
898 
899  n = n1 + n2;
900  for (i = N; i > 0; i--, ParamDesc++, m++, m1++, m2++) {
901  if (ParamDesc->Circular) {
902  // if distance between means is greater than allowed
903  // reduce upper point by one "rotation" to compute mean
904  // then normalize the mean back into the accepted range
905  if ((*m2 - *m1) > ParamDesc->HalfRange) {
906  *m = (n1 * *m1 + n2 * (*m2 - ParamDesc->Range)) / n;
907  if (*m < ParamDesc->Min)
908  *m += ParamDesc->Range;
909  }
910  else if ((*m1 - *m2) > ParamDesc->HalfRange) {
911  *m = (n1 * (*m1 - ParamDesc->Range) + n2 * *m2) / n;
912  if (*m < ParamDesc->Min)
913  *m += ParamDesc->Range;
914  }
915  else
916  *m = (n1 * *m1 + n2 * *m2) / n;
917  }
918  else
919  *m = (n1 * *m1 + n2 * *m2) / n;
920  }
921  return n;
922 } // MergeClusters
923 
924 
937  LIST ClusterStack = NIL_LIST;
938  CLUSTER *Cluster;
939  PROTOTYPE *Prototype;
940 
941  // use a stack to keep track of clusters waiting to be processed
942  // initially the only cluster on the stack is the root cluster
943  if (Clusterer->Root != NULL)
944  ClusterStack = push (NIL_LIST, Clusterer->Root);
945 
946  // loop until we have analyzed all clusters which are potential prototypes
947  while (ClusterStack != NIL_LIST) {
948  // remove the next cluster to be analyzed from the stack
949  // try to make a prototype from the cluster
950  // if successful, put it on the proto list, else split the cluster
951  Cluster = (CLUSTER *) first_node (ClusterStack);
952  ClusterStack = pop (ClusterStack);
953  Prototype = MakePrototype(Clusterer, Config, Cluster);
954  if (Prototype != NULL) {
955  Clusterer->ProtoList = push (Clusterer->ProtoList, Prototype);
956  }
957  else {
958  ClusterStack = push (ClusterStack, Cluster->Right);
959  ClusterStack = push (ClusterStack, Cluster->Left);
960  }
961  }
962 } // ComputePrototypes
963 
964 
984  CLUSTER *Cluster) {
985  STATISTICS *Statistics;
986  PROTOTYPE *Proto;
987  BUCKETS *Buckets;
988 
989  // filter out clusters which contain samples from the same character
990  if (MultipleCharSamples (Clusterer, Cluster, Config->MaxIllegal))
991  return NULL;
992 
993  // compute the covariance matrix and ranges for the cluster
994  Statistics =
995  ComputeStatistics(Clusterer->SampleSize, Clusterer->ParamDesc, Cluster);
996 
997  // check for degenerate clusters which need not be analyzed further
998  // note that the MinSamples test assumes that all clusters with multiple
999  // character samples have been removed (as above)
1000  Proto = MakeDegenerateProto(
1001  Clusterer->SampleSize, Cluster, Statistics, Config->ProtoStyle,
1002  (inT32) (Config->MinSamples * Clusterer->NumChar));
1003  if (Proto != NULL) {
1004  FreeStatistics(Statistics);
1005  return Proto;
1006  }
1007  // check to ensure that all dimensions are independent
1008  if (!Independent(Clusterer->ParamDesc, Clusterer->SampleSize,
1009  Statistics->CoVariance, Config->Independence)) {
1010  FreeStatistics(Statistics);
1011  return NULL;
1012  }
1013 
1014  if (HOTELLING && Config->ProtoStyle == elliptical) {
1015  Proto = TestEllipticalProto(Clusterer, Config, Cluster, Statistics);
1016  if (Proto != NULL) {
1017  FreeStatistics(Statistics);
1018  return Proto;
1019  }
1020  }
1021 
1022  // create a histogram data structure used to evaluate distributions
1023  Buckets = GetBuckets(Clusterer, normal, Cluster->SampleCount,
1024  Config->Confidence);
1025 
1026  // create a prototype based on the statistics and test it
1027  switch (Config->ProtoStyle) {
1028  case spherical:
1029  Proto = MakeSphericalProto(Clusterer, Cluster, Statistics, Buckets);
1030  break;
1031  case elliptical:
1032  Proto = MakeEllipticalProto(Clusterer, Cluster, Statistics, Buckets);
1033  break;
1034  case mixed:
1035  Proto = MakeMixedProto(Clusterer, Cluster, Statistics, Buckets,
1036  Config->Confidence);
1037  break;
1038  case automatic:
1039  Proto = MakeSphericalProto(Clusterer, Cluster, Statistics, Buckets);
1040  if (Proto != NULL)
1041  break;
1042  Proto = MakeEllipticalProto(Clusterer, Cluster, Statistics, Buckets);
1043  if (Proto != NULL)
1044  break;
1045  Proto = MakeMixedProto(Clusterer, Cluster, Statistics, Buckets,
1046  Config->Confidence);
1047  break;
1048  }
1049  FreeStatistics(Statistics);
1050  return Proto;
1051 } // MakePrototype
1052 
1053 
1077 PROTOTYPE *MakeDegenerateProto( //this was MinSample
1078  uinT16 N,
1079  CLUSTER *Cluster,
1080  STATISTICS *Statistics,
1081  PROTOSTYLE Style,
1082  inT32 MinSamples) {
1083  PROTOTYPE *Proto = NULL;
1084 
1085  if (MinSamples < MINSAMPLESNEEDED)
1086  MinSamples = MINSAMPLESNEEDED;
1087 
1088  if (Cluster->SampleCount < MinSamples) {
1089  switch (Style) {
1090  case spherical:
1091  Proto = NewSphericalProto (N, Cluster, Statistics);
1092  break;
1093  case elliptical:
1094  case automatic:
1095  Proto = NewEllipticalProto (N, Cluster, Statistics);
1096  break;
1097  case mixed:
1098  Proto = NewMixedProto (N, Cluster, Statistics);
1099  break;
1100  }
1101  Proto->Significant = FALSE;
1102  }
1103  return (Proto);
1104 } // MakeDegenerateProto
1105 
1121  CLUSTER *Cluster,
1122  STATISTICS *Statistics) {
1123  // Fraction of the number of samples used as a range around 1 within
1124  // which a cluster has the magic size that allows a boost to the
1125  // FTable by kFTableBoostMargin, thus allowing clusters near the
1126  // magic size (equal to the number of sample characters) to be more
1127  // likely to stay together.
1128  const double kMagicSampleMargin = 0.0625;
1129  const double kFTableBoostMargin = 2.0;
1130 
1131  int N = Clusterer->SampleSize;
1132  CLUSTER* Left = Cluster->Left;
1133  CLUSTER* Right = Cluster->Right;
1134  if (Left == NULL || Right == NULL)
1135  return NULL;
1136  int TotalDims = Left->SampleCount + Right->SampleCount;
1137  if (TotalDims < N + 1 || TotalDims < 2)
1138  return NULL;
1139  const int kMatrixSize = N * N * sizeof(FLOAT32);
1140  FLOAT32* Covariance = reinterpret_cast<FLOAT32 *>(Emalloc(kMatrixSize));
1141  FLOAT32* Inverse = reinterpret_cast<FLOAT32 *>(Emalloc(kMatrixSize));
1142  FLOAT32* Delta = reinterpret_cast<FLOAT32*>(Emalloc(N * sizeof(FLOAT32)));
1143  // Compute a new covariance matrix that only uses essential features.
1144  for (int i = 0; i < N; ++i) {
1145  int row_offset = i * N;
1146  if (!Clusterer->ParamDesc[i].NonEssential) {
1147  for (int j = 0; j < N; ++j) {
1148  if (!Clusterer->ParamDesc[j].NonEssential)
1149  Covariance[j + row_offset] = Statistics->CoVariance[j + row_offset];
1150  else
1151  Covariance[j + row_offset] = 0.0f;
1152  }
1153  } else {
1154  for (int j = 0; j < N; ++j) {
1155  if (i == j)
1156  Covariance[j + row_offset] = 1.0f;
1157  else
1158  Covariance[j + row_offset] = 0.0f;
1159  }
1160  }
1161  }
1162  double err = InvertMatrix(Covariance, N, Inverse);
1163  if (err > 1) {
1164  tprintf("Clustering error: Matrix inverse failed with error %g\n", err);
1165  }
1166  int EssentialN = 0;
1167  for (int dim = 0; dim < N; ++dim) {
1168  if (!Clusterer->ParamDesc[dim].NonEssential) {
1169  Delta[dim] = Left->Mean[dim] - Right->Mean[dim];
1170  ++EssentialN;
1171  } else {
1172  Delta[dim] = 0.0f;
1173  }
1174  }
1175  // Compute Hotelling's T-squared.
1176  double Tsq = 0.0;
1177  for (int x = 0; x < N; ++x) {
1178  double temp = 0.0;
1179  for (int y = 0; y < N; ++y) {
1180  temp += Inverse[y + N*x] * Delta[y];
1181  }
1182  Tsq += Delta[x] * temp;
1183  }
1184  memfree(Covariance);
1185  memfree(Inverse);
1186  memfree(Delta);
1187  // Changed this function to match the formula in
1188  // Statistical Methods in Medical Research p 473
1189  // By Peter Armitage, Geoffrey Berry, J. N. S. Matthews.
1190  // Tsq *= Left->SampleCount * Right->SampleCount / TotalDims;
1191  double F = Tsq * (TotalDims - EssentialN - 1) / ((TotalDims - 2)*EssentialN);
1192  int Fx = EssentialN;
1193  if (Fx > FTABLE_X)
1194  Fx = FTABLE_X;
1195  --Fx;
1196  int Fy = TotalDims - EssentialN - 1;
1197  if (Fy > FTABLE_Y)
1198  Fy = FTABLE_Y;
1199  --Fy;
1200  double FTarget = FTable[Fy][Fx];
1201  if (Config->MagicSamples > 0 &&
1202  TotalDims >= Config->MagicSamples * (1.0 - kMagicSampleMargin) &&
1203  TotalDims <= Config->MagicSamples * (1.0 + kMagicSampleMargin)) {
1204  // Give magic-sized clusters a magic FTable boost.
1205  FTarget += kFTableBoostMargin;
1206  }
1207  if (F < FTarget) {
1208  return NewEllipticalProto (Clusterer->SampleSize, Cluster, Statistics);
1209  }
1210  return NULL;
1211 }
1212 
1227  CLUSTER *Cluster,
1228  STATISTICS *Statistics,
1229  BUCKETS *Buckets) {
1230  PROTOTYPE *Proto = NULL;
1231  int i;
1232 
1233  // check that each dimension is a normal distribution
1234  for (i = 0; i < Clusterer->SampleSize; i++) {
1235  if (Clusterer->ParamDesc[i].NonEssential)
1236  continue;
1237 
1238  FillBuckets (Buckets, Cluster, i, &(Clusterer->ParamDesc[i]),
1239  Cluster->Mean[i],
1240  sqrt ((FLOAT64) (Statistics->AvgVariance)));
1241  if (!DistributionOK (Buckets))
1242  break;
1243  }
1244  // if all dimensions matched a normal distribution, make a proto
1245  if (i >= Clusterer->SampleSize)
1246  Proto = NewSphericalProto (Clusterer->SampleSize, Cluster, Statistics);
1247  return (Proto);
1248 } // MakeSphericalProto
1249 
1250 
1265  CLUSTER *Cluster,
1266  STATISTICS *Statistics,
1267  BUCKETS *Buckets) {
1268  PROTOTYPE *Proto = NULL;
1269  int i;
1270 
1271  // check that each dimension is a normal distribution
1272  for (i = 0; i < Clusterer->SampleSize; i++) {
1273  if (Clusterer->ParamDesc[i].NonEssential)
1274  continue;
1275 
1276  FillBuckets (Buckets, Cluster, i, &(Clusterer->ParamDesc[i]),
1277  Cluster->Mean[i],
1278  sqrt ((FLOAT64) Statistics->
1279  CoVariance[i * (Clusterer->SampleSize + 1)]));
1280  if (!DistributionOK (Buckets))
1281  break;
1282  }
1283  // if all dimensions matched a normal distribution, make a proto
1284  if (i >= Clusterer->SampleSize)
1285  Proto = NewEllipticalProto (Clusterer->SampleSize, Cluster, Statistics);
1286  return (Proto);
1287 } // MakeEllipticalProto
1288 
1289 
1308  CLUSTER *Cluster,
1309  STATISTICS *Statistics,
1310  BUCKETS *NormalBuckets,
1311  FLOAT64 Confidence) {
1312  PROTOTYPE *Proto;
1313  int i;
1314  BUCKETS *UniformBuckets = NULL;
1315  BUCKETS *RandomBuckets = NULL;
1316 
1317  // create a mixed proto to work on - initially assume all dimensions normal*/
1318  Proto = NewMixedProto (Clusterer->SampleSize, Cluster, Statistics);
1319 
1320  // find the proper distribution for each dimension
1321  for (i = 0; i < Clusterer->SampleSize; i++) {
1322  if (Clusterer->ParamDesc[i].NonEssential)
1323  continue;
1324 
1325  FillBuckets (NormalBuckets, Cluster, i, &(Clusterer->ParamDesc[i]),
1326  Proto->Mean[i],
1327  sqrt ((FLOAT64) Proto->Variance.Elliptical[i]));
1328  if (DistributionOK (NormalBuckets))
1329  continue;
1330 
1331  if (RandomBuckets == NULL)
1332  RandomBuckets =
1333  GetBuckets(Clusterer, D_random, Cluster->SampleCount, Confidence);
1334  MakeDimRandom (i, Proto, &(Clusterer->ParamDesc[i]));
1335  FillBuckets (RandomBuckets, Cluster, i, &(Clusterer->ParamDesc[i]),
1336  Proto->Mean[i], Proto->Variance.Elliptical[i]);
1337  if (DistributionOK (RandomBuckets))
1338  continue;
1339 
1340  if (UniformBuckets == NULL)
1341  UniformBuckets =
1342  GetBuckets(Clusterer, uniform, Cluster->SampleCount, Confidence);
1343  MakeDimUniform(i, Proto, Statistics);
1344  FillBuckets (UniformBuckets, Cluster, i, &(Clusterer->ParamDesc[i]),
1345  Proto->Mean[i], Proto->Variance.Elliptical[i]);
1346  if (DistributionOK (UniformBuckets))
1347  continue;
1348  break;
1349  }
1350  // if any dimension failed to match a distribution, discard the proto
1351  if (i < Clusterer->SampleSize) {
1352  FreePrototype(Proto);
1353  Proto = NULL;
1354  }
1355  return (Proto);
1356 } // MakeMixedProto
1357 
1358 
1369 void MakeDimRandom(uinT16 i, PROTOTYPE *Proto, PARAM_DESC *ParamDesc) {
1370  Proto->Distrib[i] = D_random;
1371  Proto->Mean[i] = ParamDesc->MidRange;
1372  Proto->Variance.Elliptical[i] = ParamDesc->HalfRange;
1373 
1374  // subtract out the previous magnitude of this dimension from the total
1375  Proto->TotalMagnitude /= Proto->Magnitude.Elliptical[i];
1376  Proto->Magnitude.Elliptical[i] = 1.0 / ParamDesc->Range;
1377  Proto->TotalMagnitude *= Proto->Magnitude.Elliptical[i];
1378  Proto->LogMagnitude = log ((double) Proto->TotalMagnitude);
1379 
1380  // note that the proto Weight is irrelevant for D_random protos
1381 } // MakeDimRandom
1382 
1383 
1394 void MakeDimUniform(uinT16 i, PROTOTYPE *Proto, STATISTICS *Statistics) {
1395  Proto->Distrib[i] = uniform;
1396  Proto->Mean[i] = Proto->Cluster->Mean[i] +
1397  (Statistics->Min[i] + Statistics->Max[i]) / 2;
1398  Proto->Variance.Elliptical[i] =
1399  (Statistics->Max[i] - Statistics->Min[i]) / 2;
1400  if (Proto->Variance.Elliptical[i] < MINVARIANCE)
1401  Proto->Variance.Elliptical[i] = MINVARIANCE;
1402 
1403  // subtract out the previous magnitude of this dimension from the total
1404  Proto->TotalMagnitude /= Proto->Magnitude.Elliptical[i];
1405  Proto->Magnitude.Elliptical[i] =
1406  1.0 / (2.0 * Proto->Variance.Elliptical[i]);
1407  Proto->TotalMagnitude *= Proto->Magnitude.Elliptical[i];
1408  Proto->LogMagnitude = log ((double) Proto->TotalMagnitude);
1409 
1410  // note that the proto Weight is irrelevant for uniform protos
1411 } // MakeDimUniform
1412 
1413 
1430 STATISTICS *
1431 ComputeStatistics (inT16 N, PARAM_DESC ParamDesc[], CLUSTER * Cluster) {
1432  STATISTICS *Statistics;
1433  int i, j;
1434  FLOAT32 *CoVariance;
1435  FLOAT32 *Distance;
1436  LIST SearchState;
1437  SAMPLE *Sample;
1438  uinT32 SampleCountAdjustedForBias;
1439 
1440  // allocate memory to hold the statistics results
1441  Statistics = (STATISTICS *) Emalloc (sizeof (STATISTICS));
1442  Statistics->CoVariance = (FLOAT32 *) Emalloc (N * N * sizeof (FLOAT32));
1443  Statistics->Min = (FLOAT32 *) Emalloc (N * sizeof (FLOAT32));
1444  Statistics->Max = (FLOAT32 *) Emalloc (N * sizeof (FLOAT32));
1445 
1446  // allocate temporary memory to hold the sample to mean distances
1447  Distance = (FLOAT32 *) Emalloc (N * sizeof (FLOAT32));
1448 
1449  // initialize the statistics
1450  Statistics->AvgVariance = 1.0;
1451  CoVariance = Statistics->CoVariance;
1452  for (i = 0; i < N; i++) {
1453  Statistics->Min[i] = 0.0;
1454  Statistics->Max[i] = 0.0;
1455  for (j = 0; j < N; j++, CoVariance++)
1456  *CoVariance = 0;
1457  }
1458  // find each sample in the cluster and merge it into the statistics
1459  InitSampleSearch(SearchState, Cluster);
1460  while ((Sample = NextSample (&SearchState)) != NULL) {
1461  for (i = 0; i < N; i++) {
1462  Distance[i] = Sample->Mean[i] - Cluster->Mean[i];
1463  if (ParamDesc[i].Circular) {
1464  if (Distance[i] > ParamDesc[i].HalfRange)
1465  Distance[i] -= ParamDesc[i].Range;
1466  if (Distance[i] < -ParamDesc[i].HalfRange)
1467  Distance[i] += ParamDesc[i].Range;
1468  }
1469  if (Distance[i] < Statistics->Min[i])
1470  Statistics->Min[i] = Distance[i];
1471  if (Distance[i] > Statistics->Max[i])
1472  Statistics->Max[i] = Distance[i];
1473  }
1474  CoVariance = Statistics->CoVariance;
1475  for (i = 0; i < N; i++)
1476  for (j = 0; j < N; j++, CoVariance++)
1477  *CoVariance += Distance[i] * Distance[j];
1478  }
1479  // normalize the variances by the total number of samples
1480  // use SampleCount-1 instead of SampleCount to get an unbiased estimate
1481  // also compute the geometic mean of the diagonal variances
1482  // ensure that clusters with only 1 sample are handled correctly
1483  if (Cluster->SampleCount > 1)
1484  SampleCountAdjustedForBias = Cluster->SampleCount - 1;
1485  else
1486  SampleCountAdjustedForBias = 1;
1487  CoVariance = Statistics->CoVariance;
1488  for (i = 0; i < N; i++)
1489  for (j = 0; j < N; j++, CoVariance++) {
1490  *CoVariance /= SampleCountAdjustedForBias;
1491  if (j == i) {
1492  if (*CoVariance < MINVARIANCE)
1493  *CoVariance = MINVARIANCE;
1494  Statistics->AvgVariance *= *CoVariance;
1495  }
1496  }
1497  Statistics->AvgVariance = (float)pow((double)Statistics->AvgVariance,
1498  1.0 / N);
1499 
1500  // release temporary memory and return
1501  memfree(Distance);
1502  return (Statistics);
1503 } // ComputeStatistics
1504 
1505 
1520  CLUSTER *Cluster,
1521  STATISTICS *Statistics) {
1522  PROTOTYPE *Proto;
1523 
1524  Proto = NewSimpleProto (N, Cluster);
1525 
1526  Proto->Variance.Spherical = Statistics->AvgVariance;
1527  if (Proto->Variance.Spherical < MINVARIANCE)
1528  Proto->Variance.Spherical = MINVARIANCE;
1529 
1530  Proto->Magnitude.Spherical =
1531  1.0 / sqrt ((double) (2.0 * PI * Proto->Variance.Spherical));
1532  Proto->TotalMagnitude = (float)pow((double)Proto->Magnitude.Spherical,
1533  (double) N);
1534  Proto->Weight.Spherical = 1.0 / Proto->Variance.Spherical;
1535  Proto->LogMagnitude = log ((double) Proto->TotalMagnitude);
1536 
1537  return (Proto);
1538 } // NewSphericalProto
1539 
1540 
1554  CLUSTER *Cluster,
1555  STATISTICS *Statistics) {
1556  PROTOTYPE *Proto;
1557  FLOAT32 *CoVariance;
1558  int i;
1559 
1560  Proto = NewSimpleProto (N, Cluster);
1561  Proto->Variance.Elliptical = (FLOAT32 *) Emalloc (N * sizeof (FLOAT32));
1562  Proto->Magnitude.Elliptical = (FLOAT32 *) Emalloc (N * sizeof (FLOAT32));
1563  Proto->Weight.Elliptical = (FLOAT32 *) Emalloc (N * sizeof (FLOAT32));
1564 
1565  CoVariance = Statistics->CoVariance;
1566  Proto->TotalMagnitude = 1.0;
1567  for (i = 0; i < N; i++, CoVariance += N + 1) {
1568  Proto->Variance.Elliptical[i] = *CoVariance;
1569  if (Proto->Variance.Elliptical[i] < MINVARIANCE)
1570  Proto->Variance.Elliptical[i] = MINVARIANCE;
1571 
1572  Proto->Magnitude.Elliptical[i] =
1573  1.0 / sqrt ((double) (2.0 * PI * Proto->Variance.Elliptical[i]));
1574  Proto->Weight.Elliptical[i] = 1.0 / Proto->Variance.Elliptical[i];
1575  Proto->TotalMagnitude *= Proto->Magnitude.Elliptical[i];
1576  }
1577  Proto->LogMagnitude = log ((double) Proto->TotalMagnitude);
1578  Proto->Style = elliptical;
1579  return (Proto);
1580 } // NewEllipticalProto
1581 
1582 
1598 PROTOTYPE *NewMixedProto(inT16 N, CLUSTER *Cluster, STATISTICS *Statistics) {
1599  PROTOTYPE *Proto;
1600  int i;
1601 
1602  Proto = NewEllipticalProto (N, Cluster, Statistics);
1603  Proto->Distrib = (DISTRIBUTION *) Emalloc (N * sizeof (DISTRIBUTION));
1604 
1605  for (i = 0; i < N; i++) {
1606  Proto->Distrib[i] = normal;
1607  }
1608  Proto->Style = mixed;
1609  return (Proto);
1610 } // NewMixedProto
1611 
1612 
1624  PROTOTYPE *Proto;
1625  int i;
1626 
1627  Proto = (PROTOTYPE *) Emalloc (sizeof (PROTOTYPE));
1628  Proto->Mean = (FLOAT32 *) Emalloc (N * sizeof (FLOAT32));
1629 
1630  for (i = 0; i < N; i++)
1631  Proto->Mean[i] = Cluster->Mean[i];
1632  Proto->Distrib = NULL;
1633 
1634  Proto->Significant = TRUE;
1635  Proto->Merged = FALSE;
1636  Proto->Style = spherical;
1637  Proto->NumSamples = Cluster->SampleCount;
1638  Proto->Cluster = Cluster;
1639  Proto->Cluster->Prototype = TRUE;
1640  return (Proto);
1641 } // NewSimpleProto
1642 
1643 
1664 BOOL8
1666 inT16 N, FLOAT32 * CoVariance, FLOAT32 Independence) {
1667  int i, j;
1668  FLOAT32 *VARii; // points to ith on-diagonal element
1669  FLOAT32 *VARjj; // points to jth on-diagonal element
1670  FLOAT32 CorrelationCoeff;
1671 
1672  VARii = CoVariance;
1673  for (i = 0; i < N; i++, VARii += N + 1) {
1674  if (ParamDesc[i].NonEssential)
1675  continue;
1676 
1677  VARjj = VARii + N + 1;
1678  CoVariance = VARii + 1;
1679  for (j = i + 1; j < N; j++, CoVariance++, VARjj += N + 1) {
1680  if (ParamDesc[j].NonEssential)
1681  continue;
1682 
1683  if ((*VARii == 0.0) || (*VARjj == 0.0))
1684  CorrelationCoeff = 0.0;
1685  else
1686  CorrelationCoeff =
1687  sqrt (sqrt (*CoVariance * *CoVariance / (*VARii * *VARjj)));
1688  if (CorrelationCoeff > Independence)
1689  return (FALSE);
1690  }
1691  }
1692  return (TRUE);
1693 } // Independent
1694 
1695 
1714  DISTRIBUTION Distribution,
1715  uinT32 SampleCount,
1716  FLOAT64 Confidence) {
1717  // Get an old bucket structure with the same number of buckets.
1718  uinT16 NumberOfBuckets = OptimumNumberOfBuckets(SampleCount);
1719  BUCKETS *Buckets =
1720  clusterer->bucket_cache[Distribution][NumberOfBuckets - MINBUCKETS];
1721 
1722  // If a matching bucket structure is not found, make one and save it.
1723  if (Buckets == NULL) {
1724  Buckets = MakeBuckets(Distribution, SampleCount, Confidence);
1725  clusterer->bucket_cache[Distribution][NumberOfBuckets - MINBUCKETS] =
1726  Buckets;
1727  } else {
1728  // Just adjust the existing buckets.
1729  if (SampleCount != Buckets->SampleCount)
1730  AdjustBuckets(Buckets, SampleCount);
1731  if (Confidence != Buckets->Confidence) {
1732  Buckets->Confidence = Confidence;
1733  Buckets->ChiSquared = ComputeChiSquared(
1734  DegreesOfFreedom(Distribution, Buckets->NumberOfBuckets),
1735  Confidence);
1736  }
1737  InitBuckets(Buckets);
1738  }
1739  return Buckets;
1740 } // GetBuckets
1741 
1742 
1762  uinT32 SampleCount,
1763  FLOAT64 Confidence) {
1764  const DENSITYFUNC DensityFunction[] =
1765  { NormalDensity, UniformDensity, UniformDensity };
1766  int i, j;
1767  BUCKETS *Buckets;
1768  FLOAT64 BucketProbability;
1769  FLOAT64 NextBucketBoundary;
1770  FLOAT64 Probability;
1771  FLOAT64 ProbabilityDelta;
1772  FLOAT64 LastProbDensity;
1773  FLOAT64 ProbDensity;
1774  uinT16 CurrentBucket;
1775  BOOL8 Symmetrical;
1776 
1777  // allocate memory needed for data structure
1778  Buckets = reinterpret_cast<BUCKETS*>(Emalloc(sizeof(BUCKETS)));
1779  Buckets->NumberOfBuckets = OptimumNumberOfBuckets(SampleCount);
1780  Buckets->SampleCount = SampleCount;
1781  Buckets->Confidence = Confidence;
1782  Buckets->Count = reinterpret_cast<uinT32*>(
1783  Emalloc(Buckets->NumberOfBuckets * sizeof(uinT32)));
1784  Buckets->ExpectedCount = reinterpret_cast<FLOAT32*>(
1785  Emalloc(Buckets->NumberOfBuckets * sizeof(FLOAT32)));
1786 
1787  // initialize simple fields
1788  Buckets->Distribution = Distribution;
1789  for (i = 0; i < Buckets->NumberOfBuckets; i++) {
1790  Buckets->Count[i] = 0;
1791  Buckets->ExpectedCount[i] = 0.0;
1792  }
1793 
1794  // all currently defined distributions are symmetrical
1795  Symmetrical = TRUE;
1796  Buckets->ChiSquared = ComputeChiSquared(
1797  DegreesOfFreedom(Distribution, Buckets->NumberOfBuckets), Confidence);
1798 
1799  if (Symmetrical) {
1800  // allocate buckets so that all have approx. equal probability
1801  BucketProbability = 1.0 / (FLOAT64) (Buckets->NumberOfBuckets);
1802 
1803  // distribution is symmetric so fill in upper half then copy
1804  CurrentBucket = Buckets->NumberOfBuckets / 2;
1805  if (Odd (Buckets->NumberOfBuckets))
1806  NextBucketBoundary = BucketProbability / 2;
1807  else
1808  NextBucketBoundary = BucketProbability;
1809 
1810  Probability = 0.0;
1811  LastProbDensity =
1812  (*DensityFunction[(int) Distribution]) (BUCKETTABLESIZE / 2);
1813  for (i = BUCKETTABLESIZE / 2; i < BUCKETTABLESIZE; i++) {
1814  ProbDensity = (*DensityFunction[(int) Distribution]) (i + 1);
1815  ProbabilityDelta = Integral (LastProbDensity, ProbDensity, 1.0);
1816  Probability += ProbabilityDelta;
1817  if (Probability > NextBucketBoundary) {
1818  if (CurrentBucket < Buckets->NumberOfBuckets - 1)
1819  CurrentBucket++;
1820  NextBucketBoundary += BucketProbability;
1821  }
1822  Buckets->Bucket[i] = CurrentBucket;
1823  Buckets->ExpectedCount[CurrentBucket] +=
1824  (FLOAT32) (ProbabilityDelta * SampleCount);
1825  LastProbDensity = ProbDensity;
1826  }
1827  // place any leftover probability into the last bucket
1828  Buckets->ExpectedCount[CurrentBucket] +=
1829  (FLOAT32) ((0.5 - Probability) * SampleCount);
1830 
1831  // copy upper half of distribution to lower half
1832  for (i = 0, j = BUCKETTABLESIZE - 1; i < j; i++, j--)
1833  Buckets->Bucket[i] =
1834  Mirror(Buckets->Bucket[j], Buckets->NumberOfBuckets);
1835 
1836  // copy upper half of expected counts to lower half
1837  for (i = 0, j = Buckets->NumberOfBuckets - 1; i <= j; i++, j--)
1838  Buckets->ExpectedCount[i] += Buckets->ExpectedCount[j];
1839  }
1840  return Buckets;
1841 } // MakeBuckets
1842 
1843 
1860  uinT8 Last, Next;
1861  FLOAT32 Slope;
1862 
1863  if (SampleCount < kCountTable[0])
1864  return kBucketsTable[0];
1865 
1866  for (Last = 0, Next = 1; Next < LOOKUPTABLESIZE; Last++, Next++) {
1867  if (SampleCount <= kCountTable[Next]) {
1868  Slope = (FLOAT32) (kBucketsTable[Next] - kBucketsTable[Last]) /
1869  (FLOAT32) (kCountTable[Next] - kCountTable[Last]);
1870  return ((uinT16) (kBucketsTable[Last] +
1871  Slope * (SampleCount - kCountTable[Last])));
1872  }
1873  }
1874  return kBucketsTable[Last];
1875 } // OptimumNumberOfBuckets
1876 
1877 
1896 FLOAT64
1898 #define CHIACCURACY 0.01
1899 #define MINALPHA (1e-200)
1900 {
1901  static LIST ChiWith[MAXDEGREESOFFREEDOM + 1];
1902 
1903  CHISTRUCT *OldChiSquared;
1904  CHISTRUCT SearchKey;
1905 
1906  // limit the minimum alpha that can be used - if alpha is too small
1907  // it may not be possible to compute chi-squared.
1908  Alpha = ClipToRange(Alpha, MINALPHA, 1.0);
1909  if (Odd (DegreesOfFreedom))
1910  DegreesOfFreedom++;
1911 
1912  /* find the list of chi-squared values which have already been computed
1913  for the specified number of degrees of freedom. Search the list for
1914  the desired chi-squared. */
1915  SearchKey.Alpha = Alpha;
1916  OldChiSquared = (CHISTRUCT *) first_node (search (ChiWith[DegreesOfFreedom],
1917  &SearchKey, AlphaMatch));
1918 
1919  if (OldChiSquared == NULL) {
1920  OldChiSquared = NewChiStruct (DegreesOfFreedom, Alpha);
1921  OldChiSquared->ChiSquared = Solve (ChiArea, OldChiSquared,
1922  (FLOAT64) DegreesOfFreedom,
1923  (FLOAT64) CHIACCURACY);
1924  ChiWith[DegreesOfFreedom] = push (ChiWith[DegreesOfFreedom],
1925  OldChiSquared);
1926  }
1927  else {
1928  // further optimization might move OldChiSquared to front of list
1929  }
1930 
1931  return (OldChiSquared->ChiSquared);
1932 
1933 } // ComputeChiSquared
1934 
1935 
1952  FLOAT64 Distance;
1953 
1954  Distance = x - kNormalMean;
1955  return kNormalMagnitude * exp(-0.5 * Distance * Distance / kNormalVariance);
1956 } // NormalDensity
1957 
1958 
1969  static FLOAT64 UniformDistributionDensity = (FLOAT64) 1.0 / BUCKETTABLESIZE;
1970 
1971  if ((x >= 0.0) && (x <= BUCKETTABLESIZE))
1972  return UniformDistributionDensity;
1973  else
1974  return (FLOAT64) 0.0;
1975 } // UniformDensity
1976 
1977 
1989  return (f1 + f2) * Dx / 2.0;
1990 } // Integral
1991 
1992 
2015 void FillBuckets(BUCKETS *Buckets,
2016  CLUSTER *Cluster,
2017  uinT16 Dim,
2018  PARAM_DESC *ParamDesc,
2019  FLOAT32 Mean,
2020  FLOAT32 StdDev) {
2021  uinT16 BucketID;
2022  int i;
2023  LIST SearchState;
2024  SAMPLE *Sample;
2025 
2026  // initialize the histogram bucket counts to 0
2027  for (i = 0; i < Buckets->NumberOfBuckets; i++)
2028  Buckets->Count[i] = 0;
2029 
2030  if (StdDev == 0.0) {
2031  /* if the standard deviation is zero, then we can't statistically
2032  analyze the cluster. Use a pseudo-analysis: samples exactly on
2033  the mean are distributed evenly across all buckets. Samples greater
2034  than the mean are placed in the last bucket; samples less than the
2035  mean are placed in the first bucket. */
2036 
2037  InitSampleSearch(SearchState, Cluster);
2038  i = 0;
2039  while ((Sample = NextSample (&SearchState)) != NULL) {
2040  if (Sample->Mean[Dim] > Mean)
2041  BucketID = Buckets->NumberOfBuckets - 1;
2042  else if (Sample->Mean[Dim] < Mean)
2043  BucketID = 0;
2044  else
2045  BucketID = i;
2046  Buckets->Count[BucketID] += 1;
2047  i++;
2048  if (i >= Buckets->NumberOfBuckets)
2049  i = 0;
2050  }
2051  }
2052  else {
2053  // search for all samples in the cluster and add to histogram buckets
2054  InitSampleSearch(SearchState, Cluster);
2055  while ((Sample = NextSample (&SearchState)) != NULL) {
2056  switch (Buckets->Distribution) {
2057  case normal:
2058  BucketID = NormalBucket (ParamDesc, Sample->Mean[Dim],
2059  Mean, StdDev);
2060  break;
2061  case D_random:
2062  case uniform:
2063  BucketID = UniformBucket (ParamDesc, Sample->Mean[Dim],
2064  Mean, StdDev);
2065  break;
2066  default:
2067  BucketID = 0;
2068  }
2069  Buckets->Count[Buckets->Bucket[BucketID]] += 1;
2070  }
2071  }
2072 } // FillBuckets
2073 
2074 
2089  FLOAT32 x,
2090  FLOAT32 Mean,
2091  FLOAT32 StdDev) {
2092  FLOAT32 X;
2093 
2094  // wraparound circular parameters if necessary
2095  if (ParamDesc->Circular) {
2096  if (x - Mean > ParamDesc->HalfRange)
2097  x -= ParamDesc->Range;
2098  else if (x - Mean < -ParamDesc->HalfRange)
2099  x += ParamDesc->Range;
2100  }
2101 
2102  X = ((x - Mean) / StdDev) * kNormalStdDev + kNormalMean;
2103  if (X < 0)
2104  return 0;
2105  if (X > BUCKETTABLESIZE - 1)
2106  return ((uinT16) (BUCKETTABLESIZE - 1));
2107  return (uinT16) floor((FLOAT64) X);
2108 } // NormalBucket
2109 
2110 
2125  FLOAT32 x,
2126  FLOAT32 Mean,
2127  FLOAT32 StdDev) {
2128  FLOAT32 X;
2129 
2130  // wraparound circular parameters if necessary
2131  if (ParamDesc->Circular) {
2132  if (x - Mean > ParamDesc->HalfRange)
2133  x -= ParamDesc->Range;
2134  else if (x - Mean < -ParamDesc->HalfRange)
2135  x += ParamDesc->Range;
2136  }
2137 
2138  X = ((x - Mean) / (2 * StdDev) * BUCKETTABLESIZE + BUCKETTABLESIZE / 2.0);
2139  if (X < 0)
2140  return 0;
2141  if (X > BUCKETTABLESIZE - 1)
2142  return (uinT16) (BUCKETTABLESIZE - 1);
2143  return (uinT16) floor((FLOAT64) X);
2144 } // UniformBucket
2145 
2146 
2160  FLOAT32 FrequencyDifference;
2161  FLOAT32 TotalDifference;
2162  int i;
2163 
2164  // compute how well the histogram matches the expected histogram
2165  TotalDifference = 0.0;
2166  for (i = 0; i < Buckets->NumberOfBuckets; i++) {
2167  FrequencyDifference = Buckets->Count[i] - Buckets->ExpectedCount[i];
2168  TotalDifference += (FrequencyDifference * FrequencyDifference) /
2169  Buckets->ExpectedCount[i];
2170  }
2171 
2172  // test to see if the difference is more than expected
2173  if (TotalDifference > Buckets->ChiSquared)
2174  return FALSE;
2175  else
2176  return TRUE;
2177 } // DistributionOK
2178 
2179 
2188 void FreeStatistics(STATISTICS *Statistics) {
2189  memfree (Statistics->CoVariance);
2190  memfree (Statistics->Min);
2191  memfree (Statistics->Max);
2192  memfree(Statistics);
2193 } // FreeStatistics
2194 
2195 
2201 void FreeBuckets(BUCKETS *buckets) {
2202  Efree(buckets->Count);
2203  Efree(buckets->ExpectedCount);
2204  Efree(buckets);
2205 } // FreeBuckets
2206 
2207 
2220 void FreeCluster(CLUSTER *Cluster) {
2221  if (Cluster != NULL) {
2222  FreeCluster (Cluster->Left);
2223  FreeCluster (Cluster->Right);
2224  memfree(Cluster);
2225  }
2226 } // FreeCluster
2227 
2228 
2243 uinT16 DegreesOfFreedom(DISTRIBUTION Distribution, uinT16 HistogramBuckets) {
2244  static uinT8 DegreeOffsets[] = { 3, 3, 1 };
2245 
2246  uinT16 AdjustedNumBuckets;
2247 
2248  AdjustedNumBuckets = HistogramBuckets - DegreeOffsets[(int) Distribution];
2249  if (Odd (AdjustedNumBuckets))
2250  AdjustedNumBuckets++;
2251  return (AdjustedNumBuckets);
2252 
2253 } // DegreesOfFreedom
2254 
2255 
2266 int NumBucketsMatch(void *arg1, // BUCKETS *Histogram,
2267  void *arg2) { // uinT16 *DesiredNumberOfBuckets)
2268  BUCKETS *Histogram = (BUCKETS *) arg1;
2269  uinT16 *DesiredNumberOfBuckets = (uinT16 *) arg2;
2270 
2271  return (*DesiredNumberOfBuckets == Histogram->NumberOfBuckets);
2272 
2273 } // NumBucketsMatch
2274 
2275 
2284 int ListEntryMatch(void *arg1, //ListNode
2285  void *arg2) { //Key
2286  return (arg1 == arg2);
2287 
2288 } // ListEntryMatch
2289 
2290 
2301 void AdjustBuckets(BUCKETS *Buckets, uinT32 NewSampleCount) {
2302  int i;
2303  FLOAT64 AdjustFactor;
2304 
2305  AdjustFactor = (((FLOAT64) NewSampleCount) /
2306  ((FLOAT64) Buckets->SampleCount));
2307 
2308  for (i = 0; i < Buckets->NumberOfBuckets; i++) {
2309  Buckets->ExpectedCount[i] *= AdjustFactor;
2310  }
2311 
2312  Buckets->SampleCount = NewSampleCount;
2313 
2314 } // AdjustBuckets
2315 
2316 
2325 void InitBuckets(BUCKETS *Buckets) {
2326  int i;
2327 
2328  for (i = 0; i < Buckets->NumberOfBuckets; i++) {
2329  Buckets->Count[i] = 0;
2330  }
2331 
2332 } // InitBuckets
2333 
2334 
2349 int AlphaMatch(void *arg1, //CHISTRUCT *ChiStruct,
2350  void *arg2) { //CHISTRUCT *SearchKey)
2351  CHISTRUCT *ChiStruct = (CHISTRUCT *) arg1;
2352  CHISTRUCT *SearchKey = (CHISTRUCT *) arg2;
2353 
2354  return (ChiStruct->Alpha == SearchKey->Alpha);
2355 
2356 } // AlphaMatch
2357 
2358 
2370 CHISTRUCT *NewChiStruct(uinT16 DegreesOfFreedom, FLOAT64 Alpha) {
2372 
2373  NewChiStruct = (CHISTRUCT *) Emalloc (sizeof (CHISTRUCT));
2374  NewChiStruct->DegreesOfFreedom = DegreesOfFreedom;
2375  NewChiStruct->Alpha = Alpha;
2376  return (NewChiStruct);
2377 
2378 } // NewChiStruct
2379 
2380 
2396 FLOAT64
2397 Solve (SOLVEFUNC Function,
2398 void *FunctionParams, FLOAT64 InitialGuess, FLOAT64 Accuracy)
2399 #define INITIALDELTA 0.1
2400 #define DELTARATIO 0.1
2401 {
2402  FLOAT64 x;
2403  FLOAT64 f;
2404  FLOAT64 Slope;
2405  FLOAT64 Delta;
2406  FLOAT64 NewDelta;
2407  FLOAT64 xDelta;
2408  FLOAT64 LastPosX, LastNegX;
2409 
2410  x = InitialGuess;
2411  Delta = INITIALDELTA;
2412  LastPosX = MAX_FLOAT32;
2413  LastNegX = -MAX_FLOAT32;
2414  f = (*Function) ((CHISTRUCT *) FunctionParams, x);
2415  while (Abs (LastPosX - LastNegX) > Accuracy) {
2416  // keep track of outer bounds of current estimate
2417  if (f < 0)
2418  LastNegX = x;
2419  else
2420  LastPosX = x;
2421 
2422  // compute the approx. slope of f(x) at the current point
2423  Slope =
2424  ((*Function) ((CHISTRUCT *) FunctionParams, x + Delta) - f) / Delta;
2425 
2426  // compute the next solution guess */
2427  xDelta = f / Slope;
2428  x -= xDelta;
2429 
2430  // reduce the delta used for computing slope to be a fraction of
2431  //the amount moved to get to the new guess
2432  NewDelta = Abs (xDelta) * DELTARATIO;
2433  if (NewDelta < Delta)
2434  Delta = NewDelta;
2435 
2436  // compute the value of the function at the new guess
2437  f = (*Function) ((CHISTRUCT *) FunctionParams, x);
2438  }
2439  return (x);
2440 
2441 } // Solve
2442 
2443 
2465  int i, N;
2466  FLOAT64 SeriesTotal;
2467  FLOAT64 Denominator;
2468  FLOAT64 PowerOfx;
2469 
2470  N = ChiParams->DegreesOfFreedom / 2 - 1;
2471  SeriesTotal = 1;
2472  Denominator = 1;
2473  PowerOfx = 1;
2474  for (i = 1; i <= N; i++) {
2475  Denominator *= 2 * i;
2476  PowerOfx *= x;
2477  SeriesTotal += PowerOfx / Denominator;
2478  }
2479  return ((SeriesTotal * exp (-0.5 * x)) - ChiParams->Alpha);
2480 
2481 } // ChiArea
2482 
2483 
2511 BOOL8
2513 CLUSTER * Cluster, FLOAT32 MaxIllegal)
2514 #define ILLEGAL_CHAR 2
2515 {
2516  static BOOL8 *CharFlags = NULL;
2517  static inT32 NumFlags = 0;
2518  int i;
2519  LIST SearchState;
2520  SAMPLE *Sample;
2521  inT32 CharID;
2522  inT32 NumCharInCluster;
2523  inT32 NumIllegalInCluster;
2524  FLOAT32 PercentIllegal;
2525 
2526  // initial estimate assumes that no illegal chars exist in the cluster
2527  NumCharInCluster = Cluster->SampleCount;
2528  NumIllegalInCluster = 0;
2529 
2530  if (Clusterer->NumChar > NumFlags) {
2531  if (CharFlags != NULL)
2532  memfree(CharFlags);
2533  NumFlags = Clusterer->NumChar;
2534  CharFlags = (BOOL8 *) Emalloc (NumFlags * sizeof (BOOL8));
2535  }
2536 
2537  for (i = 0; i < NumFlags; i++)
2538  CharFlags[i] = FALSE;
2539 
2540  // find each sample in the cluster and check if we have seen it before
2541  InitSampleSearch(SearchState, Cluster);
2542  while ((Sample = NextSample (&SearchState)) != NULL) {
2543  CharID = Sample->CharID;
2544  if (CharFlags[CharID] == FALSE) {
2545  CharFlags[CharID] = TRUE;
2546  }
2547  else {
2548  if (CharFlags[CharID] == TRUE) {
2549  NumIllegalInCluster++;
2550  CharFlags[CharID] = ILLEGAL_CHAR;
2551  }
2552  NumCharInCluster--;
2553  PercentIllegal = (FLOAT32) NumIllegalInCluster / NumCharInCluster;
2554  if (PercentIllegal > MaxIllegal) {
2555  destroy(SearchState);
2556  return (TRUE);
2557  }
2558  }
2559  }
2560  return (FALSE);
2561 
2562 } // MultipleCharSamples
2563 
2569 double InvertMatrix(const float* input, int size, float* inv) {
2570  // Allocate memory for the 2D arrays.
2571  GENERIC_2D_ARRAY<double> U(size, size, 0.0);
2572  GENERIC_2D_ARRAY<double> U_inv(size, size, 0.0);
2573  GENERIC_2D_ARRAY<double> L(size, size, 0.0);
2574 
2575  // Initialize the working matrices. U starts as input, L as I and U_inv as O.
2576  int row;
2577  int col;
2578  for (row = 0; row < size; row++) {
2579  for (col = 0; col < size; col++) {
2580  U[row][col] = input[row*size + col];
2581  L[row][col] = row == col ? 1.0 : 0.0;
2582  U_inv[row][col] = 0.0;
2583  }
2584  }
2585 
2586  // Compute forward matrix by inversion by LU decomposition of input.
2587  for (col = 0; col < size; ++col) {
2588  // Find best pivot
2589  int best_row = 0;
2590  double best_pivot = -1.0;
2591  for (row = col; row < size; ++row) {
2592  if (Abs(U[row][col]) > best_pivot) {
2593  best_pivot = Abs(U[row][col]);
2594  best_row = row;
2595  }
2596  }
2597  // Exchange pivot rows.
2598  if (best_row != col) {
2599  for (int k = 0; k < size; ++k) {
2600  double tmp = U[best_row][k];
2601  U[best_row][k] = U[col][k];
2602  U[col][k] = tmp;
2603  tmp = L[best_row][k];
2604  L[best_row][k] = L[col][k];
2605  L[col][k] = tmp;
2606  }
2607  }
2608  // Now do the pivot itself.
2609  for (row = col + 1; row < size; ++row) {
2610  double ratio = -U[row][col] / U[col][col];
2611  for (int j = col; j < size; ++j) {
2612  U[row][j] += U[col][j] * ratio;
2613  }
2614  for (int k = 0; k < size; ++k) {
2615  L[row][k] += L[col][k] * ratio;
2616  }
2617  }
2618  }
2619  // Next invert U.
2620  for (col = 0; col < size; ++col) {
2621  U_inv[col][col] = 1.0 / U[col][col];
2622  for (row = col - 1; row >= 0; --row) {
2623  double total = 0.0;
2624  for (int k = col; k > row; --k) {
2625  total += U[row][k] * U_inv[k][col];
2626  }
2627  U_inv[row][col] = -total / U[row][row];
2628  }
2629  }
2630  // Now the answer is U_inv.L.
2631  for (row = 0; row < size; row++) {
2632  for (col = 0; col < size; col++) {
2633  double sum = 0.0;
2634  for (int k = row; k < size; ++k) {
2635  sum += U_inv[row][k] * L[k][col];
2636  }
2637  inv[row*size + col] = sum;
2638  }
2639  }
2640  // Check matrix product.
2641  double error_sum = 0.0;
2642  for (row = 0; row < size; row++) {
2643  for (col = 0; col < size; col++) {
2644  double sum = 0.0;
2645  for (int k = 0; k < size; ++k) {
2646  sum += input[row*size + k] * inv[k *size + col];
2647  }
2648  if (row != col) {
2649  error_sum += Abs(sum);
2650  }
2651  }
2652  }
2653  return error_sum;
2654 }
#define MINVARIANCE
Definition: cluster.cpp:142
#define Odd(N)
Definition: cluster.cpp:206
FLOAT32 * Max
Definition: cluster.cpp:175
bool Pop(Pair *entry)
Definition: genericheap.h:116
void memfree(void *element)
Definition: freelist.cpp:30
FLOAT32 * Min
Definition: cluster.cpp:174
FLOAT32 Min
Definition: ocrfeatures.h:49
uinT16 UniformBucket(PARAM_DESC *ParamDesc, FLOAT32 x, FLOAT32 Mean, FLOAT32 StdDev)
Definition: cluster.cpp:2124
PROTOTYPE * MakeEllipticalProto(CLUSTERER *Clusterer, CLUSTER *Cluster, STATISTICS *Statistics, BUCKETS *Buckets)
Definition: cluster.cpp:1264
#define INITIALDELTA
#define FTABLE_Y
Definition: cluster.cpp:32
struct sample * Left
Definition: cluster.h:36
FLOAT32 AvgVariance
Definition: cluster.cpp:172
CHISTRUCT * NewChiStruct(uinT16 DegreesOfFreedom, FLOAT64 Alpha)
Definition: cluster.cpp:2370
float FLOAT32
Definition: host.h:111
#define ILLEGAL_CHAR
Definition: kdtree.h:49
PROTOTYPE * NewMixedProto(inT16 N, CLUSTER *Cluster, STATISTICS *Statistics)
Definition: cluster.cpp:1598
void FreeBuckets(BUCKETS *Buckets)
Definition: cluster.cpp:2201
BOOL8 Independent(PARAM_DESC ParamDesc[], inT16 N, FLOAT32 *CoVariance, FLOAT32 Independence)
Definition: cluster.cpp:1665
unsigned Clustered
Definition: cluster.h:33
#define MINBUCKETS
Definition: cluster.h:26
#define NIL_LIST
Definition: oldlist.h:126
void FreeCluster(CLUSTER *Cluster)
Definition: cluster.cpp:2220
void MakeDimRandom(uinT16 i, PROTOTYPE *Proto, PARAM_DESC *ParamDesc)
Definition: cluster.cpp:1369
DISTRIBUTION * Distrib
Definition: cluster.h:77
PROTOTYPE * TestEllipticalProto(CLUSTERER *Clusterer, CLUSTERCONFIG *Config, CLUSTER *Cluster, STATISTICS *Statistics)
Definition: cluster.cpp:1119
BOOL8 MultipleCharSamples(CLUSTERER *Clusterer, CLUSTER *Cluster, FLOAT32 MaxIllegal)
Definition: cluster.cpp:2512
#define FTABLE_X
Definition: cluster.cpp:31
void AdjustBuckets(BUCKETS *Buckets, uinT32 NewSampleCount)
Definition: cluster.cpp:2301
unsigned SampleCount
Definition: cluster.h:35
#define tprintf(...)
Definition: tprintf.h:31
#define BUCKETTABLESIZE
Definition: cluster.cpp:160
#define LOOKUPTABLESIZE
Definition: cluster.cpp:228
uinT16 Bucket[BUCKETTABLESIZE]
Definition: cluster.cpp:184
BUCKETS * bucket_cache[DISTRIBUTION_COUNT][MAXBUCKETS+1-MINBUCKETS]
Definition: cluster.h:95
double InvertMatrix(const float *input, int size, float *inv)
Definition: cluster.cpp:2569
struct sample * Right
Definition: cluster.h:37
void KDNearestNeighborSearch(KDTREE *Tree, FLOAT32 Query[], int QuerySize, FLOAT32 MaxDistance, int *NumberOfResults, void **NBuffer, FLOAT32 DBuffer[])
Definition: kdtree.cpp:322
CLUSTERER * MakeClusterer(inT16 SampleSize, const PARAM_DESC ParamDesc[])
Definition: cluster.cpp:400
unsigned char BOOL8
Definition: host.h:113
FLOAT64 ComputeChiSquared(uinT16 DegreesOfFreedom, FLOAT64 Alpha)
Definition: cluster.cpp:1897
#define SqrtOf2Pi
Definition: cluster.cpp:218
SAMPLE * MakeSample(CLUSTERER *Clusterer, const FLOAT32 *Feature, inT32 CharID)
Definition: cluster.cpp:457
#define Abs(N)
Definition: cluster.cpp:208
FLOAT32 Spherical
Definition: cluster.h:63
PROTOTYPE * MakeDegenerateProto(uinT16 N, CLUSTER *Cluster, STATISTICS *Statistics, PROTOSTYLE Style, inT32 MinSamples)
Definition: cluster.cpp:1077
uinT16 OptimumNumberOfBuckets(uinT32 SampleCount)
Definition: cluster.cpp:1859
DISTRIBUTION
Definition: cluster.h:58
FLOAT32 LogMagnitude
Definition: cluster.h:80
FLOATUNION Variance
Definition: cluster.h:81
FLOAT32 * Mean
Definition: cluster.h:78
#define MAXNEIGHBORS
Definition: cluster.h:59
KDTREE * KDTree
Definition: cluster.h:90
FLOAT32 HalfRange
Definition: ocrfeatures.h:52
#define NORMALEXTENT
Definition: cluster.cpp:161
FLOAT64 Confidence
Definition: cluster.cpp:181
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
Definition: helpers.h:115
#define ASSERT_HOST(x)
Definition: errcode.h:84
void FreeKDTree(KDTREE *Tree)
Definition: kdtree.cpp:351
PROTOTYPE * NewEllipticalProto(inT16 N, CLUSTER *Cluster, STATISTICS *Statistics)
Definition: cluster.cpp:1553
inT32 NumberOfSamples
Definition: cluster.h:89
unsigned Significant
Definition: cluster.h:68
FLOATUNION Weight
Definition: cluster.h:83
#define MINSAMPLESNEEDED
Definition: cluster.cpp:152
FLOAT32 Independence
Definition: cluster.h:53
#define MAXDISTANCE
FLOAT32 MaxIllegal
Definition: cluster.h:51
FLOAT32 TotalMagnitude
Definition: cluster.h:79
int ListEntryMatch(void *arg1, void *arg2)
Definition: cluster.cpp:2284
int MagicSamples
Definition: cluster.h:55
LIST search(LIST list, void *key, int_compare is_equal)
Definition: oldlist.cpp:413
#define MINSAMPLES
Definition: cluster.cpp:151
FLOAT64 ChiSquared
Definition: cluster.cpp:192
FLOAT64 UniformDensity(inT32 x)
Definition: cluster.cpp:1968
unsigned int uinT32
Definition: host.h:103
void MakePotentialClusters(ClusteringContext *context, CLUSTER *Cluster, inT32 Level)
Definition: cluster.cpp:773
BUCKETS * MakeBuckets(DISTRIBUTION Distribution, uinT32 SampleCount, FLOAT64 Confidence)
Definition: cluster.cpp:1761
int AlphaMatch(void *arg1, void *arg2)
Definition: cluster.cpp:2349
CLUSTER * Neighbor
Definition: cluster.cpp:165
#define HOTELLING
Definition: cluster.cpp:30
LIST ClusterSamples(CLUSTERER *Clusterer, CLUSTERCONFIG *Config)
Definition: cluster.cpp:515
FLOAT32 MidRange
Definition: ocrfeatures.h:53
unsigned NumSamples
Definition: cluster.h:75
void FreeProtoList(LIST *ProtoList)
Definition: cluster.cpp:571
FLOAT32 Mean[1]
Definition: cluster.h:39
FLOAT64 ChiArea(CHISTRUCT *ChiParams, FLOAT64 x)
Definition: cluster.cpp:2464
CLUSTER * Root
Definition: cluster.h:91
void(* void_proc)(...)
Definition: cutil.h:66
FLOAT64 Solve(SOLVEFUNC Function, void *FunctionParams, FLOAT64 InitialGuess, FLOAT64 Accuracy)
Definition: cluster.cpp:2397
#define CHIACCURACY
uinT32 SampleCount
Definition: cluster.cpp:180
#define RootOf(T)
Definition: kdtree.h:58
#define Mirror(N, R)
Definition: cluster.cpp:207
uinT16 NumberOfBuckets
Definition: cluster.cpp:183
void InitBuckets(BUCKETS *Buckets)
Definition: cluster.cpp:2325
FLOAT64 Confidence
Definition: cluster.h:54
void * Emalloc(int Size)
Definition: emalloc.cpp:47
void destroy_nodes(LIST list, void_dest destructor)
Definition: oldlist.cpp:204
ClusterHeap * heap
Definition: cluster.cpp:197
void Push(Pair *entry)
Definition: genericheap.h:95
#define ALREADYCLUSTERED
Definition: cluster.h:133
FLOATUNION Magnitude
Definition: cluster.h:82
FLOAT32 * Elliptical
Definition: cluster.h:64
CLUSTER * Cluster
Definition: cluster.h:76
CLUSTER * FindNearestNeighbor(KDTREE *Tree, CLUSTER *Cluster, FLOAT32 *Distance)
Definition: cluster.cpp:807
FLOAT32 MinSamples
Definition: cluster.h:50
const double FTable[FTABLE_Y][FTABLE_X]
Definition: cluster.cpp:35
int NumBucketsMatch(void *arg1, void *arg2)
Definition: cluster.cpp:2266
PROTOSTYLE ProtoStyle
Definition: cluster.h:49
PROTOTYPE * MakePrototype(CLUSTERER *Clusterer, CLUSTERCONFIG *Config, CLUSTER *Cluster)
Definition: cluster.cpp:982
inT8 NonEssential
Definition: ocrfeatures.h:48
BOOL8 DistributionOK(BUCKETS *Buckets)
Definition: cluster.cpp:2159
inT32 NumChar
Definition: cluster.h:93
inT8 Circular
Definition: ocrfeatures.h:47
#define first_node(l)
Definition: oldlist.h:139
LIST destroy(LIST list)
Definition: oldlist.cpp:187
uinT16 NormalBucket(PARAM_DESC *ParamDesc, FLOAT32 x, FLOAT32 Mean, FLOAT32 StdDev)
Definition: cluster.cpp:2088
void FillBuckets(BUCKETS *Buckets, CLUSTER *Cluster, uinT16 Dim, PARAM_DESC *ParamDesc, FLOAT32 Mean, FLOAT32 StdDev)
Definition: cluster.cpp:2015
LIST pop(LIST list)
Definition: oldlist.cpp:305
LIST ProtoList
Definition: cluster.h:92
void KDStore(KDTREE *Tree, FLOAT32 *Key, void *Data)
Definition: kdtree.cpp:218
void ComputePrototypes(CLUSTERER *Clusterer, CLUSTERCONFIG *Config)
Definition: cluster.cpp:936
CLUSTERCONFIG Config
Definition: cluster.h:45
unsigned Prototype
Definition: cluster.h:34
CLUSTER * MakeNewCluster(CLUSTERER *Clusterer, TEMPCLUSTER *TempCluster)
Definition: cluster.cpp:846
FLOAT32 Mean(PROTOTYPE *Proto, uinT16 Dimension)
Definition: cluster.cpp:650
#define FALSE
Definition: capi.h:29
PARAM_DESC * ParamDesc
Definition: cluster.h:88
FLOAT32 Range
Definition: ocrfeatures.h:51
void Efree(void *ptr)
Definition: emalloc.cpp:79
inT32 MergeClusters(inT16 N, register PARAM_DESC ParamDesc[], register inT32 n1, register inT32 n2, register FLOAT32 m[], register FLOAT32 m1[], register FLOAT32 m2[])
STATISTICS * ComputeStatistics(inT16 N, PARAM_DESC ParamDesc[], CLUSTER *Cluster)
Definition: cluster.cpp:1431
Definition: cluster.h:32
PROTOTYPE * MakeSphericalProto(CLUSTERER *Clusterer, CLUSTER *Cluster, STATISTICS *Statistics, BUCKETS *Buckets)
Definition: cluster.cpp:1226
CLUSTER * NextSample(LIST *SearchState)
Definition: cluster.cpp:625
uinT16 DegreesOfFreedom(DISTRIBUTION Distribution, uinT16 HistogramBuckets)
Definition: cluster.cpp:2243
#define PI
Definition: const.h:19
#define InitSampleSearch(S, C)
Definition: cluster.h:105
#define TRUE
Definition: capi.h:28
PROTOTYPE * NewSphericalProto(uinT16 N, CLUSTER *Cluster, STATISTICS *Statistics)
Definition: cluster.cpp:1519
unsigned Style
Definition: cluster.h:74
#define MAXBUCKETS
Definition: cluster.h:27
tesseract::KDPairInc< float, TEMPCLUSTER * > ClusterPair
Definition: cluster.cpp:168
void FreeClusterer(CLUSTERER *Clusterer)
Definition: cluster.cpp:543
#define MAX_FLOAT32
Definition: host.h:124
uinT32 * Count
Definition: cluster.cpp:185
void KDWalk(KDTREE *Tree, void_proc action, void *context)
Definition: kdtree.cpp:332
FLOAT64(* SOLVEFUNC)(CHISTRUCT *, double)
Definition: cluster.cpp:204
void CreateClusterTree(CLUSTERER *Clusterer)
Definition: cluster.cpp:705
CLUSTER * Cluster
Definition: cluster.cpp:164
#define DELTARATIO
void DoError(int Error, const char *Message)
Definition: danerror.cpp:42
inT32 CharID
Definition: cluster.h:38
void FreePrototype(void *arg)
Definition: cluster.cpp:586
BUCKETS * GetBuckets(CLUSTERER *clusterer, DISTRIBUTION Distribution, uinT32 SampleCount, FLOAT64 Confidence)
Definition: cluster.cpp:1713
FLOAT64 NormalDensity(inT32 x)
Definition: cluster.cpp:1951
#define NULL
Definition: host.h:144
FLOAT64(* DENSITYFUNC)(inT32)
Definition: cluster.cpp:203
void KDDelete(KDTREE *Tree, FLOAT32 Key[], void *Data)
Definition: kdtree.cpp:265
PROTOTYPE * MakeMixedProto(CLUSTERER *Clusterer, CLUSTER *Cluster, STATISTICS *Statistics, BUCKETS *NormalBuckets, FLOAT64 Confidence)
Definition: cluster.cpp:1307
DISTRIBUTION Distribution
Definition: cluster.cpp:179
#define MAXDEGREESOFFREEDOM
Definition: cluster.cpp:229
unsigned Merged
Definition: cluster.h:69
uinT16 DegreesOfFreedom
Definition: cluster.cpp:190
void MakeDimUniform(uinT16 i, PROTOTYPE *Proto, STATISTICS *Statistics)
Definition: cluster.cpp:1394
KDTREE * MakeKDTree(inT16 KeySize, const PARAM_DESC KeyDesc[])
Definition: kdtree.cpp:182
TEMPCLUSTER * candidates
Definition: cluster.cpp:198
FLOAT32 StandardDeviation(PROTOTYPE *Proto, uinT16 Dimension)
Definition: cluster.cpp:664
inT16 SampleSize
Definition: cluster.h:87
void FreeStatistics(STATISTICS *Statistics)
Definition: cluster.cpp:2188
PROTOSTYLE
Definition: cluster.h:44
FLOAT64 Alpha
Definition: cluster.cpp:191
FLOAT32 * CoVariance
Definition: cluster.cpp:173
PROTOTYPE * NewSimpleProto(inT16 N, CLUSTER *Cluster)
Definition: cluster.cpp:1623
FLOAT64 Integral(FLOAT64 f1, FLOAT64 f2, FLOAT64 Dx)
Definition: cluster.cpp:1988
FLOAT32 Max
Definition: ocrfeatures.h:50
FLOAT32 * ExpectedCount
Definition: cluster.cpp:186
LIST push(LIST list, void *element)
Definition: oldlist.cpp:323
unsigned short uinT16
Definition: host.h:101
double FLOAT64
Definition: host.h:112
tesseract::GenericHeap< ClusterPair > ClusterHeap
Definition: cluster.cpp:169
FLOAT64 ChiSquared
Definition: cluster.cpp:182
#define MINALPHA
short inT16
Definition: host.h:100
int inT32
Definition: host.h:102
unsigned char uinT8
Definition: host.h:99