%% Specify Aggregation Method for Each Variable
% This example shows how to specify the appropriate aggregation method for the units of a variable.
% Load the simulated stock price data and corresponding logarithmic returns SimulatedStockSeries.mat.

load SimulatedStockSeries

% The price series Price contains absolute measurements, whereas the log returns series Log_Return is the rate of change of the price series among successive observations. Because the series have different units, you must specify the appropriate method when you aggregate the series. Specifically, if you report the final price for a given periodicity, you must report the sum of the log returns within each period.
% Aggregate the data so that the result has a daily periodicity. For each series, specify the aggregation method that is appropriate for the unit.

DailyTT = convert2daily(DataTable,Aggregation=["lastvalue" "sum"])

% DailyTT1 is a timetable containing the daily final prices and log returns.
% Verify the results for January 1, 2018 through January 3, 2018.

jan42018 = datetime(2018,01,04);
DataTable(DataTable.Time < jan42018,:)
DailyTT(DailyTT.Time < jan42018,:)

% By visual comparison, the daily final results match. Each computed daily log return is the sum of the log returns recorded during the corresponding day in the raw data. Cross-check the log returns of January 2 and 3 by computing the difference between the log final prices for each day.

verify = diff(log(DailyTT.Price));
verify(1:2)

% Copyright 2021 The MathWorks, Inc.