invariant-causal-prediction


Nameinvariant-causal-prediction JSON
Version 0.1.1 PyPI version JSON
download
home_pageNone
SummaryInvariant Causal Prediction (ICP) for Python: causal predictor discovery via invariance across environments with confidence intervals and R-parity tests.
upload_time2025-08-10 15:02:13
maintainerNone
docs_urlNone
authorNone
requires_python>=3.9
licenseGNU GENERAL PUBLIC LICENSE Version 2, June 1991 Copyright (C) 1989, 1991 Free Software Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software—to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Lesser General Public License instead.) You can apply it to your programs, too. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs
keywords causal-inference invariant-causal-prediction icp statistics machine-learning robustness
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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

```bash
pip install -e .
```

## Quickstart (regression)

```python
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)

```python
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

```python
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. 

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "invariant-causal-prediction",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": "causal-inference, invariant-causal-prediction, icp, statistics, machine-learning, robustness",
    "author": null,
    "author_email": "Simon Zimmermann <simon.zimmermann@arc-intelligence.de>",
    "download_url": "https://files.pythonhosted.org/packages/b9/b8/a78f7675b0135c659c6f4134c48cf0e6635f5de884062e83a8a29b5d4d2f/invariant_causal_prediction-0.1.1.tar.gz",
    "platform": null,
    "description": "# invariant-causal-prediction\n\nInvariant Causal Prediction (ICP) in Python.\n\n- Identify causal predictors of a target across environments by testing invariance of residuals.\n- Returns confidence intervals, accepted sets, and diagnostics.\n\n## Install\n\n```bash\npip install -e .\n```\n\n## Quickstart (regression)\n\n```python\nimport numpy as np\nfrom invariant_causal_prediction import icp, plot_conf_intervals, summarize_icp\n\nrng = np.random.default_rng(0)\nn = 800\np = 5\nX = rng.standard_normal((n, p))\nExpInd = np.r_[np.zeros(n//2), np.ones(n - n//2)]\nX[ExpInd == 1] *= rng.normal(1.5, 0.3, size=(p,))\nbeta = np.array([1.0, 1.0] + [0.0]*(p-2))\nY = X @ beta + rng.standard_normal(n)\n\nres = icp(X, Y, ExpInd, alpha=0.01, test=\"normal\", selection=\"lasso\", max_no_variables=5, max_set_size=3)\nprint(summarize_icp(res))\nplot_conf_intervals(res)\n```\n\n## Quickstart (binary classification)\n\n```python\nimport numpy as np\nfrom invariant_causal_prediction import icp\n\nrng = np.random.default_rng(1)\nX = rng.standard_normal((600, 4))\nExpInd = np.r_[np.zeros(300), np.ones(300)]\nX[ExpInd == 1] *= 1.2\nlogit_beta = np.array([1.0, 0.7, 0.0, 0.0])\nlin = X @ logit_beta\np = 1/(1+np.exp(-lin))\nY = (rng.random(600) < p).astype(int)\nres = icp(X, Y, ExpInd, alpha=0.05, test=\"ks\", selection=\"stability\", max_set_size=2)\n```\n\n## Hidden ICP\n\n```python\nfrom invariant_causal_prediction import hidden_icp\nres_hidden = hidden_icp(X, Y, ExpInd, alpha=0.05, test=\"normal\", max_set_size=2)\nprint(res_hidden[\"accepted_sets\"])  # accepted sets allowing hidden-parent shifts\n```\n\n## API\n\n- `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)`\n  - `fit(X, y, exp_ind)` \u2192 dict with keys: `conf_int`, `maximin_coefficients`, `accepted_sets`, `used_variables`, `pvalues`, `model_reject`, `best_model_pvalue`, `noEnv`, `factor`.\n- `icp(...)` convenience function with same parameters.\n- `hidden_icp(...)`: simplified hidden-variable ICP under shift/additive interventions; returns accepted sets and diagnostics.\n- Plotting helpers: `plot_conf_intervals(result, feature_names=None)`, `plot_accepted_sets(result, feature_names=None)`.\n- Summary: `summarize_icp(result, feature_names=None)`.\n\n## Notes\n\n- 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.\n- The `exact` test is implemented via permutation with optional subsampling by `max_no_obs`.\n- For classification targets, invariance is applied to logistic residuals. \n",
    "bugtrack_url": null,
    "license": "GNU GENERAL PUBLIC LICENSE\n        \n        Version 2, June 1991\n        \n        Copyright (C) 1989, 1991 Free Software Foundation, Inc.\n        51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n        \n        Everyone is permitted to copy and distribute verbatim copies\n        of this license document, but changing it is not allowed.\n        \n        Preamble\n        \n        The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software\u2014to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Lesser General Public License instead.) You can apply it to your programs, too.\n        \n        END OF TERMS AND CONDITIONS\n        \n        How to Apply These Terms to Your New Programs ",
    "summary": "Invariant Causal Prediction (ICP) for Python: causal predictor discovery via invariance across environments with confidence intervals and R-parity tests.",
    "version": "0.1.1",
    "project_urls": {
        "Homepage": "https://github.com/simzim96/invariant_causal_prediction",
        "Issues": "https://github.com/simzim96/invariant_causal_prediction/issues",
        "Repository": "https://github.com/simzim96/invariant_causal_prediction"
    },
    "split_keywords": [
        "causal-inference",
        " invariant-causal-prediction",
        " icp",
        " statistics",
        " machine-learning",
        " robustness"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "4bcfff7153a72c0fa969204a922f698ba4132c6c8cd0ca14a74e293b5f6dd44d",
                "md5": "04f60040a305371c3b237374ca04857c",
                "sha256": "c729002f79217538e2dfebb43ae09068ea4222d8f559dbae24a90e30786e9aef"
            },
            "downloads": -1,
            "filename": "invariant_causal_prediction-0.1.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "04f60040a305371c3b237374ca04857c",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 16700,
            "upload_time": "2025-08-10T15:02:11",
            "upload_time_iso_8601": "2025-08-10T15:02:11.689124Z",
            "url": "https://files.pythonhosted.org/packages/4b/cf/ff7153a72c0fa969204a922f698ba4132c6c8cd0ca14a74e293b5f6dd44d/invariant_causal_prediction-0.1.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "b9b8a78f7675b0135c659c6f4134c48cf0e6635f5de884062e83a8a29b5d4d2f",
                "md5": "99aa1f4b2d84f6006967ddc12c9b43ae",
                "sha256": "f38710f5bffc0de1a2dfd94f0c4114fb9cebdc4743e349a8eebb896cfac652b0"
            },
            "downloads": -1,
            "filename": "invariant_causal_prediction-0.1.1.tar.gz",
            "has_sig": false,
            "md5_digest": "99aa1f4b2d84f6006967ddc12c9b43ae",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 14764,
            "upload_time": "2025-08-10T15:02:13",
            "upload_time_iso_8601": "2025-08-10T15:02:13.077559Z",
            "url": "https://files.pythonhosted.org/packages/b9/b8/a78f7675b0135c659c6f4134c48cf0e6635f5de884062e83a8a29b5d4d2f/invariant_causal_prediction-0.1.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-08-10 15:02:13",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "simzim96",
    "github_project": "invariant_causal_prediction",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "invariant-causal-prediction"
}
        
Elapsed time: 0.48258s