PropensityScore#

class causalpy.pymc_models.PropensityScore[source]#

Custom PyMC model for inverse propensity score models

Defines the PyMC model

\[\begin{split}\beta &\sim \mathrm{Normal}(0, 1) \\ \sigma &\sim \mathrm{HalfNormal}(1) \\ \mu &= X \cdot \beta \\ p &= \text{logit}^{-1}(\mu) \\ t &\sim \mathrm{Bernoulli}(p)\end{split}\]

Example

>>> import causalpy as cp
>>> import numpy as np
>>> from causalpy.pymc_models import PropensityScore
>>> df = cp.load_data('nhefs')
>>> X = df[["age", "race"]]
>>> t = np.asarray(df["trt"])
>>> ps = PropensityScore(sample_kwargs={"progressbar": False})
>>> ps.fit(X, t, coords={
...                 'coeffs': ['age', 'race'],
...                 'obs_ind': np.arange(df.shape[0])
...                },
...                prior={'b': [0, 1]},
... )
Inference...

Methods

PropensityScore.__init__([sample_kwargs])

PropensityScore.add_coord(name[, values, length])

Register a dimension coordinate with the model.

PropensityScore.add_coords(coords, *[, lengths])

Vectorized version of Model.add_coord.

PropensityScore.add_named_variable(var[, dims])

Add a random graph variable to the named variables of the model.

PropensityScore.build_model(X, t, coords, ...)

Defines the PyMC propensity model

PropensityScore.calculate_cumulative_impact(impact)

PropensityScore.calculate_impact(y_true, y_pred)

PropensityScore.check_start_vals(start, **kwargs)

Check that the logp is defined and finite at the starting point.

PropensityScore.compile_d2logp([vars, ...])

Compiled log probability density hessian function.

PropensityScore.compile_dlogp([vars, jacobian])

Compiled log probability density gradient function.

PropensityScore.compile_fn(outs, *[, ...])

Compiles a PyTensor function.

PropensityScore.compile_logp([vars, ...])

Compiled log probability density function.

PropensityScore.copy()

Clone the model.

PropensityScore.create_value_var(rv_var, *, ...)

Create a TensorVariable that will be used as the random variable's "value" in log-likelihood graphs.

PropensityScore.d2logp([vars, jacobian, ...])

Hessian of the models log-probability w.r.t.

PropensityScore.debug([point, fn, verbose])

Debug model function at point.

PropensityScore.dlogp([vars, jacobian])

Gradient of the models log-probability w.r.t.

PropensityScore.eval_rv_shapes()

Evaluate shapes of untransformed AND transformed free variables.

PropensityScore.fit(X, t, coords[, prior, ...])

Draw samples from posterior, prior predictive, and posterior predictive distributions.

PropensityScore.fit_outcome_model(X_outcome, ...)

Fit a Bayesian outcome model using covariates and previously estimated propensity scores.

PropensityScore.get_context([error_if_none, ...])

PropensityScore.initial_point([random_seed])

Compute the initial point of the model.

PropensityScore.logp([vars, jacobian, sum])

Elemwise log-probability of the model.

PropensityScore.logp_dlogp_function([...])

Compile a PyTensor function that computes logp and gradient.

PropensityScore.make_obs_var(rv_var, data, ...)

Create a TensorVariable for an observed random variable.

PropensityScore.name_for(name)

Check if name has prefix and adds if needed.

PropensityScore.name_of(name)

Check if name has prefix and deletes if needed.

PropensityScore.point_logps([point, round_vals])

Compute the log probability of point for all random variables in the model.

PropensityScore.predict(X)

Predict data given input data X

PropensityScore.print_coefficients(labels[, ...])

PropensityScore.profile(outs, *[, n, point, ...])

Compile and profile a PyTensor function which returns outs and takes values of model vars as a dict as an argument.

PropensityScore.register_data_var(data[, dims])

Register a data variable with the model.

PropensityScore.register_rv(rv_var, name, *)

Register an (un)observed random variable with the model.

PropensityScore.replace_rvs_by_values(...)

Clone and replace random variables in graphs with their value variables.

PropensityScore.score(X, y)

Score the Bayesian \(R^2\) given inputs X and outputs y.

PropensityScore.set_data(name, values[, coords])

Change the values of a data variable in the model.

PropensityScore.set_dim(name, new_length[, ...])

Update a mutable dimension.

PropensityScore.set_initval(rv_var, initval)

Set an initial value (strategy) for a random variable.

PropensityScore.shape_from_dims(dims)

PropensityScore.to_graphviz(*[, var_names, ...])

Produce a graphviz Digraph from a PyMC model.

Attributes

basic_RVs

List of random variables the model is defined in terms of.

continuous_value_vars

All the continuous value variables in the model.

coords

Coordinate values for model dimensions.

datalogp

PyTensor scalar of log-probability of the observed variables and potential terms.

dim_lengths

The symbolic lengths of dimensions in the model.

discrete_value_vars

All the discrete value variables in the model.

isroot

observedlogp

PyTensor scalar of log-probability of the observed variables.

parent

potentiallogp

PyTensor scalar of log-probability of the Potential terms.

prefix

root

unobserved_RVs

List of all random variables, including deterministic ones.

unobserved_value_vars

List of all random variables (including untransformed projections), as well as deterministics used as inputs and outputs of the model's log-likelihood graph.

value_vars

List of unobserved random variables used as inputs to the model's log-likelihood (which excludes deterministics).

varlogp

PyTensor scalar of log-probability of the unobserved random variables (excluding deterministic).

varlogp_nojac

PyTensor scalar of log-probability of the unobserved random variables (excluding deterministic) without jacobian term.

__init__(sample_kwargs=None)#
Parameters:

sample_kwargs (Optional[Dict[str, Any]]) – A dictionary of kwargs that get unpacked and passed to the pymc.sample() function. Defaults to an empty dictionary.

classmethod __new__(*args, **kwargs)#