Calculating and plotting conditional distribution.

19 views (last 30 days)
Dear community,
I hope you are all good here. Could you please help me with a code for the next problem. I have the following code
lambda = 0.3;
p_bar = 1;
p_tilda = (1 - lambda)/(1 + lambda);
S = 0:0.01:p_tilda;
withoutReplacement = randperm(numel(S), 2); % Ask for 2 of the elements of S
S(withoutReplacement);
p1 = S(withoutReplacement(1));
p2 = S(withoutReplacement(2));
if p1 < p2
pe1 = @(p1,p2) p1 * ((1 - lambda)/2 + lambda) + p2 * ((1 - lambda)/2 + lambda);
pe2 = @(p1) p1 * ((1 - lambda)/2 + lambda) + p_bar * (1 - lambda)/2;
y1 = pe1(p1,p2);
y2 = pe2(p1);
else
y1 = 0;
y2 = 0;
end
I generate two random values from the interval S and then calculate two functions if p1 < p2.
Now I want to calculate and plot a distribution for all possible values of y1 and y2 when p1 < p2. I assume that I should make a loop of many drawings for all p1 < p2, but my MathLab skills is not sufficient to do it. I hope someone knows how to code it. Cheers!

Accepted Answer

Torsten
Torsten on 15 Aug 2022
You mean this
lambda = 0.3;
p_bar = 1;
p_tilda = (1 - lambda)/(1 + lambda);
S = 0:0.01:p_tilda;
n = 1000000;
icount = 0;
for i = 1:n
withoutReplacement = randperm(numel(S), 2); % Ask for 3 of the elements of S
p1 = S(withoutReplacement(1));
p2 = S(withoutReplacement(2));
if p1 < p2
icount = icount + 1;
y1(icount) = p1 * ((1 - lambda)/2 + lambda) + p2 * ((1 - lambda)/2 + lambda);
y2(icount) = p1 * ((1 - lambda)/2 + lambda) + p_bar * (1 - lambda)/2;
%else
% y1(i) = 0;
% y2(i) = 0;
end
end
figure(1)
ksdensity(y1)
hold on
ksdensity(y2)
hold off
or this
lambda = 0.3;
p_bar = 1;
p_tilda = (1 - lambda)/(1 + lambda);
S = 0:0.01:p_tilda;
n = 10000000;
for i = 1:n
withoutReplacement = randperm(numel(S), 2); % Ask for 3 of the elements of S
p1 = S(withoutReplacement(1));
p2 = S(withoutReplacement(2));
if p1 < p2
y1(i) = p1 * ((1 - lambda)/2 + lambda) + p2 * ((1 - lambda)/2 + lambda);
y2(i) = p1 * ((1 - lambda)/2 + lambda) + p_bar * (1 - lambda)/2;
else
y1(i) = 0;
y2(i) = 0;
end
end
figure(2)
ksdensity(y1)
hold on
ksdensity(y2)
hold off
?
  24 Comments
Yuriy
Yuriy on 19 Aug 2022
Thank you!
Is it possible to plot y1 and y2 on the same graph to see how this surfaces interact (cross each other)? Same as it was on 2D plot before where I could see which one is dominating.

Sign in to comment.

More Answers (0)

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!