Suppose Xi for i=1, 2, 3… has uniform (0, 1) distribution.
A one unit stick is broken randomly into two pieces.
I am completely confused on how to start this simulation. Some clarity and direction would be great. Thank you!
I think it is helpful to concentrate on one problem at a time. See if you can understand this modification of your program, which addresses only the first problem ("E(M)").
/* Let M = min {n: X1 + X2 + ... + Xn> 1}. Find E(M)=expected value of M */
data new ;
do i=1 to 10000;
sum=0 ;
M=0 ;
do until (sum>1) ;
u = rand('uniform');
sum+u;
M+1 ;
end ;
output;
end;
run;
proc means data=new;
var M;
run;
If you understand this problem, do something similar for the second problem. I don't think you need an array. Hint: the DO UNTIL loop might look something like DO UNTIL(u < prevU). Somewhere in the program you will need to set prevU=u.
PrevU = u
Try first to answer these basic questions (in plain language):
What is E(something)?
How do you estimate E(something) by simulation?
Maybe this will give you a start on the second.
data start; do i= 1 to 100; r= rand('uniform'); Short = min(r,1-r); long = max(r,1-r); output; end; run;
You can obviously add variables for the ratios between the long and short pieces.
The r variable above will have a sample of uniform(0,1) values.
If
You might want to read the article (especially the comments) "The expected number of uniform variates whose sum exceeds one."
For the broken stick problem, see the article "Random segments and broken sticks". The last paragraph gives some hints about how to simulate the problem in the DATA step.
Hi Rick,
Thank you for pointing me in the right direction. The first link that you provided is very similar to as what I am trying to do. However, I am new to SAS and have not gotten to proc iml yet. So loop would be more sufficient for me at this point. The code I got below seems to fit that. However, I am confused on some of the code. What does the first two lines do. retain and array? Then I am trying to print the E(N)/E(M) . As well after I am trying to find if there is a convergence or divergence.
data new ;
retain sum count mean _2 - _12 ;
array counts[*] _2 - _12 ;
do i=1 to 500 ;
sum=0 ;
count=0 ;
do until (sum>1) ;
sum+rand('uniform') ;
count+1 ;
end ;
mean+count ;
do j=2 to dim(counts) ;
if j=count then counts[j-1]+1 ;
end ;
end ;
mean=mean/i ;
output ;
run ;
proc freq data= doh;
table sum / plots=FreqPlot(scale=percent) nocum;
run;
I think it is helpful to concentrate on one problem at a time. See if you can understand this modification of your program, which addresses only the first problem ("E(M)").
/* Let M = min {n: X1 + X2 + ... + Xn> 1}. Find E(M)=expected value of M */
data new ;
do i=1 to 10000;
sum=0 ;
M=0 ;
do until (sum>1) ;
u = rand('uniform');
sum+u;
M+1 ;
end ;
output;
end;
run;
proc means data=new;
var M;
run;
If you understand this problem, do something similar for the second problem. I don't think you need an array. Hint: the DO UNTIL loop might look something like DO UNTIL(u < prevU). Somewhere in the program you will need to set prevU=u.
PrevU = u
I got it. So I came up with this. However, It is stuck in running the data step, so I am always having to cancel the submitted statement. Can you take a look if I did something wrong. Thanks
data dt1 ;
do i=1 to 10000;
sum = 0;
M=0 ;
u = rand('uniform');
prevU = u;
do until (u<prevU) ;
sum+u;
M+1 ;
end ;
output;
end;
run;
proc means data=dt1;
var M;
title'hope';
run;
I assume this is for #2 (expected value of N). You are close. You need to update the values of prevU INSIDE the loop or else the loop will never end.
data dt1;
do i=1 to 10000;
N=1; /* want 1 here instead of 0 */
u = rand('uniform');
prevU = u;
do until (prevU > u) ;
prevU = u; /* remember last u */
u = rand('uniform');/* generate new u */
N+1 ;
end;
output;
end;
run;
Just wondering, how would we know if it converges or diverges? If it does converge how do we find the value?
But how would you know if it converges?
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.