1 | using HEAL.Attic;
|
---|
2 | using System;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using System.Linq;
|
---|
5 | using System.Text;
|
---|
6 | using System.Threading.Tasks;
|
---|
7 |
|
---|
8 | namespace HeuristicLab.Algorithms.OESRALPS.DriftDetection
|
---|
9 | {
|
---|
10 | [StorableType("714CAAE4-A88F-44A1-A2D0-92F4D8252C04")]
|
---|
11 | public abstract class ADWIN
|
---|
12 | {
|
---|
13 | [Storable]
|
---|
14 | public int MinCutSize { get; set; }
|
---|
15 | [Storable]
|
---|
16 | public int MinKeepSize { get; set; }
|
---|
17 | [Storable]
|
---|
18 | public double Delta { get; set; }
|
---|
19 |
|
---|
20 | public ADWIN() { }
|
---|
21 |
|
---|
22 | public ADWIN(double delta, int minKeepSize, int minCutSize)
|
---|
23 | {
|
---|
24 | Delta = delta;
|
---|
25 | MinKeepSize = minKeepSize;
|
---|
26 | MinCutSize = minCutSize;
|
---|
27 | }
|
---|
28 |
|
---|
29 | public bool CheckHistogramForCut(Histogram histogram, IEnumerator<Bucket> buckets, int numCutPointsToCheck)
|
---|
30 | {
|
---|
31 | double keepTotal = histogram.Total;
|
---|
32 | double keepVariance = histogram.Total;
|
---|
33 | int keepSize = histogram.NumElements;
|
---|
34 |
|
---|
35 | double cutTotal = 0;
|
---|
36 | double cutVariance = 0;
|
---|
37 | int cutSize = 0;
|
---|
38 |
|
---|
39 | double bucketTotal, bucketVariance, bucketSize, bucketMean;
|
---|
40 | int cutPointsChecked = 0;
|
---|
41 | while (buckets.MoveNext())
|
---|
42 | {
|
---|
43 | cutPointsChecked++;
|
---|
44 | bucketTotal = buckets.Current.Total;
|
---|
45 | bucketVariance = buckets.Current.Variance;
|
---|
46 | bucketSize = buckets.Current.NumElements;
|
---|
47 | bucketMean = buckets.Current.Mean;
|
---|
48 |
|
---|
49 | keepTotal -= bucketTotal;
|
---|
50 | keepVariance -= bucketVariance + keepSize * bucketSize * Math.Pow(keepTotal / keepSize - bucketMean, 2) / (keepSize + bucketSize);
|
---|
51 | keepSize -= (int)bucketSize;
|
---|
52 |
|
---|
53 | cutTotal += bucketTotal;
|
---|
54 | if (cutSize > 0)
|
---|
55 | cutVariance += bucketVariance + cutSize * bucketSize * Math.Pow(cutTotal / cutSize - bucketMean, 2) / (cutSize + bucketSize);
|
---|
56 | cutSize += (int)bucketSize;
|
---|
57 |
|
---|
58 | if (keepSize >= MinKeepSize && cutSize >= MinCutSize && IsCutPoint(histogram, keepTotal, keepVariance, keepSize, cutTotal, cutVariance, cutSize))
|
---|
59 | {
|
---|
60 | return true;
|
---|
61 | }
|
---|
62 | else if (keepSize < MinKeepSize)
|
---|
63 | {
|
---|
64 | return false;
|
---|
65 | }
|
---|
66 | else if (cutPointsChecked == numCutPointsToCheck)
|
---|
67 | {
|
---|
68 | return false;
|
---|
69 | }
|
---|
70 | }
|
---|
71 | return false;
|
---|
72 | }
|
---|
73 |
|
---|
74 | private bool IsCutPoint(Histogram histogram, double keepTotal, double keepVariance, int keepSize, double cutTotal, double cutVariance, int cutSize)
|
---|
75 | {
|
---|
76 | double absMeanDifference = Math.Abs(keepTotal / keepSize - cutTotal / cutSize);
|
---|
77 | double dd = Math.Log(2.0 * Math.Log(histogram.NumElements) / Delta);
|
---|
78 | double m = 1.0 / (keepSize - MinKeepSize + 3) + 1.0 / (cutSize - MinCutSize + 3);
|
---|
79 | double epsilon = Math.Sqrt(2.0 * m * (histogram.Variance / histogram.NumElements) * dd) + 2.0 / 3.0 * dd * m;
|
---|
80 | return absMeanDifference > epsilon;
|
---|
81 | }
|
---|
82 |
|
---|
83 | public abstract bool Execute(ref Histogram histogram);
|
---|
84 | public abstract void Terminate();
|
---|
85 | }
|
---|
86 | }
|
---|