Skip to main content

Neural network-based two-stage package for causal inference in partially-linear instrumental variable settings

Project description

DeepPLIV — Deep Partial Linear Model with Instrumental Variables

License: MIT Python 3.10+

A neural network-based two-stage package for causal inference in partially-linear instrumental variable settings.

Overview

DeepPLIV is a Python package that implements a deep learning approach to causal inference in partially linear models with instrumental variables. The package relaxes the linearity constraint between instrumental variables and the exposure of interest, while still providing consistent estimates of causal effects.

Key Features:

  • Two-stage neural network architecture for flexible modeling
  • Handles high-dimensional settings
  • Supports non-linear relationships between instruments and endogenous variables
  • Maintains linear treatment effects for interpretability
  • Built on PyTorch for GPU acceleration

This package implements the methodology from the paper: "Causal Inference in Partially Linear Models with Instrumental Variables" by Tomer Weiss & Malka Gorfine, 2025.

Installation

From PyPI (recommended)

pip install deeppliv

For Development

git clone https://github.com/tomerweiss/deeppliv.git
cd deeppliv
pip install -e .

With Optional Dependencies

For running the examples:

pip install deeppliv[examples]

For AWS functionality:

pip install deeppliv[aws]

For development with testing:

pip install deeppliv[dev]

Quick Start

from deeppliv import DeepPLIV
import numpy as np

# Prepare your data
# v_train: endogenous variable (training)
# z_train: instrumental variables (training)
# z_predict: instrumental variables (prediction)
# x: exogenous variables
# y: outcome variable

# Initialize and fit the model
model = DeepPLIV()
first_stage, second_stage = model.fit(
    v_1=v_train,
    z_1=z_train,
    z_2=z_predict,
    x=x,
    y=y,
    first_stage_epochs=500,
    second_stage_epochs=500
)

# Get the causal effect estimate
causal_effect = model.get_v_predicted_coefficient()
print(f"Estimated causal effect: {causal_effect}")

# Make predictions
predictions = model.predict(z=z_new, x=x_new)

Model Architecture

DeepPLIV uses a two-stage approach:

First Stage

  • Input: Instrumental variables + exogenous variables
  • Architecture: Deep neural network (64→64→16→output) with dropout regularization
  • Purpose: Predict the endogenous variable from instruments

Second Stage

  • Input: Predicted endogenous variable + exogenous variables
  • Architecture: Hybrid model with:
    • Deep network (32→16→16) for modeling confounding through exogenous variables
    • Linear layer for the treatment effect
  • Purpose: Estimate the causal effect while controlling for confounding

Examples

The package includes three main example scripts demonstrating different use cases:

1. Simple Usage (example_usage_simple.py)

Demonstrates basic usage with simulated data across 5 different scenarios:

python examples/example_usage_simple.py

2. Deep IV Setting (example_deep_iv_setting.py)

Compares DeepPLIV with Deep IV methods:

python examples/example_deep_iv_setting.py

3. Genetic IV Application (example_genetic_iv.py)

Shows application to genetic instrumental variables:

python examples/example_genetic_iv.py

API Reference

DeepPLIV Class

fit(v_1, z_1, z_2, x, y, first_stage_epochs=500, second_stage_epochs=500, ...)

Fit the two-stage model.

Parameters:

  • v_1 (np.array): Endogenous variable (training data)
  • z_1 (np.array): Instrumental variables for first stage
  • z_2 (np.array): Instrumental variables for prediction
  • x (np.array): Exogenous variables
  • y (np.array): Outcome variable
  • first_stage_epochs (int): Number of training epochs for first stage (default: 500)
  • second_stage_epochs (int): Number of training epochs for second stage (default: 500)
  • first_stage_learning_rate (float): Learning rate for first stage (default: 0.01)
  • second_stage_learning_rate (float): Learning rate for second stage (default: 0.01)
  • dropout (float): Dropout rate for regularization (default: 0.6)

Returns:

  • Tuple of (first_stage_model, second_stage_model)

predict(z, x)

Predict outcomes using the fitted model.

Parameters:

  • z (np.array): Instrumental variables
  • x (np.array): Exogenous variables

Returns:

  • np.array: Predicted outcomes

get_v_predicted_coefficient()

Get the estimated causal effect coefficient.

Returns:

  • float: The causal effect estimate

Requirements

  • Python >= 3.10
  • PyTorch >= 2.5.1
  • NumPy >= 2.0.2
  • pandas >= 2.2.3
  • scikit-learn >= 1.5.2
  • matplotlib >= 3.9.2
  • seaborn >= 0.13.2
  • scipy >= 1.13.1

Citation

If you use this package in your research, please cite:

@article{weiss2025deeppliv,
  title={Causal Inference in Partially Linear Models with Instrumental Variables},
  author={Weiss, Tomer and Gorfine, Malka},
  year={2025}
}

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Contact

For questions and feedback, please open an issue on GitHub.

Acknowledgments

This package was developed as part of a Master's thesis on causal inference in partially linear settings.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

deeppliv-0.1.1.tar.gz (13.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

deeppliv-0.1.1-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

File details

Details for the file deeppliv-0.1.1.tar.gz.

File metadata

  • Download URL: deeppliv-0.1.1.tar.gz
  • Upload date:
  • Size: 13.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for deeppliv-0.1.1.tar.gz
Algorithm Hash digest
SHA256 0751ffa5e7a993272b1390c0344c34e0cde67a0cacc8bcf8c467aa84302f14b0
MD5 0a58e70eb1ed88127f58654314cf436d
BLAKE2b-256 6517e42dacc5f0d543d3f1e27ccca5c786845698fc43c00775bfb61dc036f14d

See more details on using hashes here.

File details

Details for the file deeppliv-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: deeppliv-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 12.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for deeppliv-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 20e637dc4ba3be491734b7a74a3fe07f7bcfb8ada5dd0be4dcc0b89057c0efd1
MD5 75a45ae8bab03caee76ca25b45aeda12
BLAKE2b-256 e3e9a32ebd26ad84901d8a05893a3382f71b17dbcc48b437309bf79c801c745f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page