From cc11b62fac2aea8f1d7be026df346b9909f6f2e1 Mon Sep 17 00:00:00 2001
From: LItvinov Sergey
Date: Sun, 1 Dec 2013 16:38:07 +0100
Subject: [PATCH] Add tests for finance.mac, fix typos.
* doc/info/finance.texi: Fix typos and non-English text, fix an example for npv
* share/finance/finance.mac: Fix typos, isolate `realonly: true' in the block
* share/finance/rtest_finance.mac: add tests
---
doc/info/finance.texi | 44 ++++++++++++++++++++--------------------
share/finance/finance.mac | 38 +++++++++++++++++-----------------
share/finance/rtest_finance.mac | 45 +++++++++++++++++++++++++++++++++++++++++
3 files changed, 85 insertions(+), 42 deletions(-)
create mode 100644 share/finance/rtest_finance.mac
diff --git a/doc/info/finance.texi b/doc/info/finance.texi
index a84d048..4d63ce5 100644
--- a/doc/info/finance.texi
+++ b/doc/info/finance.texi
@@ -10,7 +10,7 @@ This is the Finance Package (Ver 0.1).
In all the functions, @var{rate} is the compound interest
rate, @var{num} is the number of periods and must be
-postivive and @var{flow} refers to cash flow so if you
+positive and @var{flow} refers to cash flow so if you
have an Output the flow is negative and positive
for Inputs.
@@ -107,7 +107,7 @@ Example:
@deffn {Function} annuity_pv (@var{rate},@var{PV},@var{num})
-We can calculate the annuity knowing the present value (like an ammount),
+We can calculate the annuity knowing the present value (like an amount),
it is a constant and periodic payment. @var{rate} is the interest rate,
@var{PV} is the present value and @var{num} is the number of periods.
@@ -149,7 +149,7 @@ Example:
@deffn {Function} geo_annuity_pv (@var{rate},@var{growing_rate},@var{PV},@var{num})
-We can calculate the annuity knowing the present value (like an ammount),
+We can calculate the annuity knowing the present value (like an amount),
in a growing periodic payment. @var{rate} is the interest rate, @var{growing_rate}
is the growing rate, @var{PV} is the present value and @var{num} is the number of periods.
@@ -188,10 +188,10 @@ Example:
@end deffn
-@deffn {Function} amortization (@var{rate},@var{ammount},@var{num})
+@deffn {Function} amortization (@var{rate},@var{amount},@var{num})
-Amortization table determinated by a specific rate.
-@var{rate} is the interest rate, @var{ammount} is the ammount value,
+Amortization table determined by a specific rate.
+@var{rate} is the interest rate, @var{amount} is the amount value,
and @var{num} is the number of periods.
Example:
@@ -222,15 +222,15 @@ Example:
@end deffn
-@deffn {Function} arit_amortization (@var{rate},@var{increment},@var{ammount},@var{num})
+@deffn {Function} arit_amortization (@var{rate},@var{increment},@var{amount},@var{num})
-The amortization table determinated by a specific rate and with growing payment
-can be claculated by @code{arit_amortization}.
+The amortization table determined by a specific rate and with growing payment
+can be calculated by @code{arit_amortization}.
Notice that the payment is not constant, it presents
an arithmetic growing, increment is then the difference between two
consecutive rows in the "Payment" column.
-@var{rate} is the interest rate, @var{increment} is the increment, @var{ammount}
-is the ammount value, and @var{num} is the number of periods.
+@var{rate} is the interest rate, @var{increment} is the increment, @var{amount}
+is the amount value, and @var{num} is the number of periods.
Example:
@@ -260,15 +260,15 @@ Example:
@end deffn
-@deffn {Function} geo_amortization (@var{rate},@var{growing_rate},@var{ammount},@var{num})
+@deffn {Function} geo_amortization (@var{rate},@var{growing_rate},@var{amount},@var{num})
-The amortization table determinated by rate, ammount,
+The amortization table determined by rate, amount,
and number of periods can be found by @code{geo_amortization}.
Notice that the payment is not constant, it presents
a geometric growing, @var{growing_rate} is then the quotient between two
consecutive rows in the "Payment" column.
-@var{rate} is the interest rate, @var{ammount}
-is the ammount value, and @var{num} is the number of periods.
+@var{rate} is the interest rate, @var{amount}
+is the amount value, and @var{num} is the number of periods.
Example:
@@ -298,11 +298,11 @@ Example:
@end deffn
-@deffn {Function} saving (@var{rate},@var{ammount},@var{num})
+@deffn {Function} saving (@var{rate},@var{amount},@var{num})
The table that represents the values in a constant and periodic
saving can be found by @code{saving}.
-@var{ammount} represents the desired quantity and num the number
+@var{amount} represents the desired quantity and num the number
of periods to save.
Example:
@@ -338,16 +338,16 @@ Example:
@deffn {Function} npv (@var{rate},@var{val})
-Calculates de present value of a value series to evaluate the viability in a
+Calculates the present value of a value series to evaluate the viability in a
project.
-@var{flowValues} es una lista con los valores para cada periodo.
+@var{val} is a list of varying cash flows.
Example:
@example
(%i1) load(finance)$
(%i2) npv(0.25,[100,500,323,124,300]);
-(%o2) 714.4703999999999
+(%o2) 893.0880000000001
@end example
@opencatbox
@@ -361,8 +361,8 @@ Example:
IRR (Internal Rate of Return) is the value of rate which makes Net Present Value
zero.
-@var{flowValues} los valores para cada periodo (para periodos mayores a 0) y
-@var{I0} el valor para el periodo cero.
+@var{flowValues} is a list of varying cash flows,
+@var{I0} is the initial investment.
Example:
diff --git a/share/finance/finance.mac b/share/finance/finance.mac
index 4efe91e..afe9ccd 100644
--- a/share/finance/finance.mac
+++ b/share/finance/finance.mac
@@ -34,17 +34,17 @@ http://www.gnu.org/copyleft/gpl.html
load(draw)$
/*
-Returns the amortization table determinated by rate, Ammount,
+Returns the amortization table determinated by rate, Amount,
and number of periods.
*/
-amortization(rate,Ammount,num):=
+amortization(rate,Amount,num):=
block([result,A,payment,k,a],
if num<0 then error("num must be a positive value")
else A:genmatrix(a, num+2, 5),
- payment:Ammount*rate*((1+rate)^(num)/((1+rate)^num-1)),
+ payment:Amount*rate*((1+rate)^(num)/((1+rate)^num-1)),
A[1]:["n", "Balance", "Interest", "Amortization","Payment"],
A[2]:[0,0,0,0,0],
- A[2,2]:Ammount,
+ A[2,2]:Amount,
for k:3 thru num+2 step 1 do (
A[k,5]:payment,
A[k,1]:k-2,
@@ -65,25 +65,25 @@ it is a constant and periodic payment.
annuity_fv(rate,FV,num):=FV*rate/((1+rate)^num-1)$
/*
-Returns the annuity knowing the present value (like an ammount),
+Returns the annuity knowing the present value (like an amount),
it is a constant and periodic payment.
*/
annuity_pv(rate,PV,num):=PV*rate*((1+rate)^(num)/((1+rate)^num-1))$
/*
-Returns the amortization table determinated by rate, Ammount,
+Returns the amortization table determinated by rate, Amount,
and number of periods. The payment is not constant, it presents
an arithmetic growing, increment is then the difference between two
consecutive rows in the "Payment" column.
*/
-arit_amortization(rate,increment,Ammount,num):=
+arit_amortization(rate,increment,Amount,num):=
block([result,A,payment,k,a],
if num<0 then error("num must be a positive value")
else A:genmatrix(a, num+2, 5),
- payment:(rate^2*(rate+1)^num*Ammount+(rate*num-(1+rate)^num+1)*increment)/(rate*(rate+1)^num-rate),
+ payment:(rate^2*(rate+1)^num*Amount+(rate*num-(1+rate)^num+1)*increment)/(rate*(rate+1)^num-rate),
A[1]:["n", "Balance", "Interest", "Amortization","Payment"],
A[2]:[0,0,0,0,0],
- A[2,2]:Ammount,
+ A[2,2]:Amount,
A[3,5]:payment,
for k:4 thru num+2 step 1 do (A[k,5]:A[k-1,5]+increment),
for k:3 thru num+2 step 1 do (
@@ -153,20 +153,20 @@ block([vectors, options],
)$
/*
-Returns the amortization table determinated by rate, Ammount,
+Returns the amortization table determinated by rate, Amount,
and number of periods. The payment is not constant, it presents
ah geometric growing, growinRate is then the cocient between two
consecutive rows in the "Payment" column.
*/
-geo_amortization(rate,growing_rate,Ammount,num):=
+geo_amortization(rate,growing_rate,Amount,num):=
block([result,A,payment,k,a],
if num<0 then error("num must be a positive value")
else
A:genmatrix(a,num+2, 5),
- payment:Ammount*(rate-growing_rate)/(1-(1+growing_rate)^(num)/((1+rate)^num)),
+ payment:Amount*(rate-growing_rate)/(1-(1+growing_rate)^(num)/((1+rate)^num)),
A[1]:["n", "Balance", "Interest", "Amortization","Payment"],
A[2]:[0,0,0,0,0],
- A[2,2]:Ammount,
+ A[2,2]:Amount,
A[3,5]:payment,
for k:4 thru num+2 step 1 do (A[k,5]:A[k-1,5]*(1+growing_rate)),
for k:3 thru num+2 step 1 do (
@@ -188,7 +188,7 @@ it is a growing payment.
geo_annuity_fv(rate,growing_rate,FV,num):=FV*(rate-growing_rate)/((1+rate)^num-(1+growing_rate)^(num))$
/*
-Returns the growing annuity knowing the desired value (like an ammount),
+Returns the growing annuity knowing the desired value (like an amount),
it is a growing payment.
*/
geo_annuity_pv(rate,growing_rate,PV,num):=PV*(rate-growing_rate)/(1-(1+growing_rate)^(num)/((1+rate)^num))$
@@ -198,11 +198,9 @@ IRR (Internal Rate of Return), is the value of rate wich makes Net Present Value
zero.
*/
irr(flowValues,I0):=
-block([r,sum],
+block([r,sum, realonly: true],
sum:npv(r,flowValues)-I0,
- realonly:true,
result:float(algsys([sum=0],[r])),
- realonly:false,
result
)$
@@ -226,14 +224,14 @@ pv(rate,FV,num):=FV/(1+rate)^num$
/*
Returns a table that represent the values in a constant an periodic
-saving. Ammount represent the desired quantity and num the number
+saving. Amount represent the desired quantity and num the number
of periods to save.
*/
-saving(rate,Ammount,num):=
+saving(rate,Amount,num):=
block([result,A,payment,k,a],
if num<0 then error("num must be a positive value")
else A:genmatrix(a,num+2, 4),
- payment:Ammount*rate/((1+rate)^num-1),
+ payment:Amount*rate/((1+rate)^num-1),
A[1]:["n", "Balance", "Interest","Payment"],
A[2]:[0,0,0,0],
A[2,2]:0,
diff --git a/share/finance/rtest_finance.mac b/share/finance/rtest_finance.mac
new file mode 100644
index 0000000..d192c78
--- /dev/null
+++ b/share/finance/rtest_finance.mac
@@ -0,0 +1,45 @@
+/*******************************************************************************
+ *
+ * Examples for the finance
+ *
+ ******************************************************************************/
+
+kill(all);
+done$
+
+(load("finance.mac"),done);
+done$
+
+days360(2008,12,16,2007,3,25);
+-621$
+
+fv(0.12,1000, 3);
+1404.928$
+
+pv(0.12,1000,3);
+711.7802478134108$
+
+annuity_pv(0.12,5000,10);
+884.9208207992201$
+
+
+annuity_fv(0.12,65000,10);
+3703.970670389863$
+
+geo_annuity_pv(0.14,0.05,5000,10);
+802.6888176505123$
+
+geo_annuity_fv(0.14,0.05,5000,10);
+216.5203395312695$
+
+block([l: [100,500,323,124,300],
+ r: 0.25, ref],
+ ref: block([s], (s: 0, for n thru length(l) do s: s + l[n]/(1+r)^(n-1), s)),
+ npv(r, l) - ref);
+0.0$
+
+(res:irr([-5000,0,800,1300,1500,2000],0), rhs(res[1][1]));
+.03009250374237132$
+
+benefit_cost(0.24,[0,300,500,150],[100,320,0,180]);
+1.427249324905784$
--
1.8.1.2