-
Notifications
You must be signed in to change notification settings - Fork 0
/
DUD.java
108 lines (96 loc) · 4.79 KB
/
DUD.java
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
import java.util.ArrayList ;
public class DUD {
private static int INCREMENT = 1 ;
private static double IMPOSSIBLE = -1000.0 ;
public static double[] logOddsOfUnmodeledUnsorted(int minimumBaseline, int wait, int monitor, Data data, Features features) {
Window baselineWindow, monitorWindow ;
double[] result = new double[data.numberOfDays()] ;
for (int c=0 ; c<(minimumBaseline+wait+monitor) ; c++) { result[c] = IMPOSSIBLE ; }
for (int c=(minimumBaseline+wait+monitor) ; c<data.numberOfDays() ; c++) {
baselineWindow = new Window(0, c-(wait+monitor), data, features) ;
monitorWindow = new Window(c-monitor, monitor, data, features) ;
result[c] = oddsUnmodeled(baselineWindow, monitorWindow, features).log() ;
}
return result ;
}
public static double[] logOddsOfUnmodeledBaselineSort(int minimumBaseline, int wait, int monitor, Data data, Features features, double fraction) {
Window calibrationWindow, baselineWindow, monitorWindow ;
double[] result = new double[data.numberOfDays()] ;
for (int c=0 ; c<(minimumBaseline+wait+monitor) ; c++) { result[c] = IMPOSSIBLE ; }
for (int c=(minimumBaseline+wait+monitor) ; c<data.numberOfDays() ; c++) {
calibrationWindow = new Window(0, c-(wait+monitor), data, features) ;
baselineWindow = new Window(0, c-(wait+monitor), data, features, calibrationWindow, fraction) ;
monitorWindow = new Window(c-monitor, monitor, data, features, calibrationWindow, fraction) ;
result[c] = oddsUnmodeled(baselineWindow, monitorWindow, features).log() ;
}
return result ;
}
public static double[] logOddsOfUnmodeledSeparateSort(int minimumBaseline, int wait, int monitor, Data data, Features features, double fraction) {
Window calibrationWindow, baselineWindow, monitorWindow ;
double[] result = new double[data.numberOfDays()] ;
for (int c=0 ; c<(minimumBaseline+wait+monitor) ; c++) { result[c] = IMPOSSIBLE ; }
for (int c=(minimumBaseline+wait+monitor) ; c<data.numberOfDays() ; c++) {
calibrationWindow = new Window(0, c-(wait+monitor), data, features) ;
baselineWindow = new Window(0, c-(wait+monitor), data, features, calibrationWindow, fraction) ;
calibrationWindow = new Window(c-monitor, monitor, data, features) ;
monitorWindow = new Window(c-monitor, monitor, data, features, calibrationWindow, fraction) ;
result[c] = oddsUnmodeled(baselineWindow, monitorWindow, features).log() ;
}
return result ;
}
private static Precise likelihoodModeled(Window baselineWindow, Window monitorWindow, int feature) {
double theta ;
int k, N ;
theta = baselineWindow.theta(feature) ;
N = monitorWindow.total() ;
k = monitorWindow.count(feature) ;
return Precise.binomial(N,k,theta) ;
}
private static Precise likelihoodModeled(Window baselineWindow, Window monitorWindow, Features features) {
Precise result = new Precise(1.0) ;
for (int feature=0 ; feature<features.numberOfFeatures() ; feature++) {
if ( !features.isLegal(feature) ) continue ;
result = result.multiply( likelihoodModeled(baselineWindow,monitorWindow,feature) ) ;
}
return result ;
}
private static Precise probabilityOfFeatureCount(int k, int m, int u, double thetaM) {
Precise result = new Precise(0.0), a, b ;
for (int i=0 ; i<=k ; i++) {
a = Precise.binomial(m,i,thetaM) ;
if ( (k-i)<=u ) b = new Precise( 1.0 / (u+1.0) ) ; else b = new Precise(0.0) ;
result = result.add( a.multiply(b) ) ;
}
return result ;
}
private static Precise likelihoodUnmodeled(Window baselineWindow, Window monitorWindow, int m, int u, Features features) {
Precise result = new Precise(1.0) ;
int k ;
double thetaM ;
for (int feature=0 ; feature<features.numberOfFeatures() ; feature++) {
if ( !features.isLegal(feature) ) continue ;
k = monitorWindow.count(feature) ;
thetaM = baselineWindow.theta(feature) ;
result = result.multiply( probabilityOfFeatureCount(k,m,u,thetaM) ) ;
}
return result ;
}
private static Precise likelihoodUnmodeled(Window baselineWindow, Window monitorWindow, Features features) {
Precise current, result ;
int intervals, m ;
result = new Precise(0.0) ;
intervals = 0 ;
for (int u=1 ; u<=monitorWindow.total() ; u+=INCREMENT) {
m = monitorWindow.total()-u ;
current = likelihoodUnmodeled(baselineWindow, monitorWindow, m, u, features) ;
result = result.add( current ) ;
intervals++ ;
}
return result.divide(new Precise(intervals)) ;
}
private static Precise oddsUnmodeled(Window baselineWindow, Window monitorWindow, Features features) {
Precise numerator = likelihoodUnmodeled(baselineWindow, monitorWindow, features) ;
Precise denominator = likelihoodModeled(baselineWindow, monitorWindow, features) ;
return numerator.divide(denominator) ;
}
}