import csv, sys, collections, statistics
path = sys.argv[1]
rows = list(csv.DictReader(open(path, newline='')))
def base(r):
    return (r['occupancy_type']=='1' and r['lien_status']=='1' and r['business_or_commercial_purpose']!='1'
            and r['reverse_mortgage']!='1' and r['open-end_line_of_credit']!='1' and r['total_units'] in ('1','2','3','4'))
print('all purchase rows', len(rows))
b = [r for r in rows if base(r)]
print('owner-occ 1st-lien 1-4u closed-end non-business purchase rows', len(b))
act = collections.Counter(r['action_taken'] for r in b)
print('actions', dict(sorted(act.items())))
dec = [r for r in b if r['action_taken'] in ('1','2','3')]
den = [r for r in b if r['action_taken']=='3']
print('decided (1+2+3)=%d  denied=%d  denial rate=%.1f%%' % (len(dec), len(den), 100*len(den)/len(dec)))
names = {'1':'DTI','2':'Employment','3':'Credit history','4':'Collateral','5':'Insufficient cash','6':'Unverifiable info','7':'Incomplete app','8':'MI denied','9':'Other','10':'NA','1111':'Exempt'}
prim = collections.Counter(r['denial_reason-1'] for r in den)
print('PRIMARY denial reason (denial_reason-1) share of denials:')
for k,v in prim.most_common(): print('  %-18s %5d  %.1f%%' % (names.get(k,k), v, 100*v/len(den)))
anyr = collections.Counter()
for r in den:
    s = set(r[f'denial_reason-{i}'] for i in range(1,5)) - {'', '10'}
    for x in s: anyr[x]+=1
print('ANY reason listed (share of denials):')
for k,v in anyr.most_common(): print('  %-18s %5d  %.1f%%' % (names.get(k,k), v, 100*v/len(den)))
lt = {'1':'Conventional','2':'FHA','3':'VA','4':'USDA'}
orig = [r for r in b if r['action_taken']=='1']
print('originations', len(orig))
c = collections.Counter(r['loan_type'] for r in orig)
for k,v in sorted(c.items()): print('  orig %-12s %5d %.1f%%' % (lt[k], v, 100*v/len(orig)))
for k in ('1','2'):
    d = [r for r in dec if r['loan_type']==k]; dn=[r for r in d if r['action_taken']=='3']
    print('  denial rate %s: %d/%d = %.1f%%' % (lt[k], len(dn), len(d), 100*len(dn)/len(d)))
la = [float(r['loan_amount']) for r in orig if r['loan_amount'] not in ('','NA','Exempt')]
print('median loan amount orig: %.0f' % statistics.median(la))
pv = [float(r['property_value']) for r in orig if r['property_value'] not in ('','NA','Exempt')]
print('median property value orig: %.0f' % statistics.median(pv))
inc = [float(r['income']) for r in orig if r['income'] not in ('','NA','Exempt')]
print('median income orig (thousands): %.0f' % statistics.median(inc))
ltv = [float(r['loan_to_value_ratio']) for r in orig if r['loan_to_value_ratio'] not in ('','NA','Exempt')]
print('share orig LTV>=95: %.1f%%  (n=%d)' % (100*sum(1 for x in ltv if x>=95)/len(ltv), len(ltv)))
dti = collections.Counter(r['debt_to_income_ratio'] for r in orig)
hi = sum(v for k,v in dti.items() if k in ('50%-60%','>60%'))
tot = sum(v for k,v in dti.items() if k not in ('NA','Exempt',''))
print('share orig DTI >=50%%: %.1f%% (n=%d)' % (100*hi/tot, tot))
# withdrawn/incomplete
print('withdrawn (4) %d, incomplete (5) %d of all apps %d' % (act['4'], act['5'], sum(act[k] for k in '12345')))
