Class AbstractPGM<NodeType extends Node>

java.lang.Object
es.upm.fi.cig.multictbnc.models.AbstractPGM<NodeType>
Type Parameters:
NodeType - type of nodes used by the model, e.g., nodes with conditional probability tables (CPTNode) or conditional intensity matrices (@code CIMNode)
All Implemented Interfaces:
PGM<NodeType>
Direct Known Subclasses:
BN, CTBN, MultiCTBNC

public abstract class AbstractPGM<NodeType extends Node> extends Object implements PGM<NodeType>
Contains common attributes and methods for PGM.
  • Constructor Details

    • AbstractPGM

      public AbstractPGM(List<NodeType> nodes)
      Common initialisation for PGMs.
      Parameters:
      nodes - nodes of the PGM
    • AbstractPGM

      public AbstractPGM(List<NodeType> nodes, Dataset dataset)
      Common initialisation for PGMs. The provided dataset is used to learn the model. References to the provided nodes are stored.
      Parameters:
      nodes - nodes of the PGM
      dataset - dataset used to learn the model
    • AbstractPGM

      public AbstractPGM()
      Default constructor.
  • Method Details

    • addNodes

      public void addNodes(List<NodeType> nodes, boolean createNodes)
      Description copied from interface: PGM
      Adds the provided nodes to the PGM.
      Specified by:
      addNodes in interface PGM<NodeType extends Node>
      Parameters:
      nodes - nodes to add
      createNodes - true to create new nodes from the provided ones, false otherwise (any change to the provided nodes will affect this model).
    • areParametersEstimated

      public boolean areParametersEstimated()
      Description copied from interface: PGM
      Returns true if all the parameters were estimated.
      Specified by:
      areParametersEstimated in interface PGM<NodeType extends Node>
      Returns:
      true if all the parameters were estimated
    • computeSufficientStatistics

      public void computeSufficientStatistics(List<Integer> idxNodes)
      Description copied from interface: PGM
      Computes the sufficient statistics for the nodes whose indexes are specified.
      Specified by:
      computeSufficientStatistics in interface PGM<NodeType extends Node>
      Parameters:
      idxNodes - node indexes
    • display

      public void display()
      Description copied from interface: PGM
      Displays the probabilistic graphical model.
      Specified by:
      display in interface PGM<NodeType extends Node>
    • display

      public void display(String title)
      Description copied from interface: PGM
      Displays the probabilistic graphical model.
      Specified by:
      display in interface PGM<NodeType extends Node>
      Parameters:
      title - title of the window where the model is displayed
    • display

      public void display(String title, List<Integer> nodesToHighlight)
      Description copied from interface: PGM
      Displays the probabilistic graphical model.
      Specified by:
      display in interface PGM<NodeType extends Node>
      Parameters:
      title - title of the window where the model is displayed
      nodesToHighlight - indexes of the nodes to be highlighted in red
    • getAdjacencyMatrix

      public boolean[][] getAdjacencyMatrix()
      Returns the adjacency matrix of the PGM by analysing the parents of each node.
      Specified by:
      getAdjacencyMatrix in interface PGM<NodeType extends Node>
      Returns:
      two-dimensional boolean array representing the adjacency matrix
    • getIndexNodes

      public List<Integer> getIndexNodes()
      Description copied from interface: PGM
      Returns the indexes of the nodes.
      Specified by:
      getIndexNodes in interface PGM<NodeType extends Node>
      Returns:
      node indexes
    • getNamesNodesByIndex

      public List<String> getNamesNodesByIndex(List<Integer> indexes)
      Description copied from interface: PGM
      Return the names of the nodes whose indexes are given.
      Specified by:
      getNamesNodesByIndex in interface PGM<NodeType extends Node>
      Parameters:
      indexes - node indexes
      Returns:
      node names
    • getNodeByIndex

      public NodeType getNodeByIndex(int index)
      Description copied from interface: PGM
      Obtains the node (feature or class variable) with a certain index.
      Specified by:
      getNodeByIndex in interface PGM<NodeType extends Node>
      Parameters:
      index - node index
      Returns:
      node with the specified index
    • getIndexOfNode

      public int getIndexOfNode(Node node)
      Description copied from interface: PGM
      Returns the index of the provided node.
      Specified by:
      getIndexOfNode in interface PGM<NodeType extends Node>
      Parameters:
      node - node for which the index is needed
      Returns:
      index of the node
    • getNodeByName

      public NodeType getNodeByName(String nameVariable)
      Description copied from interface: PGM
      Returns the node whose variable name is given.
      Specified by:
      getNodeByName in interface PGM<NodeType extends Node>
      Parameters:
      nameVariable - name of the variable
      Returns:
      requested node
    • getNodeIndexer

      public NodeIndexer<NodeType> getNodeIndexer()
      Returns the node indexer of the model.
      Specified by:
      getNodeIndexer in interface PGM<NodeType extends Node>
      Returns:
      node indexer
    • getNodes

      public List<NodeType> getNodes()
      Description copied from interface: PGM
      Returns all the nodes in the model.
      Specified by:
      getNodes in interface PGM<NodeType extends Node>
      Returns:
      list of nodes
    • getNumNodes

      public int getNumNodes()
      Description copied from interface: PGM
      Returns the number of nodes.
      Specified by:
      getNumNodes in interface PGM<NodeType extends Node>
      Returns:
      number of nodes
    • getParameterLearningAlg

      public ParameterLearningAlgorithm getParameterLearningAlg()
      Description copied from interface: PGM
      Returns the algorithm that is used to learn the parameters of the PGM.
      Specified by:
      getParameterLearningAlg in interface PGM<NodeType extends Node>
      Returns:
      parameter learning algorithm
    • initialiseModel

      public void initialiseModel(Dataset dataset)
      Description copied from interface: PGM
      Sets the dataset that will be used to estimate the structure and parameters of the model and creates its nodes.
      Specified by:
      initialiseModel in interface PGM<NodeType extends Node>
      Parameters:
      dataset - dataset used to learn the model
    • isStructureLegal

      public boolean isStructureLegal(boolean[][] adjacencyMatrix)
      Determines if the structure is legal.
      Specified by:
      isStructureLegal in interface PGM<NodeType extends Node>
      Parameters:
      adjacencyMatrix - adjacency matrix
      Returns:
      true if the structure is valid, false otherwise
    • learn

      public void learn() throws ErroneousValueException
      Description copied from interface: PGM
      Learns the structure and parameters of the model.
      Specified by:
      learn in interface PGM<NodeType extends Node>
      Throws:
      ErroneousValueException - if a provided parameter is erroneous for the requested task
    • learn

      public long learn(Dataset dataset) throws ErroneousValueException
      Description copied from interface: PGM
      Learns the structure and parameters of the model from a given dataset.
      Specified by:
      learn in interface PGM<NodeType extends Node>
      Parameters:
      dataset - dataset used to learn the model
      Returns:
      time to learn the model
      Throws:
      ErroneousValueException - if a provided parameter is erroneous for the requested task
    • learn

      public void learn(Dataset dataset, int idxNode) throws ErroneousValueException
      Description copied from interface: PGM
      Learns the parameters and parent set of a model's node from a given dataset.
      Specified by:
      learn in interface PGM<NodeType extends Node>
      Parameters:
      dataset - dataset used to learn the parameters and parent set of the node
      idxNode - node index
      Throws:
      ErroneousValueException - if a provided parameter is erroneous for the requested task
    • learn

      public void learn(Dataset dataset, List<Integer> idxNodes) throws ErroneousValueException
      Description copied from interface: PGM
      Learns the parameters and parent set of some nodes of the model from a given dataset.
      Specified by:
      learn in interface PGM<NodeType extends Node>
      Parameters:
      dataset - dataset used to learn the parameters and parent set of the nodes
      idxNodes - node indexes
      Throws:
      ErroneousValueException - if a provided parameter is erroneous for the requested task
    • learnParameters

      public void learnParameters()
      Description copied from interface: PGM
      Learns the parameters of the PGM.
      Specified by:
      learnParameters in interface PGM<NodeType extends Node>
    • learnParameters

      public void learnParameters(Dataset dataset)
      Description copied from interface: PGM
      Learns the parameters of the PGM using the provided dataset.
      Specified by:
      learnParameters in interface PGM<NodeType extends Node>
      Parameters:
      dataset - dataset used to learn the parameters
    • learnParameters

      public void learnParameters(Dataset dataset, int idxNode)
      Description copied from interface: PGM
      Learns the parameters of a certain node of the PGM using the provided dataset.
      Specified by:
      learnParameters in interface PGM<NodeType extends Node>
      Parameters:
      dataset - dataset used to learn the parameters
      idxNode - node index
    • learnParameters

      public void learnParameters(List<Integer> idxNodes)
      Description copied from interface: PGM
      Learns the parameters of the nodes whose indexes are specified.
      Specified by:
      learnParameters in interface PGM<NodeType extends Node>
      Parameters:
      idxNodes - indexes of the nodes whose parameters should be learnt
    • learnParameters

      public void learnParameters(Integer idxNode)
      Description copied from interface: PGM
      Learns the parameters of the node whose index is specified.
      Specified by:
      learnParameters in interface PGM<NodeType extends Node>
      Parameters:
      idxNode - index of the node whose parameters should be learnt
    • learnParameters

      public void learnParameters(List<Integer> idxNodes, ParameterLearningAlgorithm parameterLearningAlg)
      Description copied from interface: PGM
      Learns the parameters of the nodes whose indexes are specified using a provider parameter learning algorithm.
      Specified by:
      learnParameters in interface PGM<NodeType extends Node>
      Parameters:
      idxNodes - indexes of the nodes whose parameters should be learnt
      parameterLearningAlg - a ParameterLearningAlgorithm
    • removeAllEdges

      public void removeAllEdges()
      Description copied from interface: PGM
      Remove all edges between the nodes of the PGM.
      Specified by:
      removeAllEdges in interface PGM<NodeType extends Node>
    • removeAllNodes

      public void removeAllNodes()
      Description copied from interface: PGM
      Removes all the nodes from the PGM.
      Specified by:
      removeAllNodes in interface PGM<NodeType extends Node>
    • saveGraph

      public void saveGraph(String destinationPath, String filename, List<Integer> idxNodesToHighlight)
      Description copied from interface: PGM
      Saves the PGM graph to a file.
      Specified by:
      saveGraph in interface PGM<NodeType extends Node>
      Parameters:
      destinationPath - destination path of the file
      filename - filename
      idxNodesToHighlight - indexes of the nodes to highlight
    • setStructure

      public void setStructure(boolean[][] adjacencyMatrix)
      Description copied from interface: PGM
      Modifies the structure of the PGM by changing the parents of the nodes.
      Specified by:
      setStructure in interface PGM<NodeType extends Node>
      Parameters:
      adjacencyMatrix - adjacency matrix with the new structure of the PGM
    • setStructure

      public void setStructure(int idxNode, boolean[][] adjacencyMatrix)
      Description copied from interface: PGM
      Updates the structure of the model only for the specified node.
      Specified by:
      setStructure in interface PGM<NodeType extends Node>
      Parameters:
      idxNode - node index
      adjacencyMatrix - adjacency matrix
    • setStructure

      public void setStructure(List<Integer> idxNodes, boolean[][] adjacencyMatrix)
      Description copied from interface: PGM
      Updates the structure of the model only for the specified node.
      Specified by:
      setStructure in interface PGM<NodeType extends Node>
      Parameters:
      idxNodes - node indexes
      adjacencyMatrix - adjacency matrix found
    • setStructureModifiedNodes

      public void setStructureModifiedNodes(boolean[][] newAdjacencyMatrix)
      Description copied from interface: PGM
      Modifies the structure of the PGM by changing the parents and CPDs of those nodes which have different parents between the current adjacency matrix and the new one.
      Specified by:
      setStructureModifiedNodes in interface PGM<NodeType extends Node>
      Parameters:
      newAdjacencyMatrix - new adjacency matrix
    • getNodeFactory

      protected NodeFactory<NodeType> getNodeFactory()
      Returns a NodeFactory for the nodes of the PGM.
      Returns:
      a NodeFactory
    • getNodeClass

      public Class<NodeType> getNodeClass()
      Returns the type of the nodes.
      Returns:
      type of the nodes
    • getDataset

      public Dataset getDataset()
      Returns the dataset used to learn the PGM.
      Returns:
      dataset
    • setDataset

      public void setDataset(Dataset dataset)
      Set the dataset used to learn the PGM.
      Parameters:
      dataset - dataset
    • getHyperparameters

      public Map<String,String> getHyperparameters()
      Returns the hyperparameters of the model the user sets.
      Returns:
      a Map with the hyperparameters
    • getNameVariables

      public List<String> getNameVariables()
      Returns the names of the variables of the PGM.
      Returns:
      names of the variables
    • setNameVariables

      public void setNameVariables(List<String> nameVariables)
      Set the name of the variables in the PGM.
      Parameters:
      nameVariables - names of the variables
    • getStructureConstraints

      public StructureConstraints getStructureConstraints()
      Returns the constraints that the PGM needs to meet.
      Returns:
      structure constraints
    • setStructureConstraints

      public void setStructureConstraints(StructureConstraints structureConstraints)
      Establishes the constraints that the PGM needs to meet.
      Parameters:
      structureConstraints - structure constraints to take into account during the learning of the model
    • getStructureLearningAlg

      public StructureLearningAlgorithm getStructureLearningAlg()
      Returns the algorithm used to learn the structure of the PGM.
      Returns:
      structure learning algorithm
    • setParameterLearningAlgorithm

      public void setParameterLearningAlgorithm(ParameterLearningAlgorithm parameterLearningAlg)
      Establishes the algorithm that will be used to learn the parameters of the PGM.
      Parameters:
      parameterLearningAlg - parameter learning algorithm
    • setStructureLearningAlgorithm

      public void setStructureLearningAlgorithm(StructureLearningAlgorithm structureLearningAlg)
      Establishes the algorithm that will be used to learn the structure of the PGM.
      Parameters:
      structureLearningAlg - structure learning algorithm