Package edu.cmu.tetrad.graph
Class SemGraph
java.lang.Object
edu.cmu.tetrad.graph.SemGraph
- All Implemented Interfaces:
Graph
,TetradSerializable
,Serializable
Represents the graphical structure of a structural equation model. The linear structure of the structural equation
model is constructed by adding non-error nodes to the graph and connecting them with directed edges. As this is done,
the graph automatically maintains the invariant that endogenous non-error nodes are associated with explicit error
nodes in the graph and exogenous non-error nodes are not. An associated error node for a node N is an error node that
has N as its only child, E-->N. Error nodes for exogenous nodes are always implicit in the graph. So as nodes
become endogenous, error nodes are added for them, and as they become exogenous, error nodes are removed for them.
Correlated errors are represented using directed edges among exogenous nodes. Directed edges may therefore be added
among any exogenous nodes in the graph, though the easiest way to add (or remove) exogenous nodes is to determine
which non-exogenous nodes N1, N2 they are representing correlated errors for and then to use this formulation:
addBidirectedEdge(getExogenous(node1), getExogenous(node2)); removeEdge(getExogenous(node1), getExogenous(node2));This avoids the problem of not knowing whether the exogenous node for a node is itself or its associated error node.
- Author:
- josephramsey
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
addAmbiguousTriple
(Node x, Node y, Node z) void
addAttribute
(String key, Object value) boolean
addBidirectedEdge
(Node nodeA, Node nodeB) Adds a bidirected edges <-> to the graph.boolean
addDirectedEdge
(Node nodeA, Node nodeB) Adds a directed edge --> to the graph.void
addDottedUnderlineTriple
(Node x, Node y, Node z) boolean
Adds the specified edge to the graph, provided it is not already in the graph.boolean
Adds a node to the graph.boolean
addNondirectedEdge
(Node nodeA, Node nodeB) Adds a nondirected edges o-o to the graph.boolean
addPartiallyOrientedEdge
(Node nodeA, Node nodeB) Adds a partially oriented edge o-> to the graph.void
Adds a PropertyChangeListener to the graph.void
addUnderlineTriple
(Node x, Node y, Node z) boolean
addUndirectedEdge
(Node nodeA, Node nodeB) Adds an undirected edge --- to the graph.void
clear()
Removes all nodes (and therefore all edges) from the graph.boolean
containsEdge
(Edge edge) Determines whether this graph contains the given edge.boolean
containsNode
(Node node) Determines whether this graph contains the given node.boolean
Determines whether this graph is equal to some other graph, in the sense that they contain the same nodes and the sets of edges defined over these nodes in the two graphs are isomorphic typewise.void
fullyConnect
(Endpoint endpoint) Removes all edges from the graph and fully connects it using #-# edges, where # is the given endpoint.getAdjacentNodes
(Node node) getAttribute
(String key) getChildren
(Node node) int
int
getDirectedEdge
(Node node1, Node node2) getEdges()
getEndpoint
(Node node1, Node node2) getErrorNode
(Node node) getExogenous
(Node node) int
getIndegree
(Node node) getNodes()
getNodesInTo
(Node node, Endpoint endpoint) Nodes adjacent to the given node with the given proximal endpoint.getNodesOutTo
(Node node, Endpoint n) Nodes adjacent to the given node with the given distal endpoint.int
int
getNumEdges
(Node node) int
int
getOutdegree
(Node node) getParents
(Node node) getVarNode
(Node node) boolean
isAdjacentTo
(Node nodeX, Node nodeY) boolean
isAmbiguousTriple
(Node x, Node y, Node z) States whether r-s-r is an underline triple or not.boolean
boolean
isDefCollider
(Node node1, Node node2, Node node3) Added by ekorber, 2004/6/9.boolean
isDefNoncollider
(Node node1, Node node2, Node node3) Added by ekorber, 2004/6/9.static boolean
isErrorEdge
(Edge edge) boolean
isExogenous
(Node node) boolean
isParameterizable
(Node node) boolean
isParentOf
(Node node1, Node node2) Determines whether node1 is a parent of node2.boolean
boolean
boolean
isUnderlineTriple
(Node x, Node y, Node z) States whether r-s-r is an underline triple or not.paths()
void
removeAmbiguousTriple
(Node x, Node y, Node z) void
removeAttribute
(String key) void
removeDottedUnderlineTriple
(Node x, Node y, Node z) boolean
removeEdge
(Edge edge) Removes the given edge from the graph.boolean
removeEdge
(Node node1, Node node2) Removes the edge connecting the two given nodes, provided there is exactly one such edge.boolean
removeEdges
(Node node1, Node node2) Removes all edges connecting node A to node B.boolean
removeEdges
(Collection<Edge> edges) Iterates through the list and removes any permissible edges found.boolean
removeNode
(Node node) Removes a node from the graph.boolean
removeNodes
(List<Node> nodes) Iterates through the list and removes any permissible nodes found.void
void
removeUnderlineTriple
(Node x, Node y, Node z) void
reorientAllWith
(Endpoint endpoint) Reorients all edges in the graph with the given endpoint.void
static SemGraph
Generates a simple exemplar of this class to test serialization.void
setAmbiguousTriples
(Set<Triple> triples) void
setDottedUnderLineTriples
(Set<Triple> triples) boolean
setEndpoint
(Node node1, Node node2, Endpoint endpoint) Sets the endpoint type at the 'to' end of the edge from 'from' to 'to' to the given endpoint.void
void
setShowErrorTerms
(boolean showErrorTerms) void
setUnderLineTriples
(Set<Triple> triples) Constructs and returns a subgraph consisting of a given subset of the nodes of this graph together with the edges between them.toString()
void
transferAttributes
(Graph graph) void
transferNodesAndEdges
(Graph graph) Transfers nodes and edges from one graph to another.
-
Constructor Details
-
SemGraph
public SemGraph()Constructs a new, empty SemGraph. -
SemGraph
Constructs a new SemGraph from the nodes and edges of the given graph. -
SemGraph
Copy constructor.
-
-
Method Details
-
serializableInstance
Generates a simple exemplar of this class to test serialization. -
isErrorEdge
- Returns:
- true iff either node associated with edge is an error term.
-
getErrorNode
- Returns:
- the error node associated with the given node, or null if the node has no associated error node.
-
isParameterizable
- Specified by:
isParameterizable
in interfaceGraph
- Returns:
- true if the given node is parameterizable.
-
getFullTierOrdering
- Returns:
- a tier order, including error terms, if they are shown.
- Throws:
IllegalStateException
- if the graph is cyclic.
-
getVarNode
- Returns:
- the variable node for this node--that is, the associated node, if this is an error node, or the node itself, if it is not.
-
getExogenous
- Parameters:
node
- the node you want the exogenous node for.- Returns:
- the exogenous node for that node.
-
transferNodesAndEdges
Description copied from interface:Graph
Transfers nodes and edges from one graph to another. One way this is used is to change graph types. One constructs a new graph based on the old graph, and this method is called to transfer the nodes and edges of the old graph to the new graph.- Specified by:
transferNodesAndEdges
in interfaceGraph
- Parameters:
graph
- the graph from which nodes and edges are to be pilfered.- Throws:
IllegalArgumentException
- This exception is thrown if adding some node.
-
transferAttributes
- Specified by:
transferAttributes
in interfaceGraph
- Throws:
IllegalArgumentException
-
paths
-
getNodeNames
- Specified by:
getNodeNames
in interfaceGraph
- Returns:
- the names of the nodes, in the order of
getNodes
.
-
fullyConnect
Description copied from interface:Graph
Removes all edges from the graph and fully connects it using #-# edges, where # is the given endpoint.- Specified by:
fullyConnect
in interfaceGraph
-
reorientAllWith
Description copied from interface:Graph
Reorients all edges in the graph with the given endpoint.- Specified by:
reorientAllWith
in interfaceGraph
-
getAdjacentNodes
- Specified by:
getAdjacentNodes
in interfaceGraph
- Returns:
- a mutable list of nodes adjacent to the given node.
-
getNodesInTo
Description copied from interface:Graph
Nodes adjacent to the given node with the given proximal endpoint.- Specified by:
getNodesInTo
in interfaceGraph
-
getNodesOutTo
Description copied from interface:Graph
Nodes adjacent to the given node with the given distal endpoint.- Specified by:
getNodesOutTo
in interfaceGraph
-
getNodes
-
setNodes
-
removeEdge
Description copied from interface:Graph
Removes the edge connecting the two given nodes, provided there is exactly one such edge.- Specified by:
removeEdge
in interfaceGraph
-
removeEdges
Description copied from interface:Graph
Removes all edges connecting node A to node B. In most cases, this will remove at most one edge, but since multiple edges are permitted in some graph implementations, the number will in some cases be greater than one.- Specified by:
removeEdges
in interfaceGraph
- Returns:
- true if edges were removed, false if not.
-
isAdjacentTo
- Specified by:
isAdjacentTo
in interfaceGraph
- Returns:
- true iff node1 is adjacent to node2 in the graph.
-
setEndpoint
Description copied from interface:Graph
Sets the endpoint type at the 'to' end of the edge from 'from' to 'to' to the given endpoint. Note: NOT CONSTRAINT SAFE- Specified by:
setEndpoint
in interfaceGraph
-
getEndpoint
- Specified by:
getEndpoint
in interfaceGraph
- Returns:
- the endpoint along the edge from node to node2 at the node2 end.
-
equals
Description copied from interface:Graph
Determines whether this graph is equal to some other graph, in the sense that they contain the same nodes and the sets of edges defined over these nodes in the two graphs are isomorphic typewise. That is, if node A and B exist in both graphs, and if there are, e.g., three edges between A and B in the first graph, two of which are directed edges and one of which is an undirected edge, then in the second graph there must also be two directed edges and one undirected edge between nodes A and B. -
subgraph
Description copied from interface:Graph
Constructs and returns a subgraph consisting of a given subset of the nodes of this graph together with the edges between them. -
addDirectedEdge
Description copied from interface:Graph
Adds a directed edge --> to the graph.- Specified by:
addDirectedEdge
in interfaceGraph
-
addUndirectedEdge
Description copied from interface:Graph
Adds an undirected edge --- to the graph.- Specified by:
addUndirectedEdge
in interfaceGraph
-
addNondirectedEdge
Description copied from interface:Graph
Adds a nondirected edges o-o to the graph.- Specified by:
addNondirectedEdge
in interfaceGraph
-
addPartiallyOrientedEdge
Description copied from interface:Graph
Adds a partially oriented edge o-> to the graph.- Specified by:
addPartiallyOrientedEdge
in interfaceGraph
-
addBidirectedEdge
Description copied from interface:Graph
Adds a bidirected edges <-> to the graph.- Specified by:
addBidirectedEdge
in interfaceGraph
-
addEdge
Description copied from interface:Graph
Adds the specified edge to the graph, provided it is not already in the graph. -
addNode
Description copied from interface:Graph
Adds a node to the graph. Precondition: The proposed name of the node cannot already be used by any other node in the same graph. -
addPropertyChangeListener
Description copied from interface:Graph
Adds a PropertyChangeListener to the graph.- Specified by:
addPropertyChangeListener
in interfaceGraph
-
containsEdge
Description copied from interface:Graph
Determines whether this graph contains the given edge.- Specified by:
containsEdge
in interfaceGraph
- Returns:
- true iff the graph contain 'edge'.
-
containsNode
Description copied from interface:Graph
Determines whether this graph contains the given node.- Specified by:
containsNode
in interfaceGraph
- Returns:
- true iff the graph contains 'node'.
-
getEdges
-
getEdges
-
getEdges
-
getNode
-
getNumEdges
public int getNumEdges()- Specified by:
getNumEdges
in interfaceGraph
- Returns:
- the number of edges in the (entire) graph.
-
getNumNodes
public int getNumNodes()- Specified by:
getNumNodes
in interfaceGraph
- Returns:
- the number of nodes in the graph.
-
getNumEdges
- Specified by:
getNumEdges
in interfaceGraph
- Returns:
- the number of edges in the graph which are connected to a particular node.
-
removeEdge
Description copied from interface:Graph
Removes the given edge from the graph.- Specified by:
removeEdge
in interfaceGraph
- Returns:
- true if the edge was removed, false if not.
-
removeEdges
Description copied from interface:Graph
Iterates through the list and removes any permissible edges found. The order in which edges are added is the order in which they are presented in the iterator.- Specified by:
removeEdges
in interfaceGraph
- Returns:
- true if edges were added, false if not.
-
removeNode
Description copied from interface:Graph
Removes a node from the graph.- Specified by:
removeNode
in interfaceGraph
- Returns:
- true if the node was removed, false if not.
-
clear
public void clear()Description copied from interface:Graph
Removes all nodes (and therefore all edges) from the graph. -
removeNodes
Description copied from interface:Graph
Iterates through the list and removes any permissible nodes found. The order in which nodes are removed is the order in which they are presented in the iterator.- Specified by:
removeNodes
in interfaceGraph
- Returns:
- true if nodes were added, false if not.
-
isDefNoncollider
Description copied from interface:Graph
Added by ekorber, 2004/6/9.- Specified by:
isDefNoncollider
in interfaceGraph
- Returns:
- true if node 2 is a definite noncollider between 1 and 3
-
isDefCollider
Description copied from interface:Graph
Added by ekorber, 2004/6/9.- Specified by:
isDefCollider
in interfaceGraph
- Returns:
- true if node 2 is a definite collider between 1 and 3
-
getChildren
- Specified by:
getChildren
in interfaceGraph
- Returns:
- a mutable list of children for a node.
-
getDegree
public int getDegree() -
getEdge
-
getDirectedEdge
- Specified by:
getDirectedEdge
in interfaceGraph
- Returns:
- the directed edge from node1 to node2, if there is one.
-
getParents
- Specified by:
getParents
in interfaceGraph
- Returns:
- the list of parents for a node.
-
getIndegree
- Specified by:
getIndegree
in interfaceGraph
- Returns:
- the number of arrow endpoints adjacent to a node.
-
getDegree
-
getOutdegree
- Specified by:
getOutdegree
in interfaceGraph
- Returns:
- the number of null endpoints adjacent to an edge.
-
isChildOf
-
isParentOf
Description copied from interface:Graph
Determines whether node1 is a parent of node2.- Specified by:
isParentOf
in interfaceGraph
-
isExogenous
- Specified by:
isExogenous
in interfaceGraph
- Returns:
- true iff the given node is exogenous in the graph.
-
toString
-
isShowErrorTerms
public boolean isShowErrorTerms() -
setShowErrorTerms
public void setShowErrorTerms(boolean showErrorTerms) -
isTimeLagModel
public boolean isTimeLagModel()- Specified by:
isTimeLagModel
in interfaceGraph
- Returns:
- true if this is a time lag model, in which case getTimeLagGraph() returns the graph.
-
getTimeLagGraph
- Specified by:
getTimeLagGraph
in interfaceGraph
- Returns:
- the underlying time lag model, if there is one; otherwise, returns null.
-
getSepset
-
resetErrorPositions
public void resetErrorPositions() -
getAllAttributes
- Specified by:
getAllAttributes
in interfaceGraph
-
getAttribute
- Specified by:
getAttribute
in interfaceGraph
-
removeAttribute
- Specified by:
removeAttribute
in interfaceGraph
-
addAttribute
- Specified by:
addAttribute
in interfaceGraph
-
getAmbiguousTriples
- Specified by:
getAmbiguousTriples
in interfaceGraph
-
setAmbiguousTriples
- Specified by:
setAmbiguousTriples
in interfaceGraph
-
getUnderLines
- Specified by:
getUnderLines
in interfaceGraph
-
getDottedUnderlines
- Specified by:
getDottedUnderlines
in interfaceGraph
-
isAmbiguousTriple
States whether r-s-r is an underline triple or not.- Specified by:
isAmbiguousTriple
in interfaceGraph
-
isUnderlineTriple
States whether r-s-r is an underline triple or not.- Specified by:
isUnderlineTriple
in interfaceGraph
-
addAmbiguousTriple
- Specified by:
addAmbiguousTriple
in interfaceGraph
-
addUnderlineTriple
- Specified by:
addUnderlineTriple
in interfaceGraph
-
addDottedUnderlineTriple
- Specified by:
addDottedUnderlineTriple
in interfaceGraph
-
removeAmbiguousTriple
- Specified by:
removeAmbiguousTriple
in interfaceGraph
-
removeUnderlineTriple
- Specified by:
removeUnderlineTriple
in interfaceGraph
-
removeDottedUnderlineTriple
- Specified by:
removeDottedUnderlineTriple
in interfaceGraph
-
setUnderLineTriples
- Specified by:
setUnderLineTriples
in interfaceGraph
-
setDottedUnderLineTriples
- Specified by:
setDottedUnderLineTriples
in interfaceGraph
-
removeTriplesNotInGraph
public void removeTriplesNotInGraph()- Specified by:
removeTriplesNotInGraph
in interfaceGraph
-