Assume we have the following market data file:
{
"MarketData": {
"Market Prices": {},
"Price Factors": {
"FxRate.USD": {
"Spot": 1,
"Domestic_Currency": "",
"Interest_Rate": "USD-MASTER"
},
"InterestRate.ZAR-SWAP": {
"Curve": {
".Curve": {
"meta": [],
"data": [[0.00274, 0.05], [10, 0.08]]
}
},
"Sub_Type": "",
"Day_Count": "ACT_365",
"Accrual_Calendar": "",
"Currency": ""
},
"InterestRate.ZAR-SWAP.ZAR-USD-BASIS": {
"Curve": {
".Curve": {
"meta": [],
"data": [[0.00274,0.01], [10,-0.01]]
}
},
"Sub_Type": "",
"Day_Count": "ACT_365",
"Accrual_Calendar": "",
"Currency": ""
},
"InterestRate.USD-MASTER": {
"Curve": {
".Curve": {
"meta": [],
"data": [[0.00274,0.01], [10,0.02]]
}
},
"Sub_Type": "",
"Day_Count": "ACT_365",
"Accrual_Calendar": "",
"Currency": "USD"
},
"FxRate.ZAR": {
"Domestic_Currency": "USD",
"Interest_Rate": "ZAR-SWAP.ZAR-USD-BASIS",
"Spot": 0.07
}
},
"System Parameters": {
"Base_Currency": "USD",
"Correlations_Healing_Method": "Eigenvalue_Raising"
},
"Correlations": {
"HWInterestRate.ZAR-SWAP.F1": {
"HWInterestRate.USD-MASTER.F1": 0.2,
"LognormalDiffusionProcess.ZAR": 0.5
},
"HWInterestRate.USD-MASTER.F1": {
"LognormalDiffusionProcess.ZAR": -0.1
}
},
"Price Models": {
"HullWhite1FactorInterestRateModel.USD-MASTER": {
"Alpha": 1,
"Lambda": 0,
"Sigma": {
".Curve": {
"meta": [],
"data": [[0,0.004]]
}
},
"Quanto_FX_Volatility":null
},
"HullWhite1FactorInterestRateModel.ZAR-SWAP": {
"Alpha": 2,
"Lambda": 0,
"Sigma": {
".Curve": {
"meta": [],
"data": [[0,0.005]]
}
},
"Quanto_FX_Volatility":null
},
"GBMAssetPriceModel.ZAR": {
"Drift": -0.03,
"Vol": 0.17
},
"HullWhite1FactorInterestRateModel.ZAR-SWAP.ZAR-USD-BASIS": {
"Alpha": 3.5,
"Lambda": 0,
"Sigma": {
".Curve": {
"meta": [],
"data": [[0,0.01]]
}
},
"Quanto_FX_Volatility":null
}
},
"Model Configuration": {
".ModelParams": {
"modeldefaults": {
"FxRate": "GBMAssetPriceModel",
"InterestRate": "HullWhite1FactorInterestRateModel"
},
"modelfilters": {}
}
}
},
"Version": null
}
and the trade data file defined earlier
Everything in RiskFlow is based off a Context. All calculations are constructed with reference to one. Note that the interest rate curves start one day from now i.e. . No interest rate curve can start at time 0 (although the rate at time 0 is flat extrapolated from the first timepoint).
Assume the Market Data file is called MarketData.json, the trade data is called fxfwd.json and the calendar file is called calendars.xml. Here's how to perform a Base Revaluation:
import riskflow
#create a calculation context
cx = riskflow.Context()
#load calendars
cx.parse_calendar_file('calendars.xml')
#load marketdata
cx.parse_json('MarketData.json')
#load tradedata
cx.parse_json('fxfwd.json')
#all calculations take a context as an input
calc = riskflow.calculation.construct_calculation('Base_Revaluation', cx)
#execute the calculation with the following parameters
out = calc.execute ( { 'calc_name':('test1',), 'Run_Date':'2017-08-01',
'Currency':'ZAR' } )
out
will now contain the stats for the computation in a dictionary with the field
Result containing the results.
To check the results, use the following code:
out['Results']
should return a pandas dataframe and look like:
Test NettingCollateralSet 0.0
341 FXNonDeliverableForward -343.123474121
So the market value at 1 August 2017 of the forward is -343 ZAR. If a credit simulation was needed, we could reuse the context and construct a new calculation
#first define the parameters
time_grid = '0d 2d 1w(1w) 3m(1m) 2y(3m)'
params = { 'calc_name':('test2',), 'Time_grid':time_grid,
'Run_Date':'2017-08-01', 'Currency':'ZAR', 'Simulation_Batches':10,
'Batch_Size':512, 'Random_Seed':6126, 'Calc_Scenarios':'No'
'Generate_Cashflows':'Yes', 'Dynamic_Scenario_Dates': 'Yes'
}
cmc = riskflow.Calculation.construct_calculation('Credit_Monte_Carlo', cx)
#execute the calculation with the previous parameters
out = cmc.execute ( params )
To see the output, access the Results
key i.e.
out['Results'])
should output
EE PFE
2017-08-01 0.000000 0.000000
2017-08-03 1.809047 0.000000
2017-08-08 25.414378 201.102859
2017-08-15 62.647971 414.040855
2017-08-22 96.029947 574.368042
2017-08-29 130.653986 737.920593
2017-08-31 132.900141 745.158432
2017-09-01 0.000000 0.000000
Where EE is the Expected Exposure and PFE is the Peak Exposure at 95% (the default). Note that
this returns a pandas dataframe and as such, can be plotted by using the .plot()
method
(assuming matplotlib is installed).