3191
|
1 ## Copyright (C) 1995, 1996, 1997 Kurt Hornik |
|
2 ## |
|
3 ## This program is free software; you can redistribute it and/or modify |
|
4 ## it under the terms of the GNU General Public License as published by |
|
5 ## the Free Software Foundation; either version 2, or (at your option) |
|
6 ## any later version. |
|
7 ## |
|
8 ## This program is distributed in the hope that it will be useful, but |
|
9 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
11 ## General Public License for more details. |
|
12 ## |
|
13 ## You should have received a copy of the GNU General Public License |
|
14 ## along with this file. If not, write to the Free Software Foundation, |
|
15 ## 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
16 |
3407
|
17 ## -*- texinfo -*- |
|
18 ## @deftypefn {Function File} {} npv (@var{r}, @var{p} [, @var{i}]) |
3191
|
19 ## Returns the net present value of a series of irregular (i.e., not |
3407
|
20 ## necessarily identical) payments @var{p} which occur at the ends of @var{n} |
|
21 ## consecutive periods. @var{r} specifies the one-period interest rates and |
3191
|
22 ## can either be a scalar (constant rates) or a vector of the same |
3407
|
23 ## length as @var{p}. |
3191
|
24 ## |
3407
|
25 ## With the optional scalar argument @var{i}, one can specify an initial |
3191
|
26 ## investment. |
|
27 ## |
|
28 ## Note that rates are not specified in percent, i.e., one has to write |
|
29 ## 0.05 rather than 5 %. |
3407
|
30 ## @end deftypefn |
|
31 ## @seealso{irr, pv} |
3191
|
32 |
|
33 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
34 ## Description: Net present value of a series of payments |
|
35 |
|
36 function v = npv (r, p, i) |
|
37 |
|
38 if ((nargin < 2) || (nargin > 3)) |
|
39 usage ("npv (r, p [, i]"); |
|
40 endif |
|
41 |
|
42 if !(is_vector (p)) |
|
43 error ("npv: p has to be a vector"); |
|
44 else |
|
45 n = length (p); |
|
46 p = reshape (p, 1, n); |
|
47 endif |
|
48 |
|
49 if any (any (r <= -1)) |
|
50 error ("npv: all interest rates must be > -1"); |
|
51 endif |
|
52 if is_scalar (r) |
|
53 d = 1 ./ (1 + r) .^ (0 : n); |
|
54 elseif (is_vector (r) && (length (r) == n)) |
3388
|
55 d = [1, (1 ./ cumprod (reshape (1 + r, 1, n)))]; |
3191
|
56 else |
3388
|
57 error ("npv: r must be a scalar or a vector of the same length as p"); |
3191
|
58 endif |
|
59 |
|
60 if (nargin == 3) |
|
61 if !is_scalar (i) |
|
62 error ("npv: I_0 must be a scalar"); |
|
63 endif |
|
64 else |
|
65 i = 0; |
|
66 endif |
|
67 |
|
68 p = [i, p]; |
|
69 v = sum (d .* p); |
|
70 |
3407
|
71 endfunction |