-
Notifications
You must be signed in to change notification settings - Fork 112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Draft]: Calculate local markov blanket against true graph for parted nodes that passed AD Test #1757
Draft
vbcwonderland
wants to merge
6
commits into
development
Choose a base branch
from
vbc-04-07
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
[Draft]: Calculate local markov blanket against true graph for parted nodes that passed AD Test #1757
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0fe7866
partite all nodes into pass or not pass the AndersonDarling Test
vbcwonderland 3443c1d
get markov banket for each node and calculate against true graph for …
vbcwonderland 2355c6a
Accepts and Recalls with process prints
vbcwonderland d3db254
nit
vbcwonderland e7ea86f
nit fix imports
vbcwonderland 49c96b0
MB part done
vbcwonderland File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,10 +2,7 @@ | |
|
||
import edu.cmu.tetrad.data.GeneralAndersonDarlingTest; | ||
import edu.cmu.tetrad.data.Knowledge; | ||
import edu.cmu.tetrad.graph.Graph; | ||
import edu.cmu.tetrad.graph.GraphUtils; | ||
import edu.cmu.tetrad.graph.IndependenceFact; | ||
import edu.cmu.tetrad.graph.Node; | ||
import edu.cmu.tetrad.graph.*; | ||
import edu.cmu.tetrad.search.test.*; | ||
import edu.cmu.tetrad.util.SublistGenerator; | ||
import edu.cmu.tetrad.util.TetradLogger; | ||
|
@@ -251,6 +248,119 @@ public Double checkAgainstAndersonDarlingTest(List<Double> pValues) { | |
return generalAndersonDarlingTest.getP(); | ||
} | ||
|
||
public List<List<Node>> getAndersonDarlingTestAcceptsRejectsNodesForAllNodes(IndependenceTest independenceTest, Graph graph, Double threshold) { | ||
// when calling, default reject null as <=0.05 | ||
List<List<Node>> accepts_rejects = new ArrayList<>(); | ||
List<Node> accepts = new ArrayList<>(); | ||
List<Node> rejects = new ArrayList<>(); | ||
List<Node> allNodes = graph.getNodes(); | ||
for (Node x : allNodes) { | ||
List<IndependenceFact> localIndependenceFacts = getLocalIndependenceFacts(x); | ||
List<Double> localPValues = getLocalPValues(independenceTest, localIndependenceFacts); | ||
Double ADTest = checkAgainstAndersonDarlingTest(localPValues); | ||
if (ADTest <= threshold) { | ||
rejects.add(x); | ||
} else { | ||
accepts.add(x); | ||
} | ||
} | ||
accepts_rejects.add(accepts); | ||
accepts_rejects.add(rejects); | ||
return accepts_rejects; | ||
} | ||
|
||
private Graph getMarkovBlanketSubgraph(Graph graph, Node targetNode) { | ||
EdgeListGraph g = new EdgeListGraph(graph); | ||
Set<Node> mbNodes = GraphUtils.markovBlanket(targetNode, g); | ||
mbNodes.add(targetNode); | ||
return g.subgraph(new ArrayList<>(mbNodes)); | ||
} | ||
|
||
|
||
|
||
public Double getPrecisionOrRecallOnMarkovBlanketGraph(Node x, Graph estimatedGraph, Graph trueGraph, boolean getPrecision) { | ||
List<Node> singleNode = Arrays.asList(x); | ||
// Lookup graph is the same structure as trueGraph's structure but node objects replaced by estimated graph nodes. | ||
Graph lookupGraph = GraphUtils.replaceNodes(trueGraph, estimatedGraph.getNodes()); | ||
System.out.println("@@@@@@@@@@@@@@@@"); | ||
System.out.println("Node: " + x); | ||
System.out.println("True Graph:" + trueGraph); | ||
System.out.println("LookupGraph:" + lookupGraph); // print should look the same as the true graph | ||
|
||
// TODO VBC: The Trim method is the most accurate in terms of all nodes and the edges | ||
Graph RecommendedxMBLookupGraph = getMarkovBlanketSubgraph(lookupGraph, x); // Recommended, not working | ||
Graph xMBLookupGraph = GraphUtils.markovBlanketSubgraph(x, lookupGraph); // TODO VBC: this one should include the target node | ||
Graph TrimxMBLookupGraph = GraphUtils.trimGraph(singleNode, lookupGraph, 3); // Best | ||
Set<Edge> xMBLookupGraphEdges = xMBLookupGraph.getEdges(); | ||
|
||
System.out.println("xMBLookupGraphEdges size: " + xMBLookupGraphEdges.size()); | ||
System.out.println("xMBLookupGraph Nodes size: " + xMBLookupGraph.getNodes().size()); | ||
System.out.println("xMBLookupGraph:" + xMBLookupGraph); // The MB trim of the lookup graph, so it should be a subset of the lookup graph print | ||
System.out.println("RecommendedxMBLookupGraph:" + RecommendedxMBLookupGraph); // The MB trim of the lookup graph, so it should be a subset of the lookup graph print | ||
System.out.println("TrimxMBLookupGraph:" + TrimxMBLookupGraph); // The MB trim of the lookup graph, so it should be a subset of the lookup graph print | ||
|
||
// Get Markov Blanket Subgraph for this node x. | ||
// Graph xMBEstimatedGraph = getMarkovBlanketSubgraph(estimatedGraph, x); | ||
Graph xMBEstimatedGraph = GraphUtils.markovBlanketSubgraph(x, estimatedGraph); | ||
// Graph xMBEstimatedGraph = GraphUtils.trimGraph(singleNode, estimatedGraph, 3); | ||
Set<Edge> xMBEstimatedGraphEdges = xMBEstimatedGraph.getEdges(); | ||
System.out.println("xMBEstimatedGraphEdges size: " + xMBEstimatedGraphEdges.size()); | ||
System.out.println("xMBEstimatedGraph Nodes size: " + xMBEstimatedGraph.getNodes().size()); | ||
System.out.println("xMBEstimatedGraph:" + xMBEstimatedGraph); // This should be compared with the xMBLookupGraph | ||
System.out.println("@@@@@@@@@@@@@@@@"); | ||
|
||
HashSet<Edge> truePositive = new HashSet<>(); | ||
HashSet<Edge> falsePositive = new HashSet<>(); | ||
HashSet<Edge> falseNegative = new HashSet<>(); | ||
Set<Edge> trueGraphEdgesEdges = trueGraph.getEdges(); | ||
Set<Edge> estimatedGraphEdgesEdges = estimatedGraph.getEdges(); | ||
if (trueGraphEdgesEdges != null && estimatedGraphEdgesEdges != null) { | ||
for (Edge te: trueGraphEdgesEdges) { | ||
for (Edge ee: estimatedGraphEdgesEdges) { | ||
// True Graph's Edge info | ||
Node teNode1 = te.getNode1(); | ||
Node teNode2 = te.getNode1(); | ||
Endpoint teEndpoint1 = te.getEndpoint1(); | ||
Endpoint teEndpoint2 = te.getEndpoint2(); | ||
// Estimated Graph's Edge info | ||
Node eeNode1 = te.getNode1(); | ||
Node eeNode2 = te.getNode1(); | ||
Endpoint eeEndpoint1 = ee.getEndpoint1(); | ||
Endpoint eeEndpoint2 = ee.getEndpoint2(); | ||
boolean isSameNode1 = areSame(teNode1, eeNode1); | ||
boolean isSameNode2 = areSame(teNode2, eeNode2); | ||
|
||
// EdgeTypeProbability.EdgeType teType = te.getEdgeTypeProbabilities().getFirst().getEdgeType(); | ||
|
||
// If both n1 n2 are the same, compare the endpoint1 endpoint2 | ||
if (isSameNode1 && isSameNode2) { | ||
// if (teEndpoint1.compareTo(eeEndpoint1)) | ||
// QUESTION: // TODO VBC: seems Edge#compareTo() only comparing the node itself not the endpoint? | ||
// QUESTION: do we only care about edge type here? | ||
|
||
} | ||
|
||
|
||
|
||
} | ||
} | ||
} | ||
// TODO VBC: | ||
// Logic of comparing true graph with estimated graph | ||
|
||
double precision = (double) truePositive.size() / (truePositive.size() + falsePositive.size()); | ||
double recall = (double) truePositive.size() / (truePositive.size() + falseNegative.size()); | ||
return getPrecision ? precision : recall; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These outprints seems correct to me, maybe you want a second check : ) |
||
} | ||
|
||
private boolean areSame(Node n1, Node n2) { | ||
// TODO VBC: Compare the Nodes are of the same. | ||
// QUESTION: the compareTo() method in Node class is very complicated, involves Lag etc. is that what we want to use? | ||
// or shall we just compare by names of these nodes | ||
|
||
return n1.getName().equals(n2.getName()); | ||
} | ||
|
||
|
||
/** | ||
* Returns the variables of the independence test. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jdramsey for confirmation :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the method I had in mind:
You could put that method somewhere helpful; it may not already exist in the code.