Skip to main content

A library for estimates of causal effects.

Project description

CausalEstimate

Unittests Lint using flake8 Formatting using black


CausalEstimate is a Python tool designed to produce causal estimates from propensity scores. It provides functionalities for matching, weighting, and other causal inference techniques, helping researchers and data scientists derive meaningful insights from observational data.


Features

  • Propensity score matching and weighting
  • Tools for average treatment effect (ATE) estimation
  • Easy integration with pandas DataFrames
  • Bootstrap standard error estimation

Installation

To install the required dependencies, run:

pip install -r requirements.txt

Usage

Example: Matching

Here is an example of how to use the matching functionality in a Jupyter notebook:

import numpy as np
import pandas as pd
from CausalEstimate.matching import match_optimal

# Simulate data
ps = np.array([0.3, 0.90, 0.5, 0.34, 0.351, 0.32, 0.35, 0.81, 0.79, 0.77, 0.90, 0.6, 0.52, 0.55])
treated = np.array([1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
ids = np.array([101, 102, 103, 103, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211])

df = pd.DataFrame({
    'PID': ids,
    'treatment': treated,
    'ps': ps
})

# Perform matching
result = match_optimal(df, n_controls=3, caliper=0.1)
print(result)

Example: Using the Estimator

Here's an example of how to use the Estimator class to compute effects:

import pandas as pd
import numpy as np
from CausalEstimate.interface.estimator import Estimator

# Simulate data
np.random.seed(42)
n = 1000
ps = np.random.uniform(0, 1, n)
treatment = np.random.binomial(1, ps)
outcome = 2 + 0.5*treatment + np.random.normal(0, 1, n)

df = pd.DataFrame({
    'treatment': treatment,
    'outcome': outcome,
    'ps': ps
})

# Create an Estimator object
estimator = Estimator(methods=['AIPW'], effect_type='ATE')

# Compute effects
results = estimator.compute_effect(
    df,
    treatment_col='treatment',
    outcome_col='outcome',
    ps_col='ps',
    bootstrap=True,
    n_bootstraps=100,
    method_args={},
    apply_common_support=False,
    common_support_threshold=0.1
)

print(results)

This example demonstrates how to:

  1. Create an Estimator object with a specified method (AIPW in this case)
  2. Use the compute_effect method to estimate the Average Treatment Effect (ATE)
  3. Apply bootstrap for standard error estimation

Development

Running Tests

To run the unit tests, use the following command:

python -m unittest

Linting

To lint the code using flake8, run:

pip install flake8
flake8 CausalEstimate tests

Formatting

To format the code using black, run:

pip install black
black CausalEstimate tests

License

This project is licensed under the MIT License.

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

causalestimate-0.6.0.tar.gz (29.0 kB view details)

Uploaded Source

Built Distribution

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

CausalEstimate-0.6.0-py3-none-any.whl (26.5 kB view details)

Uploaded Python 3

File details

Details for the file causalestimate-0.6.0.tar.gz.

File metadata

  • Download URL: causalestimate-0.6.0.tar.gz
  • Upload date:
  • Size: 29.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for causalestimate-0.6.0.tar.gz
Algorithm Hash digest
SHA256 81075d2999dcc93f30281ce1a0fc490528f0bffc978373c9ebee0243fb0ea4e0
MD5 7039261882599de2c4f31428c5d9d8c9
BLAKE2b-256 a266d44e5a61953779b66ee9a4f4aab9fd464ccafc2a82e75526132f028f036a

See more details on using hashes here.

Provenance

The following attestation bundles were made for causalestimate-0.6.0.tar.gz:

Publisher: publish.yml on kirilklein/CausalEstimate

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file CausalEstimate-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: CausalEstimate-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 26.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for CausalEstimate-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2544eea2d5f4b2a8ab7bd1306c357acf24504baae8c4b588da1c6c88cbdf52ae
MD5 9145bec7e5142b001ef80573578ef4f6
BLAKE2b-256 ddd70a0c5de9353a7dabf2b32cc8f3f37c58cb15770a644d5a05a9de64324ddf

See more details on using hashes here.

Provenance

The following attestation bundles were made for CausalEstimate-0.6.0-py3-none-any.whl:

Publisher: publish.yml on kirilklein/CausalEstimate

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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