-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrisk.py
35 lines (25 loc) · 1.04 KB
/
risk.py
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
from helpers.helper import *
import numpy as np
def calc_risk(score, score_arr, loan_arr):
# when score is equal to one of the bin separators, treat it as if it was in the lower bin
head, tail = head_tail_list(score_arr)
if score in score_arr and score >= head+1:
score = score + 1
# split score bin equally into three qualitative levels of risk
score_arr = np.array(score_arr)
loan_arr = np.array(loan_arr)
lower_score = score_arr[score_arr <= score][-1]
upper_score = score_arr[score_arr >= score][0]
delta_score = upper_score - lower_score
risk_split = delta_score / 3
if score <= lower_score + risk_split:
risk_level = 'high'
elif score <= lower_score + 2*risk_split:
risk_level = 'medium'
else:
risk_level = 'low'
# loan amount is equal to the maximum amount of the loan bin
index, = np.where(score_arr == lower_score)
loan_amount = int(loan_arr[index+1][0])
# format risk output
return {'loan_amount': loan_amount, 'risk_level': risk_level}