GenAICoder commited on
Commit
d53c633
·
verified ·
1 Parent(s): 8582913

Update metrics/vintage_metrics.py

Browse files
Files changed (1) hide show
  1. metrics/vintage_metrics.py +57 -9
metrics/vintage_metrics.py CHANGED
@@ -20,29 +20,65 @@ def calculate_vintage_dpd(
20
  df["mob"] == mob
21
  ].copy()
22
 
23
- mob_df["bad_flag"] = np.where(
24
- mob_df["dpd"] >= dpd_threshold,
25
- 1,
 
 
 
 
 
 
 
 
 
 
 
 
26
  0
27
  )
28
 
 
 
 
 
29
  group_columns = ["booking_vintage"]
30
 
31
  if group_col is not None:
32
  group_columns.append(group_col)
33
 
 
 
 
 
34
  result = (
35
  mob_df.groupby(group_columns)
36
  .agg(
37
- total_accounts=("account_id", "nunique"),
38
- bad_accounts=("bad_flag", "sum")
 
 
 
 
 
 
 
 
 
 
 
 
39
  )
40
  .reset_index()
41
  )
42
 
 
 
 
 
43
  result["dpd_rate"] = (
44
- result["bad_accounts"]
45
- / result["total_accounts"]
46
  ) * 100
47
 
48
  result["dpd_rate"] = (
@@ -74,8 +110,20 @@ def calculate_vintage_ncl(
74
  result = (
75
  mob_df.groupby(group_columns)
76
  .agg(
77
- total_balance=("balance", "sum"),
78
- total_ncl=("ncl_amount", "sum")
 
 
 
 
 
 
 
 
 
 
 
 
79
  )
80
  .reset_index()
81
  )
 
20
  df["mob"] == mob
21
  ].copy()
22
 
23
+ # -----------------------------------
24
+ # BAD FLAG
25
+ # -----------------------------------
26
+
27
+ mob_df["bad_flag"] = (
28
+ mob_df["dpd"] >= dpd_threshold
29
+ )
30
+
31
+ # -----------------------------------
32
+ # BAD BALANCE
33
+ # -----------------------------------
34
+
35
+ mob_df["bad_balance"] = np.where(
36
+ mob_df["bad_flag"],
37
+ mob_df["balance"],
38
  0
39
  )
40
 
41
+ # -----------------------------------
42
+ # GROUPING
43
+ # -----------------------------------
44
+
45
  group_columns = ["booking_vintage"]
46
 
47
  if group_col is not None:
48
  group_columns.append(group_col)
49
 
50
+ # -----------------------------------
51
+ # AGGREGATION
52
+ # -----------------------------------
53
+
54
  result = (
55
  mob_df.groupby(group_columns)
56
  .agg(
57
+ total_accounts=(
58
+ "account_id",
59
+ "nunique"
60
+ ),
61
+
62
+ total_balance=(
63
+ "balance",
64
+ "sum"
65
+ ),
66
+
67
+ bad_balance=(
68
+ "bad_balance",
69
+ "sum"
70
+ )
71
  )
72
  .reset_index()
73
  )
74
 
75
+ # -----------------------------------
76
+ # DOLLAR RATE
77
+ # -----------------------------------
78
+
79
  result["dpd_rate"] = (
80
+ result["bad_balance"]
81
+ / result["total_balance"]
82
  ) * 100
83
 
84
  result["dpd_rate"] = (
 
110
  result = (
111
  mob_df.groupby(group_columns)
112
  .agg(
113
+ total_accounts=(
114
+ "account_id",
115
+ "nunique"
116
+ ),
117
+
118
+ total_balance=(
119
+ "balance",
120
+ "sum"
121
+ ),
122
+
123
+ total_ncl=(
124
+ "ncl_amount",
125
+ "sum"
126
+ )
127
  )
128
  .reset_index()
129
  )