def actual_nb_days_AFB_alter(start, end): # http://svn.finmath.net/finmath%20lib/trunk/src/main/java/net/finmath/time/daycount/DayCountConvention_ACT_ACT_YEARFRAC.java y1, m1, d1 = start y2, m2, d2 = end delta = date(*end) - date(*start) if delta <= 365: if is_leap_year(y1) and is_leap_year(y2): denom = 366 elif is_leap_year(y1) and date(y1, m1, d1) <= date(y1, 2, 29): denom = 366 elif is_leap_year(y2) and date(y2, m2, d2) >= date(y2, 2, 29): denom = 366 else: denom = 365 else: year_range = range(y1, y2 + 1) nb = 0 for y in year_range: nb += 366 if is_leap_year(y) else 365 denom = nb / len(year_range) return delta / denom
def actual_nb_days_AFB_alter( start, end ): # http://svn.finmath.net/finmath%20lib/trunk/src/main/java/net/finmath/time/daycount/DayCountConvention_ACT_ACT_YEARFRAC.java y1, m1, d1 = start y2, m2, d2 = end delta = date(*end) - date(*start) if delta <= 365: if is_leap_year(y1) and is_leap_year(y2): denom = 366 elif is_leap_year(y1) and date(y1, m1, d1) <= date(y1, 2, 29): denom = 366 elif is_leap_year(y2) and date(y2, m2, d2) >= date(y2, 2, 29): denom = 366 else: denom = 365 else: year_range = list(range(y1, y2 + 1)) nb = 0 for y in year_range: nb += 366 if is_leap_year(y) else 365 denom = nb / len(year_range) return delta / denom
def actual_nb_days_ISDA(start, end): # needed to separate days_in_leap_year from days_not_leap_year y1, m1, d1 = start y2, m2, d2 = end days_in_leap_year = 0 days_not_in_leap_year = 0 year_range = range(y1, y2 + 1) for y in year_range: if y == y1 and y == y2: nb_days = date(y2, m2, d2) - date(y1, m1, d1) elif y == y1: nb_days = date(y1 + 1, 1, 1) - date(y1, m1, d1) elif y == y2: nb_days = date(y2, m2, d2) - date(y2, 1, 1) else: nb_days = 366 if is_leap_year(y) else 365 if is_leap_year(y): days_in_leap_year += nb_days else: days_not_in_leap_year += nb_days return (days_not_in_leap_year, days_in_leap_year)
def actual_nb_days_ISDA( start, end ): # needed to separate days_in_leap_year from days_not_leap_year y1, m1, d1 = start y2, m2, d2 = end days_in_leap_year = 0 days_not_in_leap_year = 0 year_range = list(range(y1, y2 + 1)) for y in year_range: if y == y1 and y == y2: nb_days = date(y2, m2, d2) - date(y1, m1, d1) elif y == y1: nb_days = date(y1 + 1, 1, 1) - date(y1, m1, d1) elif y == y2: nb_days = date(y2, m2, d2) - date(y2, 1, 1) else: nb_days = 366 if is_leap_year(y) else 365 if is_leap_year(y): days_in_leap_year += nb_days else: days_not_in_leap_year += nb_days return (days_not_in_leap_year, days_in_leap_year)
def actual_nb_days_afb_alter(beg, end): # http://svn.finmath.net/finmath%20lib/trunk/src/main/java/net/ # finmath/time/daycount/DayCountConvention_ACT_ACT_YEARFRAC.java delta = date(*end) - date(*beg) if delta <= 365: if (is_leap_year(beg[0]) and date(*beg) <= date(beg[0], 2, 29) or is_leap_year(end[0]) and date(*end) >= date(end[0], 2, 29) or is_leap_year(beg[0]) and is_leap_year(end[0])): denom = 366 else: denom = 365 else: year_range = range(beg[0], end[0] + 1) nb = 0 for y in year_range: nb += 366 if is_leap_year(y) else 365 denom = nb / len(year_range) return delta / denom
def test_is_leap_year(): assert is_leap_year(1900) assert is_leap_year(1904) assert is_leap_year(2000) assert is_leap_year(2104) assert not is_leap_year(1) assert not is_leap_year(2100) assert not is_leap_year(2101) assert not is_leap_year(2103) assert not is_leap_year(2102) with pytest.raises(TypeError): is_leap_year('x') with pytest.raises(TypeError): is_leap_year(-1) with pytest.raises(TypeError): is_leap_year(0)