causal_networkx.StructuralCausalModel#
- class causal_networkx.StructuralCausalModel(exogenous, endogenous)[source]#
Structural Causal Model (SCM) class.
Assumes that all exogenous variables are independent of each other. That is no exogenous variable is a function of other exogenous variables passed in.
This assumes the causal independence mechanism, where all exogenous variables are independent of each other.
- Parameters:
exogenous :
Dict
of functionsThe exogenous variables and their functional form passed in as values. This forms a symbolic mapping from exogenous variable names to their distribution. The exogenous variable functions should not have any parameters.
endogenous :
Dict
of lambda functionsThe endogenous variable functions may have parameters.
Examples
>>> import numpy as np >>> rng = np.random.RandomState() >>> func_uxy = rng.uniform >>> func_uz = rng.uniform >>> func_x = lambda u_xy: 2 * u_xy >>> func_y = lambda x, u_xy: x >>> func_z = lambda u_z: u_z**2 >>> scm = StructuralCausalModel( exogenous={ "u_xy": func_uxy, }, endogenous={"x": func_x, "y": func_y}, )
- Attributes:
causal_dependencies :
dict
A mapping of each variable and its causal dependencies based on the SCM functions.
var_list :
list
The list of variable names in the SCM.
_symbolic_runtime :
dict
The mapping from each variable in the SCM to the sampled value of that variable. Used when sampling from the SCM.
Methods
Compute the induced causal diagram.
sample
([n, include_latents])Sample from the SCM.
- sample(n=1000, include_latents=True)[source]#
Sample from the SCM.
- Parameters:
n :
int
, optionalNumber of samples to generate, by default 1000.
include_latents : bool, optional
Whether to include latent variables in the returned dataset, by default True.
- Returns:
result_df :
pd.DataFrame
The sampled dataset.
- :rtype:py:class:
DataFrame
- :rtype:py:class: