SimpleCausalModel#
- class pgmpy.base.SimpleCausalModel(*args, backend=None, **kwargs)[source]#
Bases:
DAGA specialized DAG class for simple causal models.
This class simplifies the creation of causal graphs commonly used in causal inference, where the structure consists of exposures, outcomes, confounders, mediators, and instruments. It automatically adds the standard edges:
Exposures -> Outcomes (only if there are no mediators)
confounders -> Exposures
confounders -> Outcomes
Instruments -> Exposures
Exposures -> Mediators
Mediators -> Outcomes
If you want more control over the model structure, use the DAG class directly.
- Parameters:
- exposures: str, int, or iterable
If str or iterable, those would be used as the names of the exposure variables, If an int, exposures number of variables will be generated with role-based prefixes: E_0, E_1, …, E_n.
- outcomes: str, int, or iterable
If str or iterable, those would be used as the names of the outcome variables, If an int, outcomes number of variables will be generated with role-based prefixes: O_0, O_1, …, O_n.
- confounders: str, int, iterable, or None (default: None)
If str or iterable, those would be used as the names of the confounder variables, If an int, confounders number of variables will be generated with role-based prefixes: X_0, X_1, …, X_n.
- mediators: str, int, iterable, or None (default: None)
If str or iterable, those would be used as the names of the mediator variables, If an int, mediators number of variables will be generated with role-based prefixes: M_0, M_1, …, M_n.
- instruments: str, int, iterable, or None (default: None)
If str or iterable, those would be used as the names of the instrumental variables, If an int, instruments number of variables will be generated with role-based prefixes: I_0, I_1, …, I_n.
- latents: iterable or None (default: None)
List of latent variables.
Notes
A standard causal diagram (with mediators):
- I —> E —> M —> O
^ ^ | | X————-+
- Where:
I: Instrument E: Exposure M: Mediator O: Outcome X: Confounder (affects both E and O)
- If no mediators:
- I —> E —> O
^ ^ | | X——+
Examples
>>> from pgmpy.base import SimpleCausalModel >>> model = SimpleCausalModel( ... exposures="X", outcomes="Y", confounders="Z", mediators="M", instruments="I" ... ) >>> sorted(model.edges()) [('I', 'X'), ('M', 'Y'), ('X', 'M'), ('Z', 'X'), ('Z', 'Y')]
>>> model2 = SimpleCausalModel( ... exposures=1, outcomes=2, confounders=2, mediators=None, instruments=1 ... ) >>> sorted(model2.nodes()) ['E_0', 'I_0', 'O_0', 'O_1', 'X_0', 'X_1'] >>> from pprint import pprint >>> pprint(sorted(model2.edges())) [('E_0', 'O_0'), ('E_0', 'O_1'), ('I_0', 'E_0'), ('X_0', 'E_0'), ('X_0', 'O_0'), ('X_0', 'O_1'), ('X_1', 'E_0'), ('X_1', 'O_0'), ('X_1', 'O_1')]