def adjust_NonRegionalCountryWeights(self, countryWeightsDict, nonRegionalValidationResult): """In NSBL case, adjust the nonregional total country weights """ newCountryWeightDict = {} adjusted_Non_Regional_CountryWeightDict = {} adjusted_Regional_CountryWeightDict = {} targetWeights = RISK_LIMIT_DICT_SPB_ASSUMPTIONS.get( 'Non_Regional').get('NSBL') nonRegionalCountriesInScope = list(nonRegionalValidationResult.keys()) totalNonRegionalWeightsOrigin = sum( list(nonRegionalValidationResult.values())) adjustWeights = totalNonRegionalWeightsOrigin - targetWeights originalWeightPropotion_Non_Regional = { country: weight / totalNonRegionalWeightsOrigin for country, weight in countryWeightsDict.items() if country in nonRegionalCountriesInScope } for country, weight in nonRegionalValidationResult.items(): newWeight = originalWeightPropotion_Non_Regional.get( country) * targetWeights adjusted_Non_Regional_CountryWeightDict[country] = newWeight newCountryWeightDict = adjusted_Non_Regional_CountryWeightDict sumWeights_Regional = sum( weight for country, weight in countryWeightsDict.items() if country not in (nonRegionalCountriesInScope + self.countriesToAdjust)) originalWeightPropotion_Regional = { country: weight / sumWeights_Regional for country, weight in countryWeightsDict.items() if country not in (nonRegionalCountriesInScope + self.countriesToAdjust) } for country, originalWeight in countryWeightsDict.items(): if (country in nonRegionalCountriesInScope) or ( country in self.countriesToAdjust): continue newWeight = originalWeightPropotion_Regional.get( country) * adjustWeights + originalWeight adjusted_Regional_CountryWeightDict[country] = newWeight newCountryWeightDict.update(adjusted_Regional_CountryWeightDict) #for those not to ajust couries, get their original weight back to final country weights for country in self.countriesToAdjust: newCountryWeightDict.update({country: countryWeightsDict[country]}) newCountryWeightDict = self.updateCountryWeights(newCountryWeightDict) #self.utils.reOrderCountryWeightDict(newCountryWeightDict, reverse = True) newCountryWeightsList = list(newCountryWeightDict.values()) #self.countriesToAdjust = self.countriesToAdjust + list(nonRegionalValidationResult.keys()) self.nonRegionalCountriesNotToAdjust = nonRegionalCountriesInScope return newCountryWeightsList, newCountryWeightDict
def _valiadateNonRegionalLimit(self, countryWeightsDict): nonRegionalWeightsTotal = sum([ countryWeightsDict.get(country, 0) for country in self.nonRegional_NSBLMembers ]) if round(nonRegionalWeightsTotal, 4) > RISK_LIMIT_DICT_SPB_ASSUMPTIONS.get('Non_Regional').get( 'NSBL'): return { country: weight for country, weight in countryWeightsDict.items() if country in self.nonRegional_NSBLMembers } else: return {}
def adjust_ChinaWeights(self, countryWeightsDict, highRatingCountryWeightExcludeChina): newCountryWeightDict = {} adjusted_ChinaCountryWeightDict = {} targetWeight = RISK_LIMIT_DICT_SPB_ASSUMPTIONS.get('China').get('NSBL') chinaOriginalWeight = countryWeightsDict.get('CHN', 0) weightToAdjust = chinaOriginalWeight - targetWeight sumWeights_HighRatingCountry_ExcludeChina = sum( weight for country, weight in highRatingCountryWeightExcludeChina.items() if country not in self.countriesToAdjust) originalWeightPropotion_HighRatingCountry_ExcludeChina = { country: weight / sumWeights_HighRatingCountry_ExcludeChina for country, weight in highRatingCountryWeightExcludeChina.items() if country not in self.countriesToAdjust } adjusted_ChinaCountryWeightDict['CHN'] = targetWeight for country, originalWeight in highRatingCountryWeightExcludeChina.items( ): if country in self.countriesToAdjust: continue newWeight = originalWeightPropotion_HighRatingCountry_ExcludeChina.get( country) * weightToAdjust + originalWeight adjusted_ChinaCountryWeightDict[country] = newWeight for country in self.fullCountryList: if adjusted_ChinaCountryWeightDict.get(country): newCountryWeightDict[ country] = adjusted_ChinaCountryWeightDict.get(country) else: newCountryWeightDict[country] = countryWeightsDict.get(country) print('total weights: {0}'.format( sum(list(newCountryWeightDict.values())))) newCountryWeightDict = self.updateCountryWeights(newCountryWeightDict) #self.utils.reOrderCountryWeightDict(newCountryWeightDict, reverse = True) newCountryWeightsList = list(newCountryWeightDict.values()) return newCountryWeightsList, newCountryWeightDict
def adjust_NonRegionalCountryWeights(self, countryWeightsDict, nonRegionalValidationResult): """In SBL case, adjust the nonregional total country weights """ newCountryWeightDict = {} adjusted_Non_Regional_CountryWeightDict = {} adjusted_Regional_CountryWeightDict = {} targetWeights = RISK_LIMIT_DICT_SPB_ASSUMPTIONS.get('Non_Regional').get('SBL') nonRegionalCountriesInScope = list(nonRegionalValidationResult.keys()) totalNonRegionalWeightsOrigin = sum(list(nonRegionalValidationResult.values())) adjustWeights = totalNonRegionalWeightsOrigin - targetWeights originalWeightPropotion_Non_Regional = { country : weight/totalNonRegionalWeightsOrigin for country, weight in countryWeightsDict.items() if country in nonRegionalCountriesInScope} for country, weight in nonRegionalValidationResult.items(): newWeight = originalWeightPropotion_Non_Regional.get(country) * targetWeights adjusted_Non_Regional_CountryWeightDict[country] = newWeight newCountryWeightDict = adjusted_Non_Regional_CountryWeightDict sumWeights_Regional = sum(weight for country, weight in countryWeightsDict.items() if country not in (nonRegionalCountriesInScope + self.countriesNotToAdjust)) #get the regional countries and take out those "not to adjust" countries from the regional country list. "Not to Adjust" countries including those already reached to the limit like China if reached to 10% originalWeightPropotion_Regional = { country : weight/sumWeights_Regional for country, weight in countryWeightsDict.items() if country not in (nonRegionalCountriesInScope + self.countriesNotToAdjust)} for country, originalWeight in countryWeightsDict.items(): if (country in nonRegionalCountriesInScope) or (country in self.countriesNotToAdjust): continue newWeight = originalWeightPropotion_Regional.get(country) * adjustWeights + originalWeight adjusted_Regional_CountryWeightDict[country] = newWeight newCountryWeightDict.update(adjusted_Regional_CountryWeightDict) for country in self.countriesNotToAdjust: newCountryWeightDict.update({country: countryWeightsDict[country]}) newCountryWeightDict = self.updateCountryWeights(newCountryWeightDict) print(len(list(newCountryWeightDict.values()))) print(sum(list(newCountryWeightDict.values()))) newCountryWeightDict = self.updateCountryWeights(newCountryWeightDict) #update the not to adjust list to make non regional coutnries not to adjust further #self.countriesNotToAdjust = self.countriesNotToAdjust + nonRegionalCountriesInScope self.nonRegionalCountriesNotToAdjust = nonRegionalCountriesInScope return newCountryWeightDict
def _valiadateChinaLimit(self, countryWeightsDict): chinaWeights = countryWeightsDict.get('CHN') return chinaWeights > RISK_LIMIT_DICT_SPB_ASSUMPTIONS.get('China').get('SBL')
def adjustNSBLIndividualCountryExposure(self, countryWeights, isAdjustMaxWeight=False): """Function used to adjust for individual country weights based on particular categories - isAdjustMaxWeight: Any NSBL Country weights should never be bigger than the max of SBL country weights, if so, adjust the country weights - isAdjustChinaExposure: per President.Jin in April 2019 BOD, the Total exposure of China can not exceed 10%. we will set 10% of 10% as buffer thus the max of CHina country weights will be 9.9% """ weightsToAdjust = 0 newCountryWeightDict = {} countriesToAdjustLocal = [] print("total weights origin: {0}".format( sum(list(countryWeights.values())))) if isAdjustMaxWeight: #1. get the sum of aggregrated extra weights for those overweighted countries comparing with max(sbl(any_countries) #2. get the new weights of the overweighted countries as the max(SBL) for country, weight in countryWeights.items(): if weight > self.maxWeight_SBL: #self.countriesToAdjust.append(country) #get overweighted country list as countries to adjust countriesToAdjustLocal.append( country ) #get overweighted country list as countries to adjust weightsToAdjust = weightsToAdjust + ( weight - self.maxWeight_SBL ) #get aggregrated extra weights over max(SBL) for all countries newCountryWeightDict[ country] = self.maxWeight_SBL #set current overweighted country to use the max(SBL) as new weight chinaLimit = RISK_LIMIT_DICT_SPB_ASSUMPTIONS.get('China', 0).get('NSBL', 0) if countryWeights.get('CHN', 0) >= (chinaLimit - chinaLimit * 0.1): #threshold for check if china already in a warning area which are not appliable for adjustment pool self.countriesToAdjust.append('CHN') newCountryWeightDict['CHN'] = countryWeights.get('CHN', 0) else: raise ("No Adjustment Choice Made, please double check your code!") #if no country weights to be adjusted, return the input countryWeights if not newCountryWeightDict: return countryWeights #proportionally adjust the rest country weights by applying the aggregrated extra weights to their original proportion within the sub country group excluding the overweighted countries sumWeights_ExcludeAdjustCountry = sum( weight for country, weight in countryWeights.items() if country not in (countriesToAdjustLocal + self.countriesToAdjust)) originalWeightPropotion_ExcludeAdjustCountry = { country: weight / sumWeights_ExcludeAdjustCountry for country, weight in countryWeights.items() if country not in (countriesToAdjustLocal + self.countriesToAdjust) } for country, originalWeight in countryWeights.items(): if country in (countriesToAdjustLocal + self.countriesToAdjust): continue newWeight = originalWeightPropotion_ExcludeAdjustCountry.get( country ) * weightsToAdjust + originalWeight #countryWeights.get(country) newCountryWeightDict[country] = newWeight #attatch adjusted countries to the list for the use of excluding them from the weighted average adjustment self.countriesToAdjust = self.countriesToAdjust + countriesToAdjustLocal retCountryWeights = dict( sorted(newCountryWeightDict.items(), key=lambda kv: kv[1], reverse=True) ) #return the sorted weighted countries by decending order retCountryWeights = self.updateCountryWeights(retCountryWeights) retCountryWeightsList = list(newCountryWeightDict.values()) print("total weights: {0}".format(sum(list( retCountryWeights.values())))) return retCountryWeightsList, retCountryWeights