-
Notifications
You must be signed in to change notification settings - Fork 1
/
snippets.txt
307 lines (252 loc) · 9.93 KB
/
snippets.txt
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
extern crate hedgecut;
use hedgecut::split_stats::{gini_impurity, gini_with_impurity_before};
// fn gini_with_impurity_before(
// num_plus_left: u32,
// num_minus_left: u32,
// num_plus_right: u32,
// num_minus_right: u32,
// ) -> (f64, f64, f64) {
//
//
// let num_plus = num_plus_left + num_plus_right;
// let num_minus = num_minus_left + num_minus_right;
// let num_samples = num_plus + num_minus;
//
// let p_plus = num_plus as f64 / num_samples as f64;
//
// let impurity_before = 1.0 - (p_plus * p_plus);
//
// gini(
// impurity_before,
// num_plus_left,
// num_minus_left,
// num_plus_right,
// num_minus_right,
// )
// }
//
// fn gini_impurity(num_plus: u32, num_samples: u32) -> f64 {
// let p_plus = num_plus as f64 / num_samples as f64;
// 1.0 - (p_plus * p_plus)
// }
//
// fn gini(
// impurity_before: f64,
// num_plus_left: u32,
// num_minus_left: u32,
// num_plus_right: u32,
// num_minus_right: u32
// ) -> (f64, f64, f64) {
//
// let num_samples_left = num_plus_left + num_minus_left;
// let num_samples_right = num_plus_right + num_minus_right;
//
// if num_samples_left == 0 || num_samples_right == 0 {
// return (0.0, 0.0, 0.0);
// }
//
// let gini_left = gini_impurity(num_plus_left, num_samples_left);
// let gini_right = gini_impurity(num_plus_right, num_samples_right);
//
// let num_samples = num_samples_left + num_samples_right;
//
// let score = impurity_before -
// (num_samples_left as f64 / num_samples as f64) * gini_left -
// (num_samples_right as f64 / num_samples as f64) * gini_right;
//
// if score.is_nan() {
// println!("[{},{},{},{}]", num_plus_left, num_minus_left, num_plus_right, num_minus_right);
// println!("{}, {}, {}, {}", score, gini_left, gini_right, impurity_before);
// panic!("Invalid score encountered!");
// }
//
// (score, gini_left, gini_right)
// }
//
// fn gini_book(p1: f64, p2: f64) -> f64 {
// p1 * (1.0 - p1) + p2 * (1.0 - p2)
// }
//
// fn gini_opt(p1: f64) -> f64 {
// 2.0 * p1 * (1.0 - p1)
// }
fn gini_dbg(
num_plus_left: u32,
num_minus_left: u32,
num_plus_right: u32,
num_minus_right: u32
) {
let num_samples_left = num_plus_left + num_minus_left;
let num_samples_right = num_plus_right + num_minus_right;
let num_plus = num_plus_left + num_plus_right;
let num_samples = num_samples_left + num_samples_right;
let gini_before = gini_impurity(num_plus, num_samples);
let gini_left = gini_impurity(num_plus_left, num_samples_left);
let gini_right = gini_impurity(num_plus_right, num_samples_right);
let weight_left = num_samples_left as f64 / num_samples as f64;
let weight_right = num_samples_right as f64 / num_samples as f64;
let score = gini_before - weight_left * gini_left - weight_right * gini_right;
//println!("{} = {} - {} * {} - {} * {}", score, gini_before, weight_left, gini_left,
// weight_right, gini_right);
println!("{} = {} - {} - {} ", score, gini_before, weight_left * gini_left,
weight_right * gini_right);
}
fn main() {
// // TODO this should be a unit test
// println!("{} -- {} -- {}", gini_book(0.5, 0.5), gini_opt(0.5), gini_impurity(2, 4));
// println!("{} -- {} -- {}", gini_book(0.75, 0.25), gini_opt(0.75), gini_impurity(3, 4));
// println!("{} -- {} -- {}", gini_book(0.75, 0.25), gini_opt(0.25), gini_impurity(1, 4));
// println!("{} -- {} -- {}", gini_book(0.0, 1.0), gini_opt(0.0), gini_impurity(0, 4));
// println!("{} -- {} -- {}", gini_book(0.0, 1.0), gini_opt(1.0), gini_impurity(4, 4));
// gini_dbg(4, 0, 0, 4);
//3+ 2-
gini_dbg(1, 1, 2, 1);
gini_dbg(2, 2, 1, 0);
println!("-------------");
gini_dbg(0, 1, 2, 1);
gini_dbg(2, 2, 0, 0);
}
extern crate hedgecut;
use hedgecut::split_stats::{SplitStats};
#[derive(Eq,PartialEq,Debug)]
struct Step {
label: bool,
passes_first: bool,
passes_second: bool,
}
fn weaken_split_dbg(
current_champion_split_stats: &SplitStats,
current_runnerup_split_stats: &SplitStats,
) -> Vec<(Step, SplitStats, SplitStats)> {
let truefalse = [true, false];
let mut enumerated = Vec::new();
for is_plus in truefalse.iter() {
for passes_first in truefalse.iter() {
for passes_second in truefalse.iter() {
let mut champion_stats = current_champion_split_stats.clone();
let mut runnerup_stats = current_runnerup_split_stats.clone();
if *is_plus && *passes_first && *passes_second {
if champion_stats.num_plus_left != 0 && runnerup_stats.num_plus_left != 0 {
champion_stats.num_plus_left -= 1;
runnerup_stats.num_plus_left -= 1;
} else {
continue;
}
} else if *is_plus && !*passes_first && *passes_second {
if champion_stats.num_plus_right != 0 && runnerup_stats.num_plus_left != 0 {
champion_stats.num_plus_right -= 1;
runnerup_stats.num_plus_left -= 1;
} else {
continue;
}
} else if *is_plus && *passes_first && !*passes_second {
if champion_stats.num_plus_left != 0 && runnerup_stats.num_plus_right != 0 {
champion_stats.num_plus_left -= 1;
runnerup_stats.num_plus_right -= 1;
} else {
continue;
}
} else if *is_plus && !*passes_first && !*passes_second {
if champion_stats.num_plus_right != 0 && runnerup_stats.num_plus_right != 0 {
champion_stats.num_plus_right -= 1;
runnerup_stats.num_plus_right -= 1;
} else {
continue;
}
} else if !*is_plus && *passes_first && *passes_second {
if champion_stats.num_minus_left != 0 && runnerup_stats.num_minus_left != 0 {
champion_stats.num_minus_left -= 1;
runnerup_stats.num_minus_left -= 1;
} else {
continue;
}
} else if !*is_plus && !*passes_first && *passes_second {
if champion_stats.num_minus_right != 0 && runnerup_stats.num_minus_left != 0 {
champion_stats.num_minus_right -= 1;
runnerup_stats.num_minus_left -= 1;
} else {
continue;
}
} else if !*is_plus && *passes_first && !*passes_second {
if champion_stats.num_minus_left != 0 && runnerup_stats.num_minus_right != 0 {
champion_stats.num_minus_left -= 1;
runnerup_stats.num_minus_right -= 1;
} else {
continue;
}
} else if !*is_plus && !*passes_first && !*passes_second {
if champion_stats.num_minus_right != 0 && runnerup_stats.num_minus_right != 0 {
champion_stats.num_minus_right -= 1;
runnerup_stats.num_minus_right -= 1;
} else {
continue;
}
}
champion_stats.update_score_and_impurity_before();
runnerup_stats.update_score_and_impurity_before();
let step = Step {
label: *is_plus,
passes_first: *passes_first,
passes_second: *passes_second
};
enumerated.push((step, champion_stats, runnerup_stats));
}
}
}
enumerated
}
fn evaluate3(s: &mut SplitStats, t: &mut SplitStats) {
s.update_score_and_impurity_before();
t.update_score_and_impurity_before();
let mut diffs1 = Vec::new();
let mut diffs2 = Vec::new();
let mut diffs3 = Vec::new();
let mut diff_paths = Vec::new();
let enumerated = weaken_split_dbg(&s, &t);
for (_step, s_hat, t_hat) in enumerated {
let score_diff = s_hat.score - t_hat.score;
let enumerated_again = weaken_split_dbg(&s_hat, &t_hat);
diffs1.push(score_diff);
for (_step, s_hat, t_hat) in enumerated_again {
let score_diff_again = s_hat.score - t_hat.score;
diffs2.push(score_diff_again);
let enumerated_again_again = weaken_split_dbg(&s_hat, &t_hat);
for (_step, s_hat, t_hat) in enumerated_again_again {
let score_diff_again_again = s_hat.score - t_hat.score;
diffs3.push(score_diff_again_again);
//println!("{},{},{}", score_diff, score_diff_again, score_diff_again_again);
diff_paths.push((score_diff, score_diff_again, score_diff_again_again));
}
}
}
//println!("---------------------------------------------------------------------------");
let mins_path = (
*diffs1.iter().min().unwrap(),
*diffs2.iter().min().unwrap(),
*diffs3.iter().min().unwrap()
);
let found = diff_paths.contains(&mins_path);
println!("{}: {:?}", found, mins_path);
}
fn main() {
evaluate3(
&mut SplitStats::new(8, 6, 12, 2),
&mut SplitStats::new(10, 3, 10, 3)
);
evaluate3(
&mut SplitStats::new(12, 2, 8, 6),
&mut SplitStats::new(10, 3, 10, 3)
);
evaluate3(
&mut SplitStats::new(7, 5, 13, 3),
&mut SplitStats::new(10, 3, 10, 3)
);
evaluate3(
&mut SplitStats::new(70, 50, 130, 30),
&mut SplitStats::new(100, 30, 100, 30)
);
evaluate3(
&mut SplitStats::new(2, 2, 1, 0),
&mut SplitStats::new(1, 1, 2, 1)
);
}