-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicWindowClassificationPerformanceEvaluator.java
More file actions
491 lines (408 loc) · 22.8 KB
/
DynamicWindowClassificationPerformanceEvaluator.java
File metadata and controls
491 lines (408 loc) · 22.8 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
package moa.evaluation;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
import com.github.javacliparser.FloatOption;
import com.github.javacliparser.IntOption;
import com.yahoo.labs.samoa.instances.Instance;
import moa.core.Example;
import moa.core.Measurement;
import moa.core.Utils;
import moa.evaluation.DynamicWindowClassificationPerformanceEvaluator.DynamicWindowEstimator;
import com.github.javacliparser.FlagOption;
/**
* Classification evaluator that updates evaluation results using a sliding
* window.
*
* @author Andres L. Suarez-Cetrulo (suarezcetrulo at gmail dot com)
* @version $Revision: 0.1 $
*/
public class DynamicWindowClassificationPerformanceEvaluator extends BasicClassificationPerformanceEvaluator {
private static final long serialVersionUID = 1L;
public static int dynamicEstimatorID = 0;
// First ensemble index pos (when the evaluator was created)
int indexPos;
// Flags / Options
public IntOption widthOption = new IntOption("width",'w', "Size of Window", 10);
public IntOption defaultWidthOption = new IntOption("defaultWidth",'d', "Default Size of Window", 10);
public IntOption widthIncrementsOption = new IntOption("increments",'i', "Increments in Size of Window", 1);
public IntOption minWidthOption = new IntOption("minWidth",'m', "Minimum Size of Window", 5);
public FlagOption backgroundDynamicWindowsFlag = new FlagOption("resizeAllWindows", 'b', "Should the comparison windows for old learners be also dynamic? ");
public FloatOption thresholdOption = new FloatOption("threshold",'t', "Threshold for resizing", 0.65);
// Window properties
int windowSize;
int defaultSize; // default size when the evaluator was created the first time (the first time its model drifted)
int windowIncrements;
int minWindowSize;
int windowResizePolicy;
double decisionThreshold;
double priorEstimation;
boolean backgroundDynamicWindows;
boolean useOptions = true; // by default use options
protected String evaluatorType; // optional (for EPCH: ACTIVE, BKG or CH)
// Variables for methods overrided
private double totalWeightObserved;
@SuppressWarnings("unused")
private int lastSeenClass;
// Information about classifier that created the estimator
String createdBy;
// Constructor for dynamic internal window evaluators
public DynamicWindowClassificationPerformanceEvaluator(int windowSize, int windowIncrements, int minWindowSize,
double priorEstimation, double decisionThreshold, boolean resizingEnabled, int windowResizePolicy, int indexPos, String createdBy) {
// Initializing
this.indexPos=indexPos;
// First values
this.windowSize = windowSize;
this.defaultSize = windowSize;
this.priorEstimation = priorEstimation;
this.windowIncrements = windowIncrements;
this.minWindowSize=minWindowSize;
this.decisionThreshold=decisionThreshold;
this.backgroundDynamicWindows=resizingEnabled;
this.windowResizePolicy=windowResizePolicy;
this.useOptions=false;
this.createdBy = createdBy;
}
public void addModel(int indexPos, double priorEstimation, int windowSize) {
((DynamicWindowEstimator) this.weightCorrect).addNewModel(indexPos, priorEstimation, windowSize);
}
public void deleteModel(int indexPos) {
((DynamicWindowEstimator) this.weightCorrect).deleteModel(indexPos);
}
public boolean containsIndex(int indexPos) {
return ((DynamicWindowEstimator) this.weightCorrect).contains(indexPos);
}
public int getAmountOfApplicableModels() {
return ((DynamicWindowEstimator) this.weightCorrect).getAmountOfApplicableModels();
}
/*public void clear(){
((DynamicWindowEstimator) this.weightCorrect).clear();
this.weightCorrect = null;
// reset(this.numClasses);
}*/
// Getters and Setters
// this could be lower than window size if the buffer is not full.
public int getCurrentSize(int ensembleIndex) {
return ((DynamicWindowEstimator) weightCorrect).getActualSize(ensembleIndex);
}
// window size is the maximum of the dynamic length buffer
public int getWindowSize(int ensembleIndex) {
return ((DynamicWindowEstimator) weightCorrect).getMaxCurrentSize(ensembleIndex);
}
// Overriding next few methods to avoid calculating extra evaluators for kappa statistics - @suarezcetrulo
@Override
public void reset(){
// System.out.println("Manual reset");
this.windowSize = this.defaultSize;
reset(this.numClasses);
}
@Override
public void reset(int numClasses) {
// System.out.println("Reset by 'addResult'");
this.numClasses = numClasses;
this.weightCorrect = newEstimator(); // it will started from scratch for only one window size.
// System.out.println("RESET ESTIMATOR ID: "+(dynamicEstimatorID)+" "+createdBy+" "+" pos:"+this.indexPos);
this.lastSeenClass = 0;
this.totalWeightObserved = 0;
}
@Override
public void addResult(Example<Instance> example, double[] classVotes) {
Instance inst = example.getData();
double weight = inst.weight();
if (inst.classIsMissing() == false){
int trueClass = (int) inst.classValue();
int predictedClass = Utils.maxIndex(classVotes);
if (weight > 0.0) {
if (this.totalWeightObserved == 0) {
reset(inst.dataset().numClasses()); // are we using the correct one?
}
this.totalWeightObserved += weight;
this.weightCorrect.add(predictedClass == trueClass ? weight : 0);
}
this.lastSeenClass = trueClass;
}
}
@Override
public Measurement[] getPerformanceMeasurements() {
return new Measurement[]{
new Measurement("classified instances",
getTotalWeightObserved()),
new Measurement("classifications correct (percent)",
getFractionCorrectlyClassified() * 100.0)
};
}
// The index sent belongs to the active model that the evaluator compares against (it should be an applicable model)
public double getFractionIncorrectlyClassified(int ensembleIndex) {
return 1.0 - getFractionCorrectlyClassified(ensembleIndex) ;
}
// The index sent belongs to the active model against (it should be an applicable model)
public double getFractionCorrectlyClassified(int ensembleIndex) {
return ((DynamicWindowEstimator) this.weightCorrect).estimation(ensembleIndex);
}
// //////////////////////////////////////////////
@Override
protected Estimator newEstimator() {
dynamicEstimatorID=dynamicEstimatorID+1;
//System.out.println("NEW WINDOW ESTIMATOR CREATED - call variable weightCorrect for more info.");
if (useOptions) {
// When using the dynamic estimator as default one, there is no prior estimation (-1), so the only valid resizing policy is 2.
return new DynamicWindowEstimator(this.widthOption.getValue(),this.widthIncrementsOption.getValue(), this.minWidthOption.getValue(), -1,
this.thresholdOption.getValue(), this.backgroundDynamicWindowsFlag.isSet(), 2,
DynamicWindowClassificationPerformanceEvaluator.dynamicEstimatorID, 0); // hard coding position in the ensemble = 0
}
else {
// System.out.println("NEW "+this.evaluatorType+" ESTIMATOR WITH ID: "+DynamicWindowClassificationPerformanceEvaluator.dynamicEstimatorID);
// System.out.println("START ESTIMATOR #"+dynamicEstimatorID+" "+this.createdBy);
return new DynamicWindowEstimator(this.windowSize, this.windowIncrements, this.minWindowSize, this.priorEstimation,
this.decisionThreshold,this.backgroundDynamicWindows, this.windowResizePolicy,
DynamicWindowClassificationPerformanceEvaluator.dynamicEstimatorID, this.indexPos);
}
}
public String getEvaluatorType() {
return evaluatorType;
}
public void setEvaluatorType(String evaluatorType) {
this.evaluatorType = evaluatorType;
}
// Estimator
public class DynamicWindowEstimator implements Estimator {
/**
* by suarezcetrulo
*/
private static final long serialVersionUID = -6525799997539109003L;
public int estimatorID;
// Window container of estimation values
ArrayList<Double> window;
// Dynamic Windows (both will always have the same length)
protected HashMap<Integer,Integer> SizeWindows; // AS windowSize
protected HashMap<Integer,Integer> defaultSizes;
// Static parameters
protected int minSize;
protected int sizeIncrements;
protected int windowResizePolicy;
protected boolean resizingEnabled;
protected boolean debug = false;
// Resizing decision factor
protected HashMap<Integer,Double> priorEstimations; // prior error at the beginning of the warning window
protected double threshold;
// Constructor for window: threshold = -1 means that selected size update policy is 1
public DynamicWindowEstimator(int initialSize, int sizeIncrements, int minSize,
double priorEstimation, double threshold, boolean resizingEnabled,
int windowResizePolicy, int estimatorID, int index){
//System.out.println("Creating estimator #"+estimatorID+" with Window size: "+initialSize);
//System.out.println("CREATE ESTIMATOR estimator #"+estimatorID+" with Window size: "+initialSize);
//System.out.println("CREATE ESTIMATOR "+estimatorID+" WITH INITIAL POS "+index);
//System.out.println(index);
this.estimatorID = estimatorID;
// Initializing dynamic window
this.window = new ArrayList<Double>();
// Initializing and adding first value in HashMaps
this.SizeWindows= new HashMap<Integer,Integer>();
this.SizeWindows.put(index, initialSize);
this.defaultSizes = new HashMap<Integer,Integer> ();
this.defaultSizes.put(index, initialSize);
this.priorEstimations = new HashMap<Integer,Double>();
this.priorEstimations.put(index, priorEstimation);
// Initializing static values
this.sizeIncrements = sizeIncrements;
this.minSize = minSize; // minimum size should always be greater than size increments
this.threshold = threshold;
this.resizingEnabled=resizingEnabled;
this.windowResizePolicy=windowResizePolicy;
}
public int getAmountOfApplicableModels() {
return this.SizeWindows.size();
}
public void addNewModel (int index, double priorEstimation, int windowSize) {
//System.out.println("ADD APPLICABLE MODEL "+index+" IN ESTIMATOR "+estimatorID);
this.SizeWindows.put(index, windowSize);
this.defaultSizes.put(index, windowSize);
this.priorEstimations.put(index, priorEstimation);
}
public void deleteModel (int index) {
//System.out.println("DELETE APPLICABLE MODEL "+index+" IN ESTIMATOR "+estimatorID);
this.SizeWindows.remove(index);
this.defaultSizes.remove(index);
this.priorEstimations.remove(index);
}
public boolean contains (int index) {
return this.SizeWindows.containsKey(index);
}
public int getMaxCurrentSize(int pos) {
return this.SizeWindows.get(pos);
}
public int getActualSize(int pos) {
return getSublist(this.SizeWindows.get(pos)).size();
}
// It adds errors per classified row
public void add(double value) {
// TEST TRACE
/*System.out.println("-------------------------------------------");
System.out.println("SIZE: "+SizeWindows.size());*/
/* for (Entry<Integer, Integer> modelWindows : SizeWindows.entrySet()) {
System.out.println("ESTIMATOR: "+this.estimatorID);
System.out.println("pos: "+modelWindows.getKey()+" in estimator #"+this.estimatorID+": Adding result: "+value+" - window size should be:"+modelWindows.getValue());
System.out.println("pos: "+modelWindows.getKey()+" actual window is: "+getSublist(modelWindows.getValue())); //, this.sizeIncrements));
//System.out.println("pos: "+modelWindows.getKey()+" small window is:"+getSublist(modelWindows.getValue()-this.sizeIncrements)); //, this.sizeIncrements*2));
}*/
/*System.out.println("---");
System.out.println("Complete-largest window is: "+this.window);
System.out.println("---");
System.out.println("-- WINDOWS SIZE IS: "+window.size()+" THERE ARE "+SizeWindows.size()+" APPLICABLE MODELS");
System.out.println("-- APPLICABLE MODELS ARE: "+SizeWindows.keySet()+" AND THEIR SIZES ARE "+SizeWindows.values());
System.out.println("-- SO MAX WINDOW SIZE IS: "+Collections.max(this.SizeWindows.values())+" + WINDOWS_INCREMENT = FULL WINDOW(?)");
// END TEST TRACE*/
// System.out.println("-- WINDOWS SIZE IS: "+window.size()+" THERE ARE "+SizeWindows.size()+" APPLICABLE MODELS");
// System.out.println("-- WINDOWS SIZE IS: "+getCurrentSize(0)+" THERE ARE "+SizeWindows.size()+" APPLICABLE MODELS");
// Always storing extra results just in case the window grows
this.window.add(value);
// Remove oldest if it surpasses the maximum windowSize + three times the increments.
// Also allow it to grow at the start till the minimum size if the default size is lower than this.
if(this.window.size()>(Math.max((Collections.max(this.SizeWindows.values())+this.sizeIncrements*3), this.minSize+this.sizeIncrements*3))) {
this.window.remove(0);
}
for (Entry<Integer, Integer> modelWindows : SizeWindows.entrySet()) {
// Resize window -- this update window size here is designed for increments of 1 unit,
// as it is executed every time one instance is created.
// Otherwise, the window could grow faster than the incoming data and be pointless.
// The single remove statement above also follows this.
if (this.resizingEnabled) updateWindowSize(modelWindows.getKey());
// TEST TRACE
//System.out.println("// // // // (after resizing)");
//System.out.println("estimatorID: "+estimatorID+" pos: "+modelWindows.getKey()+" window size is:"+modelWindows.getValue());
/* System.out.println("pos: "+modelWindows.getKey()+" actual window is: "+getSublist(modelWindows.getValue())); //, this.sizeIncrements));
System.out.println("pos: "+modelWindows.getKey()+" small window is:"+getSublist(modelWindows.getValue()-this.sizeIncrements)); //, this.sizeIncrements*2));
System.out.println(); */
}
/*System.out.println("---");
System.out.println("Complete-largest window is: "+this.window);
System.out.println("---");
System.out.println("-------------------------------------------");*/
// END TEST TRACE
}
// Returns the latest error window creating a sublist of the last errors.
// Size of the sublist is WINDOW_SIZE
public double estimation(int pos){ // = getLastWindowEstimation
return estimateError(getSublist(this.SizeWindows.get(pos)));
}
// DEFAULT ESTIMATOR
public double estimation(){
System.out.println("WARNING!!!!!! USING WRONG ESTIMATOR!!");
return estimateError(getSublist(this.SizeWindows.get(0)));
}
public double getSmallerWindowEstimation (int pos){
// TODO: this reduces the window to minimum size - 1 (for sizeIncrementes=1).
// this only affects to the third policy (policy value = 2).
// tests can be run anyway, just bearing on mind that the smallerWindow wont respect this size (dont set minimum at 1 if selecting third policy)
return estimateError(getSublist(this.SizeWindows.get(pos)-this.sizeIncrements));
}
public double getLargeWindowEstimation (int pos){
return estimateError(getSublist(this.SizeWindows.get(pos)+this.sizeIncrements));
}
/**
* @param desiredSize: window size or smaller window size
* @return sublist with the desired sub window
*/
public List<Double> getSublist(int desiredSize){
/* System.out.println("$$$$$$$$4");
System.out.println(this.estimatorID);
System.out.println(this.window);
System.out.println(this.window.subList(Math.max(this.window.size() - desiredSize, 0), this.window.size()));
System.out.println("$$$$$$$$4"); */
return this.window.subList(Math.max(this.window.size() - desiredSize, 0), this.window.size());
}
public double estimateError(List<Double> list) {
Double sum = 0.0;
if(!list.isEmpty()) {
for (Double error : list) {
sum += error;
} return sum.doubleValue() / (double) list.size();
} return sum;
}
public void updateWindowSize(int pos) {
switch(this.windowResizePolicy) {
case 0: // working with ERROR
// size update policy 0 [ min(error(c,w_c)) - priorError ] >= 0 -> window size increases
//if(priorError!=-1) { .. }
// working with ERRROR
// TEST TRACES
if (debug) System.out.println("estimatorID: "+this.estimatorID+" pos:"+pos+" currentEstimation is: "+estimation(pos));
if (debug) System.out.println("estimatorID: "+this.estimatorID+" pos:"+pos+" active Model Estimation before Warning was: "+this.priorEstimations.get(pos));
if(estimation(pos) <= this.priorEstimations.get(pos)) {
if (debug) System.out.println("GROWS");
this.SizeWindows.put(pos,this.SizeWindows.get(pos)+this.sizeIncrements);
}
else { // otherwise it decreases
if (debug) System.out.println("does not grow!");
this.SizeWindows.put(pos,this.SizeWindows.get(pos)-this.sizeIncrements);
if(this.SizeWindows.get(pos) <= this.minSize) this.SizeWindows.put(pos, this.minSize);
else if(getActualSize(pos) > this.minSize) {
// System.out.println("ENTERED AS CURRENT SIZE IS: "+this.SizeWindows.get(pos)+" ALTHOUGH ACTUAL SIZE IS: "+getActualSize(pos));
// if getActualSize(pos) < this.minSize, then keep growing inside the limits (do not remove any value)
// if window size is greater than minimum size, the evaluator is decreasing size and its the maximum size model, we delete a value in the window
if (this.SizeWindows.get(pos) == Collections.max(this.SizeWindows.values())){
for(int n = 1; n <= this.sizeIncrements ; n++) this.window.remove(0); // we do it as many times as values we insert per time (sizeIncrements)
}
}
// System.out.println("MINIMUM SIZE IS: "+this.minSize+" AND ACTUAL SIZE IS: "+getActualSize(pos));
}
break;
// TODO: POLICIES 1 AND 2 STILL NEED TESTING
case 1: // working with ERRROR
// size update policy 1 [ min(error(c,w_c)) - priorError ] > threshold -> window size increases
//if(priorError!=-1) { .. }
if(estimation(pos) - this.priorEstimations.get(pos) <= this.threshold)
this.SizeWindows.put(pos,this.SizeWindows.get(pos)+this.sizeIncrements);
else {// otherwise it decreases
this.SizeWindows.put(pos,this.SizeWindows.get(pos)-this.sizeIncrements);
if(this.SizeWindows.get(pos) <= this.minSize) this.SizeWindows.put(pos,this.minSize);
else if(getActualSize(pos) > this.minSize) {
// if getActualSize(pos) < this.minSize, then keep growing inside the limits (do not remove any value)
// if window size is greater than minimum size, it is decreasing size and its the maximum size model, we delete a value in the window to free space
if (this.SizeWindows.get(pos) == Collections.max(this.SizeWindows.values())){
for(int n = 1; n <= this.sizeIncrements ; n++) this.window.remove(0); // we do it as many times as values we insert per time (sizeIncrements)
}
}
}
break;
case 2: // working with ACCURACY
//For each c, W_c=s , where s is an small number / For each iteration, independently of ny situations / a = active model
//if(priorError==-1) { .. }
double W_a_candidate_0=100-estimation(pos); // or W_a_candidate_0=getAverageOfErrors(getLastWindow())
double W_a_candidate_1=100-getLargeWindowEstimation(pos);
double W_a_candidate_2=100-getSmallerWindowEstimation (pos);
if ( W_a_candidate_1 > W_a_candidate_0 && W_a_candidate_1 > W_a_candidate_2 ) // Increase window size
this.SizeWindows.put(pos,this.SizeWindows.get(pos)+this.sizeIncrements);
else if ( W_a_candidate_2 > W_a_candidate_0 && W_a_candidate_2 > W_a_candidate_1 ) { // Decrease window size
this.SizeWindows.put(pos,this.SizeWindows.get(pos)-this.sizeIncrements);
if(this.SizeWindows.get(pos) <= this.minSize) this.SizeWindows.put(pos,this.minSize);
else if(getActualSize(pos) > this.minSize) {
// if getActualSize(pos) < this.minSize, then keep growing inside the limits (do not remove any value)
// if window size is greater than minimum size, it is decreasing size and its the maximum size model, we delete a value in the window
if (this.SizeWindows.get(pos) == Collections.max(this.SizeWindows.values())){
for(int n = 1; n <= this.sizeIncrements ; n++) this.window.remove(0); // we do it as many times as values we insert per time (sizeIncrements)
}
}
}
break;
}
}
/*public void reset(int pos) {
this.window.clear();
this.SizeWindows.put(pos,defaultSize);
}*/
// Clean full window
/*public void clear() {
this.SizeWindows.clear();
this.SizeWindows = null;
this.defaultSizes.clear();
this.defaultSizes = null;
this.priorEstimations.clear();
this.priorEstimations = null;
this.window.clear();
this.window = null;
}*/
}
}