Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pd
- import numpy as np
- import matplotlib.pyplot as plt
- df = pd.read_csv('MORTGAGES.csv')
- #Amortisation of mortgage payments, assumes 20% down payment by borrower
- #Change 360 to 180 to model 15-year mortgages
- df['RATE'] = df['RATE'] / 1200
- df['PAYMENT'] = (0.8 *df['MEDPRICE'] * df['RATE'])/(1-np.exp(-360*np.log(1+df['RATE'])))
- #Index=100 at 2012-01-01
- inf = df[['PCEPI']].copy()
- pay = df[['PAYMENT']].copy()
- #list-of-tuples
- data = []
- #The payments are deflated and put into a corrosponding tuple
- #Change 359 to 179 for 15-year mortgages
- i = 0
- while (i < 597):
- cost = (100 * pay.iat[i, 0] / inf.loc[i:i+359])
- i = i + 1
- data.append(cost)
- #The payments are summed and the list of tuples is converted to a pandas series
- data = [PCEPI.sum() for PCEPI in data]
- s = pd.concat(data, ignore_index=True)
- #The 20% down payment is added to the series
- s = s + 20 * df['MEDPRICE'] / df['PCEPI']
- #Converts series to DataFrame, attaches the real price of purchasing in cash, and sets date as index.
- dfpt = s.to_frame()
- dfpt['rprice'] = 100* df['MEDPRICE'] / df['PCEPI']
- dfpt['DATE'] = df['DATE']
- dfpt = dfpt.set_index('DATE', drop=True).rename_axis(None)
- dfpt.columns = ['Real Cost', 'Real Price']
- #aesthetics
- ax = dfpt.plot(figsize=(14,7), xticks= [0,117,237,357,417,477,596])
- plt.xticks(rotation=45)
- plt.axvline(x=237, color='green', linestyle=':')
- plt.axvline(x=417, color='green', linestyle='--')
- ax.set_xlim(left = 0, right=597)
- ## End ##
- ## SAVE THE BELOW AS MORTGAGES.CSV##
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement