All Downloads are FREE. Search and download functionalities are using the official Maven repository.

edu.cmu.tetrad.algcomparison.statistic.NodesInCyclesPrecision Maven / Gradle / Ivy

There is a newer version: 7.6.5
Show newest version
package edu.cmu.tetrad.algcomparison.statistic;

import edu.cmu.tetrad.data.DataModel;
import edu.cmu.tetrad.graph.Graph;
import edu.cmu.tetrad.graph.GraphUtils;
import edu.cmu.tetrad.graph.Node;

import java.util.HashSet;
import java.util.Set;

/**
 * The adjacency precision. The true positives are the number of adjacencies in both the true and estimated graphs.
 *
 * @author josephramsey
 */
public class NodesInCyclesPrecision implements Statistic {
    private static final long serialVersionUID = 23L;

    @Override
    public String getAbbreviation() {
        return "NICP";
    }

    @Override
    public String getDescription() {
        return "Node in cycle precision";
    }

    @Override
    public double getValue(Graph trueGraph, Graph estGraph, DataModel dataModel) {
        trueGraph = GraphUtils.replaceNodes(trueGraph, estGraph.getNodes());

        Set inTrue = getNodesInCycles(trueGraph);
        Set inEst = getNodesInCycles(estGraph);

        Set tp = new HashSet<>(inTrue);
        tp.retainAll(inEst);

        Set fp = new HashSet<>(inEst);
        fp.removeAll(inTrue);

        return tp.size() / (double) (tp.size() + fp.size());
    }

    private Set getNodesInCycles(Graph graph) {
        Set inCycle = new HashSet<>();

        for (Node x : graph.getNodes()) {
            if (graph.paths().existsDirectedPathFromTo(x, x)) {
                inCycle.add(x);
            }
        }

        return inCycle;
    }

    @Override
    public double getNormValue(double value) {
        return value;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy