Skip to main content

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)

Metadata

Release files for pynpiv 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pynpiv 0.1.0
File Size Uploaded
pynpiv-0.1.0.tar.gz 22.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pynpiv 0.1.0
File Interpreter ABI Platform
pynpiv-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 44.9 kB

Release files / pynpiv-0.1.0.tar.gz

Download URL pynpiv-0.1.0.tar.gz
Size 22.3 kB
Tags Source
SHA-256 checksum
How to use checksums
4f858afe1babbb9213ae95a596f09e1b0625ec412beff703db980104fcc072c7
BLAKE2b-256 checksum
How to use checksums
0d5a4fb309e481ac1fcbf79d2cfe584b10adbc751653124a9cceb36ce1959ffa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.5

Release files / pynpiv-0.1.0-py3-none-any.whl

Download URL pynpiv-0.1.0-py3-none-any.whl
Size 22.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7798e2ab75bc08b18064621e33a1d0f2f1c0a01eaa1c0a2c1bfb096c0c12b78c
BLAKE2b-256 checksum
How to use checksums
2a171972a8b887403b7a51692543aca178ebb6c3ae07a6e057b0571c90db67cd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.5

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page