**You are now following this question**

- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.

# Calculating and plotting conditional distribution.

14 views (last 30 days)

Show older comments

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
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
on 16 Aug 2022

Edited: Yuriy
on 16 Aug 2022

Hi Torsten! Thank you for the suggestions. I don't think it correctly illustrates what I am trying to plot. From my code you can see that for all p1 < p2 profits y1 < y2. So, I think that the plot should look like two inverted U-shape distributions where one always dominates another for all possible combinations when p1 < p2.

I tried to slighlty modify your code like this:

lambda = 0.3;

p_bar = 1;

p_tilda = (1 - lambda)/(1 + lambda);

S = 0:0.01:p_tilda;

n = 1000000;

for i = 1:n

withoutReplacement = randperm(numel(S), 2); % Ask for 2 of the elements of S

p1 = S(withoutReplacement(1));

p2 = S(withoutReplacement(2));

if p1 < p2

y1 = p1 * ((1 - lambda)/2 + lambda) + p2 * ((1 - lambda)/2 + lambda);

y2 = p1 * ((1 - lambda)/2 + lambda) + p_bar * (1 - lambda)/2;

else

y1 = 0;

y2 = 0;

end

end

figure(1)

ksdensity(y1)

hold on

ksdensity(y2)

hold off

And this looks more or less closer to my expecteations but I am not sure why it has negative values, because p1, p2, y1, y2 values are strrictly positive. I'd expect y1 curve be strictly dominating on the interval (0, p_tilda). Maybe you can tell what I am doing wrong here. Thanks a lot!

Torsten
on 16 Aug 2022

Edited: Torsten
on 16 Aug 2022

I don't think it correctly illustrates what I am trying to plot. From my code you can see that for all p1 < p2 profits y1 < y2.

The plot of the two curves is an estimate for the probability that y1 / y2 attain a certain value. The x-axis is this value, the y axis is in principle the probability that this value results from the random experiment. It has nothing to do with a plot of y over p.

And this looks more or less closer to my expecteations but I am not sure why it has negative values, because p1, p2, y1, y2 values are strrictly positive.

After changing the for-loop as you did, y1 and y2 are no longer arrays, but you overwrite the values they attain in the loop every time so that in the end, y1 and y2 are two single values. And from these two single values you try to estimate a probability density curve. Total nonsense.

Yuriy
on 16 Aug 2022

Torsten
on 16 Aug 2022

Edited: Torsten
on 16 Aug 2022

That's where we were already: give me a hint how you imagine to plot a matrix of the form (p1,p2,y1) with n = 1000000 rows and p1, p2 randomly chosen.

Plotting y2 over p1 is of course easy:

lambda = 0.3;

p_bar = 1;

p_tilda = (1 - lambda)/(1 + lambda);

S = 0:0.01:p_tilda;

y2 = S * ((1 - lambda)/2 + lambda) + p_bar * (1 - lambda)/2;

plot(S,y2)

For y1 you can get a similar plot, but over the triangle (p1,p2), 0 <= p1 < p2 <= 1, as a two-dimensional surface plot.

Torsten
on 16 Aug 2022

Edited: Torsten
on 16 Aug 2022

Or if you use the smallest and highest possible value for p2, given p1, for y1, the plots look like:

lambda = 0.3;

p_bar = 1;

p_tilda = (1 - lambda)/(1 + lambda);

S = 0:0.01:p_tilda;

y1_low = 2*S * ((1 - lambda)/2 + lambda) ;

y1_high = (S + p_tilda) * ((1 - lambda)/2 + lambda);

plot(S,[y1_low;y1_high])

Note that y1_high = y2.

Yuriy
on 16 Aug 2022

Thanks a lot! I think the best I can do is to generate all possible pairs of values for p1<p2 in a separate file, like set of pairs in the rows and values of p1 and p2 in two columns. And then use this file to calculate all possible values of y1 and y2. And then to plot it. Now I will try to find how to generate such a table in MatLab, if it's possible, of course.

Thanks a lot!

Torsten
on 16 Aug 2022

As said, the problem is not to generate the table, but to find a way to plot it sensefully.

Yuriy
on 16 Aug 2022

Yuriy
on 17 Aug 2022

Torsten
on 17 Aug 2022

In your MATLAB description, p1 and p2 can only be drawn from (p_low,p_tilda).

So you changed the rules in Excel ?

Yuriy
on 17 Aug 2022

If I will keep only values on the interval (p_low, p_tilda) then the plot will look like this:

Yuriy
on 17 Aug 2022

Torsten
on 17 Aug 2022

lambda = 0.3;

p_bar = 1;

p_tilda = (1 - lambda)/(1 + lambda);

S = 0:0.01:p_tilda;

icount = 0;

for i = 1:numel(S)-1

p1 = S(i);

for j = i+1:numel(S)

p2 = S(j);

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;

end

end

figure(1)

plot(1:icount,y1)

hold on

plot(1:icount,y2)

hold off

figure(2)

ecdf(y1)

hold on

ecdf(y2)

hold off

Yuriy
on 18 Aug 2022

Yuriy
on 19 Aug 2022

Torsten
on 19 Aug 2022

Edited: Torsten
on 19 Aug 2022

At the moment, I don't see a simple solution since you don't have to plot over a rectangular domain, but a triangle p1 < p2.

I'd open a new question.

Say in your text that with the code

lambda = 0.3;

p_bar = 1;

p_tilda = (1 - lambda)/(1 + lambda);

S = 0:0.01:p_tilda;

[S1 S2] = ndgrid(S,S);

y1 = S1 * ((1 - lambda)/2 + lambda) + S2 * ((1 - lambda)/2 + lambda);

y2 = S1 * ((1 - lambda)/2 + lambda) + p_bar * (1 - lambda)/2;

figure(1)

surf(S1,S2,y1)

figure(2)

surf(S1,S2,y2)

you want to make a surface plot only over S1 < S2.

Torsten
on 19 Aug 2022

Edited: Torsten
on 19 Aug 2022

lambda = 0.3;

p_bar = 1;

p_tilda = (1 - lambda)/(1 + lambda);

S = 0:0.01:p_tilda;

[S1,S2] = ndgrid(S,S);

idx = find(S1 < S2);

S1 = S1(idx);

S2 = S2(idx);

S = [S1,S2];

T = delaunayTriangulation(S);

y1 = T.Points(:,1) * ((1 - lambda)/2 + lambda) + T.Points(:,2) * ((1 - lambda)/2 + lambda);

y2 = T.Points(:,1) * ((1 - lambda)/2 + lambda) + p_bar * (1 - lambda)/2;

figure(1)

trisurf(T.ConnectivityList,T.Points(:,1),T.Points(:,2),y1)

figure(2)

trisurf(T.ConnectivityList,T.Points(:,1),T.Points(:,2),y2)

zlim ([0 0.8])

Yuriy
on 19 Aug 2022

Thank you! I will create a new questions for 3d plots. Regarding probability - I model a competion in 2 periods, where two firms simultanesously draw prices from distribution on interval ($\underbar{p}$ , ) in the first period. The probability to draw a price which is lower than rival's price is . Then the cheapest firm have the following options (whichever is better) in the second period:

- Increase the price up to rival's price and get a profit y1;
- Increse the price up to and get a profit y2.

All probable values of these two options I plotted here with your help. And it's only a part of my analysis.

The firm which draw higher price in the first period, always increase the price up to in the second period, however if the cheapest firm chooses option y2, the expensive firm gets additional profits.

So, I am looking for where firms are indifferent in the first period about its price, this means that such distribution of p will represent NE. For that I need to understand how will look and what the lower and upper bound it will have. So far, I found that there are some point on the interval which I call , which affects firms pricing strategies. But I am still looking for NE price distribution which will represent mixed strategies (if it exists).

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.

### More Answers (0)

### See Also

### Categories

### Tags

### Community Treasure Hunt

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

Start Hunting!**An Error Occurred**

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

Americas

- América Latina (Español)
- Canada (English)
- United States (English)

Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)

Asia Pacific

- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)