Rename *ll* and *ul* to ll and ul in in-interval
[maxima.git] / share / finance / finance.mac
blob18f730e5deca6e60e1284a10992d376fe69b2774
1 /*               COPYRIGHT NOTICE
3 Copyright (C) 2008 Nicolas Guarin Zapata
5 This program is free software; you can redistribute
6 it and/or modify it under the terms of the
7 GNU General Public License as published by
8 the Free Software Foundation.
10 This program is distributed in the hope that it
11 will be useful, but WITHOUT ANY WARRANTY;
12 without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details at
15 http://www.gnu.org/copyleft/gpl.html
20 ****************************************************
21 *             FINANCE PACKAGE V.0.1                *
22 ****************************************************
23 *This is the Finance Package.                      *
24 *Actually, there are only basic functions, we hope *
25 *you could improve the package with brand new lines*
26 ****************************************************
27 *In all the functions rate is the compound interest*
28 *rate, num is the number of periods and must be    *
29 *postivive & "flow" refers to cash flow so if you  *
30 *have and Output the flow is negative & positive   *
31 *for Inputs.                                       *
32 ****************************************************
34 load(draw)$
37 Returns the amortization table determinated by rate, Amount,
38 and number of periods.
40 amortization(rate,Amount,num):=
41 block([result,A,payment,k,a],
42         if num<0 then error("num must be a positive value")
43         else A:genmatrix(a, num+2, 5),
44         payment:Amount*rate*((1+rate)^(num)/((1+rate)^num-1)),
45         A[1]:["n", "Balance", "Interest", "Amortization","Payment"],
46         A[2]:[0,0,0,0,0],
47         A[2,2]:Amount,
48         for k:3 thru num+2 step 1 do (
49                 A[k,5]:payment,
50                 A[k,1]:k-2,
51                 A[k,3]:A[k-1,2]*rate,
52                 A[k,4]:A[k,5]-A[k,3],
53                 A[k,2]:A[k-1,2]-A[k,4]
54                 ),
55         printf(true,"           "),
56         printf(true,"~7s~14s~13s~16s~14s ~% ~{ ~{ ~12,3f ~} ~% ~}",
57         A[1,1],A[1,2],A[1,3],A[1,4],A[1,5],
58         args(submatrix(1,A)))
62 Returns the annuity knowing the desired value (future value),
63 it is a constant and periodic payment.
65 annuity_fv(rate,FV,num):=FV*rate/((1+rate)^num-1)$
68 Returns the annuity knowing the present value (like an amount),
69 it is a constant and periodic payment.
71 annuity_pv(rate,PV,num):=PV*rate*((1+rate)^(num)/((1+rate)^num-1))$
74 Returns the amortization table determinated by rate, Amount,
75 and number of periods. The payment is not constant, it presents
76 an arithmetic growing, increment is then the difference between two
77 consecutive rows in the "Payment" column.
79 arit_amortization(rate,increment,Amount,num):=
80 block([result,A,payment,k,a],
81         if num<0 then error("num must be a positive value")
82         else A:genmatrix(a, num+2, 5),
83         payment:(rate^2*(rate+1)^num*Amount+(rate*num-(1+rate)^num+1)*increment)/(rate*(rate+1)^num-rate),
84         A[1]:["n", "Balance", "Interest", "Amortization","Payment"],
85         A[2]:[0,0,0,0,0],
86         A[2,2]:Amount,
87         A[3,5]:payment,
88         for k:4 thru num+2 step 1 do (A[k,5]:A[k-1,5]+increment),
89         for k:3 thru num+2 step 1 do (
90                 A[k,1]:k-2,
91                 A[k,3]:A[k-1,2]*rate,
92                 A[k,4]:A[k,5]-A[k,3],
93                 A[k,2]:A[k-1,2]-A[k,4]
94         ),
95         printf(true,"           "),
96         printf(true,"~7s~14s~13s~16s~14s ~% ~{ ~{ ~12,3f ~} ~% ~}",
97         A[1,1],A[1,2],A[1,3],A[1,4],A[1,5],
98         args(submatrix(1,A)))
102 Calculate the ratio Benefit/Cost, Benefit is the Net Present Value (NPV)
103 of the inputs, and Cost is the Net Present Value (NPV) of the outputs.
105 benefit_cost(rate,Input,Output):=
106 block([result,NVIn,NVOut],
107         NVIn:0,NVOut:0,
108         if length(Input)#length(Output)
109         then error("Input & Output must have the same length")
110         else
111                 (NVIn:npv(rate,Input),
112                 NVOut:npv(rate,Output)),
113         result:NVIn/NVOut
118 Calculate the distance between 2 dates, assuming 360 days years,
119 30 days months.
121 days360(year1,month1,day1,year2,month2,day2):=block([years,months,days],
122 if month2>month1 then (years:year2-year1,
123         if day2>day1 then (months:month2-month1,days:day2-day1)
124         else (months:month2-month1-1,days:day2+30-day1)
126 else (years:year2-year1-1,
127         if day2>day1 then (months:month2+12-month1,days:day2-day1)
128         else (months:month2+11-month1, days:day2+30-day1)
129 ),      days:days+years*360+months*30
133 Returns the Future Value of a Present one, the diference in the
134 values is the Interest given by the interest rate.
136 fv(rate,PV,num):=PV*(1+rate)^num$
139 Plot the money flow in a time line, the positive values in blue
140 and upside; the negative ones in red and downside.
141 The direction of the flow is given by the sign of the value.
143 graph_flow(flowValues):=
144 block([vectors, options], 
145         vectors: flatten(makelist([color = if flowValues[k]<0 then 'red else
146         'blue,vector([k-1,0],[0,flowValues[k]])],
147         k,1,length(flowValues))),
148         options: [head_length=0.05,
149                         xaxis=true,
150                         yaxis=false,
151                         xrange=[-1,length(flowValues)+1]],
152         apply(draw2d,append(options,vectors))
156 Returns the amortization table determinated by rate, Amount,
157 and number of periods. The payment is not constant, it presents
158 ah geometric growing, growinRate is then the cocient between two
159 consecutive rows in the "Payment" column.
161 geo_amortization(rate,growing_rate,Amount,num):=
162 block([result,A,payment,k,a],
163         if num<0 then error("num must be a positive value")
164         else
165         A:genmatrix(a,num+2, 5),
166         payment:Amount*(rate-growing_rate)/(1-(1+growing_rate)^(num)/((1+rate)^num)),
167         A[1]:["n", "Balance", "Interest", "Amortization","Payment"],
168         A[2]:[0,0,0,0,0],
169         A[2,2]:Amount,
170         A[3,5]:payment,
171         for k:4 thru num+2 step 1 do (A[k,5]:A[k-1,5]*(1+growing_rate)),
172         for k:3 thru num+2 step 1 do (
173                 A[k,1]:k-2,
174                 A[k,3]:A[k-1,2]*rate,
175                 A[k,4]:A[k,5]-A[k,3],
176                 A[k,2]:A[k-1,2]-A[k,4]
177         ),
178         printf(true,"           "),
179         printf(true,"~7s~14s~13s~16s~14s ~% ~{ ~{ ~12,3f ~} ~% ~}",
180         A[1,1],A[1,2],A[1,3],A[1,4],A[1,5],
181         args(submatrix(1,A)))
185 Returns the growing annuity knowing the desired value (future value),
186 it is a growing payment.
188 geo_annuity_fv(rate,growing_rate,FV,num):=FV*(rate-growing_rate)/((1+rate)^num-(1+growing_rate)^(num))$
191 Returns the growing annuity knowing the desired value (like an amount),
192 it is a growing payment.
194 geo_annuity_pv(rate,growing_rate,PV,num):=PV*(rate-growing_rate)/(1-(1+growing_rate)^(num)/((1+rate)^num))$
197 IRR (Internal Rate of Return), is the value of rate which makes Net Present Value
198 zero.
200 irr(flowValues,I0):=
201 block([r,sum, realonly: true],
202         sum:npv(r,flowValues)-I0,
203         result:float(algsys([sum=0],[r])),
204         result
208 Calculate the present value of a value series to evaluate the viability in a
209 project.
211 npv(rate,flowValues):=
212 block([sum,k],
213    sum:0,
214    for k:1 thru length(flowValues) step 1 do
215    (sum:sum+pv(rate,flowValues[k],k-1)),
216    sum
220 Returns the Present Value of a Future one, the diference in the
221 values is the Interest given by the interest rate.
223 pv(rate,FV,num):=FV/(1+rate)^num$
226 Returns a table that represent the values in a constant an periodic
227 saving. Amount represent the desired quantity and num the number
228 of periods to save.
230 saving(rate,Amount,num):=
231 block([result,A,payment,k,a],
232         if num<0 then error("num must be a positive value")
233         else A:genmatrix(a,num+2, 4),
234         payment:Amount*rate/((1+rate)^num-1),
235         A[1]:["n", "Balance", "Interest","Payment"],
236         A[2]:[0,0,0,0],
237         A[2,2]:0,
238         for k:3 thru num+2 step 1 do(
239                 A[k,4]:payment,
240                 A[k,1]:k-2,
241                 A[k,3]:A[k-1,2]*rate,
242                 A[k,2]:A[k-1,2]+A[k,3]+A[k,4]
243         ),
244         printf(true,"           "),
245         printf(true,"~7s~14s~13s~14s ~% ~{ ~{ ~12,3f ~} ~% ~}",
246         A[1,1],A[1,2],A[1,3],A[1,4],
247         args(submatrix(1,A)))