Calculating and plotting conditional distribution.

15 次查看(过去 30 天)
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!

采纳的回答

Torsten
Torsten 2022-8-15
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 个评论
Yuriy
Yuriy 2022-8-19
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Wireless Communications 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by