-
Notifications
You must be signed in to change notification settings - Fork 8
/
comparator.js
37 lines (28 loc) · 788 Bytes
/
comparator.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
const DTW = require('./dtw/dtw')
const Utils = require('./utils')
class FeatureComparator {
constructor(options) {
this.options = options || {}
this._dtw = new DTW({distanceFunction: FeatureComparator.calculateDistance})
}
static calculateDistance(ax, bx) {
return 1 - Utils.cosineSimilarity(ax, bx)
}
get bandSize() {
return this.options.bandSize || 5
}
get ref() {
return this.options.ref || 0.22
}
compare(a, b) {
const cost = this._dtw.compute(a, b, this.bandSize)
const normalizedCost = cost / ( a.length + b.length )
return this.computeProbability(normalizedCost)
}
computeProbability(cost) {
return 1 / ( 1 + Math.exp( ( cost - this.ref ) / this.ref ) )
}
destroy() {
}
}
module.exports = FeatureComparator