edu.cmu.tetrad.algcomparison.statistic.NodesInCyclesPrecision Maven / Gradle / Ivy
The 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.io.Serial;
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
* @version $Id: $Id
*/
public class NodesInCyclesPrecision implements Statistic {
@Serial
private static final long serialVersionUID = 23L;
/**
* Constructs a new instance of the statistic.
*/
public NodesInCyclesPrecision() {
}
/**
* {@inheritDoc}
*/
@Override
public String getAbbreviation() {
return "NICP";
}
/**
* {@inheritDoc}
*/
@Override
public String getDescription() {
return "Node in cycle precision";
}
/**
* {@inheritDoc}
*/
@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().existsDirectedPath(x, x)) {
inCycle.add(x);
}
}
return inCycle;
}
/**
* {@inheritDoc}
*/
@Override
public double getNormValue(double value) {
return value;
}
}