How to tell Matlab to ignore NaNs in calculations/loops
43 次查看(过去 30 天)
显示 更早的评论
Hi, I am trying to make some operations with Matlab but I need to ignore the generatedmissing Values / NaNs Matlab gives out after reading in the data from excel. Since now the code is only taking the empty cells and copy them to the new spreadsheet. Instead the NaNs should be ignored. (I am replicating a Finance paper which makes use of the momentum strategy described in Jegadeesh/Titman(1993).) The code is posted below
The code is taken to some part from paper which is about quite the same topic. I have made only some few channges. I Can't delete all the columns which have some NaNs in it.
Thank you for any helpful comments
The code as used is
% [stockdata] = xlsread('Argentina.xlsm',-1);
% [stockid] = xlsread('Argentina.xlsm',-1);
s=-1+(1-(-1)).*rand(715,215);
stockdata=transpose(s);
stockdata(stockdata > 0.5) = NaN;
stockid=[1:715];
[totalmonths,totalstocks] = size(stockdata);% Number of months, number of stocks
period1 = 6; %order period
period2 = 6; % holding period
startmonth=14;
for i=1:totalmonths
nbrstocks(i) = totalstocks - sum(isnan(stockdata(i,:)));
end
for i = 1:length(period1)
for j = 1:length(period2)
p1 = period1(i);
p2 = period2(j);
Rwinner = zeros(totalmonths,1);
Rloser = zeros(totalmonths,1);
idwinner = zeros(totalmonths,round(max(nbrstocks)*0.33));% empty matrix for winners
idloser = zeros(totalmonths,round(max(nbrstocks)*0.33));% empty matrix for losers
for k = startmonth:totalmonths+1-p1-p2 % loop from start till end
start1 = k;
stop1 = start1+p1-1;
ordermonths = start1:stop1;
start2 = stop1+1;
stop2 = start2+p2-1;
holdmonths = start2:stop2;
nstocks = nbrstocks(k);
% order R for the ordering weeks
data1 = stockdata(ordermonths, 1:nstocks);
R1 = ones(1,nstocks);
for m = 1:length(ordermonths)
R1 = R1.*data1(m,:);
end
R1 = R1-1;
% select for the winners and losers
[B,idx] = sort(R1,'descend');
ncandidates = round(nstocks*0.33);
idwinner(k,1:ncandidates) = stockid(idx(1:ncandidates));
idloser(k,1:ncandidates) = stockid(idx(end-ncandidates+1:end));
% caculate the R for winners and losers in holding months
data2winner = stockdata(holdmonths, idx(1:ncandidates));
data2loser = stockdata(holdmonths, idx(end-ncandidates+1:end));
R2w = ones(1,ncandidates);
R2l = ones(1,ncandidates);
for m = 1:length(holdmonths)
R2w = R2w.*data2winner(m,:);
R2l = R2l.*data2loser(m,:);
end
R2w = R2w-1;
R2l = R2l - 1;
Rwinner(k) = mean(R2w);
Rloser(k) = mean(R2l);
end
xlswrite(fullfile(['strategy_' num2str(p1) '_' num2str(p2) '_''.xlsx']),Rwinner,'Rwinner');
xlswrite(fullfile(['strategy_' num2str(p1) '_' num2str(p2) '_''.xlsx']),Rloser,'Rloser');
xlswrite(fullfile(['strategy_' num2str(p1) '_' num2str(p2) '_''.xlsx']),idwinner,'idwinner');
xlswrite(fullfile(['strategy_' num2str(p1) '_' num2str(p2) '_''.xlsx']),idloser,'idloser');
end
end
0 个评论
回答(4 个)
Oleg Komarov
2012-8-8
编辑:Oleg Komarov
2012-8-9
EDIT
What are you trying to do with:
% order R for the ordering weeks
data1 = stockdata(ordermonths, 1:nstocks);
R1 = ones(1,nstocks);
for m = 1:length(ordermonths)
R1 = R1.*data1(m,:);
end
R1 = R1-1;
the cumulative return? Then set the NaNs to 1.
idx = isnan(data1);
data1(idx) = 1;
R1 = prod(data1) - 1;
no need to loop, since prod() is vectorized.
Apply same concept here:
% caculate the R for winners and losers in holding months
data2winner = stockdata(holdmonths, idx(1:ncandidates));
data2loser = stockdata(holdmonths, idx(end-ncandidates+1:end));
R2w = ones(1,ncandidates);
R2l = ones(1,ncandidates);
for m = 1:length(holdmonths)
R2w = R2w.*data2winner(m,:);
R2l = R2l.*data2loser(m,:);
end
R2w = R2w-1;
R2l = R2l - 1;
AND then use nanmean().
6 个评论
Albert Yam
2012-8-8
How about just going low level. At the start of the loop (or where applicable),
if any(isnan(VARIABLE))
continue
end
4 个评论
Albert Yam
2012-8-9
Looking at others' answers, not sure what you want to skip. This will work if you put it in the right place. Not sure where the right place is.
if any(isnan(VARIABLE)) % if this is true, run the 'continue'
continue % this skips this iteration of the loop
%any code here is skipped, due to the continue
end
[B,idx] = sort(R1,'descend'); %should go here if you want to run it
Sebastian Holmqvist
2012-8-9
data_vec = [1 2 3 4 5 NaN 7 8 NaN 10 12];
data_vec(~isnan(data_vec))
ans =
1 2 3 4 5 7 8 10 12
3 个评论
Sebastian Holmqvist
2012-8-9
Well, this code excludes the NaN elements. So it all depends on what you want to do. I can't make much of your code (since I don't know the input data or what you want to produce) so it's hard for me to make an educated guess. A guess (probably a malfunctioning one) would be to insert
R1 = R1(~isnan(R1));
before
[B,idx] = sort(R1,'descend');
borge
2012-12-6
Hi Tobi83,
I am attempting to do a similar analysis andI have attempted to a compile a code similar to yours, but ran into the same problem, so I was wondering how your code ended up looking like?
Many thanks in advance!
Best, borge
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!