Class DiscreteVariable

java.lang.Object
edu.cmu.tetrad.data.AbstractVariable
edu.cmu.tetrad.data.DiscreteVariable
All Implemented Interfaces:
Variable, Node, TetradSerializable, Serializable, Comparable<Node>

public final class DiscreteVariable extends AbstractVariable implements Node

Represents a discrete variable as a range of integer-valued categories 0, 1, ..., m - 1, where m is the number of categories for the variable. These integer-valued categories may be associated with categories that may be explicitly set. Categories that are not explicitly set take the are set to DataUtils.defaultCategory(i) for category i.

Instances of this class may currently be used only to represent nominal discrete variables. Support for ordinal discrete variables may be added in the future.

Like other variable classes, DiscreteVariable implements the Node interface. The purpose of this is to allow variables to serve as nodes in graphs.

The index value used to indicate missing data is -99.

Author:
josephramsey
See Also:
  • Field Details

    • MISSING_VALUE

      public static final int MISSING_VALUE
      This is the index in the data which represents missing data internally for this variable.
      See Also:
  • Constructor Details

    • DiscreteVariable

      public DiscreteVariable(String name)
      Builds a discrete variable with the given name and an empty list of categories. Use this constructor if a variable is needed to represent just a list of integer categories with no categories associated with the categories.
    • DiscreteVariable

      public DiscreteVariable(String name, int numCategories)
      Builds a qualitative variable with the given name and number of categories. The categories have the form 'category'.
    • DiscreteVariable

      public DiscreteVariable(String name, List<String> categories)
      Builds a qualitative variable with the given name and array of possible categories.
      Parameters:
      name - The name of the variable.
      categories - A String[] array of categories, where the categories[i] is the category for index i.
    • DiscreteVariable

      public DiscreteVariable(DiscreteVariable variable)
      Copy constructor.
  • Method Details

    • serializableInstance

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

      public int getIndex(String category)
      Returns:
      the index of the given String category, or -1 if the category is not a category for this variable.
    • getNumCategories

      public int getNumCategories()
      Returns:
      the number of possible categories for this variable. If categories are associated, this is just the number of string categories. If no categories are associated, this is the maximum integer in the column.
    • getMissingValueMarker

      public Object getMissingValueMarker()
      Description copied from interface: Variable
      Returns the name of the variable.
      Specified by:
      getMissingValueMarker in interface Variable
      Specified by:
      getMissingValueMarker in class AbstractVariable
      Returns:
      the missing value marker as an Integer.
    • getCategory

      public String getCategory(int category)
      Returns:
      the variable category specified by the given category.
    • getCategories

      public List<String> getCategories()
      Returns:
      a copy of the array containing the categories for this variable. The string at index i is the category for index i.
    • checkValue

      public boolean checkValue(int category)
      Parameters:
      category - a category to be checked
      Returns:
      true if the given category is legal.
    • isMissingValue

      public boolean isMissingValue(Object value)
      Determines whether the given value is the missing value marker.
      Specified by:
      isMissingValue in interface Variable
      Specified by:
      isMissingValue in class AbstractVariable
      Parameters:
      value - the value to test; should be an Integer or a String.
      Returns:
      true iff the given object is equals to getMissingValueMarker().
    • isCategoryNamesDisplayed

      public boolean isCategoryNamesDisplayed()
      Returns:
      true iff categories for this variable should be displayed.
    • setCategoryNamesDisplayed

      public void setCategoryNamesDisplayed(boolean categoryNamesDisplayed)
      Sets whether categories for this variable should be displayed.
    • hashCode

      public int hashCode()
      Description copied from interface: Node
      Removes a property change listener.
      Specified by:
      hashCode in interface Node
      Overrides:
      hashCode in class Object
      Returns:
      a hashcode for this variable.
    • equals

      public boolean equals(Object o)
      Description copied from interface: Node
      Tests whether this variable is equal to the given variable.
      Specified by:
      equals in interface Node
      Overrides:
      equals in class Object
      Returns:
      true iff the given object is a discrete variable with the same number of categories and the same categories.
    • getNodeType

      public NodeType getNodeType()
      Description copied from interface: Node
      Returns the node type for this node.
      Specified by:
      getNodeType in interface Node
      Returns:
      the node type for this node.
    • setNodeType

      public void setNodeType(NodeType nodeType)
      Description copied from interface: Node
      Sets the node type for this node.
      Specified by:
      setNodeType in interface Node
      Parameters:
      nodeType - the node type for this node.
    • isAccommodateNewCategories

      public boolean isAccommodateNewCategories()
    • getCenterX

      public int getCenterX()
      Description copied from interface: Node
      Returns the x coordinate of the center of this node.
      Specified by:
      getCenterX in interface Node
      Returns:
      the x coordinate of the center of the node.
    • setCenterX

      public void setCenterX(int centerX)
      Sets the x coordinate of the center of this node.
      Specified by:
      setCenterX in interface Node
      Parameters:
      centerX - This coordinate.
    • getCenterY

      public int getCenterY()
      Description copied from interface: Node
      Returns the y coordinate of the center of this node.
      Specified by:
      getCenterY in interface Node
      Returns:
      the y coordinate of the center of the node.
    • setCenterY

      public void setCenterY(int centerY)
      Sets the y coordinate of the center of this node.
      Specified by:
      setCenterY in interface Node
      Parameters:
      centerY - This coordinate.
    • setCenter

      public void setCenter(int centerX, int centerY)
      Sets the (x, y) coordinates of the center of this node.
      Specified by:
      setCenter in interface Node
      Parameters:
      centerX - The x coordinate.
      centerY - The y coordinate.
    • addPropertyChangeListener

      public void addPropertyChangeListener(PropertyChangeListener l)
      Adds a property change listener.
      Specified by:
      addPropertyChangeListener in interface Node
      Parameters:
      l - This listener.
    • toString

      public String toString()
      Description copied from interface: Node
      Returns the intervention type for this node.
      Specified by:
      toString in interface Node
      Overrides:
      toString in class AbstractVariable
      Returns:
      the name of the variable followed by its list of categories.
    • like

      public Node like(String name)
      Description copied from interface: Node
      Creates a new node of the same type as this one with the given name.
      Specified by:
      like in interface Node
      Specified by:
      like in class AbstractVariable
      Parameters:
      name - the name of the new node.
      Returns:
      the new node.
    • getNodeVariableType

      public NodeVariableType getNodeVariableType()
      Description copied from interface: Node
      Returns the node shape for this node.
      Specified by:
      getNodeVariableType in interface Node
      Returns:
      the intervention type
    • setNodeVariableType

      public void setNodeVariableType(NodeVariableType nodeVariableType)
      Description copied from interface: Node
      Sets the type (domain, interventional status, interventional value..) for this node variable
      Specified by:
      setNodeVariableType in interface Node
      Parameters:
      nodeVariableType - the type (domain, interventional status, interventional value..) for this node variable
    • getAllAttributes

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

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

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

      public void addAttribute(String key, Object value)
      Specified by:
      addAttribute in interface Node