home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fb.zip / octave / SCRIPTS.ZIP / scripts.fat / finance / npv.m < prev    next >
Text File  |  1999-12-24  |  2KB  |  71 lines

  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.  
  17. ## usage:  npv (r, p [, i])
  18. ##
  19. ## Returns the net present value of a series of irregular (i.e., not
  20. ## necessarily identical) payments p which occur at the ends of n
  21. ## consecutive periods.  r specifies the one-period interest rates and
  22. ## can either be a scalar (constant rates) or a vector of the same
  23. ## length as p.
  24. ##
  25. ## With the optional scalar argument i, one can specify an initial
  26. ## investment.
  27. ##
  28. ## Note that rates are not specified in percent, i.e., one has to write
  29. ## 0.05 rather than 5 %.
  30. ##
  31. ## See also:  irr;  pv.
  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_vec (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_scal (r)
  53.     d = 1 ./ (1 + r) .^ (0 : n);
  54.   elseif (is_vec (r) && (length (r) == n))
  55.     d = [1, (1 ./ cumprod (reshape (1 + r, 1, n)))];
  56.   else
  57.     error ("npv: r must be a scalar or a vector of the same length as p");
  58.   endif
  59.  
  60.   if (nargin == 3)
  61.     if !is_scal (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.   
  71. endfunction