%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import math
def compound_interest(principal, rate, compounds_per_year, years):
return principal * (1 + rate / compounds_per_year) ** (compounds_per_year * years)
print compound_interest(100, 0.07, 1, 1) # 7% interest, compounded 1 time per year, for 1 year
print compound_interest(100, 0.07, 1, 10) # 7% interest, compounded 1 time per year, for 10 years
print compound_interest(1, 1.00, 1, 1) # 100% interest, compounded 1 time per year, for one year
107.0 196.715135729 2.0
def ci_by_year_and_compounds(years_array, compounds):
return [compound_interest(1, 1.00, compounds, y) for y in years_array]
x = np.arange(0, 1 + 1./12, 1./12)
y = [compound_interest(1, 1.00, 1, y) for y in x]
y2 = ci_by_year_and_compounds(x, 2)
y3 = ci_by_year_and_compounds(x, 10)
y4 = ci_by_year_and_compounds(x, 100)
y_e = [math.e**(n) for n in x]
plot(x,y, 'b') # blue
plot(x,y2, 'g') # green
plot(x,y3, 'r') # red
plot(x,y4, 'c') # cyan
plot(x,y_e, 'ro') # red dots
[<matplotlib.lines.Line2D at 0x104cf1d10>]
print "Compounded 1 time: ", compound_interest(1, 1.0, 1, 1)
print "Compounded 10 times: ", compound_interest(1, 1.0, 10, 1)
print "Compounded 100 times: ", compound_interest(1, 1.0, 100, 1)
print "Compounded 1000 times: ", compound_interest(1, 1.0, 1000, 1)
print
print "Value of e: ", math.e
Compounded 1 time: 2.0 Compounded 10 times: 2.5937424601 Compounded 100 times: 2.70481382942 Compounded 1000 times: 2.71692393224 Value of e: 2.71828182846