-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSequenceSuite.js
70 lines (63 loc) · 2.38 KB
/
SequenceSuite.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
const Sequence = require('./Sequence.js');
const NaturalnessModel = require('./NaturalnessModel.js');
const DEPTH = 4;
const PROBA_OF_UNKNOWN = 0;//0.000001;
const UPPER_BOUND = -Math.log2(PROBA_OF_UNKNOWN);
class SequenceSuite {
constructor(suite, depth, probaOfUnknown) {
this.depth = depth || DEPTH;
this.probaOfUnknown = probaOfUnknown || PROBA_OF_UNKNOWN;
if (suite === null || suite === undefined) {
throw 'cannot create SequenceSuite with no suite';
}
if (!(Array.isArray(suite))) {
throw 'cannot create SequenceSuite with no array';
}
suite.forEach(seq => {
if (!(seq instanceof Sequence)) {
throw 'cannot create SequenceSuite with no array of Sequence';
}
})
this.suite = suite;
}
getMoreNatural() {
if (this.suite.length === 0) {
throw 'Suite is empty';
}
if (this.suite.length === 1) {
return {
crossEntropy : UPPER_BOUND,
sequence : this.suite[0]
}
}
let moreNatural = {
crossEntropy : UPPER_BOUND
}
this.suite.forEach( (sequence, index) => {
let model = new NaturalnessModel(this.depth, this.probaOfUnknown);
let suiteCopy = this.suite.slice(0);
suiteCopy.splice(index, 1);
suiteCopy.forEach(seq => model.learn(seq));
let crossEntropy = model.crossEntropy(sequence);
if (crossEntropy <= moreNatural.crossEntropy) {
moreNatural.crossEntropy = crossEntropy;
moreNatural.sequence = sequence;
moreNatural.index = index;
}
});
return moreNatural;
}
rank() {
if (this.suite.length === 0) return [];
let rank = [];
let moreNatural = this.getMoreNatural();
if (moreNatural.sequence === undefined) console.log('undefined!!!');
rank.push({sequence:moreNatural.sequence, crossEntropy:moreNatural.crossEntropy});
let suiteCopy = this.suite.slice(0);
suiteCopy.splice(moreNatural.index, 1);
let other = new SequenceSuite(suiteCopy, this.depth, this.probaOfUnknown);
rank.push(...other.rank());
return rank;
}
}
module.exports = SequenceSuite;