Calculating and plotting conditional distribution.

조회 수: 21 (최근 30일)
Yuriy
Yuriy 2022년 8월 15일
댓글: Yuriy 2022년 8월 19일
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
Torsten
Torsten 2022년 8월 19일
The 3d plot is complete (see code above).
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