def test_sample_crifImport(self): csvLocation = 'test/NochKleineresTestportfolio.csv' # CrifUtil.read_csv should align different formatting styles of the CRIF. crif = Crif(CRIF.CrifUtil.read_csv(csvLocation, sep=',')) # After the creation the CRIF consists of ArrayLists of Java objects (sensitivitities, scheduleNotionals etc) print(str(crif.getAllSensitivities())) print(str(crif.getAllAddonNotionals())) # The data set used for creating the crif is stored within the crif object: print(str(crif.asDataFrame.head(20))) asdf = 1
def test_originalStaysTheSame(self): Input = CRIF.CrifUtil.read_csv(r'../NochKleineresTestportfolio.csv') crif = Crif(Input) simm = Simm() result = simm.calculateStandard(crif.getAllSensitivities(), 'USD').doubleValue() tradeIds = crif.Sensitivities.keys() tradeId = next(iter(tradeIds)) bumpedCrif = BumpedCrif(crif, tradeId, 0.01) result2 = simm.calculateStandard(crif.getAllSensitivities(), 'USD').doubleValue() self.assertEqual(result, result2)
def test1(self): Input = CrifUtil.read_csv(r'../NochKleineresTestportfolio.csv') crif = Crif(Input) self.assertEqual('IMLedis_NRW_Bank', crif.Counterparty) self.assertEqual('collect', crif.direction) self.assertEqual('EMIR', crif.regulation) del crif
def test_performance_SA(self): Input = CRIF.CrifUtil.read_csv(r'../NochKleineresTestportfolio.csv') tic = time.time() crif = Crif(Input) imTree = StandardCalculation.calculate(crif) imTree = StandaloneAllocation.calculate(imTree) toc = time.time() self.assertTrue((toc-tic) < 0.8)
def test_toDataFrameOneLine(self): OneLineCrif = '''ValuationDate,IMLedis,tradeId,IMModel,productClass,riskType,qualifier,bucket,label1,label2,amount,amountCurrency,amountUSD,EndDate,CollectRegulations,PostRegulations ,IMLedis_NRW_Bank,DZ14788807,SIMM,RatesFX,Risk_IRCurve,EUR,1,10y,Libor3m,1000000,EUR,1000000,,EMIR,''' Input = CRIF.CrifUtil.read_csv(StringIO(OneLineCrif)) crif = Crif(Input) imTree = StandardCalculation.calculate(crif) df = imTree.toDataFrame() asdf = 1
def test_toDataFrameWithBothAllocations(self): Input = CRIF.CrifUtil.read_csv(r'..\NochKleineresTestportfolio.csv') crif = Crif(Input) imTree = StandardCalculation.calculate(crif) imTree = StandaloneAllocation.calculate(imTree) imTree = EulerAllocation.calculate(imTree) df = imTree.toDataFrame() asdf = 1
def allocate_im(crif): if crif is None: raise PreventUpdate crif = Crif(pd.read_json(crif)) imTree = StandardCalculation.calculate(crif) imTree = EulerAllocation.calculate(imTree) imTree = StandaloneAllocation.calculate(imTree) imTree = imTree.toDataFrame() return imTree.to_json()
def test_performance_JPM_komplett(self): Input = CRIF.CrifUtil.read_csv(r'../NochKleineresTestportfolio.csv') tic = time.time() crif = Crif(Input) imTree = StandardCalculation.calculate(crif) imTree = EulerAllocation.calculate(imTree) toc = time.time() print(toc - tic) asfd = 1 self.assertTrue((toc - tic) < 20)
def test_both_allocations(self): Input = CRIF.CrifUtil.read_csv(r'../NochKleineresTestportfolio.csv') tic = time.time() crif = Crif(Input) imTree = StandardCalculation.calculate(crif) imTree = EulerAllocation.calculate(imTree) imTree = StandaloneAllocation.calculate(imTree) toc = time.time() print(toc - tic) asfd = 1 self.assertTrue((toc - tic) < 40)
def testJsonCreatorStandardCalculation(self): OneLineCrif = '''ValuationDate,IMLedis,tradeId,IMModel,productClass,riskType,qualifier,bucket,label1,label2,amount,amountCurrency,amountUSD,EndDate,CollectRegulations,PostRegulations ,IMLedis_NRW_Bank,DZ14788807,SIMM,RatesFX,Risk_IRCurve,EUR,1,10y,Libor3m,1000000,EUR,1000000,,EMIR,''' Input = CRIF.CrifUtil.read_csv(StringIO(OneLineCrif)) crif = Crif(Input) imTree = StandardCalculation.calculate(crif) testJson = imTree.to_json(with_data=True) self.assertEqual( testJson, r'''{"Total 51000000": {"children": [{"SIMM-P 51000000": {"children": [{"RatesFX 51000000": {"children": [{"Interest Rate 51000000": {"children": [{"Delta 51000000": {"children": [{"EUR 51000000": {"data": {"rowNumber": 5, "Level": 6, "levelName": "Bucket", "ExposureAmount": 51000000.0, "manifestation": "EUR", "identifier": "SIMM-P_RatesFX_Interest Rate_Delta_EUR"}}}], "data": {"rowNumber": 4, "Level": 5, "levelName": "Sensitivity Type", "ExposureAmount": 51000000.0, "manifestation": "Delta", "identifier": "SIMM-P_RatesFX_Interest Rate_Delta"}}}], "data": {"rowNumber": 3, "Level": 4, "levelName": "Risk Class", "ExposureAmount": 51000000.0, "manifestation": "Interest Rate", "identifier": "SIMM-P_RatesFX_Interest Rate"}}}], "data": {"rowNumber": 2, "Level": 3, "levelName": "Silo", "ExposureAmount": 51000000.0, "manifestation": "RatesFX", "identifier": "SIMM-P_RatesFX"}}}], "data": {"rowNumber": 1, "Level": 2, "levelName": "ImModel", "ExposureAmount": 51000000.0, "manifestation": "SIMM-P", "identifier": "SIMM-P"}}}], "data": {"rowNumber": 0, "Level": 1, "levelName": "Total", "ExposureAmount": 51000000.0, "manifestation": "Total", "identifier": ""}}}''' )
def __init__(self, CRIFsdataframe): CRIFs = CRIFsdataframe[[ 'IMLedis', 'CollectRegulations', 'PostRegulations' ]].drop_duplicates() for row in CRIFs.itertuples(): identifier = tuple([row[1], row[2], row[3]]) CrifFrame = CRIFsdataframe[ (CRIFsdataframe.IMLedis == row[1]) & (CRIFsdataframe.CollectRegulations == row[2]) & (CRIFsdataframe.PostRegulations == row[3])] thisCrif = Crif(CrifFrame) self[str(thisCrif)] = thisCrif
def test_bumpWorked(self): eps = 0.1 tradeId = 'DZ14788807' Input = CRIF.CrifUtil.read_csv(r'../NochKleineresTestportfolio.csv') crif = Crif(Input) simm = Simm() bumpedCrif = BumpedCrif(crif, tradeId, eps) InputManBump = pd.read_csv( r'../NochKleineresTestportfolioManualBump.csv') manuelCrif = Crif(InputManBump) resultAutoBumped = simm.calculateStandard( bumpedCrif.getAllSensitivities(), 'USD').doubleValue() resultManBumped = simm.calculateStandard( manuelCrif.getAllSensitivities(), 'USD').doubleValue() resultOrig = simm.calculateStandard(crif.getAllSensitivities(), 'USD').doubleValue() autoSensis = bumpedCrif.Sensitivities[tradeId] manuellSensis = manuelCrif.Sensitivities[tradeId] for i in range(0, autoSensis.size()): self.assertAlmostEqual(getSensitivityAmount(autoSensis.get(i)), getSensitivityAmount(manuellSensis.get(i)), places=5) self.assertAlmostEqual(resultAutoBumped, resultManBumped, places=3) self.assertNotAlmostEqual(resultOrig, resultManBumped, places=0) self.assertNotAlmostEqual(resultOrig, resultAutoBumped, places=0)
def test5(self): Input = CrifUtil.read_csv(r'../testIR11asCRIF.csv') crif1 = Crif(Input) crif2 = Crif(Input) self.assertEqual( 31773442304, round( Simm.calculateStandard(crif1.getAllSensitivities(), 'USD').doubleValue())) self.assertEqual( 31773442304, round( Simm.calculateStandard(crif2.getAllSensitivities(), 'USD').doubleValue()))
def test_calculate_initial_margin(self): # To streamline it is only possible to create an IM Tree from which the Overall Initial Margin may be extracted # afterwards. # The structure of the IM Tree has been adopted from the open source acadiasoft engine. csvLocation = 'test/NochKleineresTestportfolio.csv' crif = Crif(CRIF.CrifUtil.read_csv(csvLocation, sep=',')) # call StandardCalculation.calculate(crif) to calculate the Initial Margin and create an IM Tree imTree = StandardCalculation.calculate(crif) imTree = EulerAllocation.calculate(imTree) imTree = StandaloneAllocation.calculate(imTree) # The resulting IM Tree can be displayed relatively conveniently with print print(imTree) # Alternatively it may be transformed into a DataFrame to simplify analysis of the result in Python imTree_asDataFrame = imTree.toDataFrame() print(imTree_asDataFrame.head(10)) print(imTree_asDataFrame.columns) # If you want to analyse further outside of python you can save the imTree as a .csv File imTree.printToCsv('./ExampleTreeAsCsv.csv') # Finally, you can extract the overall Initial Margin as follows: im = imTree.getMargin() print('The initial Margin of ' + str(crif) + ' is ' + '{:,.2f}'.format(im) + ' USD.') # By default the calculationCurrency and the currency in which the margin is returned is USD asdf = 1
def calculate_IM(crif): if crif is None: raise PreventUpdate crif = Crif(pd.read_json(crif)) imTree = StandardCalculation.calculate(crif) return imTree.toDataFrame().to_json()
def test3(self): Input = CrifUtil.read_csv(r'../NochKleineresTestportfolio.csv') crif = Crif(Input) self.assertEqual(True, crif.asDataFrame.equals(Input)) del crif
def test_toDataFrameMultiLine(self): Input = CRIF.CrifUtil.read_csv(r'..\NochKleineresTestportfolio.csv') crif = Crif(Input) imTree = StandardCalculation.calculate(crif) df = imTree.toDataFrame()