def load_market_data(rundate, path, json_name='MarketData.json', cva_default=True): """ Loads a json marketdata file and corresponding calendar (assumed to be named 'calendars.cal') :param rundate: folder inside path where the marketdata file resides :param path: root folder for the marketdata, calendar and trades :param json_name: name of the marketdata json file (default MarketData.json) :param cva_default: loads a survival curve with recovery 50% (useful for testing) :return: a context object with the data and calendars loaded """ from riskflow.adaptiv import AdaptivContext as Context context = Context() context.parse_json(os.path.join(path, rundate, json_name)) context.parse_calendar_file(os.path.join(path, 'calendars.cal')) context.params['System Parameters']['Base_Date'] = pd.Timestamp(rundate) if cva_default: context.params['Price Factors']['SurvivalProb.DEFAULT'] = { 'Recovery_Rate': 0.5, 'Curve': utils.Curve([], [[0.0, 0.0], [.5, .01], [1, .02], [3, .07], [5, .15], [10, .35], [20, .71], [30, 1.0]]), 'Property_Aliases': None } return context
def as_internal(dct): if '.Curve' in dct: return utils.Curve(dct['.Curve']['meta'], dct['.Curve']['data']) elif '.Percent' in dct: return utils.Percent(dct['.Percent']) elif '.Deal' in dct: return construct_instrument( dct['.Deal'], self.params['Valuation Configuration']) elif '.Basis' in dct: return utils.Basis(dct['.Basis']) elif '.Descriptor' in dct: return utils.Descriptor(dct['.Descriptor']) elif '.DateList' in dct: return utils.DateList( OrderedDict([(Timestamp(date), val) for date, val in dct['.DateList']])) elif '.DateEqualList' in dct: return utils.DateEqualList([[Timestamp(values[0])] + values[1:] for values in dct['.DateEqualList'] ]) elif '.CreditSupportList' in dct: return utils.CreditSupportList(dct['.CreditSupportList']) elif '.DateOffset' in dct: return DateOffset(**dct['.DateOffset']) elif '.Offsets' in dct: return utils.Offsets(dct['.Offsets']) elif '.Timestamp' in dct: return Timestamp(dct['.Timestamp']) elif '.ModelParams' in dct: return ModelParams((dct['.ModelParams']['modeldefaults'], dct['.ModelParams']['modelfilters'])) return dct
def get_tenor(self): """Gets the tenor points stored in the Curve attribute""" if self.param['Curve'] is None or not isinstance( self.param['Curve'], utils.Curve): self.param['Curve'] = utils.Curve([], [(0.0, 0.0)]) # make sure there are no duplicate tenors tenors = np.unique(self.param['Curve'].array[:, 0]) rates = np.interp(tenors, *self.param['Curve'].array.T) self.param['Curve'].array = np.vstack((tenors, rates)).T return self.param['Curve'].array[:, 0]
def reduce_curve(self, factor, price_factors, interpolation='Hermite'): tenors = self.benchmark_tenors() factor_name = utils.check_tuple_name(factor) # fetch the actual rates rates = np.interp(tenors, *price_factors[factor_name]['Curve'].array.T) # overwrite the 'Curve' price_factors[factor_name]['Curve'] = utils.Curve([], np.dstack( (tenors, rates))[-1]) # set the interpolation price_factors[factor_name]['Interpolation'] = interpolation
def makeflatcurve(curr, bps, daycount='ACT_365', tenor=30): """ generates a constant (flat) curve in basis points with the given daycount and tenor :return: a dictionary containing the curve definition """ return { 'Currency': curr, 'Curve': utils.Curve([], [[0, bps * 0.01 * 0.01], [tenor, bps * 0.01 * 0.01]]), 'Day_Count': daycount, 'Property_Aliases': None, 'Sub_Type': 'None' }
def get_tenor(self): """Gets the tenor points stored in the Curve attribute""" if self.param['Quanto_FX_Volatility'] is None: self.param['Quanto_FX_Volatility'] = utils.Curve([], [(0.0, 0.0)]) return self.param['Quanto_FX_Volatility'].array[:, 0]
def pushCurve(strg, loc, toks): '''need to allow differentiation between adding a spread and scaling by a factor''' return utils.Curve( [], toks[0].asList()) if len(toks) == 1 else utils.Curve( toks[:-1], toks[-1].asList())