bartz.bcf.bcf¶
- class bartz.bcf.bcf(x_train, y_train, z_train, *, pihat_train=None, x_test=None, z_test=None, pihat_test=None, include_pihat_in_mu=True, include_pihat_in_tau=False, num_trees_mu=250, num_trees_tau=100, ndpost=1000, nskip=100, num_chains=None, k_mu=2.0, k_tau=10.0, sigma_df=3.0, sigma_scale='auto', sigma_init='auto', leaf_prior_cov_inv_mu=None, leaf_prior_cov_inv_tau=None, min_points_per_leaf_mu=5, min_points_per_leaf_tau=5, maxdepth_mu=6, maxdepth_tau=6, tau_0_prior_var=None, sample_intercept=True, adaptive_coding=False, sample_sigma2_leaf_mu=True, sigma2_leaf_shape_mu=3.0, sigma2_leaf_scale_mu=None, sample_sigma2_leaf_tau=False, sigma2_leaf_shape_tau=3.0, sigma2_leaf_scale_tau=None, standardize=True, outcome_type='continuous', delta_max=0.9, seed=0)[source]¶
Bayesian Causal Forests (BCF).
Regress
y_trainonx_trainandz_train(treatment) with two latent mean functions represented as sums of decision trees: Y = mu(X, pihat) + tau(X, pihat) * Z + errorFor continuous outcomes, the hyperparameters with units are on the scale of the standardized response, see
standardize, while the outputs are on the scale ofy_train.- Parameters:
x_train (
Real[Array, 'n p']|Real[ndarray, 'n p']|DataFrame) – The training predictors (confounders/modifiers).y_train (
Float32[Array, 'n']|Float32[ndarray, 'n']|Series) – The training responses.z_train (
Float32[Array, 'n']|Float32[ndarray, 'n']|Series) – The binary treatment assignment (0 or 1).pihat_train (
Float32[Array, 'n']|Float32[ndarray, 'n']|Series|None, default:None) – The estimated propensity scores. If provided, appended tox_train.x_test (
Real[Array, 'm p']|Real[ndarray, 'm p']|DataFrame|None, default:None) – The test predictors. If provided, predictions at these points are computed and stored inmu_testandtau_test.z_test (
Float32[Array, 'm']|Float32[ndarray, 'm']|Series|None, default:None) – The test treatment assignment (0 or 1). If provided together withx_test, the outcome prediction is stored inyhat_test.pihat_test (
Float32[Array, 'm']|Float32[ndarray, 'm']|Series|None, default:None) – The test propensity scores. Must be passed together withpihat_train.include_pihat_in_mu (
bool, default:True) – Whether to include propensity scores in the prognostic forest.include_pihat_in_tau (
bool, default:False) – Whether to include propensity scores in the treatment effect forest.num_trees_mu (
int, default:250) – The number of trees used for the prognostic forestmu.num_trees_tau (
int, default:100) – The number of trees used for the treatment effect foresttau.ndpost (
int, default:1000) – The number of MCMC samples to save, after burn-in, per chain. The posterior samples of all chains are concatenated, for a total ofnum_chains * ndpost.nskip (
int, default:100) – The number of initial MCMC samples to discard as burn-in, per chain.num_chains (
int|None, default:None) – The number of independent MCMC chains.Nonefor a single chain without an explicit chain axis, which is equivalent to 1.k_mu (
float, default:2.0) – Prior parameter k for prognostic forest.k_tau (
float, default:10.0) – Prior parameter k for treatment forest.sigma_df (
float, default:3.0) – Prior degrees of freedom for error variance.sigma_scale (
float|Literal['auto'], default:'auto') – Prior scale of the error standard deviation.sigma_init (
float|Literal['auto'], default:'auto') – Initial value of the error standard deviation.leaf_prior_cov_inv_mu (
float|Float[Array, '']|Float[ndarray, '']|None, default:None) – Custom leaf prior precision for the prognostic forest.leaf_prior_cov_inv_tau (
float|Float[Array, '']|Float[ndarray, '']|None, default:None) – Custom leaf prior precision for the treatment effect forest.min_points_per_leaf_mu (
int, default:5) – Minimum data points per leaf for prognostic forest.min_points_per_leaf_tau (
int, default:5) – Minimum data points per leaf for treatment forest, counting untreated units too.maxdepth_mu (
int, default:6)maxdepth_tau (
int, default:6) – The maximum depth of the trees in the prognostic/treatment effect forest. This is 1-based, so with the defaultmaxdepth_mu/tau=6, the depths of the levels range from 0 to 5.tau_0_prior_var (
float|None, default:None) – Prior variance for global intercept tau_0.sample_intercept (
bool, default:True) – Whether to sample a global treatment intercepttau_0.adaptive_coding (
bool, default:False) – Whether to use adaptive coding for the treatment effect, i.e., to sample the coding weights of untreated and treated units. Their priors are independent N(0, 1/2), truncated to|b| >= 0.01for numerical accuracy.sample_sigma2_leaf_mu (
bool, default:True) – Whether to sample the leaf parameter variance for the prognostic forest.sigma2_leaf_shape_mu (
float, default:3.0) – The shape parameter for the Inverse-Gamma prior on the prognostic forest leaf variance.sigma2_leaf_scale_mu (
float|Float[Array, '']|Float[ndarray, '']|None, default:None) – The scale parameter for the Inverse-Gamma prior on the prognostic forest leaf variance.sample_sigma2_leaf_tau (
bool, default:False) – Whether to sample the leaf parameter variance for the treatment effect forest.sigma2_leaf_shape_tau (
float, default:3.0) – The shape parameter for the Inverse-Gamma prior on the treatment effect forest leaf variance.sigma2_leaf_scale_tau (
float|Float[Array, '']|Float[ndarray, '']|None, default:None) – The scale parameter for the Inverse-Gamma prior on the treatment effect forest leaf variance.standardize (
bool, default:True) – Whether to standardizey_traininternally, ignored for binary outcomes. IfFalse, the hyperparameters with units (sigma_scale,sigma_init,leaf_prior_cov_inv_*,tau_0_prior_var,sigma2_leaf_scale_*) are on the scale ofy_traininstead of the standardized one.outcome_type (
Literal['continuous','binary'], default:'continuous') – Either ‘continuous’ or ‘binary’ (probit link).delta_max (
float, default:0.9) – Maximum plausible treatment effect on the probability scale for binary probit models.seed (
int|Key[Array, ''], default:0) – The seed for the random number generator.
- Raises:
ValueError – If
z_train,z_test, ory_trainfor binary outcomes, has values other than 0 or 1, or if the format ofx_testdoes not matchx_trainformat, or ifz_testorpihat_testis passed withoutx_test, or if only one ofpihat_trainandpihat_testis passed, or if the length ofy_train,z_trainorpihat_traindoes not matchx_train, or the length ofz_testorpihat_testdoes not matchx_test, or ifpihat_trainis passed but excluded from both forests, or ifx_train,x_test,pihat_trainorpihat_testcontains NaN.
- save_npz(path)[source]¶
Save the fitted model to a compressed npz archive.
Notes
Files written by a bartz release can be loaded by later releases, but not by earlier ones; files written by development versions may not be.
- classmethod load_npz(path)[source]¶
Load a model saved with
save_npz.Archives written by
save_npzin bartz 0.13 load as models that can only predict.
- predict(x_test, *, pihat_test=None)[source]¶
Compute the control mean and the treatment effect at
x_test.- Parameters:
- Returns:
BCFPrediction– The posterior samples atx_test.
- property offset: Float32[Array, ''][source]¶
The prior mean of the prognostic function.
On the latent probit scale for binary outcomes.
- property sigma_trace: Float32[Array, 'num_samples'][source]¶
The posterior trace of residual standard deviation on the outcome scale, chains concatenated.
- property mu_test: Float32[Array, 'num_samples m'] | None[source]¶
The control mean at
x_testfor each MCMC iteration.On the latent probit scale for binary outcomes.
- property tau_test: Float32[Array, 'num_samples m'] | None[source]¶
The treatment effect at
x_testfor each MCMC iteration.On the latent probit scale for binary outcomes.
- property yhat_test: Float32[Array, 'num_samples m'] | None[source]¶
The expected outcome at
x_testunderz_testfor each MCMC iteration.On the latent probit scale for binary outcomes; see
prob_test.
- property prob_test: Float32[Array, 'num_samples m'] | None[source]¶
The probability of y being True at
x_testunderz_test.Noneunless the outcome is binary andx_testandz_testwere passed to the constructor.
- predict_potential_outcomes(x_test, *, key, pihat_test=None, rho=0.0)[source]¶
Sample joint posterior predictive potential outcomes Y(0), Y(1), and “lift” Y(1) - Y(0).
- Parameters:
x_test (
Real[Array, 'm p']|Real[ndarray, 'm p']|DataFrame) – The test predictors.key (
int|Key[Array, '']) – A jax random key or an integer seed for sampling the errors.pihat_test (
Float32[Array, 'm']|Float32[ndarray, 'm']|Series|None, default:None) – The test propensity scores, required iff the model was fit withpihat_train.rho (
float|Float[Array, '']|Float[ndarray, ''], default:0.0) – The correlation in [-1, 1] between the errors ofy0andy1. The data carry no information on it, see [1]. It affects onlydeltainBCFPotentialOutcomes, widening it asrhodecreases.
- Returns:
BCFPotentialOutcomes– The posterior predictive samples atx_test.
References