-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTree.java
More file actions
574 lines (523 loc) · 18 KB
/
Tree.java
File metadata and controls
574 lines (523 loc) · 18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
package rrcf.general;
import java.util.Map;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Random;
import java.util.function.Consumer;
import java.io.Serializable;
/**
* Robust random cut tree data structure used for anomaly detection on streaming
* data
*
* Represents a single random cut tree, supporting generalized data points of any dimension
*
* Modified from: rrcf: Implementation of the Robust Random Cut Forest algorithm
* for anomaly detection on streams Matthew D. Bartos1, Abhiram Mullapudi1, and
* Sara C. Troutman
*
* Original paper: S. Guha, N. Mishra, G. Roy, & O. Schrijvers. Robust random
* cut forest based anomaly detection on streams, in Proceedings of the 33rd
* International conference on machine learning, New York, NY, 2016 (pp.
* 2712-2721).
*/
public class Tree implements Serializable {
private Node root;
// Number of dimensions for each point
private int ndim;
// Allows leaves to be accessed with external key
private Map<Object, Leaf> leavesMap;
private Random random;
public Tree(Random r, double[][] points) {
leavesMap = new HashMap<>();
random = r;
if (points.length == 0) {
return;
}
int[] indices = new int[points.length];
for (int i = 0; i < points.length; i++) {
indices[i] = i;
}
// Has to be before tree building
ndim = points[0].length;
root = buildTreeDown(points, indices, 0);
}
public Tree(double[][] points) {
this(new Random(), points);
}
public Tree(Random r) {
this(r, new double[0][0]);
}
public Tree() {
this(new Random());
}
private Node buildTreeDown(double[][] points, int[] indices, int depth) {
// TODO: Somewhat inefficient, potentially improve?
double[][] box = getBoxFromPoints(points);
// Check if the array is all duplicates and return a leaf if it is
for (int i = 0; i < box[0].length; i++) {
if (box[0][i] != box[box.length - 1][i]) {
break;
}
// All duplicates found
if (i == box[0].length - 1) {
Leaf leaf = new Leaf(box[0], depth);
leaf.num = points.length;
for (int d : indices) {
leavesMap.put(d, leaf);
}
return leaf;
}
}
Cut c = insertCut(box);
boolean[] goesLeft = new boolean[points.length];
int leftSize = 0;
for (int i = 0; i < points.length; i++) {
if (points[i][c.dim] <= c.value) {
goesLeft[i] = true;
leftSize++;
} else {
goesLeft[i] = false;
}
}
int[] leftI = new int[leftSize];
double[][] leftP = new double[leftSize][points[0].length];
int[] rightI = new int[points.length - leftSize];
double[][] rightP = new double[points.length - leftSize][points[0].length];
int leftIndex = 0;
int rightIndex = 0;
for (int i = 0; i < points.length; i++) {
if (goesLeft[i]) {
leftI[leftIndex] = indices[i];
leftP[leftIndex] = points[i];
leftIndex++;
} else {
rightI[rightIndex] = indices[i];
rightP[rightIndex] = points[i];
rightIndex++;
}
}
Node left = buildTreeDown(leftP, leftI, depth + 1);
Node right = buildTreeDown(rightP, rightI, depth + 1);
Branch branch = new Branch(c, left, right, left.num + right.num);
branch.point = mergeChildrenBoxes(branch);
left.parent = branch;
right.parent = branch;
return branch;
}
@Override
public String toString() {
String[] depthAndTreeString = { "", "" };
printNodeToString(root, depthAndTreeString);
return depthAndTreeString[1];
}
/**
* Number of points stored in the tree
*/
public int size() {
return leavesMap.size();
}
/**
* Prints a node to provided string
* Updates the given string array: { depth, tree } strings
*/
private void printNodeToString(Node node, String[] depthAndTreeString) {
Consumer<Character> ppush = (c) -> {
String branch = String.format(" %c ", c);
depthAndTreeString[0] += branch;
};
Runnable ppop = () -> {
depthAndTreeString[0] = depthAndTreeString[0].substring(0, depthAndTreeString[0].length() - 4);
};
if (node instanceof Leaf) {
depthAndTreeString[1] += String.format("(%s)\n", Arrays.toString(node.point[0]));
} else if (node instanceof Branch) {
Branch b = (Branch)node;
depthAndTreeString[1] += String.format("%c+ cut: (%d, %f), box: (%s, %s)\n", 9472, b.cut.dim, b.cut.value, Arrays.toString(b.point[0]), Arrays.toString(b.point[b.point.length - 1]));
depthAndTreeString[1] += String.format("%s %c%c%c", depthAndTreeString[0], 9500, 9472, 9472);
ppush.accept((char) 9474);
printNodeToString(b.left, depthAndTreeString);
ppop.run();
depthAndTreeString[1] += String.format("%s %c%c%c", depthAndTreeString[0], 9492, 9472, 9472);
ppush.accept(' ');
printNodeToString(b.right, depthAndTreeString);
ppop.run();
}
}
public void mapLeaves(Consumer<Leaf> func) {
mapLeaves(func, root);
}
private void mapLeaves(Consumer<Leaf> func, Node n) {
if (n instanceof Leaf) {
func.accept((Leaf) n);
} else {
Branch b = (Branch) n;
if (b.left != null) {
mapLeaves(func, b.left);
}
if (b.right != null) {
mapLeaves(func, b.right);
}
}
}
public void mapBranches(Consumer<Branch> func) {
mapBranches(func, root);
}
private void mapBranches(Consumer<Branch> func, Node n) {
if (!(n instanceof Leaf)) {
Branch b = (Branch) n;
if (b.left != null) {
mapBranches(func, b.left);
}
if (b.right != null) {
mapBranches(func, b.right);
}
func.accept(b);
}
}
/**
* Delete a leaf (found from index) from the tree and return deleted node
*/
public Node forgetPoint(Object index) {
Node leaf = leavesMap.get(index);
// If duplicate points exist, decrease num for all nodes above
if (leaf.num > 1) {
updateLeafCountUpwards(leaf, -1);
return leavesMap.remove(index);
}
// If leaf is root
if (root.equals(leaf)) {
root = null;
ndim = -1;
return leavesMap.remove(index);
}
// Calculate parent and sibling
Branch parent = leaf.parent;
Node sibling = getSibling(leaf);
// If parent is root, set sibling to root and update depths
if (root.equals(parent)) {
sibling.parent = null;
leaf.parent = null; // In case the returned node is used somehow
root = sibling;
increaseLeafDepth(-1, sibling);
return leavesMap.remove(index);
}
// Move sibling up a layer and link nodes
Branch grandparent = parent.parent;
sibling.parent = grandparent;
if (parent.equals(grandparent.left)) {
grandparent.left = sibling;
} else {
grandparent.right = sibling;
}
parent = grandparent;
// Update depths
increaseLeafDepth(-1, sibling);
// Update leaf counts for each branch
updateLeafCountUpwards(parent, -1);
// Update bounding boxes
shrinkBoxUp(parent, leaf.point[0]);
return leavesMap.remove(index);
}
/**
* Insert a point into the tree with a given index and create a new leaf
*/
public Leaf insertPoint(double[] point, Object index) {
// If no points, set necessary variables
if (root == null) {
Leaf leaf = new Leaf(point, 0);
root = leaf;
ndim = point.length;
return leavesMap.put(index, leaf);
}
// Check that dimensions are consistent and index doesn't exist
assert point.length == ndim;
assert !leavesMap.containsKey(index);
// Check for duplicates and only update counts if it exists
Leaf duplicate = findLeaf(point);
if (duplicate != null) {
updateLeafCountUpwards(duplicate, 1);
leavesMap.put(index, duplicate);
return duplicate;
}
// No duplicates found, continue
Node node = root;
Branch parent = null;
Leaf leaf = null;
Branch branch = null;
boolean useLeftSide = false;
// Traverse tree until insertion spot found
for (int i = 0; i < size(); i++) {
double[][] bbox = node.point;
Cut c = insertPointCut(point, bbox);
if (c.value <= bbox[0][c.dim]) {
leaf = new Leaf(point, i);
branch = new Branch(c, leaf, node, leaf.num + node.num);
break;
} else if (c.value >= bbox[bbox.length - 1][c.dim]) {
leaf = new Leaf(point, i);
branch = new Branch(c, node, leaf, leaf.num + node.num);
break;
} else {
Branch b = (Branch) node;
parent = b;
if (point[b.cut.dim] <= b.cut.value) {
node = b.left;
useLeftSide = true;
} else {
node = b.right;
useLeftSide = false;
}
}
}
// Check if cut was found
assert branch != null;
node.parent = branch;
leaf.parent = branch;
branch.parent = parent;
if (parent != null) {
if (useLeftSide) {
parent.left = branch;
} else {
parent.right = branch;
}
} else {
root = branch;
}
increaseLeafDepth(1, branch);
updateLeafCountUpwards(parent, 1);
expandBoxUp(branch);
leavesMap.put(index, leaf);
return leaf;
}
/**
* Gets the sibling of a node
*/
private Node getSibling(Node n) {
Branch parent = n.parent;
if (n.equals(parent.left)) {
return parent.right;
}
return parent.left;
}
/**
* Increases the leaf number for all ancestors above a given node by increment
*/
private void updateLeafCountUpwards(Node node, int increment) {
while (node != null) {
node.num += increment;
node = node.parent;
}
}
/**
* When a point is deleted, contract bounding box of nodes above point
* If the deleted point was on the boundary for any dimension
*/
private void shrinkBoxUp(Branch node, double[] point) {
while (node != null) {
// Check if any of the current box's values match the point
// Can exit otherwise, no shrinking necessary
for (int i = 0; i < ndim; i++) {
if (node.point[0][i] == point[i] || node.point[node.point.length - 1][i] == point[i]) {
break;
}
if (i == ndim - 1) {
// None equal
return;
}
}
node.point = mergeChildrenBoxes(node);
node = node.parent;
}
}
/**
* When a point is inserted, expand bounding box of nodes above new point
*/
private void expandBoxUp(Branch node) {
double[][] bbox = mergeChildrenBoxes(node);
node.point = bbox;
node = node.parent;
while (node != null) {
boolean anyChanged = false;
for (int i = 0; i < ndim; i++) {
if (bbox[0][i] < node.point[0][i]) {
node.point[0][i] = bbox[0][i];
anyChanged = true;
}
if (bbox[bbox.length - 1][i] > node.point[node.point.length - 1][i]) {
node.point[node.point.length - 1][i] = bbox[bbox.length - 1][i];
anyChanged = true;
}
}
if (!anyChanged) {
return;
}
node = node.parent;
}
}
/**
* Get bounding box of branch based on its children
*/
private double[][] mergeChildrenBoxes(Branch node) {
double[][] box = new double[2][ndim];
for (int i = 0; i < ndim; i++) {
box[0][i] = Math.min(node.left.point[0][i], node.right.point[0][i]);
box[1][i] = Math.max(node.left.point[node.left.point.length - 1][i],
node.right.point[node.right.point.length - 1][i]);
}
return box;
}
/**
* Adds `increment` to all leaves' depths under a node
*/
private void increaseLeafDepth(int increment, Node n) {
mapLeaves((leaf) -> {
leaf.depth += increment;
}, n);
}
/**
* Wrapper for query from root
*/
public Leaf query(double[] point) {
return query(point, root);
}
/**
* Finds the closest leaf to a point under a specified node
*/
private Leaf query(double[] point, Node n) {
while (!(n instanceof Leaf)) {
Branch b = (Branch) n;
if (point[b.cut.dim] <= b.cut.value) {
n = b.left;
} else {
n = b.right;
}
}
return (Leaf) n;
}
/**
* Wrapper for getDisplacment by leaf
*/
public int getDisplacement(Object key) {
return getDisplacement(leavesMap.get(key));
}
/**
* The number of nodes displaced by removing a leaf
* Removing a leaf shorts the sibling to the leaf's grandparent, so displacement is the sibling's count
* Can serve as a measure for outliers, but is affected by masking
*/
public int getDisplacement(Leaf leaf) {
if (leaf.equals(root)) {
return 0;
}
Branch parent = leaf.parent;
Node sibling = getSibling(leaf);
return sibling.num;
}
/**
* Wrapper for gcd by leaf
*/
public int getCollusiveDisplacement(Object key) {
return getCollusiveDisplacement(leavesMap.get(key));
}
/**
* The maximum number of nodes displaced by removing any subset of the tree including a leaf
* In practice, there are too many subsets to consider so it can be estimated by looking up the tree
* There is no definitive algorithm to empirically calculate codisp, so the ratio of sibling num to node num is used
*/
public int getCollusiveDisplacement(Leaf leaf) {
if (leaf.equals(root)) {
return 0;
}
Node node = leaf;
int maxResult = -1;
while (node.parent != null) {
Node sibling = getSibling(node);
int deleted = node.num;
int displacement = sibling.num;
maxResult = Math.max(maxResult, displacement / deleted);
node = node.parent;
}
return maxResult;
}
/**
* Returns a leaf containing a point if it exists
*/
public Leaf findLeaf(double[] point) {
Leaf nearest = query(point);
if (Arrays.equals(nearest.point[0], point)) {
return nearest;
}
return null;
}
private double[][] getBoxFromPoints(double[][] points) {
double[][] box = new double[2][points[0].length];
for (int i = 0; i < points[0].length; i++) {
box[0][i] = Double.MAX_VALUE;
box[box.length - 1][i] = -Double.MAX_VALUE;
}
// For all dimensions
for (int j = 0; j < points[0].length; j++) {
// For all points, set box to min and max
for (int i = 0; i < points.length; i++) {
box[0][j] = Math.min(box[0][j], points[i][j]);
box[box.length - 1][j] = Math.max(box[box.length - 1][j], points[i][j]);
}
}
return box;
}
/**
* Generates a random cut from the span of a bounding box
*/
private Cut insertCut(double[][] bbox) {
double[] span = new double[bbox[0].length];
// Cumulative sum of span
double[] spanSum = new double[bbox[0].length];
for (int i = 0; i < bbox[0].length; i++) {
span[i] = bbox[bbox.length - 1][i] - bbox[0][i];
if (i > 0) {
spanSum[i] = spanSum[i - 1] + span[i];
} else {
spanSum[i] = span[0];
}
}
// Weighted random with each dimension's span
double range = spanSum[spanSum.length - 1];
double r = random.nextDouble() * range;
int dimension = -1;
for (int i = 0; i < bbox[0].length; i++) {
// Finds first value greater than chosen
if (spanSum[i] > r) {
dimension = i;
break;
}
}
assert dimension > -1;
double value = bbox[0][dimension] + spanSum[dimension] - r;
return new Cut(dimension, value);
}
/**
* Generates a random cut from the span of a point and bounding box
*/
private Cut insertPointCut(double[] point, double[][] bbox) {
double[][] newBox = new double[2][bbox[0].length];
for (int i = 0; i < ndim; i++) {
newBox[0][i] = Math.min(bbox[0][i], point[i]);
newBox[newBox.length - 1][i] = Math.max(bbox[bbox.length - 1][i], point[i]);
}
return insertCut(newBox);
}
/**
* Java doesn't have tuples :(
*/
public static class Cut implements Serializable {
// Dimension of cut
public int dim;
// Value of cut
public double value;
public Cut(int d, double v) {
dim = d;
value = v;
}
}
}