-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEventTree.js
180 lines (165 loc) · 6.91 KB
/
EventTree.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
const EventTreeNode = require('./EventTreeNode.js');
const Event = require('./Event.js');
class EventTree {
constructor(depth, interpolation, denominatorBias) {
if (isNaN(depth)) {
throw 'Cannot create EventTree with no depth';
}
if (depth <= 1) {
throw 'depth should be greater than 1';
}
if (isNaN(interpolation)) {
throw 'Cannot create EventTree with no interpolation';
}
if (interpolation <= 0) {
throw 'interpolation should be positive';
}
if (isNaN(denominatorBias)) {
throw 'Cannot create EventTree with no denominatorBias ';
}
if (interpolation < 0) {
throw 'denominatorBias should be positive';
}
this.depth = depth;
this.interpolation = interpolation;
this.denominatorBias = denominatorBias;
this.occurrence = 0;
this.children = new Map();
}
learn(sequence) {
if (sequence === null || sequence === undefined || sequence.length === 0) {
return;
}
let lastEvent = sequence[sequence.length - 1];
let childTreeNode = this.children.get(lastEvent.key);
if (childTreeNode === undefined) {
childTreeNode = new EventTreeNode(lastEvent, this.depth);
this.children.set(lastEvent.key, childTreeNode);
}
childTreeNode.learn(sequence);
this.occurrence++;
}
learnAllSuffix(sequence) {
generateSuffixSequenceArray(sequence).map(suffix => {this.learn(suffix)});
}
learnWithSlidingWindow(sequence) {
for (let index = 0; index <= sequence.length; index++) {
let start = index - sequence.length;
if (start < 0) {
start = 0;
}
this.learn(sequence.slice(start, index));
}
}
getCandidate() {
let res = new Map();
for (let candidate of this.children.keys()) {
res.set(candidate,this.children.get(candidate).event);
}
return res;
}
getInterpolatedProbabilityMap(sequence) {
let res = new Map();
let probaAllCandidateArray = [];
let probabilityMap = this.getProbabilityMap(sequence);
for (let candidate of probabilityMap.keys()) {
let probaArray = probabilityMap.get(candidate);
let proba = probaArray.reduce( (prev, cur, index) => {
return prev + cur * Math.pow(this.interpolation,probaArray.length - index);
}, 0);
probaAllCandidateArray.push(proba);
res.set(candidate, proba);
}
let probaSum = probaAllCandidateArray.reduce( (prev, cur) => prev+cur,0);
for (let candidate of probabilityMap.keys()) {
let oldProba = res.get(candidate);
res.set(candidate, probaSum === 0 ? 0 : oldProba / probaSum);
}
return res;
}
getProbabilityMap(sequence) {
let candidateSuffixMatrix = this.getProbabilityMatrix(sequence);
let keyProbaMap = new Map();
for (let candidate of this.children.keys()) {
keyProbaMap.set(candidate,[]);
}
if (sequence.length >= this.depth) {
sequence = sequence.slice(0, this.depth);
}
let suffixSequenceArray = generateSuffixSequenceArray(sequence);
for (let suffixSequenceId = 0; suffixSequenceId < suffixSequenceArray.length; suffixSequenceId++) {
let allOccurrence = candidateSuffixMatrix[suffixSequenceId].reduce( (prev , cur) => cur + prev, 0);
let candidateId = 0;
for (let candidate of this.children.keys()) {
let proba;
if (allOccurrence === 0) {
proba = 0;
} else {
proba = candidateSuffixMatrix[suffixSequenceId][candidateId]/ (allOccurrence+this.denominatorBias);
}
keyProbaMap.get(candidate).push(proba);
candidateId++;
}
}
return keyProbaMap;
}
getProbabilityMatrix(sequence) {
if (! Array.isArray(sequence)) {
throw 'getProbabilityMatrix: sequence should be an array';
}
if (sequence.length < 1) {
throw 'getProbabilityMatrix: sequence should at least contain one event';
}
if (sequence.length >= this.depth) {
sequence = sequence.slice(0, this.depth);
}
if (! (sequence[0] instanceof Event)) {
throw 'getProbabilityMatrix: sequence should contain Event only !';
}
let suffixSequenceArray = generateSuffixSequenceArray(sequence);
let candidateSuffixMatrix = [];
for (let suffixSequenceId = 0; suffixSequenceId < suffixSequenceArray.length; suffixSequenceId++) {
const suffixSequence = suffixSequenceArray[suffixSequenceId];
candidateSuffixMatrix[suffixSequenceId] = [];
let candidateId = 0;
for (let candidate of this.children.keys()) {
let suffixSequenceWithCandidate = [...suffixSequence];
suffixSequenceWithCandidate.push(this.children.get(candidate).event);
let occurrence = this.children.get(candidate).getOccurence(suffixSequenceWithCandidate);
candidateSuffixMatrix[suffixSequenceId][candidateId] = occurrence;
candidateId++;
}
}
return candidateSuffixMatrix;
}
crossEntropy(eventSequence) {
const probaOfUnknown = 1e-6;
if (eventSequence === null || eventSequence === undefined) {
throw new Error('no eventSequence');
}
if (eventSequence.length === 0) return probaOfUnknown;
let probabilitySum = 0;
for (let index = eventSequence.length-1; index > 0; index--) {
let lastEvent = eventSequence[index];
let contextSequence = eventSequence.slice(0, index);
let probabilityMap = this.getInterpolatedProbabilityMap(contextSequence);
let modelProba = probabilityMap.get(lastEvent.key);
let proba;
if (modelProba === null || modelProba === undefined || modelProba === 0) {
proba = probaOfUnknown;
} else {
proba = modelProba * (1 - probaOfUnknown);
}
probabilitySum = probabilitySum + Math.log2(proba);
}
return -(probabilitySum / (eventSequence.length-1));
}
}
function generateSuffixSequenceArray(sequence) {
let suffixSequenceArray = [];
for (let index = 0; index < sequence.length; index++) {
suffixSequenceArray.push(sequence.slice(index, sequence.length));
}
return suffixSequenceArray;
}
module.exports = EventTree;