18 views (last 30 days)

Hi, dear friends,

I am working a drawing 10000 random numbers for variable, X, based on the following two conditions:

condition 1: X follows a normal distribution with mean=12.21 and standard deviation = 0.63;

condition 2: given another variable Y, the probability of X >= Y is 0.97: prob (X(n)>=Y(n) =0.97).

I know how to draw a random variable with specific mean and standard deviation, but I don't know how to plug in the second condition to my code.

Can anyone give me some help?

I do really appreciate.

Thank you again

Best,

Yanting

Adam Danz
on 23 Sep 2019

Edited: Adam Danz
on 23 Sep 2019

The first condition is easy,

X = randn(1,100000) * .63 + 12.21;

figure

histogram(X)

mean(X) % <--- ~12.21

std(X) % <--- ~0.63

To find the cuttoff of the distribution that defines 97% of the data on the right and 3% of the data on the left, you need to find the 3rd percentile of the data. Any value in X has a 97% chance of being greater than 'pt'.

pt = prctile(X,3); %stats toolbox

Now add a vertical reference line to the plot and convince yourself that 97% of the data are greater than this value.

xline(pt, 'k-', '3rd percentile')

mean(X > pt) % <--- ~97%

Adam Danz
on 23 Sep 2019

Adam Danz
on 24 Sep 2019

Sign in to comment.

Bruno Luong
on 24 Sep 2019

Edited: Bruno Luong
on 24 Sep 2019

Your problem is not well defined, there are many Y that can meet such requirement (if you guys think it is impossible beside a scalar you are wrong). One these is given by this code:

mux = 12.21;

sigma = 0.63;

muy = fzero(@(muy) integral2(@(x,t) pdffun(x,t,muy), -Inf, Inf, 0, Inf)-0.97, 0);

muy = mux + sigma*muy; % 10.534298385905991 you can enter this value without using FZERO

X = mux + sigma*randn(1,1e6);

Y = muy + sigma*randn(1,1e6);

% Check how many X >= Y, it should be about 0.97

sum(X>=Y) / length(X)

function p = pdffun(x,t,muy)

y = x - t - muy;

p = 1/(2*pi)*exp(-x.^2/2).*exp(-y.^2/2);

end

Adam Danz
on 24 Sep 2019

"Your problem is not well defined, there are many Y that can meet such requirement (if you guys think it is impossible beside a scalar you are wrong)."

Agreed that the problem is not well defined but some valid interpretations of the problem (including the correct interpretation, according to OP) would result in impossibilities as I've shown above.

Interpretation 1

The distribution X only has 1 value where 97% of the data are greater than that value (the 3rd percentile). So it would be impossible to have a normal distribution with a known mean and known std and have multiple y values that define that boundary.

Interpretation 2

If 'n' random Ys are drawn from distribution y and 'n' random Xs are drawn from a distribution x, 97% of the Y(1:n) should be greater than X(1:n). Given the means and std's the OP defined, I've shown that this is indeed impossible and plotted the simulated results across 1000 repetitions where there were never more than ~60% Y values greater than their X pair.

Note that in your solution, the two distributions do not have the means and std's given by OP: "...generate 10000 observations for two normal distributed variables X (mean= 12.21, SD= 0.63) and Y (mean =12.01, SD=0.61)."-OP

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/481736-how-to-draw-random-number-from-a-conditional-distribution#comment_749122

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/481736-how-to-draw-random-number-from-a-conditional-distribution#comment_749122

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/481736-how-to-draw-random-number-from-a-conditional-distribution#comment_749148

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/481736-how-to-draw-random-number-from-a-conditional-distribution#comment_749148

Sign in to comment.