kirb.py (Source)

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jan 28 03:14:48 2019

@author: randlow
"""

import scipy as sp
import pandas as pandas
import matplotlib.pyplot as plt
import scipy.stats as stats
import numpy as np
import ipdb

def kirbCalc(pd,lgd,expType,m=1,avc=1,sales=10):
    r_1a = (1-np.exp(-50*pd))/(1-np.exp(-50))
    
    if expType=='lge': # corporate exposure (large)
        r = avc*(0.12*r_1a)+0.24*(1-r_1a)
    elif expType=='sme': # corporate exposure (small & medium)
        r = (0.12*r_1a)+(0.24*(1-r_1a))-0.04*(1-(max(sales-5,0)/45))
    elif expType=='resid': # residential mortgages
        r = 0.15
    else:
        r = 0.04      # qualifying revolving retail exposure
        
    if expType=='lge' or expType=='sme': # corporate exposures
        b = (0.11852-0.05478*np.log(pd))**2
        mtrAdj = (1+(m-2.5)*b)/(1-1.5*b)
    else:
        mtrAdj = 1
       
    k1a = np.sqrt(1/(1-r))*stats.norm.ppf(pd) # wtg of expected PD
    k1b = np.sqrt(r/(1-r))*stats.norm.ppf(0.999) # wtg of unexpected PD
    k1c = stats.norm.cdf(k1a+k1b)
    
    unexpECL = k1c*lgd  # unexpected ECL    
    expECL = pd*lgd # expected ECL
    
    kirb = unexpECL-expECL
    return kirb, unexpECL, expECL

pd = np.arange(0.05,1.05,0.05) # probability of default 
m = 1                          # effective maturity 
lgd = 100                      # loss given default
expType = ['resid','cc','lge','sme']                # type of exposure (lge,sme,resid,cc)
avg = 1
sales = 10

exp = 'sme'
(kirb,unexpECL,expECL) = kirbCalc(pd,lgd,exp)


plt.plot(pd,kirb)