-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindow.java
More file actions
74 lines (62 loc) · 2.65 KB
/
Copy pathWindow.java
File metadata and controls
74 lines (62 loc) · 2.65 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
import java.util.ArrayList ;
import java.util.Arrays ;
public class Window {
public static double SMOOTHING = 1.0 ;
private int start, window, total ;
private Data data ;
private Features features ;
private int[] counts ;
public Window(int start, int window, Data data, Features features) {
this.start = start ;
this.window = window ;
this.data = data ;
this.features = features ;
this.counts = new int[features.numberOfFeatures()] ;
this.total = data.numberOfPatients(start,window) ;
for (int f=0 ; f<features.numberOfFeatures() ; f++) {
counts[f] = data.numberOfPatientsWithFeature(start,window,f) ;
}
}
public int numberOfFeatures() { return features.numberOfFeatures() ; }
public int count(int feature) { return counts[feature] ; }
public double theta(int feature) { return (counts[feature]+SMOOTHING)/(total+(2.0*SMOOTHING)) ; }
public int total() { return total ; }
public Window(int start, int window, Data data, Features features, Window calibrationWindow, double fraction) {
this.start = start ;
this.window = window ;
this.data = data ;
this.features = features ;
this.counts = new int[features.numberOfFeatures()] ;
this.total = data.numberOfPatients(start,window) ;
ArrayList<Double> likelihoods = new ArrayList<Double>() ;
for (int day=start ; day<(start+window) ; day++) {
for (int patient=0 ; patient<data.numberOfPatients(day) ; patient++) {
likelihoods.add( likelihood(data, day, patient,calibrationWindow) ) ;
}
}
Double[] likelihoodsArray = new Double[ likelihoods.size() ] ;
likelihoodsArray = likelihoods.toArray(likelihoodsArray) ;
Arrays.sort( likelihoodsArray ) ;
double cutoff = likelihoodsArray[ ((int)( fraction*likelihoodsArray.length )) - 1 ] ;
for (int day=start ; day<(start+window) ; day++) {
for (int patient=0 ; patient<data.numberOfPatients(day) ; patient++) {
if ( likelihood(data, day, patient, calibrationWindow)<=cutoff ) {
for (int feature=0 ; feature<features.numberOfFeatures() ; feature++) {
if ( data.patientHasFeature(day, patient, feature) ) counts[feature]++ ;
}
this.total++ ;
}
}
}
}
private double likelihood(Data data, int day, int patient, Window calibrationWindow) {
double likelihood = 1.0 ;
for (int feature=0 ; feature<numberOfFeatures() ; feature++) {
if ( data.patientHasFeature(day, patient, feature) && features.isLegal(feature) ) {
likelihood *= calibrationWindow.theta(feature) ;
}
else likelihood *= (1.0 - calibrationWindow.theta(feature) ) ;
}
return likelihood ;
}
}