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