How to calculate the highest consecutive negative results in my code

4 views (last 30 days)
Hi,
I'm new to Matlab and I have been having help to write this code. I am trying to learn on my own and I am trying now to write something that would calculate the highest consecutive negative results in my code. I would be happy to recieve some help!
clear variables
k = 5000;
risk = 0.007;
riskM = 0.1;
maxR = 0.022;
numberSteps = 105;
markets = zeros(numberSteps,1);
markets(1) = k;
for i = 1:size(markets,1) - 1
markets(i+1) = markets(i)*(1+0.1);
end
% ka = 1100
%%
numberSim = 1000;
distR = randsrc(numberSteps, numberSim,[-1 -.6 -.36 -.07 0 0.17 0.37 0.39 0.5 0.81 1.37 1.39 1.4 2.2 2.57 3.16 3.55 5.49 6.56 7.34 7.82 8.63 8.7 9.15 9.42 11.55 17.94 19.9 21.18 22.28 28.4 28.51;...
.648 .0095 .0095 .0095 0.067 .0095 .0095 .0095 .0095 .0095 .0095 .0095 .0095 .0095 .0095 .0095 .0095 .0095 .0095 .0095 .0095 .0095 .0095 .0095 .0095 .0095 .0095 .0095 .0095 .0095 .0095 .0095]);
cumR = cumsum(distR,1);
MaxD = maxdrawdown(cumR,'arithmetic');
[maxMaxD,ii] = max(MaxD);
meanD = mean(MaxD);
stdD = std(MaxD);
figure
subplot(1,2,1)
plot(cumR)
subplot(1,2,2)
plot(cumR(:,ii))
sgtitle(['Mean Drawdown=',num2str(meanD,3),' std=',num2str(stdD,3), ' Max Possible DD=',num2str(maxMaxD,3)])
  2 Comments
Mohammad Sami
Mohammad Sami on 30 Mar 2020
Edited: Mohammad Sami on 30 Mar 2020
I am not sure what you mean by highest consecutive negative results.
Camilo Acevedo
Camilo Acevedo on 30 Mar 2020
Hi Mohammad, what I mean is that for instance one of the random distribution is [2 4 3 -1 -2 5 7 8 -1 -0.3 -1 -1 -1 -1 -1 -1 -1 7] an another might be [2 2 2 4 3 -1 -1 -2 4 6 7 8 -3 -2 -1 -0.2 4 6]. So what I need is a code that finds where in all the random distribution is the highest consecutive negative sum (consecutive negative number), which in this example would be -8.3 (-1 -0.3 -1 -1 -1 -1 -1 -1 -1).

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 31 Mar 2020
Edited: Image Analyst on 31 Mar 2020
This is trivial if you have the Image Processing Toolbox. Simply use regionprops():
v = [2 4 3 -1 -2 5 7 8 -1 -0.3 -1 -1 -1 -1 -1 -1 -1 7] % Sample data.
% Find elements where v is negative.
negativeValues = v < 0
% Measure area and mean of the negative regions.
props = regionprops(negativeValues, v, 'MeanIntensity', 'Area');
% Extract values from structure into arrays, for convenience.
allMeanIntensities = [props.MeanIntensity]
allAreas = [props.Area]
% Get the sum of all values in each region.
integratedValues = allMeanIntensities .* allAreas
% Find the sum that is MOST negative.
mostNegative = min(integratedValues)
  5 Comments

Sign in to comment.

More Answers (2)

Mohammad Sami
Mohammad Sami on 30 Mar 2020
Assuming your vector is called a
a = randi([-10 10],2000,1);
ag0 = a>=0; % idx of val greater then a
id = cumsum(ag0 | circshift(ag0,1)); % create a grouping variable
% consecutive negative values are assigned to the same group
if id(1) == 0
id = id + 1; % start index at 1
end
b = accumarray(id,a,[],@sum); % sum the values for each group
[~,i] = min(b); % find the most negative sum
consective_idx = id==i; % index of the consecutive values
consective_val = a(consective_idx); %
  11 Comments
Mohammad Sami
Mohammad Sami on 3 Apr 2020
Edited: Mohammad Sami on 3 Apr 2020
Hi Camilo,
I dont have the toolbox to execute your code.
Based on the function description online I think it will generate a matrix of size numberSteps x numberSims.
The code I wrote will only work for a vector of size n x 1.
In your case can I assume that you want to find the negative sequence of each column separately ?
If that is the case you can use the for loop over each column to find the sequences with the above code.

Sign in to comment.


Camilo Acevedo
Camilo Acevedo on 6 Apr 2020
Well, I got some help from a friend expert in Matlab and he figured it out (based on Mohammad suggestions). So in case anyone in the future is looking for what I asked, her is the answer:
clear variables
numberSteps = 107;
numberSim = 250000;
distR = randsrc(numberSteps, numberSim,[-1 0 1.37 10.05 8.49 15.67 2.36 11.25 24.41 21.5 20.09 1.61 8.91 7.58 2.37 3.28 6.36 12.53 5.86 3.43 3.52 8.83 3.34 4.29 20.67;...
.709 .084 .009 .009 .009 .009 .009 .009 .009 .009 .009 .009 .009 .009 .009 .009 .009 .009 .009 .009 .009 .009 .009 .009 .009]);
%%
ag0 = distR>=0;
id = cumsum(ag0 | circshift(ag0,1));
id = id + 1;
numCons = zeros(1,numberSim);
for a4 = 1 : numberSim
c = accumarray(id(:,a4),distR(:,a4),[],@sum);
[~,d] = min(c);
numCons(a4) = sum(id(:,a4)==d);
end
maxNC = max(numCons);
meanNC = mean(numCons);
stdNC = std(numCons);

Community Treasure Hunt

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

Start Hunting!