Advertisement
Guest User

Houses

a guest
Feb 26th, 2021
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 21.25 KB | None | 0 0
  1. import pandas as pd
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4.  
  5. df = pd.read_csv('MORTGAGES.csv')
  6.  
  7. #Amortisation of mortgage payments, assumes 20% down payment by borrower
  8. #Change 360 to 180 to model 15-year mortgages
  9. df['RATE'] = df['RATE'] / 1200
  10. df['PAYMENT'] = (0.8 *df['MEDPRICE'] * df['RATE'])/(1-np.exp(-360*np.log(1+df['RATE'])))
  11.  
  12. #Index=100 at 2012-01-01
  13. inf = df[['PCEPI']].copy()
  14. pay = df[['PAYMENT']].copy()
  15.  
  16. #list-of-tuples
  17. data = []
  18.  
  19. #The payments are deflated and put into a corrosponding tuple
  20. #Change 359 to 179 for 15-year mortgages
  21. i = 0
  22. while (i < 597):
  23.   cost = (100 * pay.iat[i, 0] / inf.loc[i:i+359])
  24.   i = i + 1
  25.   data.append(cost)
  26.  
  27. #The payments are summed and the list of tuples is converted to a pandas series
  28. data = [PCEPI.sum() for PCEPI in data]
  29. s = pd.concat(data, ignore_index=True)
  30.  
  31. #The 20% down payment is added to the series
  32. s = s + 20 * df['MEDPRICE'] / df['PCEPI']
  33.  
  34. #Converts series to DataFrame, attaches the real price of purchasing in cash, and sets date as index.
  35. dfpt = s.to_frame()
  36. dfpt['rprice'] = 100* df['MEDPRICE'] / df['PCEPI']
  37. dfpt['DATE'] = df['DATE']
  38. dfpt = dfpt.set_index('DATE', drop=True).rename_axis(None)
  39. dfpt.columns = ['Real Cost', 'Real Price']
  40.  
  41. #aesthetics
  42. ax = dfpt.plot(figsize=(14,7), xticks= [0,117,237,357,417,477,596])
  43. plt.xticks(rotation=45)
  44. plt.axvline(x=237, color='green', linestyle=':')
  45. plt.axvline(x=417, color='green', linestyle='--')
  46. ax.set_xlim(left = 0, right=597)
  47.  
  48. ## End ##
  49. ## SAVE THE BELOW AS MORTGAGES.CSV##
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement