No project description provided
Project description
pycalf: Causal Inference Library
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
- Konumaru - Initial work
Acknowledgments
- Uber / causalml: https://github.com/uber/causalml
- Iwanami Data Science Vol.3: https://www.iwanami.co.jp/book/b243764.html
Project details
Release history Release notifications | RSS feed
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)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
pycalf-0.3.0-py3-none-any.whl
(12.5 kB
view details)
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
154af49d5f441b256cbff03ce15fb2b54af610446dcbd59a2ab5330731f31d5b
|
|
| MD5 |
7254cdcaa75ca88c1c16cdfc88d86db8
|
|
| BLAKE2b-256 |
4e7deeff2a612ea662df51802c8b6c247f211cf809ec6ea19197ed486013c7d7
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6f4ee1f544c8f058a2d44e0ad49f910a6aa792f9ab5df5f79e1f1af797837108
|
|
| MD5 |
7c574ef9c4c4bf6847e5e6a0325049d0
|
|
| BLAKE2b-256 |
d6a3489b3989d7c4f3829442b1e3d6b76783c7a683e84ced61e4fcb09de69f45
|