Skip to main content

Introduction

As an attempt to mimic the mob R package (https://CRAN.R-project.org/package=mob), the py_mob is a collection of python functions that would generate the monotonic binning and perform the WoE (Weight of Evidence) transformation used in consumer credit scorecard developments.

Being a piecewise constant transformation in the context of logistic regressions, the WoE has also been employed in other use cases, such as consumer credit loss estimation, prepayment, and even fraud detection models. In addition to monotonic binning and WoE transformation, Information Value and KS statistic of each independent variables is also calculated to evaluate the variable predictiveness.

Different from other python packages for the same purpose, the py_mob package is very lightweight and the underlying computation is driven by the built-in python list or the numpy array. Functions would return lists of dictionaries, which can be easily converted to other data structures, such as pandas.DataFrame or astropy.table.

What's more, six different monotonic binning algorithms are implemented, namely qtl_bin(), bad_bin(), iso_bin(), rng_bin(), kmn_bin(), and gbm_bin(), that would provide different predictability and cardinality. For details, please see https://github.com/statcompute/py_mob.

People without the background knowledge in the consumer risk modeling might be wondering why the monotonic binning and thereafter the WoE transformation are important. Below are a couple reasons based on my experience. They are perfectly generalizable in other use cases of logistic regression with binary outcomes.

  1. Because the WoE is a piecewise transformation based on the data discretization, all missing values would fall into a standalone category either by itself or to be combined with the neighbor with a similar bad rate. As a result, the special treatment for missing values is not necessary.
  2. After the monotonic binning of each variable, since the WoE value for each bin is a projection from the predictor into the response that is defined by the log ratio between event and non-event distributions, any raw value of the predictor doesn't matter anymore and therefore the issue related to outliers would disappear.
  3. While many modelers would like to use log or power transformations to achieve a good linear relationship between the predictor and log odds of the response, which is heuristic at best with no guarantee for the good outcome, the WoE transformation is strictly linear with respect to log odds of the response with the unity correlation. It is also worth mentioning that a numeric variable and its strictly monotone functions should converge to the same monotonic WoE transformation.
  4. At last, because the WoE is defined as the log ratio between event and non-event distributions, it is indicative of the separation between cases with Y = 0 and cases with Y = 1. As the weighted sum of WoE values with the weight being the difference in event and non-event distributions, the IV (Information Value) is an important statistic commonly used to measure the predictor importance.

Authors

WenSui Liu is a seasoned data scientist with 15-year experience in the financial service industry.

Joyce Liu is a college student majoring in Mathematics with a strong passion for data science.

Installation

pip3 install py_mob

Core Functions

py_mob
  |-- qtl_bin()  : An iterative discretization based on quantiles of X.  
  |-- bad_bin()  : A revised iterative discretization for records with Y = 1.
  |-- iso_bin()  : A discretization algorthm driven by the isotonic regression between X and Y. 
  |-- rng_bin()  : A revised iterative discretization based on the equal-width range of X.  
  |-- kmn_bin()  : A discretization algorthm based on the kmean clustering of X.  
  |-- gbm_bin()  : A discretization algorthm based on the gradient boosting machine.  
  |-- summ_bin() : Generates the statistical summary for the binning outcome. 
  |-- view_bin() : Displays the binning outcome in a tabular form. 
  |-- cal_woe()  : Applies the WoE transformation to a numeric vector based on the binning outcome.
  |-- pd_bin()   : Discretizes each vector in a pandas DataFrame.
  `-- pd_woe()   : Applies WoE transformaton to each vector in the pandas DataFrame.

Metadata

Release files for py-mob 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for py-mob 0.4.0
File Size Uploaded
py_mob-0.4.0.tar.gz 362.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for py-mob 0.4.0
File Interpreter ABI Platform
py_mob-0.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 370.6 kB

Release files / py_mob-0.4.0.tar.gz

Download URL py_mob-0.4.0.tar.gz
Size 362.0 kB
Tags Source
SHA-256 checksum
How to use checksums
93d236f337ea73d10b5724f4feda1e8c723a95b3afecccf91bf439c2f41ab07b
BLAKE2b-256 checksum
How to use checksums
f2301429d305b2457a316922dc66f16aba3db866cea3d062d12bf5c622b6ae09
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.7

Release files / py_mob-0.4.0-py3-none-any.whl

Download URL py_mob-0.4.0-py3-none-any.whl
Size 8.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ecfc8447281926bf8cd8c6c643cdf7ce44626bf315d8273d7dc7cf87fab0c51e
BLAKE2b-256 checksum
How to use checksums
2674c0ce73ecb68566c61ab02a31b30a2f6a8bc6a3459aba8a91859e8b8bdc9f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.7

Release history Release notifications | RSS feed

This release

0.4.0 This release

2 release files

0.3.1

2 release files

0.3

2 release files

0.2.8

2 release files

0.2.7

2 release files

0.2.6

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page