Skip to content

Commit f8d465d

Browse files
committed
Fix the dropping in OM2 as per Arun
1 parent b655195 commit f8d465d

2 files changed

Lines changed: 34 additions & 13 deletions

File tree

src/main/java/com/yahoo/egads/models/tsmm/OlympicModel2.java

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,27 @@ public WeightedValue(final double value, final int weight) {
201201
public int compareTo(final WeightedValue other) {
202202
return Double.compare(value, other.value);
203203
}
204+
205+
/**
206+
* Drops as many of the highest or lowest values as possible, leaving
207+
* at least one value in the list.
208+
* @param accumulator A non-null accumulator list.
209+
* @param count A count of 1 or more.
210+
* @param highest Drop higher values == true or drop lower values == false.
211+
*/
212+
public static void drop(final List<WeightedValue> accumulator,
213+
final int count, final boolean highest) {
214+
for (int x = 0; x < count; x++) {
215+
if (accumulator.size() <= 1) {
216+
break;
217+
}
218+
if (highest) {
219+
accumulator.remove(Collections.max(accumulator));
220+
} else {
221+
accumulator.remove(Collections.min(accumulator));
222+
}
223+
}
224+
}
204225
}
205226

206227
@Override
@@ -254,13 +275,13 @@ public void train(final DataSequence data) throws Exception {
254275
}
255276
}
256277

257-
if ((drop_lowest > 0 || drop_highest > 0)
258-
&& accumulator.size() > (drop_lowest + drop_highest)) {
259-
for (int x = 0; x < drop_lowest; x++) {
260-
accumulator.remove(Collections.min(accumulator));
261-
}
262-
for (int x = 0; x < drop_highest; x++) {
263-
accumulator.remove(Collections.max(accumulator));
278+
if (drop_lowest > 0 || drop_highest > 0) {
279+
if (drop_highest > drop_lowest) {
280+
WeightedValue.drop(accumulator, drop_highest, true);
281+
WeightedValue.drop(accumulator, drop_lowest, false);
282+
} else {
283+
WeightedValue.drop(accumulator, drop_lowest, false);
284+
WeightedValue.drop(accumulator, drop_highest, true);
264285
}
265286
}
266287

src/test/java/com/yahoo/egads/models/tsmm/TestOlympicModel2.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,7 +1051,7 @@ public void trainDrop() throws Exception {
10511051
assertEquals(1477872300, (long) model.model.get(1).getKey());
10521052
assertEquals(50.0, model.model.get(1).getValue(), 0.00001);
10531053

1054-
// two each - TOO MANY so we don't drop any
1054+
// two each - TOO MANY so we drop as much as possible
10551055
config.setProperty("NUM_TO_DROP_HIGHEST", "2");
10561056
config.setProperty("NUM_TO_DROP_LOWEST", "2");
10571057
model = new OlympicModel2(config);
@@ -1072,11 +1072,11 @@ public void trainDrop() throws Exception {
10721072

10731073
assertEquals(2, model.model.size());
10741074
assertEquals(1477872000, (long) model.model.get(0).getKey());
1075-
assertEquals(25.0, model.model.get(0).getValue(), 0.00001);
1075+
assertEquals(30.0, model.model.get(0).getValue(), 0.00001);
10761076
assertEquals(1477872300, (long) model.model.get(1).getKey());
1077-
assertEquals(47.5, model.model.get(1).getValue(), 0.00001);
1077+
assertEquals(55.0, model.model.get(1).getValue(), 0.00001);
10781078

1079-
// one each - TOO MANY with NaNs so we don't drop any
1079+
// one each - TOO MANY so we drop as much as possible
10801080
config.setProperty("NUM_TO_DROP_HIGHEST", "1");
10811081
config.setProperty("NUM_TO_DROP_LOWEST", "1");
10821082
model = new OlympicModel2(config);
@@ -1097,9 +1097,9 @@ public void trainDrop() throws Exception {
10971097

10981098
assertEquals(2, model.model.size());
10991099
assertEquals(1477872000, (long) model.model.get(0).getKey());
1100-
assertEquals(25.0, model.model.get(0).getValue(), 0.00001);
1100+
assertEquals(40.0, model.model.get(0).getValue(), 0.00001);
11011101
assertEquals(1477872300, (long) model.model.get(1).getKey());
1102-
assertEquals(45.0, model.model.get(1).getValue(), 0.00001);
1102+
assertEquals(65.0, model.model.get(1).getValue(), 0.00001);
11031103
}
11041104

11051105
@Test

0 commit comments

Comments
 (0)