use of de.lmu.ifi.dbs.elki.database.ids.HashSetModifiableDBIDs in project elki by elki-project.
the class PrecisionAtKEvaluationTest method testPrecisionAtK.
/**
* Test Precision@k score computation.
*/
@Test
public void testPrecisionAtK() {
HashSetModifiableDBIDs positive = DBIDUtil.newHashSet();
positive.add(DBIDUtil.importInteger(1));
positive.add(DBIDUtil.importInteger(2));
positive.add(DBIDUtil.importInteger(3));
positive.add(DBIDUtil.importInteger(4));
positive.add(DBIDUtil.importInteger(5));
final ModifiableDoubleDBIDList distances = DBIDUtil.newDistanceDBIDList();
// Precision: 1.0
distances.add(0.0, DBIDUtil.importInteger(1));
// Precision: 1.0
distances.add(1.0, DBIDUtil.importInteger(2));
//
distances.add(2.0, DBIDUtil.importInteger(6));
//
distances.add(3.0, DBIDUtil.importInteger(7));
// Precision: 0.6
distances.add(3.0, DBIDUtil.importInteger(3));
//
distances.add(4.0, DBIDUtil.importInteger(8));
// Precision: 4/7.
distances.add(4.0, DBIDUtil.importInteger(4));
//
distances.add(5.0, DBIDUtil.importInteger(9));
// Precision: 5/9.
distances.add(6.0, DBIDUtil.importInteger(5));
// (1+1+.6+4/7.+5/9.)/5 = 0.7453968253968254
double[] precision = new double[] { 1., 1., 2. / 3., 2.5 / 4., 3 / 5., 3.5 / 6., 4. / 7., 4. / 8., 5 / 9. };
for (int k = 0; k < precision.length; ++k) {
double pk = new PrecisionAtKEvaluation(k + 1).evaluate(positive, distances);
assertEquals("Precision at k=" + (k + 1) + " not correct.", precision[k], pk, 1e-14);
}
}
Aggregations