BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jsjoden
Obsidian | Level 7

Suppose Xi for i=1, 2, 3… has uniform (0, 1) distribution.

  1. Let M = min (n: X1 + X2 + … + Xn> 1). Find expected value of  M; E(M).
  2. Let N = min (n: Xn > Xn+1). Find expected value of  N; E (N).

     

A one unit stick is broken randomly into two pieces.

  1. Find E (the short piece divided by the long piece).
  2. Find E (the long piece divided by the short piece).

 

 

I am completely confused on how to start this simulation. Some clarity and direction would be great. Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

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

View solution in original post

9 REPLIES 9
PGStats
Opal | Level 21

Try first to answer these basic questions (in plain language):

 

What is E(something)?

 

How do you estimate E(something) by simulation?

PG
ballardw
Super User

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

Rick_SAS
SAS Super FREQ

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.

jsjoden
Obsidian | Level 7

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;

Rick_SAS
SAS Super FREQ

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

jsjoden
Obsidian | Level 7

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;

Rick_SAS
SAS Super FREQ

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;
JamesHawthorne
Fluorite | Level 6

Just wondering, how would we know if it converges or diverges? If it does converge how do we find the value?

valarievil
Obsidian | Level 7

But how would you know if it converges?

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 9 replies
  • 1231 views
  • 4 likes
  • 6 in conversation