File size: 1,262 Bytes
cda703b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

import pandas as pd 
import numpy as np
import streamlit as st
import gc

@st.cache_data(show_spinner=False)
def get_baseline_default_rate(df,target_col='bad_loan'):
    

    baseline_rate = df[target_col].mean()
    return baseline_rate

@st.cache_data(show_spinner=False)
def calculate_woe_iv(df, feature, target='bad_loan'):
    """

    Transforms a categorical variable into Weight of Evidence (WoE) scores 

    and computes the Total Information Value (IV) using vectorized operations.

    """
   
    subset = df[[feature, target]].copy()
    
    
    stats = subset.groupby(feature, observed=True)[target].agg([("Total", "count"), ("Bad", "sum")])
    
    stats['Good'] = stats['Total'] - stats['Bad']
      
    stats['Dist_Bad'] = stats['Bad'] / stats['Bad'].sum()
    stats['Dist_Good'] = stats['Good'] / stats['Good'].sum()
    
    stats['WoE'] = np.log((stats['Dist_Good'] + 0.001) / (stats['Dist_Bad'] + 0.001))
    stats['IV_Contribution'] = (stats['Dist_Good'] - stats['Dist_Bad']) * stats['WoE']
    
    woe_df = stats.sort_values(by='WoE', ascending=False).reset_index()
    total_iv = woe_df['IV_Contribution'].sum()
    
    del subset, stats
    gc.collect()
    
    return woe_df, total_iv