X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?p=CreaPhase.git;a=blobdiff_plain;f=octave_packages%2Ffinancial-0.4.0%2Fyeardays.m;fp=octave_packages%2Ffinancial-0.4.0%2Fyeardays.m;h=ed99a7e1387c0d971ecde57d58296e8b59d15935;hp=0000000000000000000000000000000000000000;hb=f5f7a74bd8a4900f0b797da6783be80e11a68d86;hpb=1705066eceaaea976f010f669ce8e972f3734b05 diff --git a/octave_packages/financial-0.4.0/yeardays.m b/octave_packages/financial-0.4.0/yeardays.m new file mode 100644 index 0000000..ed99a7e --- /dev/null +++ b/octave_packages/financial-0.4.0/yeardays.m @@ -0,0 +1,111 @@ +## Copyright (C) 2008 Bill Denney +## +## This program is free software; you can redistribute it and/or modify it under +## the terms of the GNU General Public License as published by the Free Software +## Foundation; either version 3 of the License, or (at your option) any later +## version. +## +## This program is distributed in the hope that it will be useful, but WITHOUT +## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +## details. +## +## You should have received a copy of the GNU General Public License along with +## this program; if not, see . + +## -*- texinfo -*- +## @deftypefn {Function File} {@var{d} =} yeardays (@var{y}) +## @deftypefnx {Function File} {@var{d} =} yeardays (@var{y}, @var{b}) +## Return the number of days in the year @var{y} with an optional basis +## @var{b}. +## +## Valid bases +## @itemize @bullet +## @item 0 +## actual/actual (default) +## @item 1 +## 30/360 (SIA) +## @item 2 +## actual/360 +## @item 3 +## actual/365 +## @item 4 +## 30/360 (PSA) +## @item 5 +## 30/360 (IDSA) +## @item 6 +## 30/360 (European) +## @item 7 +## actual/365 (Japanese) +## @item 8 +## actual/actual (ISMA) +## @item 9 +## actual/360 (ISMA) +## @item 10 +## actual/365 (ISMA) +## @item 11 +## 30/360E (ISMA) +## @end itemize +## @seealso{days365, days360, daysact, daysdif} +## @end deftypefn + +function d = yeardays (y, basis) + + if (nargin == 1) + basis = 0; + elseif (nargin != 2) + print_usage (); + endif + + if isscalar (y) + d = zeros (size (basis)); + elseif isscalar (basis) + ## the rest of the code is much simpler if you can be sure that + ## basis is a matrix if y is a matrix + basis = basis * ones (size (y)); + d = zeros (size (y)); + else + if ndims (y) == ndims (basis) + if ~ all (size (y) == size (basis)) + error ("year and basis must be the same size or one must be a scalar"); + else + d = zeros (size (y)); + endif + else + error ("year and basis must be the same size or one must be a scalar.") + endif + endif + + bact = ismember (basis(:), [0 8]); + b360 = ismember (basis(:), [1 2 4 5 6 9 11]); + b365 = ismember (basis(:), [3 7 10]); + + badbasismask = ~ (bact | b360 | b365); + if any (badbasismask) + badbasis = unique (basis(badbasismask)); + error ("Unsupported basis: %g\n", badbasis) + endif + + d(bact) = 365 + (eomday(y(bact), 2) == 29); + d(b360) = 360; + d(b365) = 365; + +endfunction + +## Tests +%!assert(yeardays(2000), 366) +%!assert(yeardays(2001), 365) +%!assert(yeardays(2000:2004), [366 365 365 365 366]) +%!assert(yeardays(2000, 0), 366) +%!assert(yeardays(2000, 1), 360) +%!assert(yeardays(2000, 2), 360) +%!assert(yeardays(2000, 3), 365) +%!assert(yeardays(2000, 4), 360) +%!assert(yeardays(2000, 5), 360) +%!assert(yeardays(2000, 6), 360) +%!assert(yeardays(2000, 7), 365) +%!assert(yeardays(2000, 8), 366) +%!assert(yeardays(2000, 9), 360) +%!assert(yeardays(2000, 10), 365) +%!assert(yeardays(2000, 11), 360) +