Calculating and plotting conditional distribution.

21 vues (au cours des 30 derniers jours)
Yuriy
Yuriy le 15 Août 2022
Commenté : Yuriy le 19 Août 2022
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!

Réponse acceptée

Torsten
Torsten le 15 Août 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 commentaires
Torsten
Torsten le 19 Août 2022
The 3d plot is complete (see code above).
Yuriy
Yuriy le 19 Août 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Wireless Communications dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by