def __init__(self): self.dates = [] self.OIS = OIS() self.corporates = [] self.ratings = ['AAA', 'AA'] self.corpSpreads = {} self.corporates = pd.DataFrame() self.corporates_full = {}
def getCorporates(self, trim_start, trim_end, WORKING_DIR='.'): self.OIS = OIS(trim_start=trim_start, trim_end=trim_end, WORKING_DIR=WORKING_DIR) self.datesAll = self.OIS.datesAll self.OISData = self.OIS.getOIS() self.WORKING_DIR = WORKING_DIR for rating in self.ratings: index = 'ML/' + rating + 'TRI' try: corpSpreads = 1e-4 * (Quandl.get( index, authtoken="Lqsxas8ieaKqpztgYHxk", trim_start=trim_start, trim_end=trim_end)) corpSpreads.reset_index(level=0, inplace=True) corpSpreads = pd.merge(left=self.datesAll, right=corpSpreads, how='left') corpSpreads = corpSpreads.fillna(method='ffill').fillna( method='bfill') self.corpSpreads[rating] = corpSpreads.T.fillna( method='ffill').fillna(method='bfill').T except: print(index, " not found") self.corpSpreads = pd.Panel.from_dict(self.corpSpreads) self.corporates = {} self.OISData.drop('Date', axis=1, inplace=True) ntenors = np.shape(self.OISData)[1] for rating in self.ratings: try: tiledCorps = np.tile(self.corpSpreads[rating]['Value'], ntenors) tiledCorps = tiledCorps.reshape(np.shape(self.OISData), order="F") self.corporates[rating] = pd.DataFrame( data=(tiledCorps + self.OISData.values)) self.corporates[rating].drop( self.corporates[rating].columns[[0]], axis=1, inplace=True) except: print("Error in addition of Corp Spreads") self.corporates = pd.Panel(self.corporates) return self.corporates
def lsqQ(para): ois = OIS(trim_start = startdate, trim_end = enddate) Affine = VasicekAffine(x, para, datelist) myCDS = CDS(ois.interpolationois(), datelist, Affine.calQ(), recovery) diff = np.subtract(brz_cds,myCDS.MTM()) return diff
def lsqR(para): ois = OIS(trim_start = startdate, trim_end = enddate) Affine = VasicekAffine(x, para, datelist) diff = np.subtract(ois.interpolationois(),Affine.calR()) return diff
def lsqQ(para): ois = OIS(trim_start = startdate, trim_end = enddate) Affine = VasicekAffine(x, para, datelist) myCDS = CDS(ois.interpolationois(), datelist, Affine.calQ(), recovery) diff = np.subtract(brz_cds,myCDS.MTM())
class CorporateRates(object): def __init__(self): self.dates = [] self.OIS = OIS() self.corporates = [] self.ratings = ['AAA', 'AA'] self.corpSpreads = {} self.corporates = pd.DataFrame() self.corporates_full = {} def getCorporates(self, trim_start, trim_end, WORKING_DIR='.'): self.OIS = OIS(trim_start=trim_start, trim_end=trim_end, WORKING_DIR=WORKING_DIR) self.datesAll = self.OIS.datesAll self.OISData = self.OIS.getOIS() self.WORKING_DIR = WORKING_DIR for rating in self.ratings: index = 'ML/' + rating + 'TRI' try: corpSpreads = 1e-4 * (Quandl.get( index, authtoken="Lqsxas8ieaKqpztgYHxk", trim_start=trim_start, trim_end=trim_end)) corpSpreads.reset_index(level=0, inplace=True) corpSpreads = pd.merge(left=self.datesAll, right=corpSpreads, how='left') corpSpreads = corpSpreads.fillna(method='ffill').fillna( method='bfill') self.corpSpreads[rating] = corpSpreads.T.fillna( method='ffill').fillna(method='bfill').T except: print(index, " not found") self.corpSpreads = pd.Panel.from_dict(self.corpSpreads) self.corporates = {} self.OISData.drop('Date', axis=1, inplace=True) ntenors = np.shape(self.OISData)[1] for rating in self.ratings: try: tiledCorps = np.tile(self.corpSpreads[rating]['Value'], ntenors) tiledCorps = tiledCorps.reshape(np.shape(self.OISData), order="F") self.corporates[rating] = pd.DataFrame( data=(tiledCorps + self.OISData.values)) self.corporates[rating].drop( self.corporates[rating].columns[[0]], axis=1, inplace=True) except: print("Error in addition of Corp Spreads") self.corporates = pd.Panel(self.corporates) return self.corporates def interpolation_corporates(self): start = datetime.datetime(2005, 1, 10) end = datetime.datetime(2010, 1, 10) try: row_days = (end - start).days + 1 except: print("error: may be end day is earlier than start day") ndate = [29, 89, 179, 364, 729, 1094, 1824, 2554, 3649, 7329, 10949] for rating in self.ratings: temp_dict = {} start = datetime.datetime(2005, 1, 10) for i in range(0, row_days): vdate = pd.DataFrame( self.corporates[rating].iloc[i, 0:11]).values.tolist() vdate = reduce(lambda x, y: x + y, vdate) end = start + relativedelta(years=30) ndays = (end - start).days d_series = np.arange(0, ndays, 1) temp_dict[start] = np.interp(d_series, ndate, vdate) start = start + datetime.timedelta(days=1) self.corporates_full[rating] = temp_dict h = self.corporates_full['AAA'] end_temp = datetime.datetime(2008, 1, 10) g = h[end_temp][7] # self.corporates_full=pd.Panel.from_dict(self.corporates_full) return self.corporates_full def pickleMe(self, file): pickle.dump(self.Data, open(file, "wb")) def unPickleMe(self, file): if (os.path.exists(file)): self.Data = pickle.load(open(file, "rb")) def saveMeExcel(self, whichdata, fileName, dir=None): df = pd.DataFrame(whichdata) if (dir == None): fName = os.path.join(self.WORKING_DIR, fileName + '.xlsx') else: fName = os.path.join(dir, fileName + '.xlsx') df.to_excel(fName, sheet_name=fileName, index=False)
# a=range(1,5) # for i in a: # print(i) # print(a) start = datetime.datetime(2005, 1, 10) end = datetime.datetime(2010, 1, 10) test_current_day = datetime.datetime(2009, 3, 5) test_end_day = datetime.datetime(2037, 9, 10) start_temp = start end_temp = start_temp + relativedelta(years=30) trim_start = "2005-01-10" trim_end = "2010-01-10" #a=Quandl.get("USTREASURY/YIELD", authtoken="Lqsxas8ieaKqpztgYHxk", trim_start=trim_start, trim_end=trim_end) #print(a) testois = OIS() print('discounted factor', testois.interpolationois()) a = testois.get_OIS_daily(test_current_day, test_end_day) #print(a) #test single:24+i to 28+i # a=testois.interpolationois() # ndays=(end_temp-start_temp).days # d_series=np.arange(0,ndays,1) # plt.plot(d_series,a) # plt.show() # a=(end-start).days # t=[] # t=np.arange(0,a,1) # ndate=[29,89,179,364,729,1094,1824,2554,3649,7329,10949]