High frequency large time-series matrix: Split in months

1 view (last 30 days)
Hi! I have a dataset of about 3000 samples with each having about 2 years of daily return data. Now I want to calculate monthly Sharpe Ratios, i.e. as far as I understood I need 24 matrices with monthly data.
Now: How can I have the matrix split in months considering that every month has a different amount of days (i.e. each new small matrix will have different size)?
Additionally: Would it be somehow possible to filter the small matrices even further, i.e. include only specific datasets out of the total amount of samples? I am thinking somehow using another matrix "samples"x"months" indicating 1 or 0 for being included or not.
THank you for every hint on either the first or the second or both of my questions!

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 13 Sep 2012
Edited: Azzi Abdelmalek on 13 Sep 2012
clear
data=num2cell(rand(365*2,2))
%d1: first day
%m1: first month
%y1: first year
% If the first sample was taken on 2008/08/15
y1=2008;m1=8;d1=15;
my=datevec(datestr(datenum(y1,m1,(d1:365*2+d1-1)),'yyyy/mm/dd'))
idx=find(diff(my(:,2))~=0)
idx1=1;
for k=1:numel(idx)
M{k}=data(idx1:idx(k),:)
idx1=idx(k)+1
end
  5 Comments
Sebastian
Sebastian on 14 Sep 2012
I played around with Matlab and the code a little and I think I am getting there! Thank you a lot!
I found out that I need to use cell2mat on every M{k} before being able to use sharpe, but that is OK.
I also found out that I can use...
F =
[1] [0]
[1] [0]
[1] [1]
[1] [0]
X=M{1}.*F
[0.847] [0]
[0.409] [0]
[0.192] [0.039]
[0.482] [0]
...in order to get close to what I was looking for in the second part. If you can now tell me how I can make all values zero in those rows where at least one value is =0 my complete problem would be solved! So in the example above, the 0.039 should be changed to zero because in the same row at least on other value is 0.
Azzi Abdelmalek
Azzi Abdelmalek on 14 Sep 2012
X={ 0.847 0
0.409 0
0.192 0.039
0.482 0 }
idx=find(any(cellfun(@(x) x==0,X))==1)
X(:,idx)={0}

Sign in to comment.

More Answers (1)

Andrei Bobrov
Andrei Bobrov on 14 Sep 2012
data = randi(456,800,15); % 15 samples and 800 daily data for example.
datainitial = '2009-07-13';
[y,m,d] = datevec(datainitial);
[y2,m2] = datevec(datenum(y,m,(d:d+size(data,1)-1)'));
[c,c,c] = unique([y2,m2],'rows');
[i1,i2] = ndgrid(c,1:size(data,2));
out = accumarray([i1(:),i2(:)],data(:),[],@(x){x});

Categories

Find more on Dates and Time 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!