compound

1757 days ago by JAVIER

import numpy as np import matplotlib as mpl import matplotlib.pyplot as plt 
       

We study the temporal evolution of an initial investment (or pricipal) of P dollars at an annual interest rate r for 24 months. We consider simple and compounded interest with a different number of compoundings per year. 

#Principal P = 1000 #Annual interest rate (decimal) r = 0.10 #Number of compounds per year compounds_per_year = [4,6] 
       
def future_value(t,n): return P * (1 + r/n)^np.floor(t/12*n) 
       
def simple_future_value(t): return P*(1 + r*np.floor(t/12)) 
       
fig = plt.figure() axes= fig.add_subplot(111) x = np.linspace(0,24,500) colors = ['blue'] n = 4 #copounds_per_year z = simple_future_value(x) y = future_value(x,n) axes.plot(x,z,linewidth=2,color='black',label='simple') text = 'n=' + str(n) #axes.plot(x,y,linewidth=2,color=colors[0],label=text) plt.legend(loc=2) plt.xlabel('Months') plt.ylabel('Dollars') fig.savefig('test.png') plt.close() 
       
fig = plt.figure() axes= fig.add_subplot(111) x = np.linspace(0,24,500) colors = ['blue'] n = 6 #copounds_per_year z = simple_future_value(x) y = future_value(x,n) #axes.plot(x,z,linewidth=2,color='blue',label='n') text = 'n=' + str(n) axes.plot(x,y,linewidth=2,color=colors[0],label=text) plt.legend(loc=2) plt.xlabel('Months') plt.ylabel('Dollars') fig.savefig('test.png') plt.close() 
       
fig = plt.figure() axes= fig.add_subplot(111) x = np.linspace(0,24,500) colors = ['red','blue','black'] z = simple_future_value(x) axes.plot(x,z,linewidth=2,color='black',label='simple') for k,n in enumerate(compounds_per_year): y = future_value(x,n) text = 'n=' + str(n) axes.plot(x,y,linewidth=2,color=colors[k],label=text) plt.legend(loc=2) plt.xlabel('Months') plt.ylabel('Dollars') fig.savefig('test.png') plt.close() 
       
fig = plt.figure() axes= fig.add_subplot(111) #compounds_per_year = [2,1000] x = np.linspace(0,24,500) y = future_value(x,compounds_per_year[1])-future_value(x,compounds_per_year[0]) text = 'n='+str(compounds_per_year[1]) + ' minus ' + 'n='+str(compounds_per_year[0]) axes.plot(x,y,linewidth=2,label=text) plt.legend(loc=2) plt.xlabel('Months') plt.ylabel('Dollars') fig.savefig('test.png') plt.clf() 
       
o = [1]*11 alignment = 0 x = np.arange(0,11,1)+alignment x = np.array(x,dtype=np.float64) y = [49,58,66,72,76,79,80,80,78,74,69] y = np.array(y,dtype=np.float64) y = y.transpose() w = np.array([x,y]) print w.transpose() fig = plt.figure() axes = fig.add_subplot(111) axes.plot(x,y,'ko') plt.xlabel('Time (hours after 7 am)') plt.ylabel('Temperature ($^\circ$F)') #---------------------------------------- m = np.array([o,x,x^2]) m = m.transpose() U,s,V = np.linalg.svd(m,full_matrices=False) b = U.transpose().dot(y) s = 1/s z = s * b x = V.transpose().dot(z) print x #---------------------------------------- def temp_fun(t): return x[0] + x[1] * t + x[2] * t**2 #---------------------------------------- t = np.linspace(0,10,500)+alignment axes.plot(t,temp_fun(t),linewidth=2,color='red') #---------------------------------------- fig.savefig('test.png') plt.close() 
       
[[  0.  49.]
 [  1.  58.]
 [  2.  66.]
 [  3.  72.]
 [  4.  76.]
 [  5.  79.]
 [  6.  80.]
 [  7.  80.]
 [  8.  78.]
 [  9.  74.]
 [ 10.  69.]]
[ 48.97202797  10.01864802  -0.8018648 ]
[[  0.  49.]
 [  1.  58.]
 [  2.  66.]
 [  3.  72.]
 [  4.  76.]
 [  5.  79.]
 [  6.  80.]
 [  7.  80.]
 [  8.  78.]
 [  9.  74.]
 [ 10.  69.]]
[ 48.97202797  10.01864802  -0.8018648 ]
#Choose a model