Class StructuralCausalModel

  • All Implemented Interfaces:
    ch.idsia.crema.model.GraphicalModel<ch.idsia.crema.factor.bayesian.BayesianFactor>, ch.idsia.crema.model.Model<ch.idsia.crema.factor.bayesian.BayesianFactor>

    public class StructuralCausalModel
    extends ch.idsia.crema.model.graphical.GenericSparseModel<ch.idsia.crema.factor.bayesian.BayesianFactor,​ch.idsia.crema.model.graphical.SparseDirectedAcyclicGraph>
    Author: Rafael CabaƱas Date: 04.02.2020

    A Structural Causal Model is a special type of GenericSparseModel, composed with BayesianFactor and constructed on a SparseDirectedAcyclicGraph. Differs from Bayesian networks on having 2 different kind of variables: exogenous and endogenous

    • Field Summary

      • Fields inherited from class ch.idsia.crema.model.graphical.GenericSparseModel

        cardinalities, cardinalityChanger, domainChanger, factors, max, network
    • Constructor Summary

      Constructors 
      Constructor Description
      StructuralCausalModel()
      Create the directed model using the specified network implementation.
      StructuralCausalModel​(ch.idsia.crema.model.graphical.SparseDirectedAcyclicGraph empiricalDAG, int[] endoVarSizes, int... exoVarSizes)
      Builds a simple SCM from a empirical DAG such that each each endogenous variable (i.e., those from in the DAG) has a single exogenous variable.
      StructuralCausalModel​(String name)  
    • Method Summary

      Modifier and Type Method Description
      int addVariable​(int size, boolean exogenous)
      Add a new variable to the model.
      int addVariable​(int vid, int size, boolean exogenous)  
      boolean areValidSE()  
      StructuralCausalModel average​(StructuralCausalModel model, int... vars)  
      ch.idsia.crema.factor.bayesian.BayesianFactor conditionalProb​(int[] left, int... right)  
      ch.idsia.crema.factor.bayesian.BayesianFactor conditionalProb​(int left, int... right)  
      StructuralCausalModel copy()
      Create a copy of this model (i.e.
      String edgesToString()  
      List<int[]> endoConnectComponents()  
      HashMap<Integer,​ch.idsia.crema.factor.bayesian.BayesianFactor> endogenousBlanketProb()  
      ch.idsia.crema.model.Strides endogenousMarkovBlanket​(int v)  
      List<int[]> exoConnectComponents()  
      void fillExogenousWithRandomFactors​(int prob_decimals)
      Attach to each variable (endogenous or exogenous) a random factor.
      void fillWithRandomEquations()
      Attach to each variable (endogenous) a random factor.
      void fillWithRandomFactors​(int prob_decimals)
      Attach to each variable (endogenous or exogenous) a random factor.
      void fillWithRandomFactors​(int prob_decimals, boolean EqCheck, boolean fillEqs)
      Attach to each variable (endogenous or exogenous) a random factor.
      StructuralCausalModel findModelWithEmpirical​(int prob_decimals, ch.idsia.crema.factor.bayesian.BayesianFactor[] empirical, int[] keepFactors, long maxIterations)  
      double[][] getCoeff​(int u)
      Transforms the structural equations associated to the childrens of a given U into a coefficient matrix.
      HashMap<Set<Integer>,​ch.idsia.crema.factor.bayesian.BayesianFactor> getEmpiricalMap()  
      HashMap<Set<Integer>,​ch.idsia.crema.factor.bayesian.BayesianFactor> getEmpiricalMap​(boolean fix)  
      ch.idsia.crema.model.graphical.specialized.BayesianNetwork getEmpiricalNet()  
      ch.idsia.crema.factor.bayesian.BayesianFactor[] getEmpiricalProbs()  
      int[] getEndegenousParents​(int... vars)
      Retruns an array with the IDs of parents that are endogenous variables
      int[] getEndogenousChildren​(int v)
      Retruns an array with the IDs of children that are endogenous variables
      int[] getEndogenousVars()
      Retruns an array with IDs of the endogenous variables
      int[] getExogenousChildren​(int v)
      Retruns an array with the IDs of children that are exogenous variables
      ch.idsia.crema.model.graphical.SparseDirectedAcyclicGraph getExogenousDAG()  
      int[] getExogenousParents​(int... vars)
      Retruns an array with the IDs of parents that are exogenous variables
      int getExogenousTreewidth()  
      int[] getExogenousVars()
      Array with IDs of the exogenous variables
      WorldMapping getMap()
      Assuming that this SCM is a counterfactual model, this object associates the variables across the worlds.
      String getName()
      Returns the nane of the SCM.
      ch.idsia.crema.factor.bayesian.BayesianFactor getProb​(int... vars)
      Gets the empirical probability of a endogenous variable by marginalizing out all its exogenous parents.
      gnu.trove.map.TIntObjectMap[] getRandomFactors​(int prob_decimals)
      Get valid random SCM specification (i.e., empirical probabilities + equations)
      int getTreewidth()  
      StructuralCausalModel incrementVarIDs​(int increment)  
      StructuralCausalModel intervention​(int var, int state)
      Returns a new SCM with the do operation done over a given variable.
      boolean isCompatible​(gnu.trove.map.TIntIntMap[] data, int fixDecimals)  
      boolean isEndogenous​(int variable)
      Allows to know if a variable is endogenous
      boolean isExogenous​(int variable)
      Allows to know if a variable is exogenous
      int maxEndoCC()  
      int maxExoCC()  
      StructuralCausalModel merge​(StructuralCausalModel... models)
      Merge the current SCM with other equivalent ones to create a counterfactual model.
      static StructuralCausalModel of​(ch.idsia.crema.model.graphical.SparseDirectedAcyclicGraph dag, int[] endoVarSizes)
      Constructs a makovian equationless SCM from a DAG a vector of cardinalities
      static StructuralCausalModel of​(ch.idsia.crema.model.graphical.specialized.BayesianNetwork bnet)
      Constructs a makovian equationless SCM from a bayesian network
      void printSummary()
      Prints a summary of the SCM
      void randomizeEndoChildren​(int u)  
      void randomizeEndoFactor​(int x)  
      void randomizeExoFactor​(int u, int prob_decimals)  
      void removeVariable​(int variable)
      Removes a variable from the model
      StructuralCausalModel reverseEdge​(int from, int to)  
      StructuralCausalModel reverseExoEdges()  
      gnu.trove.map.TIntIntMap sample​(int... vars)  
      gnu.trove.map.TIntIntMap sample​(gnu.trove.map.TIntIntMap obs, int... vars)  
      gnu.trove.map.TIntIntMap[] samples​(int N, int... vars)  
      gnu.trove.map.TIntIntMap[] samples​(int N, gnu.trove.map.TIntIntMap obs, int... vars)  
      gnu.trove.map.TIntIntMap[] samples​(gnu.trove.map.TIntIntMap[] observations, int... vars)  
      ch.idsia.crema.model.graphical.specialized.BayesianNetwork toBnet()  
      ch.idsia.crema.model.graphical.SparseModel toCredalNetwork​(boolean vertex, ch.idsia.crema.factor.bayesian.BayesianFactor... empiricalProbs)
      Deprecated.
      ch.idsia.crema.model.graphical.SparseModel toCredalNetwork​(ch.idsia.crema.factor.bayesian.BayesianFactor... empiricalProbs)
      Deprecated.
      ch.idsia.crema.model.graphical.SparseModel toHCredal​(ch.idsia.crema.factor.bayesian.BayesianFactor... empiricalProbs)  
      ch.idsia.crema.model.graphical.SparseModel toHCredal​(Collection empiricalProbs)  
      String toString()
      String summarizing this SCM.
      ch.idsia.crema.model.graphical.SparseModel toVCredal​(ch.idsia.crema.factor.bayesian.BayesianFactor... empiricalProbs)  
      ch.idsia.crema.model.graphical.SparseModel toVCredal​(Collection empiricalProbs)  
      ch.idsia.crema.model.graphical.SparseModel toVertexSimple​(ch.idsia.crema.factor.bayesian.BayesianFactor... empiricalProbs)
      Deprecated.
      • Methods inherited from class ch.idsia.crema.model.graphical.GenericSparseModel

        addParent, addParents, addState, addVariable, addVariable, addVariables, areConnected, convert, correctFactorDomains, getAncestors, getChildren, getDescendants, getDisconnected, getDomain, getFactor, getFactors, getFactors, getLeaves, getNetwork, getParents, getRoots, getSize, getSizes, getVariableAndParents, getVariables, getVariablesCount, inspectFactorDomains, observe, removeParent, removeParent, removeState, setFactor, setFactors
    • Constructor Detail

      • StructuralCausalModel

        public StructuralCausalModel()
        Create the directed model using the specified network implementation.
      • StructuralCausalModel

        public StructuralCausalModel​(String name)
      • StructuralCausalModel

        public StructuralCausalModel​(ch.idsia.crema.model.graphical.SparseDirectedAcyclicGraph empiricalDAG,
                                     int[] endoVarSizes,
                                     int... exoVarSizes)
        Builds a simple SCM from a empirical DAG such that each each endogenous variable (i.e., those from in the DAG) has a single exogenous variable.
        Parameters:
        empiricalDAG -
        endoVarSizes -
        exoVarSizes -
    • Method Detail

      • of

        public static StructuralCausalModel of​(ch.idsia.crema.model.graphical.specialized.BayesianNetwork bnet)
        Constructs a makovian equationless SCM from a bayesian network
        Parameters:
        bnet -
        Returns:
      • of

        public static StructuralCausalModel of​(ch.idsia.crema.model.graphical.SparseDirectedAcyclicGraph dag,
                                               int[] endoVarSizes)
        Constructs a makovian equationless SCM from a DAG a vector of cardinalities
        Parameters:
        dag -
        endoVarSizes -
        Returns:
      • copy

        public StructuralCausalModel copy()
        Create a copy of this model (i.e. dag and factors are copied)
        Specified by:
        copy in interface ch.idsia.crema.model.GraphicalModel<ch.idsia.crema.factor.bayesian.BayesianFactor>
        Specified by:
        copy in interface ch.idsia.crema.model.Model<ch.idsia.crema.factor.bayesian.BayesianFactor>
        Overrides:
        copy in class ch.idsia.crema.model.graphical.GenericSparseModel<ch.idsia.crema.factor.bayesian.BayesianFactor,​ch.idsia.crema.model.graphical.SparseDirectedAcyclicGraph>
        Returns:
      • addVariable

        public int addVariable​(int size,
                               boolean exogenous)
        Add a new variable to the model. Added variables will be appended to the model and the index of the added variable will be returned. Adding a variable will always return a value that is greater than any other variable in the model.
        Parameters:
        size - - int the number of states in the variable
        exogenous - - boolean indicating if the variable is exogenous.
        Returns:
        int - the label/index/id assigned to the variable
      • addVariable

        public int addVariable​(int vid,
                               int size,
                               boolean exogenous)
      • removeVariable

        public void removeVariable​(int variable)
        Removes a variable from the model
        Specified by:
        removeVariable in interface ch.idsia.crema.model.Model<ch.idsia.crema.factor.bayesian.BayesianFactor>
        Overrides:
        removeVariable in class ch.idsia.crema.model.graphical.GenericSparseModel<ch.idsia.crema.factor.bayesian.BayesianFactor,​ch.idsia.crema.model.graphical.SparseDirectedAcyclicGraph>
        Parameters:
        variable -
      • getExogenousVars

        public int[] getExogenousVars()
        Array with IDs of the exogenous variables
        Returns:
      • isExogenous

        public boolean isExogenous​(int variable)
        Allows to know if a variable is exogenous
        Parameters:
        variable -
        Returns:
      • isEndogenous

        public boolean isEndogenous​(int variable)
        Allows to know if a variable is endogenous
        Parameters:
        variable -
        Returns:
      • getEndogenousVars

        public int[] getEndogenousVars()
        Retruns an array with IDs of the endogenous variables
        Returns:
      • getExogenousParents

        public int[] getExogenousParents​(int... vars)
        Retruns an array with the IDs of parents that are exogenous variables
        Parameters:
        vars -
        Returns:
      • getEndegenousParents

        public int[] getEndegenousParents​(int... vars)
        Retruns an array with the IDs of parents that are endogenous variables
        Parameters:
        vars -
        Returns:
      • getExogenousChildren

        public int[] getExogenousChildren​(int v)
        Retruns an array with the IDs of children that are exogenous variables
        Parameters:
        v -
        Returns:
      • getEndogenousChildren

        public int[] getEndogenousChildren​(int v)
        Retruns an array with the IDs of children that are endogenous variables
        Parameters:
        v -
        Returns:
      • fillWithRandomFactors

        public void fillWithRandomFactors​(int prob_decimals)
        Attach to each variable (endogenous or exogenous) a random factor. Structural Equations are not checked.
        Parameters:
        prob_decimals -
      • fillWithRandomFactors

        public void fillWithRandomFactors​(int prob_decimals,
                                          boolean EqCheck,
                                          boolean fillEqs)
        Attach to each variable (endogenous or exogenous) a random factor.
        Parameters:
        prob_decimals -
        EqCheck -
      • fillExogenousWithRandomFactors

        public void fillExogenousWithRandomFactors​(int prob_decimals)
        Attach to each variable (endogenous or exogenous) a random factor.
        Parameters:
        prob_decimals -
      • randomizeExoFactor

        public void randomizeExoFactor​(int u,
                                       int prob_decimals)
      • randomizeEndoFactor

        public void randomizeEndoFactor​(int x)
      • randomizeEndoChildren

        public void randomizeEndoChildren​(int u)
      • fillWithRandomEquations

        public void fillWithRandomEquations()
        Attach to each variable (endogenous) a random factor.
      • getRandomFactors

        public gnu.trove.map.TIntObjectMap[] getRandomFactors​(int prob_decimals)
        Get valid random SCM specification (i.e., empirical probabilities + equations)
        Parameters:
        prob_decimals -
        Returns:
      • getProb

        public ch.idsia.crema.factor.bayesian.BayesianFactor getProb​(int... vars)
        Gets the empirical probability of a endogenous variable by marginalizing out all its exogenous parents. In case of more than one input variable, returns the combination of the probability of each variable.
        Parameters:
        vars -
        Returns:
      • conditionalProb

        public ch.idsia.crema.factor.bayesian.BayesianFactor conditionalProb​(int[] left,
                                                                             int... right)
      • conditionalProb

        public ch.idsia.crema.factor.bayesian.BayesianFactor conditionalProb​(int left,
                                                                             int... right)
      • intervention

        public StructuralCausalModel intervention​(int var,
                                                  int state)
        Returns a new SCM with the do operation done over a given variable.
        Parameters:
        var - - target variable.
        state - - state to fix.
        Returns:
      • printSummary

        public void printSummary()
        Prints a summary of the SCM
      • toVertexSimple

        @Deprecated
        public ch.idsia.crema.model.graphical.SparseModel toVertexSimple​(ch.idsia.crema.factor.bayesian.BayesianFactor... empiricalProbs)
        Deprecated.
        Converts the current SCM into an equivalent credal network consistent with the empirical probabilities (of the endogenous variables). This is the simple case where each endogenous variable has a single and non-shared exogenous parent.
        Parameters:
        empiricalProbs - - for each exogenous variable U, the empirical probability of the children given the endogenous parents.
        Returns:
      • toCredalNetwork

        @Deprecated
        public ch.idsia.crema.model.graphical.SparseModel toCredalNetwork​(ch.idsia.crema.factor.bayesian.BayesianFactor... empiricalProbs)
        Deprecated.
        Converts the current SCM into an equivalent credal network consistent with the empirical probabilities (of the endogenous variables). In this case exogenous parentes might have more than one endogenous child. Resulting factors are of class VertexFactor.
        Parameters:
        empiricalProbs - - for each exogenous variable U, the empirical probability of the children given the endogenous parents.
        Returns:
      • toCredalNetwork

        @Deprecated
        public ch.idsia.crema.model.graphical.SparseModel toCredalNetwork​(boolean vertex,
                                                                          ch.idsia.crema.factor.bayesian.BayesianFactor... empiricalProbs)
        Deprecated.
        Converts the current SCM into an equivalent credal network consistent with the empirical probabilities (of the endogenous variables). In this case exogenous parentes might have more than one endogenous child.
        Parameters:
        vertex - - flag indicating if the resulting factors are of class VertexFactor (true) or SeparateHalfspaceFactor (false)
        empiricalProbs - - for each exogenous variable U, the empirical probability of the children given the endogenous parents.
        Returns:
      • toHCredal

        public ch.idsia.crema.model.graphical.SparseModel toHCredal​(ch.idsia.crema.factor.bayesian.BayesianFactor... empiricalProbs)
      • toHCredal

        public ch.idsia.crema.model.graphical.SparseModel toHCredal​(Collection empiricalProbs)
      • toVCredal

        public ch.idsia.crema.model.graphical.SparseModel toVCredal​(ch.idsia.crema.factor.bayesian.BayesianFactor... empiricalProbs)
      • toVCredal

        public ch.idsia.crema.model.graphical.SparseModel toVCredal​(Collection empiricalProbs)
      • toBnet

        public ch.idsia.crema.model.graphical.specialized.BayesianNetwork toBnet()
      • getCoeff

        public double[][] getCoeff​(int u)
        Transforms the structural equations associated to the childrens of a given U into a coefficient matrix.
        Parameters:
        u -
        Returns:
      • areValidSE

        public boolean areValidSE()
      • getMap

        public WorldMapping getMap()
        Assuming that this SCM is a counterfactual model, this object associates the variables across the worlds.
        Returns:
      • toString

        public String toString()
        String summarizing this SCM.
        Overrides:
        toString in class ch.idsia.crema.model.graphical.GenericSparseModel<ch.idsia.crema.factor.bayesian.BayesianFactor,​ch.idsia.crema.model.graphical.SparseDirectedAcyclicGraph>
        Returns:
      • getName

        public String getName()
        Returns the nane of the SCM.
        Returns:
      • getEmpiricalProbs

        public ch.idsia.crema.factor.bayesian.BayesianFactor[] getEmpiricalProbs()
      • getEmpiricalMap

        public HashMap<Set<Integer>,​ch.idsia.crema.factor.bayesian.BayesianFactor> getEmpiricalMap​(boolean fix)
      • getEmpiricalMap

        public HashMap<Set<Integer>,​ch.idsia.crema.factor.bayesian.BayesianFactor> getEmpiricalMap()
      • getEmpiricalNet

        public ch.idsia.crema.model.graphical.specialized.BayesianNetwork getEmpiricalNet()
      • findModelWithEmpirical

        public StructuralCausalModel findModelWithEmpirical​(int prob_decimals,
                                                            ch.idsia.crema.factor.bayesian.BayesianFactor[] empirical,
                                                            int[] keepFactors,
                                                            long maxIterations)
      • samples

        public gnu.trove.map.TIntIntMap[] samples​(int N,
                                                  int... vars)
      • samples

        public gnu.trove.map.TIntIntMap[] samples​(gnu.trove.map.TIntIntMap[] observations,
                                                  int... vars)
      • samples

        public gnu.trove.map.TIntIntMap[] samples​(int N,
                                                  gnu.trove.map.TIntIntMap obs,
                                                  int... vars)
      • sample

        public gnu.trove.map.TIntIntMap sample​(int... vars)
      • sample

        public gnu.trove.map.TIntIntMap sample​(gnu.trove.map.TIntIntMap obs,
                                               int... vars)
      • endogenousMarkovBlanket

        public ch.idsia.crema.model.Strides endogenousMarkovBlanket​(int v)
      • edgesToString

        public String edgesToString()
      • endogenousBlanketProb

        public HashMap<Integer,​ch.idsia.crema.factor.bayesian.BayesianFactor> endogenousBlanketProb()
      • getExogenousDAG

        public ch.idsia.crema.model.graphical.SparseDirectedAcyclicGraph getExogenousDAG()
      • getExogenousTreewidth

        public int getExogenousTreewidth()
      • getTreewidth

        public int getTreewidth()
      • isCompatible

        public boolean isCompatible​(gnu.trove.map.TIntIntMap[] data,
                                    int fixDecimals)
      • endoConnectComponents

        public List<int[]> endoConnectComponents()
      • exoConnectComponents

        public List<int[]> exoConnectComponents()
      • maxExoCC

        public int maxExoCC()
      • maxEndoCC

        public int maxEndoCC()