fints toweekly vs timetable retime
3 views (last 30 days)
Show older comments
Nicola Carrer
on 4 Feb 2019
Commented: Nicola Carrer
on 5 Feb 2019
Hello,
I have some code that used the now deprecated FINTS timeseries from the financial package. In particular I need to resample a daily series to get only the points from each wednesday and, if it is a holiday, from the previous business day. The code below shows the expected behavior:
sampleDates = (datetime(2018, 11, 30):datetime(2019, 01, 31))';
[dayNumber, dayName] = weekday(sampleDates, 'en_US');
isWorking = isbusday(sampleDates);
data = (1:length(sampleDates))';
fts = fints(datenum(sampleDates), [dayNumber, isWorking, data], {'dayNumber', 'isWorking', 'data'});
ftsWeekly = toweekly(fts, 'CalcMethod', 'Exact', 'BusDays', 1, 'EOW', 5, 'EndPtTol', [0, -1]);
I correctly get all wednesdays and the previous working day for the two cases (26.12.2018 and 02.01.2019) when it was a holiday, where it takes the previous working day.
How should I do the same with the recommended retime function on timeseries (since the fints will be removed)?
I tried the following:
tt = timetable(sampleDates, dayNumber, dayName, isWorking, data);
ttWeekly = retime(tt, 'weekly', 'nearest');
but this gives me the sunday days and I don't see how to configure it to get the wednesdays.
Any help would be great
0 Comments
Accepted Answer
Peter Perkins
on 4 Feb 2019
retime won't know about holidays, and 'weekly' assumes you mean the start of the week (just as 'monthly' assumes start of month). But 'weekly' is just a convenience, you can always pass in an explicit vector of dates to retime to. You'll need the Financial Toolbox (or some other means) to deal with holidays, but a sequence of weds is easy:
>> fmt = 'eee dd-MMM-yyyy';
>> start = datetime(2019,2,1,'Format',fmt); stop = datetime(2019,3,1,'Format',fmt);
>> weds = dateshift(start,'dayofweek','wed','prev'):calweeks(1):dateshift(stop,'dayofweek','wed','next')
weds =
1×6 datetime array
Wed 30-Jan-2019 Wed 06-Feb-2019 Wed 13-Feb-2019 Wed 20-Feb-2019 Wed 27-Feb-2019 Wed 06-Mar-2019
Pass that in as the 2nd input to retime.
More Answers (2)
Guillaume
on 4 Feb 2019
I'm afraid that retime is not a suitable replacement for what you did and I doubt it will ever be.
However, I also don't think you need it for what you do. The following is untested, I don't have the financial toolbox.
tt = timetable(sampleDates, data); %your starting point. Don't need the extra columns.
%NOTE: the following assume that SampleDates are daily
dweek = day(tt.SampleDates, 'dayofweek');
tokeep = find(dweek == 4); %for wednesday
isholiday = ~isbusday(tt.SampleDates(tokeep)); %requires financial toolbox. I'm not aware of an equivalent in base matlab
while any(isholiday) %go back one day for holidays. This allows to go back multiple days if consecutive days are holidays.
tokeep(isholiday) = tokeep(isholiday) - 1;
tokeep(tokeep < 0) = []; %went back in time before the start. Drop entry
isholiday = ~isbusday(tt.SampleDates(tokeep));
end
ttWeekly = tt(tokeep, :)
0 Comments
Rick
on 5 Feb 2019
Nicola,
Could you please explain your coment "I correctly get all wednesdays and the previous working day for the two cases (26.12.2018 and 02.01.2019) when it was a holiday, where it takes the previous working day."
Your code segment reports the actual data values for 02-Jan-2019 (34) and 26-Dec-2018 (27), both of which are Wednesdays and working days according to your code.
Thanks, Rick
See Also
Categories
Find more on Calendar in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!