Skip to content

Instantly share code, notes, and snippets.

@sophacles
Created March 9, 2016 23:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sophacles/a4469f5c656e09e4bc1d to your computer and use it in GitHub Desktop.
Save sophacles/a4469f5c656e09e4bc1d to your computer and use it in GitHub Desktop.
import csv
from calendar import monthrange
def get_n_days(year, month):
return monthrange(year, month)[1]
def due_month(year, month):
due_month = ((month + 6) % 12)
if due_month == 0: due_month = 12
due_year = year + ((month + 6) // 12)
if due_year == 2016 and due_month > 3:
return (2016,3)
return (due_year, due_month)
def get_period(now, due):
due_year, due_month = due
now_year, now_month = now
if now_year == due_year:
return due_month - now_month
elif due_month == 12:
return 6
else:
return (12-now_month) + due_month
def payout_due(deposit, rate, period=6):
# interest formula - assume monthly compounding for period months
# default assumes 6 month cd
return deposit * ((1 + rate/12.0) ** period)
def load_data():
rate_dict = {}
with open('cdrate.csv', 'r') as fd:
reader = csv.reader(fd)
for entry in reader:
year, month = entry[0].partition('-')[::2]
time = (int(year), int(month))
rate = float(entry[1])/100
rate_dict[time] = rate
return rate_dict
def lifetime():
rate_dict = load_data()
due_dict = {}
for year in range(1964,2017):
for month in range(1,13):
if year == 1964 and month < 6: continue
if year == 2016 and month > 2: continue
time = (year, month)
if time in due_dict:
base = due_dict.pop(time)
else:
base = 0
addition = get_n_days(*time)
due_on = due_month(*time)
period = get_period(time, due_on)
payout = payout_due(base+addition, rate_dict[time], period)
print "creating a {} month cd in {} for {}, expecting {} on {}".format(period, time, base+addition, payout, due_on)
due_dict.setdefault(due_on, 0.0)
due_dict[due_on] += payout
return due_dict
if __name__ == '__main__':
print "Total value:", lifetime().values()[0] + get_n_days(2016, 3)
1964-6 3.97
1964-7 3.96
1964-8 3.96
1964-9 3.99
1964-10 4.04
1964-11 4.10
1964-12 4.30
1965-1 4.23
1965-2 4.27
1965-3 4.35
1965-4 4.36
1965-5 4.39
1965-6 4.37
1965-7 4.37
1965-8 4.38
1965-9 4.45
1965-10 4.52
1965-11 4.54
1965-12 4.88
1966-1 5.07
1966-2 5.16
1966-3 5.34
1966-4 5.40
1966-5 5.48
1966-6 5.59
1966-7 5.71
1966-8 5.90
1966-9 6.14
1966-10 6.04
1966-11 5.95
1966-12 5.76
1967-1 5.43
1967-2 5.20
1967-3 5.03
1967-4 4.45
1967-5 4.60
1967-6 4.86
1967-7 5.22
1967-8 5.31
1967-9 5.29
1967-10 5.50
1967-11 5.70
1967-12 5.95
1968-1 5.74
1968-2 5.53
1968-3 5.78
1968-4 5.97
1968-5 6.31
1968-6 6.29
1968-7 6.14
1968-8 5.93
1968-9 5.85
1968-10 5.94
1968-11 6.17
1968-12 6.39
1969-1 6.58
1969-2 6.62
1969-3 6.74
1969-4 6.94
1969-5 7.36
1969-6 8.10
1969-7 8.88
1969-8 8.46
1969-9 8.68
1969-10 8.77
1969-11 8.63
1969-12 8.95
1970-1 9.06
1970-2 8.71
1970-3 8.16
1970-4 7.67
1970-5 8.20
1970-6 8.16
1970-7 7.98
1970-8 7.75
1970-9 7.35
1970-10 6.96
1970-11 6.16
1970-12 5.73
1971-1 5.17
1971-2 4.50
1971-3 4.04
1971-4 4.61
1971-5 5.22
1971-6 5.69
1971-7 6.04
1971-8 6.06
1971-9 5.91
1971-10 5.43
1971-11 5.04
1971-12 4.89
1972-1 4.26
1972-2 4.02
1972-3 4.59
1972-4 5.00
1972-5 4.71
1972-6 4.98
1972-7 5.13
1972-8 5.22
1972-9 5.55
1972-10 5.59
1972-11 5.48
1972-12 5.65
1973-1 6.03
1973-2 6.55
1973-3 7.23
1973-4 7.46
1973-5 7.62
1973-6 8.16
1973-7 9.49
1973-8 10.71
1973-9 10.20
1973-10 8.71
1973-11 8.89
1973-12 8.68
1974-1 8.50
1974-2 7.78
1974-3 8.58
1974-4 10.04
1974-5 10.91
1974-6 11.09
1974-7 11.96
1974-8 12.06
1974-9 11.52
1974-10 9.59
1974-11 8.85
1974-12 8.82
1975-1 7.42
1975-2 6.43
1975-3 6.33
1975-4 6.75
1975-5 6.30
1975-6 6.25
1975-7 7.06
1975-8 7.61
1975-9 7.89
1975-10 7.23
1975-11 6.84
1975-12 6.56
1976-1 5.57
1976-2 5.68
1976-3 5.87
1976-4 5.52
1976-5 6.11
1976-6 6.31
1976-7 5.90
1976-8 5.70
1976-9 5.58
1976-10 5.30
1976-11 5.19
1976-12 4.76
1977-1 5.11
1977-2 5.15
1977-3 5.16
1977-4 5.05
1977-5 5.73
1977-6 5.64
1977-7 5.70
1977-8 6.20
1977-9 6.45
1977-10 6.96
1977-11 6.93
1977-12 6.95
1978-1 7.25
1978-2 7.25
1978-3 7.18
1978-4 7.38
1978-5 7.84
1978-6 8.24
1978-7 8.56
1978-8 8.44
1978-9 8.96
1978-10 9.84
1978-11 11.15
1978-12 11.28
1979-1 11.09
1979-2 10.62
1979-3 10.47
1979-4 10.34
1979-5 10.44
1979-6 9.98
1979-7 10.23
1979-8 10.86
1979-9 12.01
1979-10 13.83
1979-11 13.97
1979-12 13.42
1980-1 13.48
1980-2 14.58
1980-3 17.74
1980-4 15.80
1980-5 9.78
1980-6 8.33
1980-7 8.73
1980-8 10.29
1980-9 11.73
1980-10 12.99
1980-11 15.36
1980-12 17.10
1981-1 15.92
1981-2 16.00
1981-3 14.48
1981-4 15.12
1981-5 17.66
1981-6 16.09
1981-7 17.40
1981-8 17.98
1981-9 17.19
1981-10 15.71
1981-11 12.65
1981-12 13.07
1982-1 14.25
1982-2 15.12
1982-3 14.25
1982-4 14.42
1982-5 13.77
1982-6 14.66
1982-7 13.80
1982-8 11.53
1982-9 11.46
1982-10 9.67
1982-11 9.13
1982-12 8.80
1983-1 8.46
1983-2 8.77
1983-3 8.80
1983-4 8.76
1983-5 8.62
1983-6 9.45
1983-7 9.91
1983-8 10.17
1983-9 9.64
1983-10 9.31
1983-11 9.51
1983-12 9.85
1984-1 9.56
1984-2 9.73
1984-3 10.37
1984-4 10.73
1984-5 11.64
1984-6 11.96
1984-7 12.08
1984-8 11.71
1984-9 11.46
1984-10 10.63
1984-11 9.39
1984-12 8.85
1985-1 8.45
1985-2 9.04
1985-3 9.60
1985-4 8.75
1985-5 8.08
1985-6 7.58
1985-7 7.80
1985-8 7.97
1985-9 8.09
1985-10 7.97
1985-11 7.82
1985-12 7.80
1986-1 7.83
1986-2 7.70
1986-3 7.23
1986-4 6.57
1986-5 6.64
1986-6 6.72
1986-7 6.36
1986-8 5.92
1986-9 5.71
1986-10 5.70
1986-11 5.76
1986-12 5.95
1987-1 5.85
1987-2 6.10
1987-3 6.18
1987-4 6.65
1987-5 7.24
1987-6 7.15
1987-7 6.87
1987-8 7.02
1987-9 7.74
1987-10 8.19
1987-11 7.31
1987-12 7.67
1988-1 7.10
1988-2 6.69
1988-3 6.78
1988-4 7.14
1988-5 7.52
1988-6 7.69
1988-7 8.18
1988-8 8.66
1988-9 8.50
1988-10 8.48
1988-11 8.81
1988-12 9.28
1989-1 9.36
1989-2 9.71
1989-3 10.40
1989-4 10.13
1989-5 9.60
1989-6 9.09
1989-7 8.59
1989-8 8.56
1989-9 8.75
1989-10 8.45
1989-11 8.21
1989-12 8.12
1990-1 8.17
1990-2 8.26
1990-3 8.48
1990-4 8.57
1990-5 8.48
1990-6 8.28
1990-7 8.12
1990-8 7.99
1990-9 8.06
1990-10 8.05
1990-11 7.95
1990-12 7.64
1991-1 7.17
1991-2 6.51
1991-3 6.50
1991-4 6.16
1991-5 6.03
1991-6 6.26
1991-7 6.25
1991-8 5.79
1991-9 5.60
1991-10 5.32
1991-11 4.92
1991-12 4.41
1992-1 4.07
1992-2 4.13
1992-3 4.42
1992-4 4.13
1992-5 3.96
1992-6 3.97
1992-7 3.50
1992-8 3.40
1992-9 3.17
1992-10 3.27
1992-11 3.60
1992-12 3.55
1993-1 3.33
1993-2 3.22
1993-3 3.20
1993-4 3.16
1993-5 3.20
1993-6 3.36
1993-7 3.34
1993-8 3.32
1993-9 3.24
1993-10 3.25
1993-11 3.39
1993-12 3.35
1994-1 3.29
1994-2 3.62
1994-3 4.03
1994-4 4.38
1994-5 4.90
1994-6 4.85
1994-7 5.15
1994-8 5.17
1994-9 5.40
1994-10 5.79
1994-11 6.11
1994-12 6.78
1995-1 6.71
1995-2 6.44
1995-3 6.34
1995-4 6.27
1995-5 6.07
1995-6 5.80
1995-7 5.73
1995-8 5.79
1995-9 5.73
1995-10 5.76
1995-11 5.64
1995-12 5.49
1996-1 5.28
1996-2 5.03
1996-3 5.30
1996-4 5.42
1996-5 5.47
1996-6 5.64
1996-7 5.75
1996-8 5.57
1996-9 5.71
1996-10 5.51
1996-11 5.43
1996-12 5.47
1997-1 5.54
1997-2 5.47
1997-3 5.69
1997-4 5.90
1997-5 5.87
1997-6 5.78
1997-7 5.70
1997-8 5.71
1997-9 5.71
1997-10 5.72
1997-11 5.78
1997-12 5.82
1998-1 5.56
1998-2 5.55
1998-3 5.61
1998-4 5.63
1998-5 5.67
1998-6 5.65
1998-7 5.65
1998-8 5.61
1998-9 5.33
1998-10 4.99
1998-11 5.07
1998-12 5.01
1999-1 4.90
1999-2 4.95
1999-3 4.98
1999-4 4.94
1999-5 5.03
1999-6 5.31
1999-7 5.58
1999-8 5.83
1999-9 5.89
1999-10 6.04
1999-11 5.97
1999-12 6.07
2000-1 6.15
2000-2 6.26
2000-3 6.36
2000-4 6.50
2000-5 6.94
2000-6 6.91
2000-7 6.86
2000-8 6.76
2000-9 6.68
2000-10 6.65
2000-11 6.63
2000-12 6.30
2001-1 5.45
2001-2 5.12
2001-3 4.74
2001-4 4.41
2001-5 4.01
2001-6 3.74
2001-7 3.70
2001-8 3.49
2001-9 2.84
2001-10 2.26
2001-11 2.03
2001-12 1.90
2002-1 1.85
2002-2 1.95
2002-3 2.16
2002-4 2.11
2002-5 1.93
2002-6 1.92
2002-7 1.84
2002-8 1.72
2002-9 1.74
2002-10 1.69
2002-11 1.40
2002-12 1.36
2003-1 1.30
2003-2 1.27
2003-3 1.20
2003-4 1.23
2003-5 1.19
2003-6 1.02
2003-7 1.06
2003-8 1.13
2003-9 1.13
2003-10 1.14
2003-11 1.17
2003-12 1.17
2004-1 1.12
2004-2 1.11
2004-3 1.09
2004-4 1.21
2004-5 1.46
2004-6 1.76
2004-7 1.85
2004-8 1.89
2004-9 2.04
2004-10 2.18
2004-11 2.46
2004-12 2.66
2005-1 2.85
2005-2 3.00
2005-3 3.23
2005-4 3.34
2005-5 3.44
2005-6 3.56
2005-7 3.80
2005-8 3.99
2005-9 4.01
2005-10 4.32
2005-11 4.52
2005-12 4.62
2006-1 4.69
2006-2 4.88
2006-3 5.01
2006-4 5.17
2006-5 5.25
2006-6 5.46
2006-7 5.54
2006-8 5.44
2006-9 5.37
2006-10 5.35
2006-11 5.33
2006-12 5.31
2007-1 5.34
2007-2 5.34
2007-3 5.28
2007-4 5.31
2007-5 5.32
2007-6 5.36
2007-7 5.34
2007-8 5.40
2007-9 5.33
2007-10 5.00
2007-11 4.85
2007-12 4.85
2008-1 3.71
2008-2 2.99
2008-3 2.70
2008-4 2.86
2008-5 2.84
2008-6 3.09
2008-7 3.13
2008-8 3.11
2008-9 3.82
2008-10 4.37
2008-11 2.83
2008-12 2.18
2009-1 1.53
2009-2 1.75
2009-3 1.76
2009-4 1.54
2009-5 1.04
2009-6 0.67
2009-7 0.50
2009-8 0.44
2009-9 0.36
2009-10 0.31
2009-11 0.30
2009-12 0.30
2010-1 0.29
2010-2 0.30
2010-3 0.34
2010-4 0.42
2010-5 0.64
2010-6 0.75
2010-7 0.62
2010-8 0.44
2010-9 0.38
2010-10 0.35
2010-11 0.41
2010-12 0.38
2011-1 0.38
2011-2 0.37
2011-3 0.32
2011-4 0.30
2011-5 0.32
2011-6 0.35
2011-7 0.42
2011-8 0.47
2011-9 0.50
2011-10 0.58
2011-11 0.67
2011-12 0.58
2012-1 0.52
2012-2 0.50
2012-3 0.47
2012-4 0.46
2012-5 0.48
2012-6 0.46
2012-7 0.40
2012-8 0.37
2012-9 0.35
2012-10 0.33
2012-11 0.29
2012-12 0.29
2013-1 0.28
2013-2 0.27
2013-3 0.26
2013-4 0.26
2013-5 0.26
2013-6 0.26
2013-7 0.14
2013-8 0.14
2013-9 0.13
2013-10 0.13
2013-11 0.13
2013-12 0.13
2014-1 0.13
2014-2 0.13
2014-3 0.13
2014-4 0.13
2014-5 0.13
2014-6 0.13
2014-7 0.13
2014-8 0.13
2014-9 0.13
2014-10 0.13
2014-11 0.13
2014-12 0.13
2015-1 0.13
2015-2 0.13
2015-3 0.13
2015-4 0.13
2015-5 0.13
2015-6 0.13
2015-7 0.13
2015-8 0.13
2015-9 0.13
2015-10 0.13
2015-11 0.13
2015-12 0.13
2016-1 0.13
2016-2 0.13
2016-3 0.13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment