Skip to content

A Python package for inference on parameters in models with individual level heterogeneity using deep learning

License

Notifications You must be signed in to change notification settings

Center-for-Applied-AI/dl4pi

Repository files navigation

dl4pi: Deep Learning for Parameter Inference

Python Versions MIT License

dl4pi is a Python package built with torch. dl4pi is designed to extend traditional economic models using deep learning to capture heterogeneity in data. The package implements the framework proposed in Farrell, Liang and Misra (2021). It lets researchers use neural nets as parameters. These "parameter functions" retain the interpretability, economic meaning, and discipline of classical economic parameters while leveraging the power of machine learning. The package implements automatic inference for functions of these parameters.

The package was developed at the Center for Applied AI at the University of Chicago Booth School of Business.

Warning

While the code is structured as a Python package, this project is not actively maintained. It may contain bugs and there is no support available. Use at your own risk.

Quickstart

  • Follow the installation instructions below to install the package.
  • Review the example python notebooks on GitHub
    • For synthetic examples, code to generate synthetic data is contained in the respective notebook.
    • For real experimental examples, data is publicly available and can be obtained per the instructions in readme.md inside the data folder.

Installation

dl4pi is optimized for Python 3.12. To ensure compatibility, we recommend using a local version of Python and setting up a virtual environment. If you have pyenv, you can take the following steps:

# Clone the repo
git clone [email protected]:Center-for-Applied-AI/dl4pi.git

# Navigate to the directory
cd dl4pi 

# Install Python 3.12 using pyenv
pyenv install 3.12 

# Set the local Python version to 3.12
pyenv local 3.12 

# Install virtualenv
pip install virtualenv 

# Create a new virtual environment
virtualenv .venv 

# Activate the virtual environment
source .venv/bin/activate 

# Install the package
pip install .

Math overview

This package is useful for researchers interested in estimating a population parameter

$$\mu = \mathbb{E} \left[H(\hat{\theta}(X); t^{*})\right]$$

where $H$ is a function that computes the statistic of interest from individual parameters $\theta(X)$ and treatment(s) $t^{*}$. For example, in a simple demand model, $\theta(X)$ could represent individual demand curve parameters, with consumers being treated with individualized prices $t$. The parameter of interest, $H$, could then be the average consumer surplus, which is a non-linear function of parameters.

The mapping between individual characteristics and structural parameters $\theta(X)$ is estimated by minimizing the structural loss of the causal deep net

$$\hat{\theta}(.) = \text{argmin}_{\theta} \mathbb{E} \left[ l(Y,T,\theta(X)) \right]$$
Illustration of a deep neural network with a structural parameter layers

dl4pi implements the estimator proposed in Farrell, Liang and Misra (2021), which is a consistent estimator of $\mu$ under certain assumptions:

  1. Identification: The parameter function $\theta(x)$ must be identifiable;
  2. Regularity of the loss and statistic functions: $l(y, t, \theta(x))$ and $H(\theta(x))$ must be thrice continously differenetiable with respect to $\theta(x)$ and possess $q>4$ finite absolute moments and positive variance.
  3. Well-behaved Hessian estimator: the conditional expectation of Hessian $\Lambda(x)$ must have a bounded inverse.
  4. The model is conditionally unbiased: $\mathbb{E}[l(Y,t,\theta(x))|X=x, T=t] = 0$

The estimator is calculated as:

$$\psi_{i}(.) = H(\hat{\theta}_A(x_i^C)) - H_{\theta}\left(\hat{\theta}_A(x_i^C)\right) \hat{\Lambda}_B^{-1}(x_i^C) l_{\theta}(y_i^C; t_i^C; \hat{\theta}_A(x_i^C)), \text{ where } i \in C$$

where $A$, $B$, and $C$ indicate three splits of data: the structural parameters are fit on partition $A$, the Hessian model is fit on partition $B$, and the estimator is evaluated on partition $C$.

Given some dataset and a three partition split, the procedure can be repeated three times to collect $\psi$ for every datapoint.

# $\hat{\theta}$ $\hat{\Lambda}$ $\psi$
1 A B C
2 B C A
3 C A B

Usage

To use the package, all objects from the table below should be defined.

description object example
structural loss $l(.)$ dl4pi.examples.logit_loss
causal deep net $\hat{\theta}(.)$ dl4pi.examples.cdnn_logit
statistic function $H(.)$ dl4pi.examples.statistic_function_alpha
covariates $x$ torch.rand(1000,3)
outcome $y$ torch.rand(1000,1)
treatment $t$ torch.rand(1000,2)

The following code perform cross-fitting:

# Inititalize AutoEstimator
ae = dl4pi.AutoEstimator(
    structural_parameters_dimension=2,
    statistic_function=statistic_function_alpha,
    cdnn_model=cdnn_logit
    )

# Perform cross-estimation
ae.cross_estimate(x=x, y=y, t=t)

# Retrieve the results
statistic_function_values = ae.statistic_function_values
psi_values = ae.psi_values

statistic_function_values is the tensor of observation level values of $H$. psi_values is the tensor of observation level values of $\psi$, which can be used to calculate the consistent estimator of the statistics of interest $\mu$ and to obtain confidence intervals.

Detailed examples can be found in /examples.

Citation

If you found the framework useful, please cite Farrell, Liang and Misra (2021)

@article{dl4pi_paper,
      title = {Deep Learning for Individual Heterogeneity: An Automatic Inference Framework}, 
      author = {Max H. Farrell and Tengyuan Liang and Sanjog Misra},
      year = {2021},
      eprint = {2010.14694},
      archivePrefix = {arXiv},
      primaryClass = {econ.EM},
      url = {https://arxiv.org/abs/2010.14694}, 
}

If you used the dl4pi package in a publication, please also cite

@misc{dl4pi_package,
  title = {Dl4pi: Deep Learning for Parameter Inference in Python},
  author = {Max H. Farrell and Tengyuan Liang and Sanjog Misra and Janani Sekar and Kirill Skobelev and James Ross},
  year = {2024},
  url = {https://github.com/Center-for-Applied-AI/dl4pi},
  note = {Python package version 1.0.0},
}

References

Farrell, M. H., Liang, T., & Misra, S. (2021). Deep Learning for Individual Heterogeneity: An Automatic Inference Framework. arXiv:2010.14694.

About

A Python package for inference on parameters in models with individual level heterogeneity using deep learning

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages