Search in sources :

Example 1 with XYCurve

use of de.lmu.ifi.dbs.elki.math.geometry.XYCurve in project elki by elki-project.

the class ROCEvaluationTest method testROCCurve.

/**
 * Test ROC curve generation, including curve simplification
 */
@Test
public void testROCCurve() {
    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();
    // Starting point: ................................ 0.0,0. ++
    // + 0.0,.2 -- redundant
    distances.add(0.0, DBIDUtil.importInteger(1));
    // + 0.0,.4 ++
    distances.add(1.0, DBIDUtil.importInteger(2));
    // - .25,.4 ++
    distances.add(2.0, DBIDUtil.importInteger(6));
    // -
    distances.add(3.0, DBIDUtil.importInteger(7));
    // + .50,.6 -- redundant
    distances.add(3.0, DBIDUtil.importInteger(3));
    // -
    distances.add(4.0, DBIDUtil.importInteger(8));
    // + .75,.8 ++
    distances.add(4.0, DBIDUtil.importInteger(4));
    // - 1.0,.8 ++
    distances.add(5.0, DBIDUtil.importInteger(9));
    // + 1.0,1. ++
    distances.add(6.0, DBIDUtil.importInteger(5));
    XYCurve roccurve = ROCEvaluation.materializeROC(new DBIDsTest(positive), new DistanceResultAdapter(distances.iter()));
    // System.err.println(roccurve);
    assertEquals("ROC curve too complex", 6, roccurve.size());
    double auc = XYCurve.areaUnderCurve(roccurve);
    assertEquals("ROC AUC (curve) not correct.", 0.6, auc, 1e-14);
    double auc2 = new ROCEvaluation().evaluate(positive, distances);
    assertEquals("ROC AUC (direct) not correct.", 0.6, auc2, 1e-14);
}
Also used : HashSetModifiableDBIDs(de.lmu.ifi.dbs.elki.database.ids.HashSetModifiableDBIDs) XYCurve(de.lmu.ifi.dbs.elki.math.geometry.XYCurve) ModifiableDoubleDBIDList(de.lmu.ifi.dbs.elki.database.ids.ModifiableDoubleDBIDList) DBIDsTest(de.lmu.ifi.dbs.elki.evaluation.scores.adapter.DBIDsTest) DistanceResultAdapter(de.lmu.ifi.dbs.elki.evaluation.scores.adapter.DistanceResultAdapter) Test(org.junit.Test) DBIDsTest(de.lmu.ifi.dbs.elki.evaluation.scores.adapter.DBIDsTest)

Example 2 with XYCurve

use of de.lmu.ifi.dbs.elki.math.geometry.XYCurve in project elki by elki-project.

the class OutlierROCCurve method computeROCResult.

private ROCResult computeROCResult(int size, SetDBIDs positiveids, OutlierResult or) {
    XYCurve roccurve = ROCEvaluation.materializeROC(new DBIDsTest(positiveids), new OutlierScoreAdapter(or));
    double rocauc = XYCurve.areaUnderCurve(roccurve);
    return new ROCResult(roccurve, rocauc);
}
Also used : XYCurve(de.lmu.ifi.dbs.elki.math.geometry.XYCurve) OutlierScoreAdapter(de.lmu.ifi.dbs.elki.evaluation.scores.adapter.OutlierScoreAdapter) DBIDsTest(de.lmu.ifi.dbs.elki.evaluation.scores.adapter.DBIDsTest)

Example 3 with XYCurve

use of de.lmu.ifi.dbs.elki.math.geometry.XYCurve in project elki by elki-project.

the class OutlierPrecisionAtKCurve method computePrecisionResult.

private XYCurve computePrecisionResult(int size, SetDBIDs positiveids, DBIDs order) {
    if (order.size() != size) {
        throw new IllegalStateException("Iterable result doesn't match database size - incomplete ordering?");
    }
    int lastk = Math.min(size, maxk);
    XYCurve curve = new PrecisionAtKCurve("k", "Precision", lastk);
    int pos = 0;
    DBIDIter i = order.iter();
    for (int k = 1; k <= lastk; k++, i.advance()) {
        if (positiveids.contains(i)) {
            pos++;
        }
        curve.addAndSimplify(k, pos / (double) k);
    }
    return curve;
}
Also used : XYCurve(de.lmu.ifi.dbs.elki.math.geometry.XYCurve) DBIDIter(de.lmu.ifi.dbs.elki.database.ids.DBIDIter)

Example 4 with XYCurve

use of de.lmu.ifi.dbs.elki.math.geometry.XYCurve in project elki by elki-project.

the class XYCurveVisualization method makeVisualization.

@Override
public Visualization makeVisualization(VisualizerContext context, VisualizationTask task, VisualizationPlot plot, double width, double height, Projection proj) {
    XYCurve curve = task.getResult();
    setupCSS(context, plot);
    final StyleLibrary style = context.getStyleLibrary();
    final double sizex = StyleLibrary.SCALE;
    final double sizey = StyleLibrary.SCALE * height / width;
    final double margin = style.getSize(StyleLibrary.MARGIN);
    Element layer = SVGUtil.svgElement(plot.getDocument(), SVGConstants.SVG_G_TAG);
    final String transform = SVGUtil.makeMarginTransform(width, height, sizex, sizey, margin);
    SVGUtil.setAtt(layer, SVGConstants.SVG_TRANSFORM_ATTRIBUTE, transform);
    // determine scaling
    LinearScale scalex = new LinearScale(curve.getMinx(), curve.getMaxx());
    LinearScale scaley = new LinearScale(curve.getMiny(), curve.getMaxy());
    // plot the line
    SVGPath path = new SVGPath();
    for (XYCurve.Itr iterator = curve.iterator(); iterator.valid(); iterator.advance()) {
        final double x = scalex.getScaled(iterator.getX());
        final double y = 1 - scaley.getScaled(iterator.getY());
        path.drawTo(sizex * x, sizey * y);
    }
    Element line = path.makeElement(plot);
    line.setAttribute(SVGConstants.SVG_CLASS_ATTRIBUTE, SERIESID);
    // add axes
    try {
        SVGSimpleLinearAxis.drawAxis(plot, layer, scaley, 0, sizey, 0, 0, SVGSimpleLinearAxis.LabelStyle.LEFTHAND, style);
        SVGSimpleLinearAxis.drawAxis(plot, layer, scalex, 0, sizey, sizex, sizey, SVGSimpleLinearAxis.LabelStyle.RIGHTHAND, style);
    } catch (CSSNamingConflict e) {
        LoggingUtil.exception(e);
    }
    // Add axis labels
    {
        Element labelx = plot.svgText(sizex * .5, sizey + margin * .9, curve.getLabelx());
        SVGUtil.setCSSClass(labelx, CSS_AXIS_LABEL);
        layer.appendChild(labelx);
        Element labely = plot.svgText(margin * -.8, sizey * .5, curve.getLabely());
        SVGUtil.setCSSClass(labely, CSS_AXIS_LABEL);
        SVGUtil.setAtt(labely, SVGConstants.SVG_TRANSFORM_ATTRIBUTE, "rotate(-90," + FormatUtil.NF6.format(margin * -.8) + "," + FormatUtil.NF6.format(sizey * .5) + ")");
        layer.appendChild(labely);
    }
    // Add AUC value when found
    if (curve instanceof ROCResult) {
        double rocauc = ((ROCResult) curve).getAUC();
        String lt = OutlierROCCurve.ROCAUC_LABEL + ": " + FormatUtil.NF.format(rocauc);
        if (rocauc <= 0.5) {
            Element auclbl = plot.svgText(sizex * 0.5, sizey * 0.10, lt);
            SVGUtil.setCSSClass(auclbl, CSS_AXIS_LABEL);
            layer.appendChild(auclbl);
        } else {
            Element auclbl = plot.svgText(sizex * 0.5, sizey * 0.95, lt);
            SVGUtil.setCSSClass(auclbl, CSS_AXIS_LABEL);
            layer.appendChild(auclbl);
        }
    }
    if (curve instanceof PRCurve) {
        double prauc = ((PRCurve) curve).getAUC();
        String lt = OutlierPrecisionRecallCurve.PRAUC_LABEL + ": " + FormatUtil.NF.format(prauc);
        if (prauc <= 0.5) {
            Element auclbl = plot.svgText(sizex * 0.5, sizey * 0.10, lt);
            SVGUtil.setCSSClass(auclbl, CSS_AXIS_LABEL);
            layer.appendChild(auclbl);
        } else {
            Element auclbl = plot.svgText(sizex * 0.5, sizey * 0.95, lt);
            SVGUtil.setCSSClass(auclbl, CSS_AXIS_LABEL);
            layer.appendChild(auclbl);
        }
    }
    layer.appendChild(line);
    return new StaticVisualizationInstance(context, task, plot, width, height, layer);
}
Also used : LinearScale(de.lmu.ifi.dbs.elki.math.scales.LinearScale) XYCurve(de.lmu.ifi.dbs.elki.math.geometry.XYCurve) CSSNamingConflict(de.lmu.ifi.dbs.elki.visualization.css.CSSClassManager.CSSNamingConflict) Element(org.w3c.dom.Element) PRCurve(de.lmu.ifi.dbs.elki.evaluation.outlier.OutlierPrecisionRecallCurve.PRCurve) StyleLibrary(de.lmu.ifi.dbs.elki.visualization.style.StyleLibrary) SVGPath(de.lmu.ifi.dbs.elki.visualization.svg.SVGPath) StaticVisualizationInstance(de.lmu.ifi.dbs.elki.visualization.visualizers.StaticVisualizationInstance) ROCResult(de.lmu.ifi.dbs.elki.evaluation.outlier.OutlierROCCurve.ROCResult)

Example 5 with XYCurve

use of de.lmu.ifi.dbs.elki.math.geometry.XYCurve in project elki by elki-project.

the class OutlierROCCurve method computeROCResult.

private ROCResult computeROCResult(int size, SetDBIDs positiveids, DBIDs order) {
    if (order.size() != size) {
        throw new IllegalStateException("Iterable result doesn't match database size - incomplete ordering?");
    }
    XYCurve roccurve = ROCEvaluation.materializeROC(new DBIDsTest(positiveids), new SimpleAdapter(order.iter()));
    double rocauc = XYCurve.areaUnderCurve(roccurve);
    return new ROCResult(roccurve, rocauc);
}
Also used : XYCurve(de.lmu.ifi.dbs.elki.math.geometry.XYCurve) SimpleAdapter(de.lmu.ifi.dbs.elki.evaluation.scores.adapter.SimpleAdapter) DBIDsTest(de.lmu.ifi.dbs.elki.evaluation.scores.adapter.DBIDsTest)

Aggregations

XYCurve (de.lmu.ifi.dbs.elki.math.geometry.XYCurve)6 DBIDsTest (de.lmu.ifi.dbs.elki.evaluation.scores.adapter.DBIDsTest)3 DBIDIter (de.lmu.ifi.dbs.elki.database.ids.DBIDIter)1 HashSetModifiableDBIDs (de.lmu.ifi.dbs.elki.database.ids.HashSetModifiableDBIDs)1 ModifiableDoubleDBIDList (de.lmu.ifi.dbs.elki.database.ids.ModifiableDoubleDBIDList)1 PRCurve (de.lmu.ifi.dbs.elki.evaluation.outlier.OutlierPrecisionRecallCurve.PRCurve)1 ROCResult (de.lmu.ifi.dbs.elki.evaluation.outlier.OutlierROCCurve.ROCResult)1 DistanceResultAdapter (de.lmu.ifi.dbs.elki.evaluation.scores.adapter.DistanceResultAdapter)1 OutlierScoreAdapter (de.lmu.ifi.dbs.elki.evaluation.scores.adapter.OutlierScoreAdapter)1 SimpleAdapter (de.lmu.ifi.dbs.elki.evaluation.scores.adapter.SimpleAdapter)1 LinearScale (de.lmu.ifi.dbs.elki.math.scales.LinearScale)1 CSSNamingConflict (de.lmu.ifi.dbs.elki.visualization.css.CSSClassManager.CSSNamingConflict)1 StyleLibrary (de.lmu.ifi.dbs.elki.visualization.style.StyleLibrary)1 SVGPath (de.lmu.ifi.dbs.elki.visualization.svg.SVGPath)1 StaticVisualizationInstance (de.lmu.ifi.dbs.elki.visualization.visualizers.StaticVisualizationInstance)1 Test (org.junit.Test)1 Element (org.w3c.dom.Element)1