Data Exploration with Weight of Evidence and Information Value in R

Data Exploration with Weight of Evidence and Information Value in R

标签(空格分隔): Scorecard


Reference

Data Exploration with Weight of Evidence and Information Value in R


Data Exploration with Weight of Evidence and Information Value in R

Binary classification models are perhaps the most common use-case in predictive analytics. The reason is that many key client actions across a wide range of industries are binary in nature, such as defaulting on a loan, clicking on an ad, or terminating a subscription.

Prior to building a binary classification model, a common step is to perform variable screening and exploratory data analysis. This is the step where we get to know the data and weed out variables that are either ill-conditioned or simply contain no information that will help us predict the action of interest. Note that the purpose of this step should not to be confused with that of multiple-variable selection techniques, such as stepwise regression and lasso, where the variables that go into the final model are selected. Rather, this is a precursory step designed to ensure that the approaches deployed during the final modeling phases are set up for success.

The weight of evidence (WOE) and information value (IV) provide a great framework for for exploratory analysis and variable screening for binary classifiers. WOE and IV have been used extensively in the credit risk world for several decades, and the underlying theory dates back to the 1950s. However, it is still not widely used outside the credit risk world and it is a somewhat underserved area in R.

WOE and IV enable one to:

  • Consider each variable’s independent contribution to the outcome.
  • Detect linear and non-linear relationships.
  • Rank variables in terms of “univariate” predictive strength.
  • Visualize the correlations between the predictive variables and the binary outcome.
  • Seamlessly compare the strength of continuous and categorical variables without creating dummy variables.
  • Seamlessly handle missing values without imputation.
  • Assess the predictive power of missing values.

In this paper we will provide a brief description of WOE and IV as well as the Information R package, which provides an easy way to perform variable screening and exploratory analysis with WOE and IV for traditional binary classifiers as well as uplift models.

What Are WOE and IV?

Background

Weight of evidence (WOE) and information value are closely related toconcepts from information theory where one of the goals is tounderstand the uncertainty involved in predicting the outcome ofrandom events given varying degrees of knowledge of other variables (see [2], [3], and [4]). Thus this is a perfect framework for variable screening andexploratory analysis for predictive modeling.

Information theory kicked off when Claude Shannon (1948) wrote down anexpression for the entropy of a probability distribution. Entropyis often described as the level of “disorder,” but in the context of information theory it is better to think of it as one’s level of uncertainty. High entropy means high uncertainty (you don’t know what tomorrow holds) and low entropy mean low uncertainty (there will be no surprises tomorrow).

Suppose a friend tosses a coin and asks you to predict whether thetoss came up heads or tails. There’s 50 percent probability of eachpossibility, so your prediction isn’t worth very much. This turns outto be the maximum amount of entropy for an event with two possibleoutcomes. Suppose instead that you know the coin is weighted so thatit comes up heads 90 percent of the time. Now your job of making aprediction is easier – Shannon’s formula tells you exactly how muchinformation you’ve gained compared to the previous situation.

Mutual information is a way of summarizing much knowing the value of one random variable tells you about another random variable (Shannon + Weaver 1949). If $X$ and $Y$ are independent, the mutual information is zero. Any correlations (positive, negative, or nonlinear) will result in positive mutual information. It turns out the mutual information of $X$ and $Y$ is nicely expressed in terms of entropy ($H$):

$$MI(X,Y)=H(X)+H(Y)−H(X,Y)$$

Mutual information is the difference in predictability that you get from knowing the joint distribution $p(X,Y)$ compared to knowing only the two marginal distributions $p(X)$ and $p(Y)$.

Information value (IV), which we will define below, is quite closely connected to mutual information (MI), and both are intended to do the same thing: summarize how much knowing the value of X in a given trial helps you predict the value of Y for the same trial.

Setup

Let’s say that we have a binary dependent variable $Y$
and a set of predictive variables $X_1,\cdots,X_p$. As mentioned above, $Y$ can capture a wide range of outcomes, such as defaulting on a loan, clicking on an ad, or terminating a subscription.

$WOE$ and $IV$ play two distinct roles when analyzing data:

  • WOE describes the relationship between a predictive variable and a binary target variable.
  • IV measures the strength of that relationship.

The Weight of Evidence

The WOE/IV framework is based on the following relationship:

$$log \frac {P(Y=1|X_j)}{P(Y=0|X_j)} = \underbrace {log \frac {P(Y=1)}{P(Y=0)}}{smple \ log \text{-} odds} + \underbrace {log \frac {f(X_j|Y=1)}{f(X_j|Y=0)}}{WOE}$$

where $f(x_j|Y)$ denotes the conditional probability density function (or a discrete probability distribution if $X_j$ is categorical.

This relationship says that the conditional logit of $P(Y=1)$, given $X_j$, can be written as the overall log-odds (i.e., the “intercept”) plus the log-density ratio – also known as the weight of evidence.

Note that the weight of evidence and the conditional log odds of $Y=1$ are perfectly correlated since the “intercept” is constant. Hence, the greater the value of WOE, the higher the chance of observing $Y=1$.

In fact, when WOE is positive the chance of of observing $Y=1$ is above average (for the sample), and vice versa when WOE is negative. When WOE equals 0 the odds are simply equal to the sample average.

Ties to Naive Bayes and Logistic Regression

Notice that the left-hand-side of the equation above – i.e., the conditional log odds – is exactly what we are trying to predict in a logistic regression model. Hence, when building a logistic regression model – which is perhaps the most widely used technique for building binary classifiers – we are actually trying to estimate the weight of evidence.

Last, but not least, the WOE framework has ties to the well-known naive Bayes classifier, given by (see [1]):

$$log \frac {P(Y=1|x_1,\cdots,x_p)}{P(Y=0|x_1,\cdots,x_p))} = log \frac {P(Y=1}{P(Y=0)} + \sum_{j=1}^p log \frac {f(X_j|Y=1)}{f(X_j|Y=0)}$$

The naive Bayes model essentially says that the conditional log odds is equal to the sum of the individual weight of evidence vectors. The word “naive” comes from the fact that this model relies on the assumption that all predictors are conditionally independent given $Y$, which is a highly optimistic assumption.

In the credit scoring industry a “semi-Naive” version of this model is quite popular. The idea is to transform the data into WOE vectors and then use logistic regression to fit the model

$$log \frac {P(Y=1|x_1,\cdots,x_p)}{P(Y=0|x_1,\cdots,x_p))} = log \frac {P(Y=1}{P(Y=0)} + \sum_{j=1}^p \beta_j log \frac {f(X_j|Y=1)}{f(X_j|Y=0)}$$

thus partly relaxing the assumption that all predictors in the model are independent. It should be noted that the underlying WOE vectors are still estimated univariately and that the coefficients merely function as scalars. For a more general model, GAM is a great choice (see [5]).

Measuring Univariate Variable Strength with the Information Value

We can leverage WOE to measure the predictive strength of $x_j$ – i.e., how well it helps us separate cases when $Y=1$ from cases when $Y=0$. This is done through the information value (IV) which is defined like this:

$$IV_j = \int log \frac {f(X_j|Y=1)}{f(X_j|Y=0)} (f(X_j|Y=1)-f(X_j|Y=0))dx $$

Note that the IV is essentially a weighted “sum” of all the individual WOE values where the weights incorporate the absolute difference between the numerator and the denominator (WOE captures the relative difference). Generally, if $IV<0.05$ the variable has very little predictive power and will not add any meaningful predictive power to your model.

Estimating WOE

The most common approach to estimating the conditional densities needed to calculate WOE is to bin $X_j$ and then use a histogram-type estimate.

Here is how it works: create a $k×2$ table where $k$ is the number of bins, and the cells within the two columns count the number of records where $Y=1$ and $Y=0$, respectively. The bins are typically selected such that the bins are roughly evenly sized with respect to the number of records in each bin (if possible). The conditional densities are then obtained by calculating the “column percentages” from this table. The typical number of bins used is $10-20$. If $X_j$ is categorical, no binning is needed and the histogram estimator can be used directly. Moreover, missing values are treated as a separate bin and thus handled seamlessly.

If $B_1,\cdots,B_k$ denote the bins for $X_j$, the WOE for $X_j$ for bin $i$ can be written as

$$WOE_{ij} = log \frac {P(X_j \in B_i | Y=1)}{P(X_j \in B_i | Y=0)} $$

which means that the IV for variable $X_j$ can be calculated as

$$IV_j = \sum {i=1}^{k} (P(X_j \in B_i | Y=1)-P(X_j \in B_i | Y=0)) \times WOE{ij}$$

Extensions to Exploratory Analysis for Uplift Models

Consider a direct marketing program where a test group received an offer of some sort and the control group did not receive anything. The test and control groups are based on a random split. The lift of the campaign is defined as the difference in success rates between the test and control groups. In other words, the program can only be deemed successful if the offer outperforms the “do nothing” (a.k.a baseline) scenario.

The purpose of uplift models is to estimate the difference between the test and control groups and then using the resulting model to target persuadables – i.e., potential or existing clients that are on the fence and need some sort of offer to buy a product. Thus, when preparing to build an uplift model, we cannot only focus on the log odds of $Y=1$, we also need to consider the log odds ratio of $Y=1$ for the test group versus the control group. This leads to the net weight of evidence (NWOE) and the net information value (NIV). (Actual uplift modeling techniques are beyond the scope of this paper; we will stick to model exploration and variable screening.)

The net weight of evidence (NWOE) is the difference between the $WOE_s$ for the test group ($WOE_t$) and the control group ($WOE_c$),

$$NWOE = WOE_t - WOE_c$$

which is equivalent to

$$NWOE_j = log \frac {f(x_j | Y=1)_t f(x_j | Y=0)_c}{f(x_j | Y=1)_c f(x_j | Y=0)_t)}$$

The net information value for variable $X_j$ is then defined as

$$\int (f(X_j | Y=1)_t f(X_j | Y=0)_c - f(X_j | Y=1)_c f(X_j | Y=0)_t)) \times log \frac {f(X_j | Y=1)_t f(X_j | Y=0)_c}{f(X_j | Y=1)_c f(X_j | Y=0)_t)}dx_j$$

NWOE and NIV work just like IV and WOE, just from an uplift perspective. NIV measures the strength of a given variable while NWOE describes the pattern of the relationship. Specifically, the higher the value of NIV, the better the given variable is at separating self-selectors – i.e., people who are self-motivated to buy – and persuadables that need to be motivated.

The Information R Package

The information package is designed to perform exploratory data analysis and variable screening for binary classification models using WOE and IV. To make the package as efficient as possible aggregations are done in data.table and creation of WOE vectors can be distributed across multiple cores.

The package can be downloaded from https://github.com/klarsen1/Information.

There are a number of R packages available that support WOE and IV (see a partial list below), but they are either primarily designed to build WOE-based models – e.g., naive Bayes classifiers – or they do not support the uplift use-case.

Date Used in Examples

The data is from an historical marketing campaign from the insurance industry and is automatically downloaded when you install the Information package. The data is stored in two .RDA files, one for the training dataset and one for the validation dataset. Each file has 68 predictive variables and 10k records.

The datasets contain two key indicators:

  • PURCHASE This variable equals 1 if the client accepted the offer, and 0 otherwise
  • TREATMENT This variable equals 1 if the client was in the test group (received the offer), and 0 otherwise.

Key Functions

  • CreateTables() creates WOE tables and IVs for all variables in the input dataframe.
  • PlotWOE() plots the WOE vector for one variable.
  • MultiPlotWOE() plots more than one WOE vector on one page for comparisons.

The same functions work for NWOE and NIV.

The examples below show how to call these functions

External Cross Validation

The information package supports external cross validation to check that the WOE and NWOE vectors are stable.

Let's assume that we have a training and a validation dataset, and we calculate WOE for both datasets using the same bin cutoffs. The cross validation penalty for bin $B_i$ is given by

$$\text{penalty}i = |WOE{\text{train}} - WOE_{\text{valid}}|$$

and the total cross validation (CV) penalty is

$$\sum _{i=1}^{k} |P(X_j \in B_i | Y=1) - P(X_j \in B_i | Y=0)| \times \text{penalty}_i $$

where the probabilities used to form the weights are from the training data. The adjusted IV is then defined as

$$NIV = IV - \text{total CV penalty}$$

The same approach can be applied to NIV for the uplift use-case.

Example for a Traditional Binary Classification Problem

Ranking All Variables Using Adjusted IV

library(Information)
library(gridExtra)

options(scipen=10)

### Loading the data
data(train, package="Information")
data(valid, package="Information")

### Exclude the control group
train <- subset(train, TREATMENT==1)
valid <- subset(valid, TREATMENT==1)

### Ranking variables using penalized IV  
IV <- CreateTables(data=train,
                   valid=valid,
                   y="PURCHASE")

grid.table(head(IV$Summary), rows=NULL)

The strongest six variables:

woe_fig1.png
woe_fig1.png

Analyzing WOE Patterns

The IV$Tables object returned by Information is simply a list of dataframes that contains the WOE tables for all variables in the input dataset. Note that the penalty and IV columns are cumulative.

grid.table(IV$Tables$N_OPEN_REV_ACTS, rows=NULL)
woe_fig2.png
woe_fig2.png

The table shows that the odds of PURCHASE=1 increases as N_OPEN_REV_ACTS increases, although the relationship is not linear.

Note that the Information package attempts to create evenly-sized bins in terms of the number of subjects in each group. However, this is not always possible due to ties in the data, as with N_OPEN_REV_ACTS which has ties at 0. If the variable under consideration is categorical, its distinct categories will show up as rows in the WOE table. Moreover, if the variable has missing values, the WOE table will contain a separate “NA” row which can be used to gauge the impact of missing values. Thus, the framework seamlessly handles missing values and categorical variables without any dummy-coding or imputation .

Plotting WOE Patterns

We can also plot this pattern for better visualization:

PlotWOE(IV, "N_OPEN_REV_ACTS")
woe_fig6.png
woe_fig6.png

If we want to plot all variables on separate pages, we can loop through all variables:

n <- names(IV$Tables)
for (i in 1:length(n)){
   PlotWOE(IV, n[i])
}

For better visualization we can do a multiplot to compare WOE patterns. Here we plot the first nine variables on one page. Note that we can plot as many variables as we want; MultiPlotWOE will simply spread the plots over multiple pages (max of nine plots per page).

MultiPlotWOE(IV, IV$Summary$Variable[1:9])
woe_fig9.png
woe_fig9.png

Omitting Cross Validation

To run IVs without external cross validation, simply omit the validation dataset:

IV <- CreateTables(data=train, y="PURCHASE")

Changing the Number of Bins

The default number of bins is 10 but we can choose a different number if we desire more granularity. Note that the IV formula is fairly invariant to the number of bins. Also, note that the bins are selected such that the bins are evenly sized, to the extent that it is possible (depending on the number of ties in the data).

IV <- CreateTables(data=train,
                   valid=valid,
                   y="PURCHASE",
                   bins=20)

grid.table(IV$Tables$N_OPEN_REV_ACTS,
           gp=gpar(fontsize=12),
           rows=NULL)
img

Uplift Example

For an uplift model we have to include both the test group and the control group in our dataset:

data(train, package="Information")
data(valid, package="Information")

When calling the CreateTables function, all we have to do is specify the variable that identifies the test and control groups.

NIV <- CreateTables(data=train,
                  valid=valid,
                  y="PURCHASE",
                  trt="TREATMENT")

grid.table(head(NIV$Summary),
           rows=NULL,
           gp=gpar(fontsize=12))
img

Note that we cannot compare the scales of NIV and IV. Moreover, there is no rule-of-thumb cutoff for the NIV. Hence, we have to use it solely as a ranking statistic and make a judgement call.

Interestingly, N_OPEN_REV_ACTS is also the most predictive variable from an uplift perspective. However, the NWOE pattern is quite different from the WOE pattern and suggests a u-shaped pattern. This illustrates how the story can be very different when modeling the incremental effect as opposed to simply building a model to estimate the chance of $Y=1$ following a treatment.

img

img

Combining IV Analysis With Variable Clustering

Variable clustering divides a set of numeric variables into mutually exclusive clusters.The algorithm attempts to generate clusters such that

  • the correlations between variables assigned to the same cluster are maximized.
  • the correlations between variables in different clusters are minimized.

Using this algorithm we can replace a large set of variables by a single member of each cluster, often with little loss of information. The question is which member to choose from a given cluster. One option is to choose the variable that has the highest multiple correlation with the variables within its cluster, and the lowest correlation with variables outside the cluster. A more meaningful choice for a predictive modeling is to choose the variable that has the highest information value. The following code shows how to do this.

In R, we can do this with the ClustOfVar package.

library(ClustOfVar)
library(reshape2)
library(plyr)

data(train, package="Information")
data(valid, package="Information")
train <- subset(train, TREATMENT==1)
valid <- subset(valid, TREATMENT==1)

tree <- hclustvar(train[,!(names(train) %in% c("PURCHASE", "TREATMENT"))])
nvars <- length(tree[tree$height<0.7])
part_init<-cutreevar(tree,nvars)$cluster
kmeans<-kmeansvar(X.quanti=train[,!(names(train) %in% c("PURCHASE", "TREATMENT"))],init=part_init)
clusters <- cbind.data.frame(melt(kmeans$cluster), row.names(melt(kmeans$cluster)))
names(clusters) <- c("Cluster", "Variable")
clusters <- join(clusters, IV$Summary, by="Variable", type="left")
clusters <- clusters[order(clusters$Cluster),]
clusters$Rank <- ave(-clusters$AdjIV, clusters$Cluster, FUN=rank)
selected_members <- subset(clusters, Rank==1)
selected_members$Rank <- NULL

Using variable clustering in combination with IV cuts the number of variables from 68 to 21:

nrow(selected_members)
> 21
nrow(clusters)
> 69

Here are the first rows from the resulting variable ranking table:

grid.table(head(selected_members),
           rows=NULL,
           gp=gpar(fontsize=12))
img

Summary

  • The purpose of exploratory analysis and variable screening is to get to know the data and assess “univariate” predictive strength, before we deploy more sophisticated variable selection approaches.
  • The weight of evidence (WOE) and information value (IV) provide a great framework for performing exploratory analysis and variable screening prior to building a binary classifier (e.g., logistic regression). It seamlessly handles missing values and character variables, and the output is easy to interpret.
  • The information value originates from information theory and is closely related to the concept of mutual information (see [3], [4]).
  • The information package is specifically written to perform this type of analysis using parallel processing. It also supports exploratory analysis for uplift models, a growing area within marketing analytics. The information package is not designed to transfer data into WOE vectors for Naive Bayes models, although this feature could be added later.

References

[1] Hastie, Trevor , Tibshirani, Robert and Friedman, Jerome. (1986), Elements of Statistical Learning, Second Edition, Springer, 2009.

[2] Kullback S., Information Theory and Statistics, John Wiley & Sons, 1959.

[3] Shannon, C.E., “A Mathematical Theory of Communication”, Bell SystemTechnical Journal, 1948.

[4] Shannon, CE. and Weaver, W. The Mathematical Theory ofCommunication. Univ of Illinois Press, 1949.

[5] GAM: the Predictive Modeling Silver Bullet, http://multithreaded.stitchfix.com/blog/2015/07/30/gam/.

Other Packages that support WOE/IV

woe package,https://cran.r-project.org/web/packages/woe/woe.pdf.

riv package (not to be confused with the riv packake in CRAN), http://www.r-bloggers.com/r-credit-scoring-woe-information-value-in-woe-package/.

klaR package, https://cran.r-project.org/web/packages/klaR/klaR.pdf.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 205,874评论 6 479
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,151评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,270评论 0 342
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,137评论 1 278
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,116评论 5 370
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,935评论 1 283
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,261评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,895评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,342评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,854评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,978评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,609评论 4 322
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,181评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,182评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,402评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,376评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,677评论 2 344

推荐阅读更多精彩内容