## Using Computer simulation to look for convergence

Solved
Occasional Contributor
Posts: 15

# Using Computer simulation to look for convergence

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!

Accepted Solutions
Solution
‎10-24-2017 11:42 PM
SAS Super FREQ
Posts: 4,242

## Re: Using Computer simulation to look for convergence

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

All Replies
Esteemed Advisor
Posts: 5,529

## Re: Using Computer simulation to look for convergence

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

What is E(something)?

How do you estimate E(something) by simulation?

PG
Super User
Posts: 13,550

## Re: Using Computer simulation to look for convergence

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

SAS Super FREQ
Posts: 4,242

## Re: Using Computer simulation to look for convergence

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.

Occasional Contributor
Posts: 15

## Re: Using Computer simulation to look for convergence

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;

Solution
‎10-24-2017 11:42 PM
SAS Super FREQ
Posts: 4,242

## Re: Using Computer simulation to look for convergence

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

Occasional Contributor
Posts: 15

## Re: Using Computer simulation to look for convergence

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;

SAS Super FREQ
Posts: 4,242

## Re: Using Computer simulation to look for convergence

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;
``````
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
• 7 replies
• 212 views
• 2 likes
• 4 in conversation