How to calculate the highest consecutive negative results in my code
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
Mohammad Sami
2020-3-30
编辑:Mohammad Sami
2020-3-30
I am not sure what you mean by highest consecutive negative results.
采纳的回答
Image Analyst
2020-3-31
编辑:Image Analyst
2020-3-31
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)
更多回答(2 个)
Mohammad Sami
2020-3-30
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 个评论
Mohammad Sami
2020-4-3
编辑:Mohammad Sami
2020-4-3
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Signal Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!