turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- General Programming
- /
- Using Computer simulation to look for convergence

Topic Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

10-18-2017 12:19 PM

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

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

A one unit stick is broken randomly into two pieces.

- Find E (the short piece divided by the long piece).
- 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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to jsjoden

10-24-2017 11:07 AM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to jsjoden

10-18-2017 02:05 PM

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

What is E(*something*)?

How do you estimate E(*something*) by simulation?

PG

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to jsjoden

10-18-2017 05:43 PM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to jsjoden

10-19-2017 11:30 AM

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.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Rick_SAS

10-23-2017 06:42 PM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to jsjoden

10-24-2017 11:07 AM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Rick_SAS

10-24-2017 06:36 PM

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;

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to jsjoden

10-25-2017 05:35 AM

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;
```