]> Creatis software - CreaPhase.git/blob - octave_packages/financial-0.4.0/busdays.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / financial-0.4.0 / busdays.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{bdates} =} busdays (@var{sdate}, @var{edate})
18 ## @deftypefnx {Function File} {@var{bdates} =} busdays (@var{sdate}, @var{edate}, @var{bdmode})
19 ## @deftypefnx {Function File} {@var{bdates} =} busdays (@var{sdate}, @var{edate}, @var{bdmode}, @var{holvec})
20 ## Generate a list of business dates at the end of the periods defined
21 ## between (including) @var{sdate} and @var{edate}.
22 ##
23 ## @var{sdate} is the starting date, @var{edate} is the ending date,
24 ## both are in serial date format (see datenum).  @var{bdmode} is the
25 ## business day frequency ("daily", "weekly", "monthly", "quarterly",
26 ## "semiannual", or "annual"); these can be abbreviated by the first
27 ## letter and they may also use an integer corresponding to the order in
28 ## the above list (i.e. "daily" = 1).  @var{holvec} is an optional list
29 ## of holidays.  If the holidays are not given, then the holidays
30 ## function is used.
31 ## @seealso{holidays, busdate, lbusdate, isbusday, fbusdate, datenum}
32 ## @end deftypefn
33
34 function bd = busdays (sd, ed, mode=1, hol=[])
35
36   if nargin < 2 || nargin > 4
37     print_usage ();
38   endif
39   if ~isnumeric (sd)
40         sd = datenum (sd);
41   endif
42   if ~isnumeric (ed)
43     ed = datenum (ed);
44   endif
45   if ed < sd
46     error ("busdays: the start date must be less than the end date")
47   endif
48   if isempty (hol)
49     ## make the holidays take into account the whole ending year because
50     ## the day may extend beyond the actual ending date
51     edtmp = datevec (ed);
52     edtmp(2:3) = [12 31];
53     hol = holidays (sd, datenum (edtmp));
54   endif
55   ## Convert the mode to the numeric
56   modestr = "dwmqsa";
57   if ischar (mode)
58     mode = find (lower (mode(1)) == modestr);
59     if isempty (mode)
60       error ("busdays: mode must be one of '%s'", modestr)
61     endif
62   elseif isnumeric (mode)
63     if mode < 1 || mode > length (modestr)
64       error ("busdays: mode must be between 1 and %d", length (modestr))
65     endif
66   else
67     error ("busdays: mode must be a number or string")
68   endif
69
70   ## do the computation
71   if mode == 1
72     ## daily
73     bd = (sd:ed)'(isbusday (sd:ed, hol));
74   elseif mode < 6
75     if mode == 2
76       ## weekly make the start and end dates Fridays and then move back
77       ## from there
78       wd = weekday ([sd;ed]);
79       d = [sd;ed] - wd + 7;
80       ## there are generally not more than one week of holidays at a
81       ## time, but the call to unique will make certain of that.
82       bd = unique (busdate ([d(1):7:d(2)]', -1, hol));
83     else
84       d = datevec ([sd:ed]);
85       ## unique year and month list within the date range
86       ym = unique (d(:,1:2), "rows");
87       if mode == 3
88         ## monthly, do nothing to the ym list
89       elseif mode == 4
90         ## quarterly
91         if mod (ym(end), 3) != 0
92           ## make the last month an end of quarter month
93           ym(end) = ym(end) + 3 - mod (ym(end), 3);
94         endif
95         ym(mod (ym(:,2), 3) != 0, :) = [];
96       elseif mode == 5
97         ## semi-annually
98         if mod (ym(end), 6) != 0
99         ## make the last month an end of semi-annual month (6, 12)
100         ym(end) = ym(end) + 6 - mod (ym(end), 6);
101         endif
102         ym(mod (ym(:,2), 6) != 0, :) = [];
103       endif
104       bd = lbusdate (ym(:,1), ym(:,2), hol);
105     endif
106   elseif mode == 6
107     ## annual
108     d = datevec ([sd;ed]);
109     bd = lbusdate ((d(1,1):d(2,1))', 12, hol);
110   else
111     ## this should have been caught before now
112     error ("busdays: invalid mode")
113   endif
114
115 endfunction
116
117 ## Tests
118 %!assert (busdays (datenum (2008, 1, 1), datenum (2008, 1, 12)), datenum (2008, 1, [2;3;4;7;8;9;10;11]))
119 %!assert (busdays (datenum (2008, 1, 1), datenum (2008, 1, 12), "d"), datenum (2008, 1, [2;3;4;7;8;9;10;11]))
120 %!assert (busdays (datenum (2001, 1, 2), datenum (2001, 1, 9), "w"), datenum (2001, 1, [5;12]))
121 %!assert (busdays (datenum (2008, 1, 1), datenum (2008, 1, 2), "m"), datenum (2008, 1, 31))
122 %!assert (busdays (datenum (2008, 1, 1), datenum (2010, 5, 2), "m"), lbusdate ([2008*ones(12,1);2009*ones(12,1);2010*ones(5,1)], [1:12 1:12 1:5]'))
123 %!assert (busdays (datenum (2008, 1, 1), datenum (2008, 1, 2), "q"), datenum (2008, 3, 31))
124 %!assert (busdays (datenum (2008, 1, 1), datenum (2010, 5, 2), "q"), lbusdate ([2008*ones(4,1);2009*ones(4,1);2010*ones(2,1)], [3:3:12 3:3:12 3 6]'))
125 %!assert (busdays (datenum (2008, 1, 1), datenum (2008, 1, 2), "s"), datenum (2008, 6, 30))
126 %!assert (busdays (datenum (2008, 1, 1), datenum (2010, 5, 2), "s"), lbusdate ([2008;2008;2009;2009;2010], [6 12 6 12 6]'))
127 %!assert (busdays (datenum (2008, 1, 1), datenum (2011, 1, 2), "a"), datenum ([2008;2009;2010;2011], [12;12;12;12], [31;31;30;30]))