MATLAB如何过滤timeseries minute bar数据从而计算已实现波动率?
MATLAB how to filter timeseries minute bar data so as to calculate realised volatility?
我的数据集如下所示:
'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 分钟,第二列是 return。
对于每一天,我都想计算 5 分钟柱 return 的平方和。在这里,我将一天定义为从 5:00 pm - 5:00 pm。 (所以日期 2014-01-07
是从 2014-01-06 17:00
到 2014-01-07 17:00
)。因此,对于每一天,我都会从 5:00 pm - 5:00 pm 平方和 returns。输出将类似于:
'2014-01-07' [0.046]
'2014-01-08' [0.033]
我应该怎么做?
我承认你的日期在一个单元格中,你的值在一个向量中。
例如你有:
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)))))
这是替代解决方案
只是定义一些随机数据
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)))
每天平方和累加'return'
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
我的数据集如下所示:
'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 分钟,第二列是 return。
对于每一天,我都想计算 5 分钟柱 return 的平方和。在这里,我将一天定义为从 5:00 pm - 5:00 pm。 (所以日期 2014-01-07
是从 2014-01-06 17:00
到 2014-01-07 17:00
)。因此,对于每一天,我都会从 5:00 pm - 5:00 pm 平方和 returns。输出将类似于:
'2014-01-07' [0.046]
'2014-01-08' [0.033]
我应该怎么做?
我承认你的日期在一个单元格中,你的值在一个向量中。
例如你有:
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)))))
这是替代解决方案 只是定义一些随机数据
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)))
每天平方和累加'return'
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