Sunday, May 17, 2020

Numworks/Casio MicroPython/Python: Financial Functions

Numworks/Casio MicroPython/Python:  Financial Functions 

My first python script for the Numworks calculator:  finance.py

Great calculator and glad I finally have one. 

Introduction

The following scripts creates the user functions for the following financial functions:

pchg(old, new):  Returns the percent change between two numbers
Example:  pchg(1400,2600) returns 85.71

taxlplus(amt, tax): Adds the tax rate to an amount. amt + tax%.  Results are rounded up to 2 decimal places.
Example:  taxplus(59,9.5) returns 64.6

uspv(n,i):  Takes n (number of payments) and i (periodic interest rate) and calculates the uniform present value factor.  PV = PMT * USPV.  Future value is assumed to be 0.
Example:  n = 36 payments, i = 0.25%.  uspv(36,0.25) returns 34.39

usfv(n,i):  Takes n (number of payments) and i (periodic interest rate) and calculates the uniform future value factor.  FV = PMT * USFV.  Present value is assumed to be 0.
Example:  n = 36 payments, i = 0.25%.  usfv(36,0.25) returns 37.62

mopmt(yrs,rate,loan):  Calculates the monthly payment of a loan with monthly payments.  Payments are assumed to be due at the end of each month.   Results are rounded to up to 2 decimal places. 
Example:  Loan of $238,000 for 30 years at 4.28% annual rate.  mompt(30,4.28,238000) returns 1175.0

annrate(ppy,cpy,rate):  Calculates the equivalent annual rate given ppy (payments per year), cpy (compounding payments per year), rate (estimated periodic rate).
Example:  ppy = 12, cpy = 2, periodic rate = 0.74%.   annrate(12,2,0.74) returns 9.0459

Source for annrate:
Roger F. Farish and The Staff of the Texas Instruments Learning Center.  Calculator Analysis for Business and Finance.  Texas Instruments, Inc.  1977.  ISBN 0-89512-015-1

sinkfund(yrs,rate,pymt):  Calculates the balance of a sink fund (savings account)  of monthly deposits.   Number of years and the annual interest rate are needed.  Deposits are assumed to be due at the end of each month.   Results are rounded to up to 2 decimal places. 
Example:  Monthly deposits of $400.00 for 5 years, at a rate of 2.9%.  sinkfund(5,2.9,400) returns 25793.77

piti(yrs,rate,loan,tax,insur):   Calculates the monthly payment of a mortgage given:  the term of the loan in years (yrs), the annual interest rate (rate), the mortgage (loan), the annual property tax (tax), and the annual property insurance (insur).  Payments are assumed to be due at the end of each month.  Results are rounded to up to 2 decimal places.
Example:  piti(30,3.3,160000,1349,240) returns 833.15

qualinc(inc,debt,taxins,rate,yrs):  Calculates the qualifying amount given the following parameters:  monthly income (inc), monthly debt payments (debt), monthly proper taxes and insurance (taxins), interest rate (rate), and term of the mortgage in years (yrs).   Mortgage payments are assumed to be due at the end of each month.   The standard debt:income ratio of 28:36 is used.  Results are rounded to up to 2 decimal places.
Example:  qualinc(4485,375,126.83,5.30) returns 207288.6


Python Script: finance.py

# 2020-04-12 EWS

from math import *

# percent change
def pchg(old,new):
  pch=(new-old)/old*100
  return pch

# add sales tax  
def taxplus(amt,tax):
  total=amt*(1+0.01*tax)
  return round(total,2)

# uniform pv factor pv=pmt*uspv
def uspv(n,i):
  factor=(1-(1+0.01*i)**(-n))/(0.01*i)
  return factor

# uniform fv factor fv=pmt*usfv
def usfv(n,i):
  factor=((1+0.01*i)**n-1)/(0.01*i)
  return factor
  
# monthly payment
def mopmt(yrs,rate,loan):
  pymt=loan/uspv(yrs*12,rate/12)
  return round(pymt,2)
  
# equivalent annual rate
def annrate(ppy,cpy,rate):
  irate=cpy*100*((1+0.01*rate)**(ppy/cpy)-1)
  return irate
  
# sinking fund
def sinkfund(yrs,rate,pymt):
  sink=pymt*usfv(yrs*12,rate/12)
  return round(sink,2)

# piti
def piti(yrs,rate,loan,tax,insur):
  pymt=loan/uspv(yrs*12,rate/12)+(tax+insur)/12
  return round(pymt,2)

# qualifying income 28:36 ratio
def qualinc(inc,debt,taxins,rate,yrs):
  a=min(inc*0.36-debt,inc*0.28)-taxins
  qual=a*uspv(yrs*12,rate/12)
  return round(qual,2)

Eddie

All original content copyright, © 2011-2020.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.