Nonparametric Instrumental Variables

Two-stage Least Squares

When the relationship between the outcome \(y\), treatment \(x\) and covariate \(v\) are assumed to be linear, e.g., [Angrist1996],

\[\begin{split}y & = \alpha x + \beta v + e \\ x & = \gamma z + \lambda v + \eta,\end{split}\]

then the IV framework becomes direct: it will first train a linear model for \(x\) given \(z\) and \(v\), then it replaces \(x\) with the predicted values \(\hat{x}\) to train a linear model for \(y\) in the second stage. This procedure is called the two-stage least-squares (2SLS).

Nonparametric IV

Removing the linear assumptions regarding the relationships between variables, the nonparametric IV can replace the linear regression with a linear projection onto a series of known basis functions [Newey2002].

This method is similar to the conventional 2SLS and is also composed of 2 stages after finding new features of \(x\), \(v\), and \(z\),

\[\begin{split}\tilde{z}_d & = f_d(z)\\ \tilde{v}_{\mu} & = g_{\mu}(v),\end{split}\]

which are represented by some non-linear functions (basis functions) \(f_d\) and \(g_{\mu}\). After transforming into the new spaces, we then

  1. Fit the treatment model:

\[\hat{x}(z, v, w) = \sum_{d, \mu} A_{d, \mu} \tilde{z}_d \tilde{v}_{\mu} + h(v, w) + \eta\]
  1. Generate new treatments x_hat, and then fit the outcome model

\[y(\hat{x}, v, w) = \sum_{m, \mu} B_{m, \mu} \psi_m(\hat{x}) \tilde{v}_{\mu} + k(v, w) + \epsilon.\]

The final causal effect can then be estimated. For an example, the CATE given \(v\) is estimated as

\[y(\hat{x_t}, v, w) - y(\hat{x_0}, v, w) = \sum_{m, \mu} B_{m, \mu} (\psi_m(\hat{x_t}) - \psi_m(\hat{x_0})) \tilde{v}_{\mu}.\]

YLearn implement this procedure in the class NP2SLS.

Class structures

class ylearn.estimator_model.iv.NP2SLS(x_model=None, y_model=None, random_state=2022, is_discrete_treatment=False, is_discrete_outcome=False, categories='auto')
Parameters:
  • x_model (estimator, optional, default=None) – The machine learning model to model the treatment. Any valid x_model should implement the fit and predict methods, by default None

  • y_model (estimator, optional, default=None) – The machine learning model to model the outcome. Any valid y_model should implement the fit and predict methods, by default None

  • random_state (int, default=2022) –

  • is_discrete_treatment (bool, default=False) –

  • is_discrete_outcome (bool, default=False) –

  • categories (str, optional, default='auto') –

fit(data, outcome, treatment, instrument, is_discrete_instrument=False, treatment_basis=('Poly', 2), instrument_basis=('Poly', 2), covar_basis=('Poly', 2), adjustment=None, covariate=None, **kwargs)

Fit a NP2SLS. Note that when both treatment_basis and instrument_basis have degree 1 we are actually doing 2SLS.

Parameters:
  • data (DataFrame) – Training data for the model.

  • outcome (str or list of str, optional) – Names of the outcomes.

  • treatment (str or list of str, optional) – Names of the treatment.

  • covariate (str or list of str, optional, default=None) – Names of the covariate vectors.

  • instrument (str or list of str, optional) – Names of the instrument variables.

  • adjustment (str or list of str, optional, default=None) – Names of the adjustment variables.

  • treatment_basis (tuple of 2 elements, optional, default=('Poly', 2)) – Option for transforming the original treatment vectors. The first element indicates the transformation basis function while the second one denotes the degree. Currently only support ‘Poly’ in the first element.

  • instrument_basis (tuple of 2 elements, optional, default=('Poly', 2)) – Option for transforming the original instrument vectors. The first element indicates the transformation basis function while the second one denotes the degree. Currently only support ‘Poly’ in the first element.

  • covar_basis (tuple of 2 elements, optional, default=('Poly', 2)) – Option for transforming the original covariate vectors. The first element indicates the transformation basis function while the second one denotes the degree. Currently only support ‘Poly’ in the first element.

  • is_discrete_instrument (bool, default=False) –

estimate(data=None, treat=None, control=None, quantity=None)

Estimate the causal effect of the treatment on the outcome in data.

Parameters:
  • data (pandas.DataFrame, optional, default=None) – If None, data will be set as the training data.

  • quantity (str, optional, default=None) –

    Option for returned estimation result. The possible values of quantity include:

    1. ’CATE’ : the estimator will evaluate the CATE;

    2. ’ATE’ : the estimator will evaluate the ATE;

    3. None : the estimator will evaluate the ITE or CITE.

  • treat (float, optional, default=None) – Value of the treament when imposing intervention. If None, then treat will be set to 1.

  • control (float, optional, default=None) – Value of the treament such that the treament effect is \(y(do(x=treat)) - y (do(x = control))\).

Returns:

The estimated causal effect with the type of the quantity.

Return type:

ndarray or float, optional

effect_nji(data=None)

Calculate causal effects with different treatment values.

Parameters:

data (pandas.DataFrame, optional, default=None) – The test data for the estimator to evaluate the causal effect, note that the estimator will use the training data if data is None.

Returns:

Causal effects with different treatment values.

Return type:

ndarray