rejectkit 0.3.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- rejectkit/__init__.py +47 -0
- rejectkit/_compat.py +19 -0
- rejectkit/base.py +94 -0
- rejectkit/benchmark.py +141 -0
- rejectkit/datasets.py +97 -0
- rejectkit/diagnostics.py +109 -0
- rejectkit/estimator.py +105 -0
- rejectkit/methods/__init__.py +20 -0
- rejectkit/methods/augmentation.py +69 -0
- rejectkit/methods/extrapolation.py +47 -0
- rejectkit/methods/heckman.py +98 -0
- rejectkit/methods/parcelling.py +87 -0
- rejectkit/methods/reclassification.py +45 -0
- rejectkit/methods/reweighting.py +47 -0
- rejectkit/methods/semi_supervised.py +64 -0
- rejectkit/plotting.py +75 -0
- rejectkit-0.3.0.dist-info/METADATA +219 -0
- rejectkit-0.3.0.dist-info/RECORD +20 -0
- rejectkit-0.3.0.dist-info/WHEEL +4 -0
- rejectkit-0.3.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: rejectkit
|
|
3
|
+
Version: 0.3.0
|
|
4
|
+
Summary: Reject inference for credit scoring — 8 scikit-learn-compatible methods plus an honest benchmark that tells you whether it actually helps.
|
|
5
|
+
Project-URL: Homepage, https://github.com/HangilKim11/rejectkit
|
|
6
|
+
Project-URL: Repository, https://github.com/HangilKim11/rejectkit
|
|
7
|
+
Project-URL: Issues, https://github.com/HangilKim11/rejectkit/issues
|
|
8
|
+
Author-email: Han <kim.hangil.ds@gmail.com>
|
|
9
|
+
License: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: credit-scoring,machine-learning,reject-inference,risk,sample-selection-bias,scikit-learn,scorecard
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Financial and Insurance Industry
|
|
14
|
+
Classifier: Intended Audience :: Science/Research
|
|
15
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
21
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
22
|
+
Requires-Python: >=3.9
|
|
23
|
+
Requires-Dist: numpy>=1.21
|
|
24
|
+
Requires-Dist: pandas>=1.3
|
|
25
|
+
Requires-Dist: scikit-learn>=1.0
|
|
26
|
+
Provides-Extra: dev
|
|
27
|
+
Requires-Dist: matplotlib>=3.4; extra == 'dev'
|
|
28
|
+
Requires-Dist: mkdocs-material>=9.0; extra == 'dev'
|
|
29
|
+
Requires-Dist: mkdocs>=1.5; extra == 'dev'
|
|
30
|
+
Requires-Dist: polars>=0.20; extra == 'dev'
|
|
31
|
+
Requires-Dist: pytest>=7; extra == 'dev'
|
|
32
|
+
Requires-Dist: ruff>=0.1; extra == 'dev'
|
|
33
|
+
Provides-Extra: docs
|
|
34
|
+
Requires-Dist: mkdocs-material>=9.0; extra == 'docs'
|
|
35
|
+
Requires-Dist: mkdocs>=1.5; extra == 'docs'
|
|
36
|
+
Provides-Extra: plot
|
|
37
|
+
Requires-Dist: matplotlib>=3.4; extra == 'plot'
|
|
38
|
+
Provides-Extra: polars
|
|
39
|
+
Requires-Dist: polars>=0.20; extra == 'polars'
|
|
40
|
+
Description-Content-Type: text/markdown
|
|
41
|
+
|
|
42
|
+
# rejectkit
|
|
43
|
+
|
|
44
|
+
**Reject inference for credit scoring — scikit-learn-compatible methods, plus an honest benchmark that tells you whether reject inference actually helps on your data.**
|
|
45
|
+
|
|
46
|
+

|
|
47
|
+

|
|
48
|
+

|
|
49
|
+
|
|
50
|
+
<details>
|
|
51
|
+
<summary><b>한국어 요약 (Korean)</b></summary>
|
|
52
|
+
|
|
53
|
+
<br>
|
|
54
|
+
|
|
55
|
+
신용 모델은 **승인된 신청자**(나중에 good/bad 결과를 아는 사람)로만 학습하지만, 실제로는 **거절자를 포함한 전체 신청자**를 평가해야 한다 — 이 표본 선택 편향을 바로잡는 기법이 **reject inference**다. `rejectkit`은 이 고전 기법 8가지를 scikit-learn 스타일의 한 API로 묶고, **"그 보정이 내 데이터에서 실제로 도움이 되는지"** 재는 벤치마크(`MaskedRejectBenchmark`)까지 제공한다. 입력은 pandas·polars·numpy 모두 지원. 자세한 한·영·일 설명은 [docs/explainer.md](docs/explainer.md), 실데이터 예제는 [examples/real_data_home_credit.ipynb](examples/real_data_home_credit.ipynb) 참고.
|
|
56
|
+
|
|
57
|
+
</details>
|
|
58
|
+
|
|
59
|
+
<details>
|
|
60
|
+
<summary><b>日本語要約 (Japanese)</b></summary>
|
|
61
|
+
|
|
62
|
+
<br>
|
|
63
|
+
|
|
64
|
+
与信モデルは**承認された申込者**(後で good/bad の結果が分かる人)だけで学習するが、実際には**否認者を含む全申込者**を評価しなければならない — この標本選択バイアスを補正する手法が **reject inference**。`rejectkit` はこの古典的手法8種を scikit-learn 風の単一 API にまとめ、**「その補正が自分のデータで実際に役立つか」**を測るベンチマーク(`MaskedRejectBenchmark`)まで備える。入力は pandas・polars・numpy に対応。詳しい3言語解説は [docs/explainer.md](docs/explainer.md)、実データ例は [examples/real_data_home_credit.ipynb](examples/real_data_home_credit.ipynb) を参照。
|
|
65
|
+
|
|
66
|
+
</details>
|
|
67
|
+
|
|
68
|
+
---
|
|
69
|
+
|
|
70
|
+
## Why this exists
|
|
71
|
+
|
|
72
|
+
A credit model is trained on **accepted** applicants, whose good/bad outcome you eventually observe. But the model has to score the **whole** through-the-door population — including the applicants you **rejected**, who never get an outcome. Training on accepts only is a textbook case of sample-selection bias. *Reject inference* is the family of techniques that tries to correct it.
|
|
73
|
+
|
|
74
|
+
These methods are standard in the credit-risk world, yet the Python tooling is missing:
|
|
75
|
+
|
|
76
|
+
- **R** has [`scoringTools`](https://github.com/adimajo/scoringTools) (`augmentation`, `fuzzy_augmentation`, `parcelling`, `reclassification`, `twins`) — GitHub only.
|
|
77
|
+
- **Python** scorecard libraries — `scorecardpy`, `optbinning`, `scorecardbundle` — do WOE/IV binning and logistic scorecards but **skip reject inference entirely**.
|
|
78
|
+
- What's left online is one-off research code, not a packaged, tested library.
|
|
79
|
+
|
|
80
|
+
`rejectkit` fills that gap: eight reject inference methods behind one scikit-learn-style API, a benchmark harness even `scoringTools` lacks, plus drift diagnostics and plotting.
|
|
81
|
+
|
|
82
|
+
## Install
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
pip install -e . # core: numpy, pandas, scikit-learn
|
|
86
|
+
pip install -e ".[plot]" # + matplotlib plotting helpers
|
|
87
|
+
pip install -e ".[polars]" # + polars input support
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Quickstart
|
|
91
|
+
|
|
92
|
+
```python
|
|
93
|
+
from sklearn.linear_model import LogisticRegression
|
|
94
|
+
from rejectkit import RejectInferenceClassifier
|
|
95
|
+
|
|
96
|
+
# X_accept, y_accept: accepted applicants and their good(0)/bad(1) outcomes
|
|
97
|
+
# X_reject: rejected applicants — features only, no labels
|
|
98
|
+
clf = RejectInferenceClassifier(
|
|
99
|
+
estimator=LogisticRegression(max_iter=1000),
|
|
100
|
+
method="parcelling",
|
|
101
|
+
method_params={"uplift": 1.3}, # assume rejects are ~30% worse per score band
|
|
102
|
+
)
|
|
103
|
+
clf.fit(X_accept, y_accept, X_reject)
|
|
104
|
+
pd_bad = clf.predict_proba(X_new)[:, 1]
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
Just want the augmented training sample for your own pipeline?
|
|
108
|
+
|
|
109
|
+
```python
|
|
110
|
+
from rejectkit import FuzzyAugmentation
|
|
111
|
+
|
|
112
|
+
X_aug, y_aug, sample_weight = (
|
|
113
|
+
FuzzyAugmentation(LogisticRegression(max_iter=1000))
|
|
114
|
+
.fit_resample(X_accept, y_accept, X_reject)
|
|
115
|
+
)
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
Inputs may be **pandas, polars, or numpy**.
|
|
119
|
+
|
|
120
|
+
## Methods
|
|
121
|
+
|
|
122
|
+
| Method | Class | Core idea | Assumption |
|
|
123
|
+
|---|---|---|---|
|
|
124
|
+
| Simple augmentation | `SimpleAugmentation` | Hard 0/1 label by score cutoff | Accept model ranks rejects |
|
|
125
|
+
| Fuzzy augmentation | `FuzzyAugmentation` | Two weighted rows per reject (P(bad), P(good)) | MAR; smooth labels |
|
|
126
|
+
| Parcelling | `Parcelling` | Per-score-band bad rate × `uplift` | Rejects worse by a fixed factor |
|
|
127
|
+
| Reclassification | `Reclassification` | Iteratively relabel & refit | Labels converge |
|
|
128
|
+
| Extrapolation / twins | `Extrapolation` | Local bad rate of nearest accepts | Similar applicants behave alike |
|
|
129
|
+
| Inverse-propensity reweighting | `Reweighting` | Reweight accepts by `1/P(accept)` | MAR; invents no labels |
|
|
130
|
+
| Self-training | `SelfLearning` | Pseudo-label only confident rejects | MAR; confident labels reliable |
|
|
131
|
+
| Heckman control function | `HeckmanClassifier` | Add inverse Mills ratio as a feature | Gaussian selection latent |
|
|
132
|
+
|
|
133
|
+
All resamplers share `fit(X_accept, y_accept, X_reject)` → `resample()` returning `(X, y, sample_weight)`. `HeckmanClassifier` augments the feature space, so it is a standalone classifier rather than a resampler.
|
|
134
|
+
|
|
135
|
+
## Does reject inference actually help? Measure it.
|
|
136
|
+
|
|
137
|
+
You can never validate reject inference directly, because rejects have no outcome — the literature is genuinely split on whether it helps at all. `MaskedRejectBenchmark` settles the question **on your own data**: it hides the labels of a synthetically "rejected" subset of a labelled dataset and checks how well each method recovers a model close to the *oracle* (trained on the full population) versus the *naive* accepts-only baseline.
|
|
138
|
+
|
|
139
|
+
```python
|
|
140
|
+
from rejectkit import MaskedRejectBenchmark
|
|
141
|
+
from rejectkit.datasets import make_credit_data
|
|
142
|
+
|
|
143
|
+
X, y = make_credit_data(n_samples=4000, random_state=0)
|
|
144
|
+
bench = MaskedRejectBenchmark(selection="mnar", accept_rate=0.6, random_state=0)
|
|
145
|
+
print(bench.compare(
|
|
146
|
+
["fuzzy", "parcelling", "reweighting", "extrapolation", "selflearning", "heckman"],
|
|
147
|
+
X, y,
|
|
148
|
+
).round(4))
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
```
|
|
152
|
+
auc ks gini auc_recovery
|
|
153
|
+
oracle 0.8203 0.4911 0.6406 1.0000
|
|
154
|
+
naive 0.7488 0.3651 0.4975 0.0000
|
|
155
|
+
fuzzy 0.7488 0.3663 0.4977 0.0010
|
|
156
|
+
parcelling 0.7404 0.3468 0.4809 -0.1161
|
|
157
|
+
reweighting 0.7249 0.3290 0.4498 -0.3334
|
|
158
|
+
extrapolation 0.6989 0.2889 0.3977 -0.6973
|
|
159
|
+
selflearning 0.7124 0.3093 0.4248 -0.5080
|
|
160
|
+
heckman 0.7457 0.3559 0.4914 -0.0424
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
`auc_recovery`: `0` = no better than the naive accepts-only model, `1` = matches the full-data oracle.
|
|
164
|
+
|
|
165
|
+
Read this honestly. Selection here is **MNAR** (acceptance depends on the hidden outcome), so naive is badly biased (0.749 vs the 0.820 oracle) — yet the augmentation methods barely move it and several *hurt*; only Heckman nearly holds the naive line. That is what theory predicts when selection depends on the outcome: **reject inference is not a free lunch.** Switch to `selection="mar"` or `selection="cutoff"` and the verdict often flips the other way — frequently the naive model is *already* at the oracle, so `auc_recovery` returns `NaN` (no gap to recover) and reject inference is simply unnecessary. The harness exists so you find out *before* you ship it.
|
|
166
|
+
|
|
167
|
+
Selection mechanisms: `"mar"` (features only), `"mnar"` (features + hidden outcome), `"cutoff"` (accept the lowest-PD fraction — a realistic credit policy).
|
|
168
|
+
|
|
169
|
+
## Diagnostics & plotting
|
|
170
|
+
|
|
171
|
+
```python
|
|
172
|
+
from rejectkit.diagnostics import feature_drift, swap_set, psi
|
|
173
|
+
feature_drift(X_accept, X_reject) # per-feature accept-vs-reject PSI, worst first
|
|
174
|
+
swap_set(y, score_old, score_new, c_old, c_new) # who a new scorecard swaps in/out
|
|
175
|
+
|
|
176
|
+
from rejectkit import plotting # needs [plot]
|
|
177
|
+
plotting.plot_benchmark(results)
|
|
178
|
+
plotting.plot_score_distributions(score_accept, score_reject)
|
|
179
|
+
plotting.plot_ks(y_true, y_score)
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
## Caveats
|
|
183
|
+
|
|
184
|
+
- Augmentation methods infer reject labels from a model fitted on the (biased) accepts, so they cannot escape strong **MNAR** selection on their own.
|
|
185
|
+
- Reject inference often affects **calibration** more than **ranking** (AUC). Evaluate the metric you care about.
|
|
186
|
+
- Always benchmark before adopting. `rejectkit` makes that one function call.
|
|
187
|
+
|
|
188
|
+
## Documentation
|
|
189
|
+
|
|
190
|
+
Build the docs site locally:
|
|
191
|
+
|
|
192
|
+
```bash
|
|
193
|
+
pip install -e ".[docs]"
|
|
194
|
+
mkdocs serve
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
## Examples
|
|
198
|
+
|
|
199
|
+
- `examples/quickstart.py` — 60-second tour (single model + benchmark).
|
|
200
|
+
- `examples/walkthrough.ipynb` — every function on sample data (trilingual KO/EN/JA, executed).
|
|
201
|
+
- `examples/real_data_home_credit.ipynb` — **applied to the real Kaggle Home Credit dataset**: under MNAR selection the naive model collapses (AUC 0.74 → 0.57) and reject inference recovers ~7–8% of the gap; under MAR/cutoff it is unnecessary (trilingual, executed).
|
|
202
|
+
|
|
203
|
+
## Roadmap
|
|
204
|
+
|
|
205
|
+
- **v0.1** — core augmentation/parcelling/reweighting, `RejectInferenceClassifier`, benchmark. ✅
|
|
206
|
+
- **v0.2** — reclassification, extrapolation / twins. ✅
|
|
207
|
+
- **v0.3** — self-training, Heckman, polars, plotting, drift diagnostics, docs. ✅
|
|
208
|
+
- **Next** — calibration-focused benchmark metrics, deep generative reject inference (optional extra), PyPI release.
|
|
209
|
+
|
|
210
|
+
## References
|
|
211
|
+
|
|
212
|
+
- Hand & Henley (1993), *Can reject inference ever work?*
|
|
213
|
+
- Crook & Banasik (2004), *Does reject inference really improve the performance of application scoring models?*
|
|
214
|
+
- Lopes, *Should we "reject" Reject Inference? An empirical study.*
|
|
215
|
+
- `scoringTools` (R): https://github.com/adimajo/scoringTools
|
|
216
|
+
|
|
217
|
+
## License
|
|
218
|
+
|
|
219
|
+
MIT — see [LICENSE](LICENSE).
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
rejectkit/__init__.py,sha256=reOuCV5Dc2UEJFS7r_M5tJ_IPrjrzUOjnCnih5OE2-4,1369
|
|
2
|
+
rejectkit/_compat.py,sha256=qQDdvsgnDQJvfN3iRhBTS011KdeG5v_56evFUavi7aQ,592
|
|
3
|
+
rejectkit/base.py,sha256=vZESGkIpTQgmZSZJQ_LBAxw46EmCtpDlef1Ob53YGgU,3749
|
|
4
|
+
rejectkit/benchmark.py,sha256=GzjUUTcA-iGYE7NFmSgablwEnrlLG10Wj9-rE4ce1CU,5991
|
|
5
|
+
rejectkit/datasets.py,sha256=cu95GFH2OtOeh7z1EwfGkrLyGcA6RKLDDXeWEsRJdmo,3284
|
|
6
|
+
rejectkit/diagnostics.py,sha256=WgBGN5SXr0TCyyfx-bmGyP1b-1ZfiZFkdH4ZxCUKWxg,3970
|
|
7
|
+
rejectkit/estimator.py,sha256=zyS2Foz3kpq-2HQqTW4AcozCIlPlIeaJUCqBLqi2w7g,3783
|
|
8
|
+
rejectkit/plotting.py,sha256=C067_7ry6xavC6-R6-r60qD_9WHI9yL-RhRnnnJFwCQ,2615
|
|
9
|
+
rejectkit/methods/__init__.py,sha256=Ch2wL03D5BhYhZYlS-DClh9vCyzyhvpVZT9Ub2yd8rs,531
|
|
10
|
+
rejectkit/methods/augmentation.py,sha256=22HDyaYn4O4pgeiWs9F8CF7tpsLmhSlgAQCE1y2KjZ8,2525
|
|
11
|
+
rejectkit/methods/extrapolation.py,sha256=DA-TMtWFGb2YUqpU_ttZi6MGZZ062AIW-G2FZMUnvy8,1774
|
|
12
|
+
rejectkit/methods/heckman.py,sha256=dqp9UvS1a4NEcHcs6T869exRgFHEAlTfTf4soM8cijQ,3864
|
|
13
|
+
rejectkit/methods/parcelling.py,sha256=YiLQbAVAT5RgCDe6rxI6yClOuu5zO9utqWGnvbkCGaw,3386
|
|
14
|
+
rejectkit/methods/reclassification.py,sha256=3OoF94Ku3y6QciEhYZRaPddEMJqSPfRQY08_uDqHbgs,1572
|
|
15
|
+
rejectkit/methods/reweighting.py,sha256=aCNjGZzrLtjD01tMnE88zANPCNx4MW9GWDthQWtg0vs,1709
|
|
16
|
+
rejectkit/methods/semi_supervised.py,sha256=rORQ8EA_Pd3SuP2wpUCcY0oEi6NooNP4cXvmX6Mo8LM,2200
|
|
17
|
+
rejectkit-0.3.0.dist-info/METADATA,sha256=zFoY7xfnF2pz6zNgBKQwsW357TQxlzbBd_dJ0mlX-GA,11547
|
|
18
|
+
rejectkit-0.3.0.dist-info/WHEEL,sha256=mffPy8wBnZQn2VnJUU5jE99KsxaSfiyMHV9Yt0aLVxs,87
|
|
19
|
+
rejectkit-0.3.0.dist-info/licenses/LICENSE,sha256=85F57zn0vTKAlckMV80EMq2Vg2cXxhwCTt5VZlQYDvg,1060
|
|
20
|
+
rejectkit-0.3.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Han
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|