def curve_to_byte(self, resolution=500): # """Return filename of plot of the damped_vibration function.""" # interest = [element.interest for element in self.interest_rate_list] # maturity = [(element.maturity - datetime.combine(date.today(), datetime.min.time())).days / 365 for element in self.interest_rate_list] start = date.today() self.interest_rate_list.sort( key=lambda x: utils.toTimestamp(x.maturity), reverse=False) end = self.interest_rate_list[-1].maturity maturity = pd.date_range(start, end, 1000) interest = list( map(lambda x: self.get_interest_rate(x) * 100, maturity)) mat = list( map( lambda x: (x - datetime.combine(date.today( ), datetime.min.time())).days / 365, maturity)) bytes_image = None plt.clf() #plt.xaxis.set_major_formatter(FormatStrFormatter('%.2f')) plt.plot(mat, interest) plt.ylabel("Interest rate (%)") plt.xlabel("Maturity in years") plt.title("Country: " + str(self.country).capitalize() + ", Calibration time: " + str(self.start_time)) # Make Matplotlib write to BytesIO file object and grab # return the object's string bytes_image = io.BytesIO() plt.savefig(bytes_image, format='png') bytes_image.seek(0) return bytes_image
def get_interest_rate(self, date): #print(date) #print("Date type "+str(type(date.to_pydatetime()))) #print(self.interest_rate_list[0].maturity) if hasattr(self, "interpolation"): return self.interpolation(utils.toTimestamp(date.to_pydatetime())) else: return 0
def checkTimes(self): print "[times] checking events" leftoverEvents = [] for event in self.events: ta,tb,action = event # print pair[1] - pair[0] # secsLeft = (tb-ta).seconds # FIXME change to secsLeft = (time.now()-tb).seconds # secsLeft = (datetime.datetime.now()-tb).seconds tb = u.toTimestamp(tb) now = u.toTimestamp(datetime.datetime.now()) print now-tb secsLeft = int(now-tb) # print tb-datetime.datetime.now() # print "SECSLEFT", secsLeft if secsLeft > -self.THRESHOLD: self.handleTime(event) continue leftoverEvents.append(event) self.events = leftoverEvents[:]
def interpolate(self): self.interest_rate_list.sort( key=lambda x: utils.toTimestamp(x.maturity), reverse=False) interest = [element.interest for element in self.interest_rate_list] maturity = [ utils.toTimestamp(element.maturity) for element in self.interest_rate_list ] new_interest = [] new_maturity = [] for int, mat in zip(interest, maturity): if isinstance(int, numbers.Number) and isinstance( mat, numbers.Number): if np.isfinite(int) and np.isfinite( mat) and not mat in new_maturity: new_interest.append(int) new_maturity.append(mat) #print(interest) if len(new_interest) > 1: self.interpolation = CubicSpline(new_maturity, new_interest)
def writeTimes(self): print "[times] writing times" with open(TIMEFILE,"w") as outfile: for ta, tb, action in self.events: outfile.write("%i %i %s\n" % (u.toTimestamp(ta), u.toTimestamp(tb),action))