Class SemGraph

java.lang.Object
edu.cmu.tetrad.graph.SemGraph
All Implemented Interfaces:
Graph, TetradSerializable, Serializable

public final class SemGraph extends Object implements Graph
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 Details

    • SemGraph

      public SemGraph()
      Constructs a new, empty SemGraph.
    • SemGraph

      public SemGraph(Graph graph)
      Constructs a new SemGraph from the nodes and edges of the given graph.
    • SemGraph

      public SemGraph(SemGraph graph)
      Copy constructor.
  • Method Details

    • serializableInstance

      public static SemGraph serializableInstance()
      Generates a simple exemplar of this class to test serialization.
    • isErrorEdge

      public static boolean isErrorEdge(Edge edge)
      Returns:
      true iff either node associated with edge is an error term.
    • getErrorNode

      public Node getErrorNode(Node node)
      Returns:
      the error node associated with the given node, or null if the node has no associated error node.
    • isParameterizable

      public boolean isParameterizable(Node node)
      Specified by:
      isParameterizable in interface Graph
      Returns:
      true if the given node is parameterizable.
    • getFullTierOrdering

      public List<Node> getFullTierOrdering()
      Returns:
      a tier order, including error terms, if they are shown.
      Throws:
      IllegalStateException - if the graph is cyclic.
    • getVarNode

      public Node getVarNode(Node node)
      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

      public Node getExogenous(Node node)
      Parameters:
      node - the node you want the exogenous node for.
      Returns:
      the exogenous node for that node.
    • transferNodesAndEdges

      public void transferNodesAndEdges(Graph graph) throws IllegalArgumentException
      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 interface Graph
      Parameters:
      graph - the graph from which nodes and edges are to be pilfered.
      Throws:
      IllegalArgumentException - This exception is thrown if adding some node.
    • transferAttributes

      public void transferAttributes(Graph graph) throws IllegalArgumentException
      Specified by:
      transferAttributes in interface Graph
      Throws:
      IllegalArgumentException
    • paths

      public Paths paths()
      Specified by:
      paths in interface Graph
    • getNodeNames

      public List<String> getNodeNames()
      Specified by:
      getNodeNames in interface Graph
      Returns:
      the names of the nodes, in the order of getNodes.
    • fullyConnect

      public void fullyConnect(Endpoint endpoint)
      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 interface Graph
    • reorientAllWith

      public void reorientAllWith(Endpoint endpoint)
      Description copied from interface: Graph
      Reorients all edges in the graph with the given endpoint.
      Specified by:
      reorientAllWith in interface Graph
    • getAdjacentNodes

      public List<Node> getAdjacentNodes(Node node)
      Specified by:
      getAdjacentNodes in interface Graph
      Returns:
      a mutable list of nodes adjacent to the given node.
    • getNodesInTo

      public List<Node> getNodesInTo(Node node, Endpoint endpoint)
      Description copied from interface: Graph
      Nodes adjacent to the given node with the given proximal endpoint.
      Specified by:
      getNodesInTo in interface Graph
    • getNodesOutTo

      public List<Node> getNodesOutTo(Node node, Endpoint n)
      Description copied from interface: Graph
      Nodes adjacent to the given node with the given distal endpoint.
      Specified by:
      getNodesOutTo in interface Graph
    • getNodes

      public List<Node> getNodes()
      Specified by:
      getNodes in interface Graph
      Returns:
      the list of nodes for the graph.
    • setNodes

      public void setNodes(List<Node> nodes)
      Specified by:
      setNodes in interface Graph
    • removeEdge

      public boolean removeEdge(Node node1, Node node2)
      Description copied from interface: Graph
      Removes the edge connecting the two given nodes, provided there is exactly one such edge.
      Specified by:
      removeEdge in interface Graph
    • removeEdges

      public boolean removeEdges(Node node1, Node node2)
      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 interface Graph
      Returns:
      true if edges were removed, false if not.
    • isAdjacentTo

      public boolean isAdjacentTo(Node nodeX, Node nodeY)
      Specified by:
      isAdjacentTo in interface Graph
      Returns:
      true iff node1 is adjacent to node2 in the graph.
    • setEndpoint

      public boolean setEndpoint(Node node1, Node node2, Endpoint endpoint)
      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 interface Graph
    • getEndpoint

      public Endpoint getEndpoint(Node node1, Node node2)
      Specified by:
      getEndpoint in interface Graph
      Returns:
      the endpoint along the edge from node to node2 at the node2 end.
    • equals

      public boolean equals(Object o)
      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.
      Specified by:
      equals in interface Graph
      Overrides:
      equals in class Object
    • subgraph

      public Graph subgraph(List<Node> nodes)
      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.
      Specified by:
      subgraph in interface Graph
    • addDirectedEdge

      public boolean addDirectedEdge(Node nodeA, Node nodeB)
      Description copied from interface: Graph
      Adds a directed edge --> to the graph.
      Specified by:
      addDirectedEdge in interface Graph
    • addUndirectedEdge

      public boolean addUndirectedEdge(Node nodeA, Node nodeB)
      Description copied from interface: Graph
      Adds an undirected edge --- to the graph.
      Specified by:
      addUndirectedEdge in interface Graph
    • addNondirectedEdge

      public boolean addNondirectedEdge(Node nodeA, Node nodeB)
      Description copied from interface: Graph
      Adds a nondirected edges o-o to the graph.
      Specified by:
      addNondirectedEdge in interface Graph
    • addPartiallyOrientedEdge

      public boolean addPartiallyOrientedEdge(Node nodeA, Node nodeB)
      Description copied from interface: Graph
      Adds a partially oriented edge o-> to the graph.
      Specified by:
      addPartiallyOrientedEdge in interface Graph
    • addBidirectedEdge

      public boolean addBidirectedEdge(Node nodeA, Node nodeB)
      Description copied from interface: Graph
      Adds a bidirected edges <-> to the graph.
      Specified by:
      addBidirectedEdge in interface Graph
    • addEdge

      public boolean addEdge(Edge edge)
      Description copied from interface: Graph
      Adds the specified edge to the graph, provided it is not already in the graph.
      Specified by:
      addEdge in interface Graph
      Returns:
      true if the edge was added, false if not.
    • addNode

      public boolean addNode(Node node)
      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.
      Specified by:
      addNode in interface Graph
      Returns:
      true if nodes were added, false if not.
    • addPropertyChangeListener

      public void addPropertyChangeListener(PropertyChangeListener l)
      Description copied from interface: Graph
      Adds a PropertyChangeListener to the graph.
      Specified by:
      addPropertyChangeListener in interface Graph
    • containsEdge

      public boolean containsEdge(Edge edge)
      Description copied from interface: Graph
      Determines whether this graph contains the given edge.
      Specified by:
      containsEdge in interface Graph
      Returns:
      true iff the graph contain 'edge'.
    • containsNode

      public boolean containsNode(Node node)
      Description copied from interface: Graph
      Determines whether this graph contains the given node.
      Specified by:
      containsNode in interface Graph
      Returns:
      true iff the graph contains 'node'.
    • getEdges

      public Set<Edge> getEdges()
      Specified by:
      getEdges in interface Graph
      Returns:
      the set of edges in the graph. No particular ordering of the edges in the list is guaranteed.
    • getEdges

      public List<Edge> getEdges(Node node)
      Specified by:
      getEdges in interface Graph
      Returns:
      the list of edges connected to a particular node. No particular ordering of the edges in the list is guaranteed.
    • getEdges

      public List<Edge> getEdges(Node node1, Node node2)
      Specified by:
      getEdges in interface Graph
      Returns:
      the edges connecting node1 and node2.
    • getNode

      public Node getNode(String name)
      Specified by:
      getNode in interface Graph
      Returns:
      the node with the given string name. In case of accidental duplicates, the first node encountered with the given name is returned. In case no node exists with the given name, null is returned.
    • getNumEdges

      public int getNumEdges()
      Specified by:
      getNumEdges in interface Graph
      Returns:
      the number of edges in the (entire) graph.
    • getNumNodes

      public int getNumNodes()
      Specified by:
      getNumNodes in interface Graph
      Returns:
      the number of nodes in the graph.
    • getNumEdges

      public int getNumEdges(Node node)
      Specified by:
      getNumEdges in interface Graph
      Returns:
      the number of edges in the graph which are connected to a particular node.
    • removeEdge

      public boolean removeEdge(Edge edge)
      Description copied from interface: Graph
      Removes the given edge from the graph.
      Specified by:
      removeEdge in interface Graph
      Returns:
      true if the edge was removed, false if not.
    • removeEdges

      public boolean removeEdges(Collection<Edge> edges)
      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 interface Graph
      Returns:
      true if edges were added, false if not.
    • removeNode

      public boolean removeNode(Node node)
      Description copied from interface: Graph
      Removes a node from the graph.
      Specified by:
      removeNode in interface Graph
      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.
      Specified by:
      clear in interface Graph
    • removeNodes

      public boolean removeNodes(List<Node> nodes)
      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 interface Graph
      Returns:
      true if nodes were added, false if not.
    • isDefNoncollider

      public boolean isDefNoncollider(Node node1, Node node2, Node node3)
      Description copied from interface: Graph
      Added by ekorber, 2004/6/9.
      Specified by:
      isDefNoncollider in interface Graph
      Returns:
      true if node 2 is a definite noncollider between 1 and 3
    • isDefCollider

      public boolean isDefCollider(Node node1, Node node2, Node node3)
      Description copied from interface: Graph
      Added by ekorber, 2004/6/9.
      Specified by:
      isDefCollider in interface Graph
      Returns:
      true if node 2 is a definite collider between 1 and 3
    • getChildren

      public List<Node> getChildren(Node node)
      Specified by:
      getChildren in interface Graph
      Returns:
      a mutable list of children for a node.
    • getDegree

      public int getDegree()
      Specified by:
      getDegree in interface Graph
      Returns:
      the connectivity of the graph.
    • getEdge

      public Edge getEdge(Node node1, Node node2)
      Specified by:
      getEdge in interface Graph
      Returns:
      the edge connecting node1 and node2, provided a unique such edge exists.
    • getDirectedEdge

      public Edge getDirectedEdge(Node node1, Node node2)
      Specified by:
      getDirectedEdge in interface Graph
      Returns:
      the directed edge from node1 to node2, if there is one.
    • getParents

      public List<Node> getParents(Node node)
      Specified by:
      getParents in interface Graph
      Returns:
      the list of parents for a node.
    • getIndegree

      public int getIndegree(Node node)
      Specified by:
      getIndegree in interface Graph
      Returns:
      the number of arrow endpoints adjacent to a node.
    • getDegree

      public int getDegree(Node node)
      Specified by:
      getDegree in interface Graph
      Returns:
      the number of arrow endpoints adjacent to a node.
    • getOutdegree

      public int getOutdegree(Node node)
      Specified by:
      getOutdegree in interface Graph
      Returns:
      the number of null endpoints adjacent to an edge.
    • isChildOf

      public boolean isChildOf(Node node1, Node node2)
      Specified by:
      isChildOf in interface Graph
      Returns:
      true iff node1 is a child of node2 in the graph.
    • isParentOf

      public boolean isParentOf(Node node1, Node node2)
      Description copied from interface: Graph
      Determines whether node1 is a parent of node2.
      Specified by:
      isParentOf in interface Graph
    • isExogenous

      public boolean isExogenous(Node node)
      Specified by:
      isExogenous in interface Graph
      Returns:
      true iff the given node is exogenous in the graph.
    • toString

      public String toString()
      Specified by:
      toString in interface Graph
      Overrides:
      toString in class Object
      Returns:
      a string representation of the graph.
    • isShowErrorTerms

      public boolean isShowErrorTerms()
    • setShowErrorTerms

      public void setShowErrorTerms(boolean showErrorTerms)
    • isTimeLagModel

      public boolean isTimeLagModel()
      Specified by:
      isTimeLagModel in interface Graph
      Returns:
      true if this is a time lag model, in which case getTimeLagGraph() returns the graph.
    • getTimeLagGraph

      public TimeLagGraph getTimeLagGraph()
      Specified by:
      getTimeLagGraph in interface Graph
      Returns:
      the underlying time lag model, if there is one; otherwise, returns null.
    • getSepset

      public Set<Node> getSepset(Node n1, Node n2)
      Specified by:
      getSepset in interface Graph
    • resetErrorPositions

      public void resetErrorPositions()
    • getAllAttributes

      public Map<String,Object> getAllAttributes()
      Specified by:
      getAllAttributes in interface Graph
    • getAttribute

      public Object getAttribute(String key)
      Specified by:
      getAttribute in interface Graph
    • removeAttribute

      public void removeAttribute(String key)
      Specified by:
      removeAttribute in interface Graph
    • addAttribute

      public void addAttribute(String key, Object value)
      Specified by:
      addAttribute in interface Graph
    • getAmbiguousTriples

      public Set<Triple> getAmbiguousTriples()
      Specified by:
      getAmbiguousTriples in interface Graph
    • setAmbiguousTriples

      public void setAmbiguousTriples(Set<Triple> triples)
      Specified by:
      setAmbiguousTriples in interface Graph
    • getUnderLines

      public Set<Triple> getUnderLines()
      Specified by:
      getUnderLines in interface Graph
    • getDottedUnderlines

      public Set<Triple> getDottedUnderlines()
      Specified by:
      getDottedUnderlines in interface Graph
    • isAmbiguousTriple

      public boolean isAmbiguousTriple(Node x, Node y, Node z)
      States whether r-s-r is an underline triple or not.
      Specified by:
      isAmbiguousTriple in interface Graph
    • isUnderlineTriple

      public boolean isUnderlineTriple(Node x, Node y, Node z)
      States whether r-s-r is an underline triple or not.
      Specified by:
      isUnderlineTriple in interface Graph
    • addAmbiguousTriple

      public void addAmbiguousTriple(Node x, Node y, Node z)
      Specified by:
      addAmbiguousTriple in interface Graph
    • addUnderlineTriple

      public void addUnderlineTriple(Node x, Node y, Node z)
      Specified by:
      addUnderlineTriple in interface Graph
    • addDottedUnderlineTriple

      public void addDottedUnderlineTriple(Node x, Node y, Node z)
      Specified by:
      addDottedUnderlineTriple in interface Graph
    • removeAmbiguousTriple

      public void removeAmbiguousTriple(Node x, Node y, Node z)
      Specified by:
      removeAmbiguousTriple in interface Graph
    • removeUnderlineTriple

      public void removeUnderlineTriple(Node x, Node y, Node z)
      Specified by:
      removeUnderlineTriple in interface Graph
    • removeDottedUnderlineTriple

      public void removeDottedUnderlineTriple(Node x, Node y, Node z)
      Specified by:
      removeDottedUnderlineTriple in interface Graph
    • setUnderLineTriples

      public void setUnderLineTriples(Set<Triple> triples)
      Specified by:
      setUnderLineTriples in interface Graph
    • setDottedUnderLineTriples

      public void setDottedUnderLineTriples(Set<Triple> triples)
      Specified by:
      setDottedUnderLineTriples in interface Graph
    • removeTriplesNotInGraph

      public void removeTriplesNotInGraph()
      Specified by:
      removeTriplesNotInGraph in interface Graph