4
$\begingroup$

I have daily time series example data as follows:

dates = DateRange[{2017, 05, 1}, {2019, 5, 1}, "BusinessDay"][[All, {1, 2, 3}]];
data = Accumulate[RandomReal[{-1, 1}, {Length[dates], 3}]];
tsdata = TimeSeries[data, {dates}];

I can conver it to monthly data:

finaldata = MovingMap[Last, tsdata, {Quantity[1, "Month"], Right, "Month"}, 
   ResamplingMethod -> None];

The problem with the monthly data is that the first observation should be end of of May, 2017 but it is beginning of June, 2017. Any help to fix this problem would be much appreciated. Also,

finaldata2 = TimeSeriesResample[tsdata, "EndOfMonth", ResamplingMethod -> None];

gives me monthly data but the date is the end of the month, whether it be a weekend or weekdays. Is there any way to make the date as the last business day of the month?

Could anyone help me build a simple function that can convert daily univariate/multivariate time series data to weekly, biweekly, monthly, etc.? I really appreciate it for your help.

$\endgroup$
4
$\begingroup$

You can use the function TimeSeriesAggregate.

Use the third argument Last to get the values for the last day of aggregation window.

monthlymeans = TimeSeriesAggregate[tsdata, "Month"];
lastdayofmonth = TimeSeriesAggregate[tsdata, "Month", Last];
Row[{DateListPlot[tsdata, ImageSize -> 400, PlotLabel -> "daily"], 
  DateListPlot[monthlymeans, ImageSize -> 400, PlotLabel -> "monthly average"], 
  DateListPlot[lastdayofmonth, ImageSize -> 400, PlotLabel -> "lastdayofmonth"]}]

enter image description here

Update: to make the date as the last business day of the month:

lastbusinessdayofmonth = DayRound[#, "BusinessDay", "Preceding"] & /@ 
   Select[tsdata["Times"], DayMatchQ[#, "EndOfMonth"] &];
finaldata2 = TimeSeriesResample[tsdata, {lastbusinessdayofmonth }, 
   ResamplingMethod -> None];
DateListPlot[finaldata2, ImageSize -> 400, PlotLabel -> "lastbusinessdayofmonth"]

enter image description here

$\endgroup$
  • $\begingroup$ @ kglr, thank you as always. $\endgroup$ – ramesh Jun 13 at 3:35
  • $\begingroup$ @ramesh, my pleasure. $\endgroup$ – kglr Jun 13 at 18:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.