Package ch.idsia.credici.model
Class StructuralCausalModel
- java.lang.Object
-
- ch.idsia.crema.model.graphical.GenericSparseModel<ch.idsia.crema.factor.bayesian.BayesianFactor,ch.idsia.crema.model.graphical.SparseDirectedAcyclicGraph>
-
- ch.idsia.credici.model.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.2020A Structural Causal Model is a special type of
GenericSparseModel, composed withBayesianFactorand constructed on aSparseDirectedAcyclicGraph. Differs from Bayesian networks on having 2 different kind of variables: exogenous and endogenous
-
-
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 intaddVariable(int size, boolean exogenous)Add a new variable to the model.intaddVariable(int vid, int size, boolean exogenous)booleanareValidSE()StructuralCausalModelaverage(StructuralCausalModel model, int... vars)ch.idsia.crema.factor.bayesian.BayesianFactorconditionalProb(int[] left, int... right)ch.idsia.crema.factor.bayesian.BayesianFactorconditionalProb(int left, int... right)StructuralCausalModelcopy()Create a copy of this model (i.e.StringedgesToString()List<int[]>endoConnectComponents()HashMap<Integer,ch.idsia.crema.factor.bayesian.BayesianFactor>endogenousBlanketProb()ch.idsia.crema.model.StridesendogenousMarkovBlanket(int v)List<int[]>exoConnectComponents()voidfillExogenousWithRandomFactors(int prob_decimals)Attach to each variable (endogenous or exogenous) a random factor.voidfillWithRandomEquations()Attach to each variable (endogenous) a random factor.voidfillWithRandomFactors(int prob_decimals)Attach to each variable (endogenous or exogenous) a random factor.voidfillWithRandomFactors(int prob_decimals, boolean EqCheck, boolean fillEqs)Attach to each variable (endogenous or exogenous) a random factor.StructuralCausalModelfindModelWithEmpirical(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.BayesianNetworkgetEmpiricalNet()ch.idsia.crema.factor.bayesian.BayesianFactor[]getEmpiricalProbs()int[]getEndegenousParents(int... vars)Retruns an array with the IDs of parents that are endogenous variablesint[]getEndogenousChildren(int v)Retruns an array with the IDs of children that are endogenous variablesint[]getEndogenousVars()Retruns an array with IDs of the endogenous variablesint[]getExogenousChildren(int v)Retruns an array with the IDs of children that are exogenous variablesch.idsia.crema.model.graphical.SparseDirectedAcyclicGraphgetExogenousDAG()int[]getExogenousParents(int... vars)Retruns an array with the IDs of parents that are exogenous variablesintgetExogenousTreewidth()int[]getExogenousVars()Array with IDs of the exogenous variablesWorldMappinggetMap()Assuming that this SCM is a counterfactual model, this object associates the variables across the worlds.StringgetName()Returns the nane of the SCM.ch.idsia.crema.factor.bayesian.BayesianFactorgetProb(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)intgetTreewidth()StructuralCausalModelincrementVarIDs(int increment)StructuralCausalModelintervention(int var, int state)Returns a new SCM with the do operation done over a given variable.booleanisCompatible(gnu.trove.map.TIntIntMap[] data, int fixDecimals)booleanisEndogenous(int variable)Allows to know if a variable is endogenousbooleanisExogenous(int variable)Allows to know if a variable is exogenousintmaxEndoCC()intmaxExoCC()StructuralCausalModelmerge(StructuralCausalModel... models)Merge the current SCM with other equivalent ones to create a counterfactual model.static StructuralCausalModelof(ch.idsia.crema.model.graphical.SparseDirectedAcyclicGraph dag, int[] endoVarSizes)Constructs a makovian equationless SCM from a DAG a vector of cardinalitiesstatic StructuralCausalModelof(ch.idsia.crema.model.graphical.specialized.BayesianNetwork bnet)Constructs a makovian equationless SCM from a bayesian networkvoidprintSummary()Prints a summary of the SCMvoidrandomizeEndoChildren(int u)voidrandomizeEndoFactor(int x)voidrandomizeExoFactor(int u, int prob_decimals)voidremoveVariable(int variable)Removes a variable from the modelStructuralCausalModelreverseEdge(int from, int to)StructuralCausalModelreverseExoEdges()gnu.trove.map.TIntIntMapsample(int... vars)gnu.trove.map.TIntIntMapsample(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.BayesianNetworktoBnet()ch.idsia.crema.model.graphical.SparseModeltoCredalNetwork(boolean vertex, ch.idsia.crema.factor.bayesian.BayesianFactor... empiricalProbs)Deprecated.ch.idsia.crema.model.graphical.SparseModeltoCredalNetwork(ch.idsia.crema.factor.bayesian.BayesianFactor... empiricalProbs)Deprecated.ch.idsia.crema.model.graphical.SparseModeltoHCredal(ch.idsia.crema.factor.bayesian.BayesianFactor... empiricalProbs)ch.idsia.crema.model.graphical.SparseModeltoHCredal(Collection empiricalProbs)StringtoString()String summarizing this SCM.ch.idsia.crema.model.graphical.SparseModeltoVCredal(ch.idsia.crema.factor.bayesian.BayesianFactor... empiricalProbs)ch.idsia.crema.model.graphical.SparseModeltoVCredal(Collection empiricalProbs)ch.idsia.crema.model.graphical.SparseModeltoVertexSimple(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:
copyin interfacech.idsia.crema.model.GraphicalModel<ch.idsia.crema.factor.bayesian.BayesianFactor>- Specified by:
copyin interfacech.idsia.crema.model.Model<ch.idsia.crema.factor.bayesian.BayesianFactor>- Overrides:
copyin classch.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 variableexogenous- - 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:
removeVariablein interfacech.idsia.crema.model.Model<ch.idsia.crema.factor.bayesian.BayesianFactor>- Overrides:
removeVariablein classch.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:
-
merge
public StructuralCausalModel merge(StructuralCausalModel... models)
Merge the current SCM with other equivalent ones to create a counterfactual model.- Parameters:
models-- Returns:
-
toString
public String toString()
String summarizing this SCM.- Overrides:
toStringin classch.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()
-
reverseEdge
public StructuralCausalModel reverseEdge(int from, int to)
-
reverseExoEdges
public StructuralCausalModel reverseExoEdges()
-
endogenousBlanketProb
public HashMap<Integer,ch.idsia.crema.factor.bayesian.BayesianFactor> endogenousBlanketProb()
-
average
public StructuralCausalModel average(StructuralCausalModel model, int... vars)
-
getExogenousDAG
public ch.idsia.crema.model.graphical.SparseDirectedAcyclicGraph getExogenousDAG()
-
getExogenousTreewidth
public int getExogenousTreewidth()
-
getTreewidth
public int getTreewidth()
-
incrementVarIDs
public StructuralCausalModel incrementVarIDs(int increment)
-
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()
-
-