Skip to content

Instantly share code, notes, and snippets.

@fsnlarson
Last active September 20, 2018 21:47
Show Gist options
  • Save fsnlarson/f3510b0d391fde2693807aeddb52b634 to your computer and use it in GitHub Desktop.
Save fsnlarson/f3510b0d391fde2693807aeddb52b634 to your computer and use it in GitHub Desktop.
import csv
import sys
import json
import numpy as np
def money(n):
try:
return "${:,.2f}".format(n)
except Exception as e:
return n
def main():
college_tuition = 300000
number_years = 1
total_college_cost = college_tuition * number_years
college_outflows = [-college_tuition for
x in range(number_years)]
cost_of_capital = .075
project_missing_earnings = False
npvs = []
with open('hamilton_project_data.csv', 'rb') as f:
reader= csv.DictReader(f)
highschool_str = 'High School Degree or GED'
non_majors = [highschool_str, 'Year']
majors = [field for field in reader.fieldnames
if field not in non_majors]
rows = list(reader)
years = len(rows)
monthly_loan_payment = -np.pmt(cost_of_capital/12,
12*years, total_college_cost)
for major in majors:
earnings_inflows = []
earnings_inflows_after_debt = []
last_earnings_index = 0
for i, row in enumerate(rows):
high_school = float(row[highschool_str])
major_earnings_raw = row.get(major)
if not major_earnings_raw:
# incomplete data for some majors/years
if last_earnings_index == 0:
last_earnings_index = i - 1
if project_missing_earnings:
major_earnings_raw = rows[last_earnings_index].get(major)
else:
major_earnings_raw = '0'
major_earnings = float(major_earnings_raw)
earnings_difference = major_earnings - high_school
earnings_inflows.append(earnings_difference)
earnings_difference_after_debt = earnings_difference - (monthly_loan_payment * 12)
earnings_inflows_after_debt.append(earnings_difference_after_debt)
total_cashflows = college_outflows + earnings_inflows
npv = np.npv(cost_of_capital, total_cashflows)
irr = np.irr(total_cashflows)
result = {
'major': major,
'npv': npv,
'earnings_differences': earnings_inflows,
'earnings_differences_after_debt': earnings_inflows_after_debt,
}
npvs.append(result)
npvs = sorted(npvs, key=lambda k: k['npv'])
writer = csv.writer(sys.stdout)
print 'All NPVs'
print '==========================='
writer.writerow(['Major', 'NPV'])
for row in npvs:
writer.writerow([row['major'][:30], money(row['npv'])])
print '\n\n\n'
print 'NPVs with No Worse Off'
print '==========================='
npvs_plus_plus = [x for x in npvs if not
any([y < 0 for y in x['earnings_differences_after_debt']])
and x['npv'] > 0
]
writer.writerow(['Major', 'NPV'])
for row in npvs_plus_plus:
writer.writerow([row['major'][:30], money(row['npv'])])
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment