GenAICoder commited on
Commit
abcfe27
·
verified ·
1 Parent(s): af01d61

Update metrics/vintage_metrics.py

Browse files
Files changed (1) hide show
  1. metrics/vintage_metrics.py +50 -42
metrics/vintage_metrics.py CHANGED
@@ -3,21 +3,22 @@
3
  import pandas as pd
4
  import numpy as np
5
 
 
 
6
 
7
  def calculate_vintage_dpd(
8
  df,
9
  mob,
10
  dpd_threshold,
11
- metric_type="count"
 
12
  ):
13
- """
14
- Generic DPD metric calculator.
15
- Example:
16
- 30+@3
17
- 60+@6
18
- """
19
 
20
- mob_df = df[df["mob"] == mob].copy()
 
 
 
 
21
 
22
  mob_df["bad_flag"] = np.where(
23
  mob_df["dpd"] >= dpd_threshold,
@@ -25,51 +26,53 @@ def calculate_vintage_dpd(
25
  0
26
  )
27
 
28
- vintage_summary = (
29
- mob_df.groupby("booking_vintage")
 
 
 
 
 
30
  .agg(
31
  total_accounts=("account_id", "nunique"),
32
- bad_accounts=("bad_flag", "sum"),
33
- total_balance=("balance", "sum"),
34
- bad_balance=(
35
- "balance",
36
- lambda x: x[
37
- mob_df.loc[x.index, "bad_flag"] == 1
38
- ].sum()
39
- )
40
  )
41
  .reset_index()
42
  )
43
 
44
- if metric_type == "count":
45
-
46
- vintage_summary["dpd_rate"] = (
47
- vintage_summary["bad_accounts"]
48
- / vintage_summary["total_accounts"]
49
- ) * 100
50
-
51
- elif metric_type == "balance":
52
 
53
- vintage_summary["dpd_rate"] = (
54
- vintage_summary["bad_balance"]
55
- / vintage_summary["total_balance"]
56
- ) * 100
57
 
58
- return vintage_summary
59
 
60
 
61
  def calculate_vintage_ncl(
62
  df,
63
- mob
 
 
64
  ):
65
- """
66
- Calculates NCL rate at a given MOB.
67
- """
68
 
69
- mob_df = df[df["mob"] == mob].copy()
 
 
 
 
 
 
70
 
71
- vintage_summary = (
72
- mob_df.groupby("booking_vintage")
 
 
 
73
  .agg(
74
  total_balance=("balance", "sum"),
75
  total_ncl=("ncl_amount", "sum")
@@ -77,9 +80,14 @@ def calculate_vintage_ncl(
77
  .reset_index()
78
  )
79
 
80
- vintage_summary["ncl_rate"] = (
81
- vintage_summary["total_ncl"]
82
- / vintage_summary["total_balance"]
83
  ) * 100
84
 
85
- return vintage_summary
 
 
 
 
 
 
3
  import pandas as pd
4
  import numpy as np
5
 
6
+ from helpers.filter_helpers import apply_filters
7
+
8
 
9
  def calculate_vintage_dpd(
10
  df,
11
  mob,
12
  dpd_threshold,
13
+ group_col=None,
14
+ filters=None
15
  ):
 
 
 
 
 
 
16
 
17
+ df = apply_filters(df, filters)
18
+
19
+ mob_df = df[
20
+ df["mob"] == mob
21
+ ].copy()
22
 
23
  mob_df["bad_flag"] = np.where(
24
  mob_df["dpd"] >= dpd_threshold,
 
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"] = (
49
+ result["dpd_rate"]
50
+ .round(2)
51
+ )
52
 
53
+ return result
54
 
55
 
56
  def calculate_vintage_ncl(
57
  df,
58
+ mob,
59
+ group_col=None,
60
+ filters=None
61
  ):
 
 
 
62
 
63
+ df = apply_filters(df, filters)
64
+
65
+ mob_df = df[
66
+ df["mob"] == mob
67
+ ].copy()
68
+
69
+ group_columns = ["booking_vintage"]
70
 
71
+ if group_col is not None:
72
+ group_columns.append(group_col)
73
+
74
+ result = (
75
+ mob_df.groupby(group_columns)
76
  .agg(
77
  total_balance=("balance", "sum"),
78
  total_ncl=("ncl_amount", "sum")
 
80
  .reset_index()
81
  )
82
 
83
+ result["ncl_rate"] = (
84
+ result["total_ncl"]
85
+ / result["total_balance"]
86
  ) * 100
87
 
88
+ result["ncl_rate"] = (
89
+ result["ncl_rate"]
90
+ .round(2)
91
+ )
92
+
93
+ return result