]> Creatis software - CreaPhase.git/blob - octave_packages/financial-0.4.0/yeardays.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / financial-0.4.0 / yeardays.m
1 ## Copyright (C) 2008 Bill Denney <bill@denney.ws>
2 ##
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
6 ## version.
7 ##
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 ## details.
12 ##
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn {Function File} {@var{d} =} yeardays (@var{y})
18 ## @deftypefnx {Function File} {@var{d} =} yeardays (@var{y}, @var{b})
19 ## Return the number of days in the year @var{y} with an optional basis
20 ## @var{b}.
21 ##
22 ## Valid bases
23 ## @itemize @bullet
24 ## @item 0
25 ##   actual/actual (default)
26 ## @item 1
27 ##   30/360 (SIA)
28 ## @item 2
29 ##   actual/360
30 ## @item 3
31 ##   actual/365
32 ## @item 4
33 ##   30/360 (PSA)
34 ## @item 5
35 ##   30/360 (IDSA)
36 ## @item 6
37 ##   30/360 (European)
38 ## @item 7
39 ##   actual/365 (Japanese)
40 ## @item 8
41 ##   actual/actual (ISMA)
42 ## @item 9
43 ##   actual/360 (ISMA)
44 ## @item 10
45 ##   actual/365 (ISMA)
46 ## @item 11
47 ##   30/360E (ISMA)
48 ## @end itemize
49 ## @seealso{days365, days360, daysact, daysdif}
50 ## @end deftypefn
51
52 function d = yeardays (y, basis)
53
54   if (nargin == 1)
55         basis = 0;
56   elseif (nargin != 2)
57     print_usage ();
58   endif
59
60   if isscalar (y)
61         d = zeros (size (basis));
62   elseif isscalar (basis)
63         ## the rest of the code is much simpler if you can be sure that
64         ## basis is a matrix if y is a matrix
65         basis = basis * ones (size (y));
66         d = zeros (size (y));
67   else
68         if ndims (y) == ndims (basis)
69           if ~ all (size (y) == size (basis))
70                 error ("year and basis must be the same size or one must be a scalar");
71           else
72                 d = zeros (size (y));
73           endif
74         else
75           error ("year and basis must be the same size or one must be a scalar.")
76         endif
77   endif
78
79   bact = ismember (basis(:), [0 8]);
80   b360 = ismember (basis(:), [1 2 4 5 6 9 11]);
81   b365 = ismember (basis(:), [3 7 10]);
82
83   badbasismask = ~ (bact | b360 | b365);
84   if any (badbasismask)
85         badbasis = unique (basis(badbasismask));
86         error ("Unsupported basis: %g\n", badbasis)
87   endif
88
89   d(bact) = 365 + (eomday(y(bact), 2) == 29);
90   d(b360) = 360;
91   d(b365) = 365;
92
93 endfunction
94
95 ## Tests
96 %!assert(yeardays(2000), 366)
97 %!assert(yeardays(2001), 365)
98 %!assert(yeardays(2000:2004), [366 365 365 365 366])
99 %!assert(yeardays(2000, 0), 366)
100 %!assert(yeardays(2000, 1), 360)
101 %!assert(yeardays(2000, 2), 360)
102 %!assert(yeardays(2000, 3), 365)
103 %!assert(yeardays(2000, 4), 360)
104 %!assert(yeardays(2000, 5), 360)
105 %!assert(yeardays(2000, 6), 360)
106 %!assert(yeardays(2000, 7), 365)
107 %!assert(yeardays(2000, 8), 366)
108 %!assert(yeardays(2000, 9), 360)
109 %!assert(yeardays(2000, 10), 365)
110 %!assert(yeardays(2000, 11), 360)
111