finance2.py

Created by ews31415

Created on March 24, 2022

3.74 KB

(Planned finance update - 2022)


# Finance (2nd Edition)
# 2022, 2020 Edward Shore

from math import *

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

# add sales tax
def taxplus(amt,tax):
  return round(amt*(1+.01*tax),2)

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

# monthly payment,(FV = 0, n, I, PV, P/Y=12)
def mopmt(yrs,rate,loan):
  pymt=loan/uspv(yrs*12,rate/12)
  return pymt

# sinking fund,(PV=0, n, I, PMT, P/Y=12)
def sinkfund(yrs,rate,pymt):
  sink=pymt*usfv(yrs*12,rate/12)
  return sink

# piti: principal, interest, taxes, and insurance
def piti(yrs,rate,loan,tax,insur):
  pymt=loan/uspv(yrs*12,rate/12)+(tax+insur)/12
  return pymt

# qualiying 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 qual

# date number
def datenum(m,d,y):
  if m>2:
    x=int(0.4*m+2.3)
    z=y
  else:
    x=0
    z=y-1
  return 365*y+31*(m-1)+d+int(z/4)-x

# days between dates - actual
def days(m1,d1,y1,m2,d2,y2):
  return datenum(m2,d2,y2)-datenum(m1,d1,y1)

# simple interest
def simpint(p,r,t):
 return p*r/100*t

# total: simple interest
def simptot(p,r,t):
 return p+simpint(p,r,t)

# principal: simple interest
def simpprin(i,r,t):
 return i/(r/100*t)

# Cost-Sell-Margin
# cost 
def cost(sell,margin):
 return sell*(1-margin/100)

# sell
def sell(cost,margin):
 return cost/(1-margin/100)

# margin
def margin(cost,sell):
 return (1-cost/sell)*100

# PV of a single cash stream
def sppv(n,i,fv):
  return fv*(1+i/100)**(-n)

# FV of a single cash stream
def spfv(n,i,pv):
  return pv*(1+i/100)**n

# n of a single cash stream
def spn(i,pv,fv):
  return log(fv/pv)/log(1+i/100)

# i of a single cash stream
def spi(n,pv,fv):
  return ((fv/pv)**(1/n)-1)*100

# net present value - constant period
def npv(flows,rate):
  t=0
  for k in range(len(flows)):
    f=flows[k]
    t+=sppv(k,rate,f)
  return t

# net present value - different periods (days)
def xnpv(flows,dates,rate):
  t=0
  for k in range(len(flows)):
    d=dates[k]/365
    f=flows[k]
    t+=sppv(d,rate,f)
  return t

# internal rate of return - constant period
def irr(flows):
  it=1
  w0=10
  w1=1
  while abs(w1)>1e-12:
    f0=npv(flows,w0)
    f1=npv(flows,w0+.01)
    d=(f1-f0)/.01
    w1=f0/d
    w0-=w1
    it+=1
    if it==201:
      print('Iterations exceeded')
      break
  return w0

# internal rate of return - different periods (days)
def xirr(flows,dates):
  it=1
  w0=10
  w1=1
  while abs(w1)>1e-12:
    f0=xnpv(flows,dates,w0)
    f1=xnpv(flows,dates,w0+.01)
    d=(f1-f0)/.01
    w1=f0/d
    w0-=w1
    it+=1
    if it==201:
      print('Iterations exceeded')
      break
  return w0

# percent of total of a list of values
def ptot(flows):
  s=sum(flows)
  for k in range(len(flows)):
    flows[k]/=s
  return flows

# proroate a value over a list of values
def prorate(flows,amt):
  s=sum(flows)
  for k in range(len(flows)):
    flows[k]/=s
    flows[k]*=amt
  return flows
 
# time value of money - cash flow convetions are followed
# end mode implied, periodic
# payment
def payment(n,i,pv,fv):
  return (-pv-sppv(n,i,fv))/uspv(n,i)

# present value
def present(n,i,pymt,fv):
  return -(pymt*uspv(n,i)+sppv(n,i,fv))

# future value
def future(n,i,pv,pymt):
  return (-pv-pymt*uspv(n,i))*(1+.01*i)**n

# interest
def interest(n,pv,pymt,fv):
  it=1
  w0=10
  w1=1
  while abs(w1)>1e-12:
    f0=pymt*uspv(n,w0)+sppv(n,w0,fv)+pv
    f1=pymt*uspv(n,w0+.01)+sppv(n,w0+.01,fv)+pv
    d=(f1-f0)/.01
    w1=f0/d
    w0-=w1
    it+=1
    if it==201:
      print("Iterations Exceeded")
      break
  return w0

# number of payments
def npmts(i,pv,pymt,fv):
  b=fv-pymt/(.01*i)
  a=-pv-pymt/(.01*i)
  return log(b/a)/log(1+.01*i)

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our <a href="https://www.numworks.com/legal/cookies-policy/">cookies policy</a>.