-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSA_RRAO_Calc.py
105 lines (82 loc) · 4.06 KB
/
SA_RRAO_Calc.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
"""
Calculate Basel III Market Risk Residual Risk Add-On Capital reqirement using the
Standardised Approach within the frtb.net framework
Copyright © 2024 frtb.net limited
Author: Alan Skea, frtb.net limited
Contact us at <info@frtb.net> or via our website at <https://frtb.net>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import pandas as pd
import FRTBCalculator
@FRTBCalculator.registerClass
class MR_RR_SA_RRAO(FRTBCalculator.FRTBCalculator):
# This is the primary entry point and computes capital for a single risk class
# at all the necessary correlation levels. It returns a Dictionary with an entry
# for RiskClass and an entry for each computed result, such as the capital
# at each of the correlation levels. E.g.:
# {
# 'RiskClass' : 'MR_RRAO',
# 'Medium' : 123.45,
# }
#
def calcRiskClassCapital(self, riskClass, df):
bucketResult = []
for bucket, bucketSensis in df.groupby('Bucket'):
bdf = self.prepareData(riskClass, bucketSensis)
bdf = self.applyRiskWeights(riskClass, bdf)
bdf = self.collectRiskFactors(riskClass, bdf) # this ought to be a no-op for RRAO
bucketResult.extend(self.getBucketCalculator(riskClass, bucket)(riskClass, bucket, bdf))
buckets = pd.DataFrame(bucketResult)
return self.aggregateRRAO(riskClass, buckets)
def getBucketCalculator(self, riskClass, bucket):
return self.calcBucketRRAO
def prepareData(self, _, df):
if 'ExemptionClass' in df.columns: # EU wants Exepmt residual risks in the report but we don't want them in the calc (Article 325u(4) of Regulation (EU) No 575/2013)
return df[df['ExemptionClass'].isna()]
else:
return df
def applyRiskWeights(self, riskClass, df):
RW = self.getConfigItem('RiskWeight')
ndf = df.copy()
ndf.loc[:, 'RiskWeight'] = ndf['Bucket'].apply(lambda x: RW[x])
ndf.loc[:, 'WeightedNotionalAmount'] = ndf['RiskWeight'] * df['NotionalAmount']
return ndf
def collectRiskFactors(self, riskClass, df):
# df has all the input data for a single Bucket but might be more granular than the
# bucket risk factors we need. So here we aggregate all the rows for the same risk
# factor into a single row. In RRAO the buckets are artificial and there are no factor
# attributes. This exists just so it can be called consistently with the other calculators.
#
factorFields = []
valueFields = ['NotionalAmount', 'RiskWeight', 'WeightedNotionalAmount']
ndf = df[['RiskGroup', 'RiskSubGroup', 'RiskClass', 'Bucket'] + factorFields + valueFields].groupby(['RiskGroup', 'RiskSubGroup', 'RiskClass', 'Bucket'] + factorFields, dropna=False).sum().reset_index()
return ndf
# Compute RRAO for a single bucket (e.g. Exotic or Non-Exotic)
#
def calcBucketRRAO(self, riskClass, bucket, df):
capital = df['WeightedNotionalAmount'].sum()
bucketRRAO = {
'RiskClass': riskClass,
'Bucket': bucket,
'Correlation' :'Medium',
'Capital': capital
}
return [bucketRRAO]
def aggregateRRAO(self, riskClass, buckets):
capitals = []
# Create capital dictionary
capital = {}
capital['RiskClass'] = riskClass
capital['Correlation'] = 'Medium'
capital['Capital'] = buckets['Capital'].sum()
capitals.append(capital)
return capitals