## How to draw random number from a conditional distribution

### yanting wu (view profile)

on 23 Sep 2019
Latest activity Edited by Adam Danz

on 24 Sep 2019

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

the cyclist

### the cyclist (view profile)

on 23 Sep 2019
I don't understand condition 2. What does n represent? What is X(n)?
Regardless, your condition 1 fully specifies the distribution. There is only one distribution that can obey all those conditions. So, I don't see how condition 2 can also be satisfied, in general.
Or maybe I am misunderstanding something.
Edit: Maybe Adam's answer is actually what you meant, in which case you can generate values using rejection sampling.
yanting wu

### yanting wu (view profile)

on 23 Sep 2019
Hi,
The second condition is that: I have another variable Y, given the value of each nth Y, my nth variable X should follow a normal distribution and should have 97% of chance greater or equal than Y.
I did the following matlab loop and it takes me almost infinite time to run, so I want to add the second condition into each n.
The matlab code I used is:
Y=randn(10000,1) * 0.61+12.01;
count=zeros(10000,1);
control =1;
while control==1
X=randn(10000,1) * 0.63+12.21;
for n1=1:10000
if X>=Y
count(n1)=1;
else
count(n1)=0;
end
s=sum(count);
pn=s/10000;
if pn<0.97
control=1
else
control=control+1
end
end
end

### Tags

on 23 Sep 2019

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%

on 23 Sep 2019
@yanting wu , sorry for changing my comment while you were responding. I had a 2nd thought about your goals which is described above.
yanting wu

### yanting wu (view profile)

on 24 Sep 2019
Yes, I need to compare X(n) with Y(n). In total I need 10000 pairs of observations (X , Y), and at least 9700 pairs of them with X>= Y. Because Y are created in the previous step with normal distribution, I can consider Y(n) as a constant number for generating X.
Thank you
Yanting

on 24 Sep 2019
Yeah, that will be impossible as the figure shows in an earlier comment of mine (blue line). You'll never get 97% values in X>=Y if x and y are numbers from normal distributions with the means and std's you specified.

### Bruno Luong (view profile)

on 24 Sep 2019
Edited by Bruno Luong

### Bruno Luong (view profile)

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