|
| 1 | +Causal Inference |
| 2 | +================ |
| 3 | + |
| 4 | +This vignette provides a quick overview (using simulated data) of how to use ``stochtree`` for causal inference. |
| 5 | +Start by loading stochtree's ``BCFModel`` class and a number of other packages. |
| 6 | + |
| 7 | +.. code-block:: python |
| 8 | +
|
| 9 | + import numpy as np |
| 10 | + import pandas as pd |
| 11 | + import seaborn as sns |
| 12 | + import matplotlib.pyplot as plt |
| 13 | + from stochtree import BCFModel |
| 14 | + from sklearn.model_selection import train_test_split |
| 15 | +
|
| 16 | +Now, we generate a simulated causal inference problem |
| 17 | + |
| 18 | +.. code-block:: python |
| 19 | +
|
| 20 | + # RNG |
| 21 | + random_seed = 101 |
| 22 | + rng = np.random.default_rng(random_seed) |
| 23 | +
|
| 24 | + # Generate covariates and basis |
| 25 | + n = 1000 |
| 26 | + p_X = 5 |
| 27 | + X = rng.uniform(0, 1, (n, p_X)) |
| 28 | + pi_X = 0.25 + 0.5*X[:,0] |
| 29 | + Z = rng.binomial(1, pi_X, n).astype(float) |
| 30 | +
|
| 31 | + # Define the outcome mean functions (prognostic and treatment effects) |
| 32 | + mu_X = pi_X*5 |
| 33 | + # tau_X = np.sin(X[:,1]*2*np.pi) |
| 34 | + tau_X = X[:,1]*2 |
| 35 | +
|
| 36 | + # Generate outcome |
| 37 | + epsilon = rng.normal(0, 1, n) |
| 38 | + y = mu_X + tau_X*Z + epsilon |
| 39 | +
|
| 40 | +Split the dataset into train and test sets |
| 41 | + |
| 42 | +.. code-block:: python |
| 43 | +
|
| 44 | + sample_inds = np.arange(n) |
| 45 | + train_inds, test_inds = train_test_split(sample_inds, test_size=0.5) |
| 46 | + X_train = X[train_inds,:] |
| 47 | + X_test = X[test_inds,:] |
| 48 | + Z_train = Z[train_inds] |
| 49 | + Z_test = Z[test_inds] |
| 50 | + y_train = y[train_inds] |
| 51 | + y_test = y[test_inds] |
| 52 | + pi_train = pi_X[train_inds] |
| 53 | + pi_test = pi_X[test_inds] |
| 54 | + mu_train = mu_X[train_inds] |
| 55 | + mu_test = mu_X[test_inds] |
| 56 | + tau_train = tau_X[train_inds] |
| 57 | + tau_test = tau_X[test_inds] |
| 58 | +
|
| 59 | +Initialize and run a BCF sampler for 1000 iterations (after 10 "warm-start" draws) |
| 60 | + |
| 61 | +.. code-block:: python |
| 62 | +
|
| 63 | + bcf_model = BCFModel() |
| 64 | + bcf_model.sample(X_train, Z_train, y_train, pi_train, X_test, Z_test, pi_test, num_gfr=10, num_mcmc=1000) |
0 commit comments