High frequency large time-series matrix: Split in months

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!

 Réponse acceptée

Azzi Abdelmalek
Azzi Abdelmalek le 13 Sep 2012
Modifié(e) : Azzi Abdelmalek le 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 commentaires

Thank you very much Azzi!
Allow me a couple of follow-up questions (since I am a Matlab newbie)
1.) Did you generate random numbers for the matrix "data" in order to have a running code, i.e. I will replace this by the actual data? 2.) I assume idx is a vector including all the beginning-of-month dates. How exactly is this performed by "find(diff(my(:,2))~=0)"? 3.) The for- loop performs the split that I asked for in the first part of my question, but it does not enable me to filter the samples, right?
If yes, do you or does anyone else have an idea how to solve the second part?
Azzi Abdelmalek
Azzi Abdelmalek le 14 Sep 2012
Modifié(e) : Azzi Abdelmalek le 14 Sep 2012
  1. yes , I needed an example to run a code.
  2. "find(diff(my(:,2))~=0)", my(:,2) contains the months (1 1 .. 2 2 2 ... ) Diff(my(:, )=[1-1 1-1 ... 2-1 2-2 ...]; to detetct the end of each month, find the index corresponding to Diff(my(:, )=0
  3. for the second part, the question is not clear for me
what do you mean by small matrix, small compared to what?
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.
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}

Connectez-vous pour commenter.

Plus de réponses (1)

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});

Catégories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by