Skip to main content

Invariant Causal Prediction (ICP) for Python: causal predictor discovery via invariance across environments with confidence intervals and R-parity tests.

Project description

invariant-causal-prediction

Invariant Causal Prediction (ICP) in Python.

  • Identify causal predictors of a target across environments by testing invariance of residuals.
  • Returns confidence intervals, accepted sets, and diagnostics.

Install

pip install -e .

Quickstart (regression)

import numpy as np
from invariant_causal_prediction import icp, plot_conf_intervals, summarize_icp

rng = np.random.default_rng(0)
n = 800
p = 5
X = rng.standard_normal((n, p))
ExpInd = np.r_[np.zeros(n//2), np.ones(n - n//2)]
X[ExpInd == 1] *= rng.normal(1.5, 0.3, size=(p,))
beta = np.array([1.0, 1.0] + [0.0]*(p-2))
Y = X @ beta + rng.standard_normal(n)

res = icp(X, Y, ExpInd, alpha=0.01, test="normal", selection="lasso", max_no_variables=5, max_set_size=3)
print(summarize_icp(res))
plot_conf_intervals(res)

Quickstart (binary classification)

import numpy as np
from invariant_causal_prediction import icp

rng = np.random.default_rng(1)
X = rng.standard_normal((600, 4))
ExpInd = np.r_[np.zeros(300), np.ones(300)]
X[ExpInd == 1] *= 1.2
logit_beta = np.array([1.0, 0.7, 0.0, 0.0])
lin = X @ logit_beta
p = 1/(1+np.exp(-lin))
Y = (rng.random(600) < p).astype(int)
res = icp(X, Y, ExpInd, alpha=0.05, test="ks", selection="stability", max_set_size=2)

Hidden ICP

from invariant_causal_prediction import hidden_icp
res_hidden = hidden_icp(X, Y, ExpInd, alpha=0.05, test="normal", max_set_size=2)
print(res_hidden["accepted_sets"])  # accepted sets allowing hidden-parent shifts

API

  • ICP(alpha=0.01, test="normal"|"ks"|"ranks"|"correlation"|"exact"|callable, selection="all"|"lasso"|"stability"|"boosting", max_no_variables=None, max_set_size=3, show_accepted_sets=False, show_completion=False, stop_if_empty=False, gof=0.0, max_no_obs=None, random_state=0)
    • fit(X, y, exp_ind) → dict with keys: conf_int, maximin_coefficients, accepted_sets, used_variables, pvalues, model_reject, best_model_pvalue, noEnv, factor.
  • icp(...) convenience function with same parameters.
  • hidden_icp(...): simplified hidden-variable ICP under shift/additive interventions; returns accepted sets and diagnostics.
  • Plotting helpers: plot_conf_intervals(result, feature_names=None), plot_accepted_sets(result, feature_names=None).
  • Summary: summarize_icp(result, feature_names=None).

Notes

  • The implementation follows the ICP principle from the R package documentation but is not a line-by-line port. Confidence intervals are aggregated empirically from coefficients across accepted sets.
  • The exact test is implemented via permutation with optional subsampling by max_no_obs.
  • For classification targets, invariance is applied to logistic residuals.

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

invariant_causal_prediction-0.1.1.tar.gz (14.8 kB view details)

Uploaded Source

Built Distribution

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

invariant_causal_prediction-0.1.1-py3-none-any.whl (16.7 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for invariant_causal_prediction-0.1.1.tar.gz
Algorithm Hash digest
SHA256 f38710f5bffc0de1a2dfd94f0c4114fb9cebdc4743e349a8eebb896cfac652b0
MD5 99aa1f4b2d84f6006967ddc12c9b43ae
BLAKE2b-256 b9b8a78f7675b0135c659c6f4134c48cf0e6635f5de884062e83a8a29b5d4d2f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for invariant_causal_prediction-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c729002f79217538e2dfebb43ae09068ea4222d8f559dbae24a90e30786e9aef
MD5 04f60040a305371c3b237374ca04857c
BLAKE2b-256 4bcfff7153a72c0fa969204a922f698ba4132c6c8cd0ca14a74e293b5f6dd44d

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