0

我有一个数据集如下所示:

'2014-01-07 22:20:00'        [0.0016]
'2014-01-07 22:25:00'        [0.0013]
'2014-01-07 22:30:00'        [0.0017]
'2014-01-07 22:35:00'        [0.0020]
'2014-01-07 22:40:00'        [0.0019]
'2014-01-07 22:45:00'        [0.0022]
'2014-01-07 22:50:00'        [0.0019]
'2014-01-07 22:55:00'        [0.0019]
'2014-01-07 23:00:00'        [0.0021]
'2014-01-07 23:05:00'        [0.0021]
'2014-01-07 23:10:00'        [0.0026]

第一列是记录数据一切5分钟的时间戳,第二列是返回。

对于每一天,我想计算 5 分钟柱回报的平方和。在这里,我将一天定义为从下午 5:00 到下午 5:00。(所以日期2014-01-07是从2014-01-06 17:002014-01-07 17:00)。因此,对于每一天,我都会从下午 5:00 到下午 5:00 对收益进行平方和。输出将类似于:

'2014-01-07'        [0.046]
'2014-01-08'        [0.033]

我该怎么做?

4

2 回答 2

0

我承认你的日期在一个单元格中,你的值在一个向量中。

例如,您有:

date = {'2014-01-07 16:20:00','2014-01-07 22:25:00','2014-01-08 16:20:00','2014-01-08 22:25:00'};

value = [1 2 3 4]; 

您可以通过以下方式找到每个日期的总和:

%Creation of an index that separate each "day".

    [~,~,ind]   = unique(floor(cellfun(@datenum,date)+datenum(0,0,0,7,0,0))) %datenum(0,0,0,7,0,0) correspond to the offset

for i = 1:length(unique(ind))
    sumdate(i) = sum(number(ind==i).^2)
end

你可以找到每个总和的对应日期

datesum  = cellstr(datestr(unique(floor(cellfun(@datenum,date)+datenum(0,0,0,7,0,0)))))
于 2016-06-15T16:01:44.190 回答
0

这是替代解决方案只需定义一些随机数据

t1 = datetime('2016-05-31 00:00:00','InputFormat','yyyy-MM-dd HH:mm:ss ');
t2 = datetime('2016-06-05 00:00:00','InputFormat','yyyy-MM-dd HH:mm:ss ');
Samples = 288;        %because your sampling time is 5 mins            
t = (t1:1/Samples:t2).';
X = rand(1,length(t));

首先,我们找到具有给定标准的样本(可以是任何东西,在你的情况下是 00:05:00)

 n = find(t.Hour >= 5,1,'first')
    b = n;

查找给定样本后的总天数

totaldays = length(find(diff(t.Day)))

并平方并累积每天的“回报”

for i = 1:totaldays - 1
    sum_acc(i) = sum(X(b:b + (Samples - 1)).^2);
    b = b + Samples;

end

这只是为了数据的可视化

Dates = datetime(datestr(bsxfun(@plus ,datenum(t(n)) , 0:totaldays - 2)),'Format','yyyy-MM-dd')
table(Dates,sum_acc.','VariableNames',{'Date' 'Sum'})

   Date        Sum  
__________    ______

2016-05-31    93.898
2016-06-01    90.164
2016-06-02    90.039
2016-06-03    91.676
于 2016-06-15T16:49:25.617 回答