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
Export requirements.txt
poetry export -f requirements.txt --output docs/requirements.txt
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.2.12.tar.gz
(225.0 kB
view hashes)
Built Distribution
pycalf-0.2.12-py3-none-any.whl
(88.5 kB
view hashes)