Skip to main content

No project description provided

Project description

pycalf: Causal Inference Library

Documentation Status PyPI Latest Release codecov Ruff License: MIT

A library to make causal inference easier by using Python

Getting Started

Installing

pip install pycalf

Example

Inference with IPW

from pycalf import metrics
from pycalf.propensity_score import IPW

# Load Data and Define Variables
df = pd.read_csv('sample/q_data_x.csv')
covariate_cols = [
    'TVwatch_day', 'age', 'sex', 'marry_dummy', 'child_dummy', 'inc', 'pmoney',
    'area_kanto', 'area_tokai', 'area_keihanshin', 'job_dummy1', 'job_dummy2',
    'job_dummy3', 'job_dummy4', 'job_dummy5', 'job_dummy6', 'job_dummy7',
    'fam_str_dummy1', 'fam_str_dummy2', 'fam_str_dummy3', 'fam_str_dummy4'
]
outcome_cols = ['gamecount', 'gamedummy', 'gamesecond']
treatment_col = 'cm_dummy'


X = df[covariate_cols]
y = df[outcome_cols]
treatment = df[treatment_col].astype(bool).to_numpy()

# Define and Fit IPW Model.
learner = Pipeline([
    ('sclaer', preprocessing.MinMaxScaler()),
    ('clf', LogisticRegression(solver='lbfgs', max_iter=1000, random_state=42))
])
model = IPW(learner)
model.fit(X, treatment)

# metrics
print('F1 Score: ', metrics.f1_score(treatment, model.get_score(), threshold='auto'))
metrics.plot_roc_curve(treatment, model.get_score())
metrics.plot_probability_distribution(treatment, model.get_score())

# Estimate ATE
outcome_name = 'gamesecond'
z0, z1, treat_effect = model.estimate_effect(
    X, treatment, y[outcome_name].to_numpy().reshape(-1, 1), mode='ate')
metrics.plot_treatment_effect(outcome_name, z0[0], z1[0], treat_effect[0].round())

Effect size d.

Propensity Score Distribution

Average Treatment Effect

Development

This project uses uv for package management. To set up the development environment:

# Initialize the development environment
make init

# Run tests
make tests

# Run linting
make lint

# Build documentation
make docs

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

Authors

Acknowledgments

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

pycalf-0.3.0.tar.gz (16.6 kB view details)

Uploaded Source

Built Distribution

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

pycalf-0.3.0-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

Details for the file pycalf-0.3.0.tar.gz.

File metadata

  • Download URL: pycalf-0.3.0.tar.gz
  • Upload date:
  • Size: 16.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pycalf-0.3.0.tar.gz
Algorithm Hash digest
SHA256 154af49d5f441b256cbff03ce15fb2b54af610446dcbd59a2ab5330731f31d5b
MD5 7254cdcaa75ca88c1c16cdfc88d86db8
BLAKE2b-256 4e7deeff2a612ea662df51802c8b6c247f211cf809ec6ea19197ed486013c7d7

See more details on using hashes here.

File details

Details for the file pycalf-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: pycalf-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 12.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pycalf-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6f4ee1f544c8f058a2d44e0ad49f910a6aa792f9ab5df5f79e1f1af797837108
MD5 7c574ef9c4c4bf6847e5e6a0325049d0
BLAKE2b-256 d6a3489b3989d7c4f3829442b1e3d6b76783c7a683e84ced61e4fcb09de69f45

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