causalis.dgp.causaldata_instrumental.base¶
Examples¶
from causalis.dgp.causaldata_instrumental.base import InstrumentalGenerator gen = InstrumentalGenerator(k=2, first_stage=1.5, seed=3141) df = gen.generate(500) {“y”, “d”, “z”, “x1”, “x2”}.issubset(df.columns) True
Low-level generators for instrumental-variable synthetic causal datasets.
The central class in this module is :class:InstrumentalGenerator, which
builds cross-sectional datasets with one binary instrument, one binary
endogenous treatment, optional observed confounders, and optional latent
confounding between treatment and outcome.
Module Contents¶
Classes¶
Generate synthetic datasets for binary instrumental-variable estimation. |
Data¶
API¶
- class causalis.dgp.causaldata_instrumental.base.InstrumentalGenerator¶
Bases:
causalis.dgp.causaldata.base.CausalDatasetGeneratorGenerate synthetic datasets for binary instrumental-variable estimation.
The generated structural system is:
Xis sampled with the same confounder machinery as :class:~causalis.dgp.causaldata.base.CausalDatasetGenerator.Zis a binary instrument generated fromP(Z=1|X).Dis a binary endogenous treatment generated fromP(D=1|Z,X,U).Ydepends onD,X, and optionally the latentU, but has no direct dependence onZ.
Parameters inherited from
CausalDatasetGeneratorkeep their usual meaning for the outcome equation and the non-instrument part of the treatment equation. In particular,beta_d,g_d,alpha_d,target_d_rate, andu_strength_daffect treatment assignment.Parameters
instrument_name : str, default=”z” Column name for the binary instrument. first_stage : float, default=1.25 Additive log-odds effect of
Zon treatment assignment. Positive values make the instrument encourage treatment. beta_z : array-like, optional Linear coefficients of confounders in the instrument propensity. g_z : callable, optional Nonlinear instrument scoreg_z(X) -> shape (n,). alpha_z : float, default=0.0 Instrument propensity intercept. Iftarget_z_rateis set, this is calibrated on each generated sample. target_z_rate : float, optional Target marginal instrument rate. Defaults to0.5. instrument_sharpness : float, default=1.0 Multiplier on the X-driven instrument score. include_oracle : bool, default=True Whether to include oracle columns for IV nuisance functions and treatment potential-outcome means.Notes
With
include_oracle=True, returned oracle columns include:m: instrument propensityP(Z=1|X).r_z0andr_z1: first-stage nuisancesP(D=1|Z=0,X)andP(D=1|Z=1,X).g_z0andg_z1: reduced-form nuisancesE[Y|Z=0,X]andE[Y|Z=1,X).iv_first_stageandiv_reduced_form: conditional differences in the first stage and reduced form.late_xandlate: conditional and sample-average Wald ratios.g_d0,g_d1, andcate: treatment potential-outcome means and their natural-scale contrast.
- instrument_name: str¶
‘z’
- first_stage: float¶
1.25
- beta_z: Optional[numpy.ndarray]¶
None
- g_z: Optional[Callable[[numpy.ndarray], numpy.ndarray]]¶
None
- alpha_z: float¶
0.0
- target_z_rate: Optional[float]¶
0.5
- instrument_sharpness: float¶
1.0
- __post_init__() None¶
Initialize RNG and validate IV-specific configuration.
- generate(n: int, U: Optional[numpy.ndarray] = None) pandas.DataFrame¶
Draw a synthetic IV dataset of size
n.Parameters
n : int Number of samples to generate. U : numpy.ndarray, optional Latent confounder. If omitted, sampled from
N(0, 1).Returns
pandas.DataFrame Generated dataset with outcome
y, treatmentd, instrumentz(orinstrument_name), confounders, and optional oracle columns.
- to_iv_causal_data(n: int, confounders: Optional[Union[str, List[str]]] = None) causalis.data_contracts.iv_causal_data.IVCausalData¶
Generate a dataset and convert it to :class:
IVCausalData.Oracle columns are intentionally not included as confounders when
confoundersis omitted.
- causalis.dgp.causaldata_instrumental.base.IVCausalDatasetGenerator¶
None