-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
183 lines (165 loc) · 5.15 KB
/
index.js
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
var _ = require('underscore');
function vectorAdd(x, y) {
return _.map(_.zip(x, y), function (pair) {
return pair[0] + pair[1];
});
}
function vectorSubtract(x, y) {
return _.map(_.zip(x, y), function (pair) {
return pair[0] - pair[1];
});
}
function vectorMagnitude(x) {
var squaredSum = 0;
_.each(x, function (v) {
squaredSum += v * v;
});
return Math.sqrt(squaredSum);
}
function vectorMultiply(x, c) {
return _.map(x, function (v) {
return v * c;
});
}
function vectorDivide(x, c) {
return vectorMultiply(x, 1/c);
}
function gaussianKernel(x, y, sigma) {
sigma = sigma || 0.00001;
var l = vectorMagnitude(vectorSubtract(x, y));
return Math.exp(-sigma * (l*l));
}
function normalizedKernel(kernel) {
return function (x, y) {
return kernel(x, y) / Math.sqrt(kernel(x, x) + kernel(y, y));
};
}
var kernel = normalizedKernel(gaussianKernel);
function kernelDistance(x, y) {
// return kernel(x, x) - 2 * kernel(x, y) + kernel(y, y); // general form
return 2 - 2 * kernel(x, y); // for gaussian kernels
}
function Cluster(center) {
this.center = center;
/*
tom: the papers say to initialize with weight zero, but that makes clusters
you can't merge (dividing by zero), and initializing the size this way
doesn't seem to hurt performance.
*/
// this.size = 0;
this.size = kernel(center, center);
}
Cluster.prototype = {
add: function (e) {
this.size += kernel(this.center, e);
this.center = vectorAdd(this.center, vectorDivide(vectorSubtract(e, this.center), this.size));
},
merge: function (c) {
this.center = vectorDivide(vectorAdd(vectorMultiply(this.center, this.size), vectorMultiply(c.center, c.size)), this.size + c.size);
this.size += c.size;
},
resize: function (dim) {
while (this.center.length < dim) {
this.center.push(0);
}
},
toString: function () {
return 'Cluster(' + this.center + ', ' + this.size + ')';
},
};
// the distance between 2 clusters
function ClusterDistance(cluster1, cluster2, distance) {
this.cluster1 = cluster1;
this.cluster2 = cluster2;
this.distance = distance;
}
ClusterDistance.prototype = {
compareTo: function (other) {
return this.distance - other.distance;
},
toString: function () {
return 'Dist(' + this.distance + ')';
},
};
// will find at most N-1 clusters
// time taken is proportional to N
function OnlineCluster(N) {
this.n = 0;
this.N = N;
this.clusters = [];
this.numDimensions = 0; // max number of dimensions seen so far
this.distances = []; // cache of inter-cluster distances
}
OnlineCluster.prototype = {
resize: function (numDimensions) {
this.clusters.forEach(function (c) {
c.resize(numDimensions);
});
this.numDimensions = numDimensions;
},
cluster: function (e) {
if (e.length > this.numDimensions) {
this.resize(e.length);
}
if (this.clusters.length > 0) {
var closest = _.min(this.clusters, function (x) {
return kernelDistance(x.center, e);
});
closest.add(e);
this.updateDistances(closest);
}
if (this.clusters.length >= this.N) {
// merge closest two clusters
var d = this.distances.shift();
d.cluster1.merge(d.cluster2);
this.clusters = _.without(this.clusters, d.cluster2);
this.removeDistances(d.cluster2);
this.updateDistances(d.cluster1);
}
// make a new cluster for this point
var newCluster = new Cluster(e);
this.clusters.push(newCluster);
this.updateDistances(newCluster);
this.n += 1;
},
// invalidate intercluster distance cache for c
removeDistances: function (c) {
this.distances = _.filter(this.distances, function (d) {
return d.cluster1 !== c && d.cluster2 !== c;
});
this.heapify();
},
// Cluster c has changed, re-compute all intercluster distances
updateDistances: function (c) {
this.removeDistances(c);
_.each(this.clusters, function (x) {
if (x !== c) {
var d = kernelDistance(x.center, c.center);
var t = new ClusterDistance(x, c, d);
this.distances.push(t);
}
}, this);
this.heapify();
},
// doesn't really make it a heap; just sorts the array for now
heapify: function () {
this.distances.sort(function (a, b) {
return a.compareTo(b);
});
},
// Return only clusters over threshold
trimmedClusters: function () {
// find mean of clusters with size > 0
var sum = 0, count = 0;
_.each(this.clusters, function (c) {
if (c.size > 0) {
sum += c.size;
count += 1;
}
});
var mean = sum / count;
var threshold = mean * 0.1;
return _.filter(this.clusters, function (c) { return c.size >= threshold });
},
};
exports.OnlineCluster = OnlineCluster;