Skip to main content

Nonparametric IV (NPIV) estimation in Python

Project description

npiv

This is the Python package npiv (Nonparametric Instrumental Variables Estimation and Inference) written by Jeffrey S. Racine (racinej@mcmaster.ca) and co-authored and maintained by Timothy Christensen (timothy.christensen@yale.edu).

Description

This package implements methods introduced in Chen, Christensen, and Kankanala (2024) for estimating and constructing uniform confidence bands for nonparametric structural functions using instrumental variables, including data-driven choice of tuning parameters. It also provides functionality to construct uniform confidence bands using the method of Chen and Christensen (2018). All methods in this package apply to nonparametric regression as a special case.

Installation

You can install with pip.

library(devtools); install_github('JeffreyRacine/npiv')

Examples

Nonparametric instrumental variables estimation

We present a simple example to Engel curve estimation using data from the British Family Expenditure Survey. We first load the data and sort on log household expenditure for plotting purposes. We generate a grid of log expenditure from 4.5 to 6.5 for plotting.

# === 1. Load and prepare data ===
# Load the Engel (1995) dataset and sort by logexp
engel = pd.read_csv("data/data_engel.csv")
engel = engel.sort_values("logexp").reset_index(drop=True)

y = engel["food"].to_numpy()          # dependent variable
x = engel[["logexp"]].to_numpy()      # endogenous regressor
z = engel[["logwages"]].to_numpy()    # instrument
x_eval = np.linspace(4.5, 6.5, 150).reshape(-1, 1)

To nonparametrically regress Y on X using W as an instrument, use npiv(Y ~ X | W) or npiv(Y, X, W). By default, npiv uses a data-driven choice of sieve dimension based on the method of Chen, Christensen, and Kankanala (2024). We include X.eval = logexp.eval since we want to plot over a smaller region than the support of log expenditure.

food_engel = npiv(y, x, z, X_eval = x_eval)

We plot the estimated function and 95% uniform confidence bands using the command plot. By default, the confidence bands are generated using the method of Chen, Christensen, and Kankanala (2024). We include showdata = True to overlay the data points.

plot_npiv(food_engel)

Confidence bands for the derivative of the structural function are plotted by including the argument type = "deriv".

plot_npiv(food_engel, kind = 'deriv')

Nonparametric regression

We can estimate a conditional mean function by nonparametric regression with data-driven choice of sieve dimension simply by passing the regressor as the instrument. The following example estimates the Engel curve for food by regression of food on logexp:

food_engel = npiv(y, x, z, X_eval = x_eval,  knots = "quantiles")
plot_npiv(food_engel)

The plot is wiggly, indicating that the algorithm has selected a fairly large sieve dimension. This can sometimes happen when the data are far from uniform, because the default method uses splines with knots placed uniformly over the support of the data. We can restore good performance by including knots = "quantiles" to use splines with knots placed at the quantiles of the data.

food_engel = npiv(y, x, z, X_eval = x_eval,  knots = "quantiles")
plot_npiv(food_engel)

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

pynpiv-0.1.0.tar.gz (22.3 kB view details)

Uploaded Source

Built Distribution

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

pynpiv-0.1.0-py3-none-any.whl (22.6 kB view details)

Uploaded Python 3

File details

Details for the file pynpiv-0.1.0.tar.gz.

File metadata

  • Download URL: pynpiv-0.1.0.tar.gz
  • Upload date:
  • Size: 22.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for pynpiv-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4f858afe1babbb9213ae95a596f09e1b0625ec412beff703db980104fcc072c7
MD5 cb66d47140bfb6abfa3860d1a14d98be
BLAKE2b-256 0d5a4fb309e481ac1fcbf79d2cfe584b10adbc751653124a9cceb36ce1959ffa

See more details on using hashes here.

File details

Details for the file pynpiv-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pynpiv-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 22.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for pynpiv-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7798e2ab75bc08b18064621e33a1d0f2f1c0a01eaa1c0a2c1bfb096c0c12b78c
MD5 cb3bd613eda9bd40bd05b6faf88bb32c
BLAKE2b-256 2a171972a8b887403b7a51692543aca178ebb6c3ae07a6e057b0571c90db67cd

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