Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
8d8807a3f1 | |||
14e784aa6c | |||
2f5a76a92a | |||
a1cdd69c42 |
2
Makefile
2
Makefile
@@ -3,5 +3,5 @@ reference:
|
||||
amortize -P 100000 -r 0.05 -f monthly -n 36 -s > ref.txt
|
||||
|
||||
test:
|
||||
python amort.py -p 100000 -i 5.0 -t 3 -ot "{\"payment_number\":13,\"amount\":5000}"
|
||||
python src/amort.py -p 100000 -i 5.0 -t 3 -ot "{\"payment-number\":13,\"amount\":5000}"
|
||||
|
||||
|
45
README.md
Normal file
45
README.md
Normal file
@@ -0,0 +1,45 @@
|
||||
|
||||
# amort
|
||||
|
||||
Generates an amortization schedule based on the principal, interest rate, term, and any additional payments towards principal.
|
||||
|
||||
## requirements
|
||||
|
||||
Only imports `argparse` and `json` for the main block of the script.
|
||||
|
||||
* saves results to a file `schedule.csv`
|
||||
* does not access the web or internet
|
||||
|
||||
## usage
|
||||
|
||||
Invoke `make test` to generate a schedule based on loan of 100,000 over a 3 year term given an interest rate of 5.0% with a one-time payment towards principal of 5000 at the 13th month.
|
||||
|
||||
### extra payments
|
||||
|
||||
For extra payments towards principal, invoking the script will look like:
|
||||
|
||||
python src/amort.py -p {principal} -i {interest rate} -t {years} --extra-payments extra_payments.json
|
||||
|
||||
A valid json file contains a list named `extra-payments`. Each element is an object with the attributes `payment-number` and `amount`.
|
||||
|
||||
{
|
||||
"extra-payments" : [
|
||||
{
|
||||
"payment-number": 1,
|
||||
"amount": 500
|
||||
}, {
|
||||
"payment-number": 2,
|
||||
"amount": 500
|
||||
}, {
|
||||
"payment-number": 3,
|
||||
"amount": 500
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
* `payment-number` is the month when the payment has been made.
|
||||
* `amount` is self-explanatory
|
||||
|
||||
## misc
|
||||
|
||||
This originated from me playing with ChatGPT. I asked it a question and it blurted out a semi-functional script as an answer. Then I made this repo to track the errors I fixed.
|
132
amort.py
132
amort.py
@@ -1,132 +0,0 @@
|
||||
|
||||
import argparse, json
|
||||
|
||||
def generate_amortization_schedule(principal, interest_rate, loan_term, extra_payments=[]):
|
||||
# Convert interest rate to decimal and calculate periodic interest rate
|
||||
monthly_interest_rate = interest_rate / 12 / 100
|
||||
|
||||
# Calculate the number of monthly payments
|
||||
num_payments = loan_term * 12
|
||||
|
||||
# Calculate the fixed monthly payment using the amortization formula
|
||||
monthly_payment = (principal * monthly_interest_rate) / (1 - (1 + monthly_interest_rate) ** -num_payments)
|
||||
monthly_payment = round(monthly_payment, 2)
|
||||
|
||||
# Initialize variables
|
||||
remaining_balance = principal
|
||||
amortization_schedule = []
|
||||
|
||||
one_time_payment = None
|
||||
if extra_payments != []:
|
||||
one_time_payment = extra_payments.pop(0)
|
||||
for payment_number in range(1, num_payments + 1):
|
||||
# Calculate interest for the current period
|
||||
if remaining_balance == 0:
|
||||
break
|
||||
interest_payment = round(remaining_balance * monthly_interest_rate, 2)
|
||||
|
||||
# Calculate principal payment
|
||||
principal_payment = round(monthly_payment - interest_payment, 2)
|
||||
|
||||
# Apply one-time payment if provided
|
||||
if one_time_payment and payment_number == one_time_payment['payment_number']:
|
||||
principal_payment += one_time_payment['amount']
|
||||
if extra_payments != []:
|
||||
one_time_payment = extra_payments.pop(0)
|
||||
# Update remaining balance
|
||||
remaining_balance -= principal_payment
|
||||
|
||||
if remaining_balance < 0:
|
||||
principal_payment = principal_payment + remaining_balance
|
||||
monthly_payment = monthly_payment + remaining_balance
|
||||
remaining_balance = 0
|
||||
|
||||
# Create a dictionary with payment details and add it to the amortization schedule
|
||||
payment_details = {
|
||||
'Payment Number': payment_number,
|
||||
'Payment Amount': round(monthly_payment,2),
|
||||
'Interest Payment': interest_payment,
|
||||
'Principal Payment': round(principal_payment,2),
|
||||
'Remaining Balance': round(remaining_balance,2)
|
||||
}
|
||||
amortization_schedule.append(payment_details)
|
||||
return amortization_schedule
|
||||
|
||||
def get_totals(amortization_schedule, func=None):
|
||||
total_paid = 0
|
||||
total_interest_paid = 0
|
||||
total_principal_paid = 0
|
||||
# Display the amortization schedule
|
||||
messages = []
|
||||
for payment in amortization_schedule:
|
||||
# print(payment)
|
||||
total_paid += payment["Payment Amount"]
|
||||
total_interest_paid += payment["Interest Payment"]
|
||||
total_principal_paid += payment["Principal Payment"]
|
||||
if payment["Remaining Balance"] < 0:
|
||||
break
|
||||
attrs = [payment[key] for key in payment]
|
||||
if func is not None:
|
||||
messages.append("%s" % ", ".join([str(attr) for attr in attrs]))
|
||||
if func is not None:
|
||||
func(messages)
|
||||
return total_paid, total_interest_paid, total_principal_paid
|
||||
|
||||
if __name__ == "__main__":
|
||||
def get_arguments():
|
||||
p = argparse.ArgumentParser()
|
||||
p. add_argument("--principal", "-p", type=float, \
|
||||
help="set value for principal")
|
||||
p.add_argument("---interest-rate", "-i", type=float,\
|
||||
help="set the value for interest rate (percentage)")
|
||||
p.add_argument("--term", "-t", type=int,\
|
||||
help="sets the term (years)")
|
||||
p.add_argument("--one-time", "-ot", type=str,\
|
||||
help="factors in a one-time payment (json, example: {\"payment_number\":13,\"amount\":5000}")
|
||||
p.add_argument("--extra-payments", "-ep", type=str,\
|
||||
help="facts in multiple one time payments (json file name)")
|
||||
args = p.parse_args()
|
||||
l = []
|
||||
if args.extra_payments is not None:
|
||||
with open(args.extra_payments) as f:
|
||||
l = json.loads(f.read())
|
||||
# print(extra["extra-payments"])
|
||||
if args.one_time is not None:
|
||||
l.append(json.loads(args.one_time))
|
||||
extra = l["extra-payments"]
|
||||
extra.sort(key=lambda k: k["payment_number"])
|
||||
return args.principal, args.interest_rate, args.term, extra
|
||||
|
||||
def compare(with_extra_payments, without):
|
||||
x,y,z = get_totals(with_extra_payments)
|
||||
a,b,c = get_totals(without)
|
||||
print(chr(916), "paid: ", round(x-a,2))
|
||||
print(chr(916), "interest paid: ", round(y-b,2))
|
||||
print(chr(916), "principal paid: ", round(abs(z-c),2))
|
||||
|
||||
def display(table):
|
||||
print("id, paid, interest payment, principal payment, remaining")
|
||||
for row in table:
|
||||
print(row)
|
||||
|
||||
def export(table, filename="schedule.csv"):
|
||||
with open(filename, 'w') as f:
|
||||
print("id, paid, interest payment, principal payment, remaining", file=f)
|
||||
for row in table:
|
||||
print(row, file=f)
|
||||
print("wrote to file", filename)
|
||||
|
||||
principal, interest_rate, loan_term, extra_payments = get_arguments()
|
||||
|
||||
schedule = generate_amortization_schedule(principal, interest_rate, loan_term, extra_payments)
|
||||
paid, interest_paid, principal_paid = get_totals(schedule,export)
|
||||
|
||||
print("total paid: ", round(paid,2))
|
||||
print("total interest paid: ", round(interest_paid,2))
|
||||
print("total principal paid: ", round(principal_paid,2))
|
||||
|
||||
# without extra payments for comparison
|
||||
compare(schedule, generate_amortization_schedule(
|
||||
principal, interest_rate, loan_term)
|
||||
)
|
||||
|
183
src/amort.py
Normal file
183
src/amort.py
Normal file
@@ -0,0 +1,183 @@
|
||||
|
||||
def generate_amortization_schedule(principal, interest_rate, loan_term, events=[]):
|
||||
# Convert interest rate to decimal and calculate periodic interest rate
|
||||
monthly_interest_rate = interest_rate / 12 / 100
|
||||
|
||||
# Calculate the number of monthly payments
|
||||
num_payments = loan_term * 12
|
||||
|
||||
# Calculate the fixed monthly payment using the amortization formula
|
||||
monthly_payment = (principal * monthly_interest_rate) / (1 - (1 + monthly_interest_rate) ** -num_payments)
|
||||
monthly_payment = round(monthly_payment, 2)
|
||||
|
||||
# Initialize variables
|
||||
remaining_balance = principal
|
||||
amortization_schedule = []
|
||||
|
||||
one_time_payment = None
|
||||
if events != []:
|
||||
one_time_payment = events["extra-payments"].pop(0)
|
||||
|
||||
for payment_number in range(1, num_payments + 1):
|
||||
# Calculate interest for the current period
|
||||
if remaining_balance == 0:
|
||||
break
|
||||
interest_payment = round(remaining_balance * monthly_interest_rate, 2)
|
||||
|
||||
# Calculate principal payment
|
||||
principal_payment = round(monthly_payment - interest_payment, 2)
|
||||
|
||||
# Apply one-time payment if provided
|
||||
if one_time_payment and payment_number == one_time_payment['payment-number']:
|
||||
principal_payment += one_time_payment['amount']
|
||||
if events["extra-payments"] != []:
|
||||
one_time_payment = events["extra-payments"].pop(0)
|
||||
# Update remaining balance
|
||||
remaining_balance -= principal_payment
|
||||
|
||||
if remaining_balance < 0:
|
||||
principal_payment = principal_payment + remaining_balance
|
||||
monthly_payment = monthly_payment + remaining_balance
|
||||
remaining_balance = 0
|
||||
|
||||
# Create a dictionary with payment details and add it to the amortization schedule
|
||||
payment_details = {
|
||||
'Payment Number': payment_number,
|
||||
'Payment Amount': round(monthly_payment,2),
|
||||
'Interest Payment': interest_payment,
|
||||
'Principal Payment': round(principal_payment,2),
|
||||
'Remaining Balance': round(remaining_balance,2)
|
||||
}
|
||||
amortization_schedule.append(payment_details)
|
||||
return amortization_schedule
|
||||
|
||||
def get_totals(amortization_schedule, func=None):
|
||||
total_paid = 0
|
||||
total_interest_paid = 0
|
||||
total_principal_paid = 0
|
||||
# Display the amortization schedule
|
||||
messages = []
|
||||
for payment in amortization_schedule:
|
||||
# total_paid += payment["Payment Amount"]
|
||||
total_interest_paid += payment["Interest Payment"]
|
||||
total_principal_paid += payment["Principal Payment"]
|
||||
if payment["Remaining Balance"] < 0:
|
||||
break
|
||||
attrs = [payment[key] for key in payment]
|
||||
if func is not None:
|
||||
messages.append("%s" % ", ".join([str(attr) for attr in attrs]))
|
||||
if func is not None:
|
||||
func(messages)
|
||||
return ((total_interest_paid + total_principal_paid),
|
||||
total_interest_paid,
|
||||
total_principal_paid)
|
||||
|
||||
def compare(with_extra_payments, without):
|
||||
x,y,z = get_totals(with_extra_payments)
|
||||
a,b,c = get_totals(without)
|
||||
term_length = len(without) - len(with_extra_payments)
|
||||
print(chr(916), "paid: ", round(x-a,2))
|
||||
print(chr(916), "interest paid: ", round(y-b,2))
|
||||
print(chr(916), "principal paid: ", round(abs(z-c),2))
|
||||
print(chr(916), "term (months): ", term_length)
|
||||
|
||||
def whatif(events, x = 500, terms = 120):
|
||||
"""
|
||||
What if I paid an extra x dollars each term.
|
||||
Parameters:
|
||||
x = amount to pay
|
||||
terms = total expected terms
|
||||
"""
|
||||
key_list = "extra-payments"
|
||||
key_attr1 = "payment-number"
|
||||
key_attr2 = "amount"
|
||||
# get the last payment number from events
|
||||
latest = events[key_list][-1][key_attr1]
|
||||
start = latest + 1
|
||||
for i in range(start, 120):
|
||||
new_event = dict()
|
||||
new_event[key_attr1] = i
|
||||
new_event[key_attr2] = x
|
||||
events[key_list].append(new_event)
|
||||
return events, start
|
||||
|
||||
def display(table):
|
||||
print("id, due, interest payment, principal payment, remaining")
|
||||
for row in table:
|
||||
print(row)
|
||||
|
||||
def export(table, filename="schedule.csv"):
|
||||
with open(filename, 'w') as f:
|
||||
print("id, due, interest payment, principal payment, remaining", file=f)
|
||||
for row in table:
|
||||
print(row, file=f)
|
||||
print("wrote to file", filename)
|
||||
|
||||
if __name__ == "__main__":
|
||||
import argparse, json, copy
|
||||
|
||||
def get_arguments():
|
||||
p = argparse.ArgumentParser()
|
||||
p. add_argument("--principal", "-p", type=float, \
|
||||
help="set value for principal")
|
||||
p.add_argument("---interest-rate", "-i", type=float,\
|
||||
help="set the value for interest rate (percentage)")
|
||||
p.add_argument("--term", "-t", type=int,\
|
||||
help="sets the term (years)")
|
||||
p.add_argument("--one-time", "-ot", type=str,\
|
||||
help="a one-time payment (json, example: {\"payment-number\":13,\"amount\":5000}")
|
||||
p.add_argument("--events", "-e", type=str,\
|
||||
help="name of .json file containing events such as one-time payments or interest rate changes")
|
||||
args = p.parse_args()
|
||||
l = []
|
||||
if args.events is not None:
|
||||
with open(args.events) as f:
|
||||
l = json.loads(f.read())
|
||||
if args.one_time is not None:
|
||||
l["extra-payments"].append(json.loads(args.one_time))
|
||||
return args.principal, args.interest_rate, args.term, l
|
||||
|
||||
def main():
|
||||
principal, interest_rate, loan_term, events = get_arguments()
|
||||
|
||||
wi_start = 0
|
||||
wi_x = 500
|
||||
if events:
|
||||
# the amount paid ahead canonically
|
||||
ahead_paid = sum(pay["amount"] for pay in events["extra-payments"])
|
||||
ahead_msg = f"total extra payments: {ahead_paid}"
|
||||
border = "~" * len(ahead_msg)
|
||||
print(border)
|
||||
print(ahead_msg)
|
||||
print(border)
|
||||
events, wi_start = whatif(events, x=wi_x)
|
||||
# generate the schedule
|
||||
schedule = generate_amortization_schedule(
|
||||
principal, interest_rate, loan_term, copy.deepcopy(events)
|
||||
)
|
||||
paid, interest_paid, principal_paid = get_totals(schedule,export)
|
||||
# report on schedule
|
||||
print("total paid: ", round(paid,2))
|
||||
print("total interest paid: ", round(interest_paid,2))
|
||||
print("total principal paid: ", round(principal_paid,2))
|
||||
# get the last term of the schedule
|
||||
if events:
|
||||
wi_end = schedule[-1]["Payment Number"]
|
||||
wi_paid = sum(
|
||||
pay["amount"] for pay in \
|
||||
events["extra-payments"][wi_start:wi_end]
|
||||
)
|
||||
wi_msg = f"total if {wi_x} was also paid each term: {wi_paid}"
|
||||
border = '~' * len(wi_msg)
|
||||
print(border)
|
||||
print(wi_msg)
|
||||
print(border)
|
||||
# without extra payments for comparison
|
||||
compare(schedule, generate_amortization_schedule(
|
||||
principal, interest_rate, loan_term)
|
||||
)
|
||||
print()
|
||||
# with open("extra_payments.json") as f:
|
||||
# e = json.load(f)
|
||||
# print(whatif(e))
|
||||
main()
|
Reference in New Issue
Block a user