Solved
Contributor
Posts: 63

# Mix distribution simulation

[ Edited ]

Hi, I am writing a code for sequential simulation with N=5000, state 1 = N(0,3), state 2 = N(5,10), P(state1)=70%, P(state2)=30%. I wrote a code for this simulation and I also want to show how does this simulation look like. However, in my "showing simulation" part, the result given is 10,000 rather than 5,000. Could anyone provide some hint? Thank you.

P.S. Does streaminit (12345) and streaminit(123) make any significant difference? Althogh I can see the results differentiate from different streaminits, I don't quite understand how my result is going to be affected, so any further explanation would be appreciate.

Here is my code:

/* sample from a mixture distribution */
%let N = 5000;
data States(drop=i);
call streaminit(12345);
array prob [2] _temporary_ (0.7 0.3);
do i = 1 to &N;
type = rand("Table", of prob[*]);
if type=1 then Time = rand("Normal", 0, 3); /* state 1 */
else Time = rand("Normal", 5, 10); /* steta 2 */
output;
end;
run;
proc univariate data=States;
ods select Histogram;
histogram Time / vscale=proportion
kernel(lower=0 c=SJPI);
run;
/* Showing simulation */
data A(drop=i);
call streaminit(123);
do i = 1 to 5000;
S1 = rand("Normal", 0, 3); output;
S2 = rand("Normal", 5, 10); output;
end;
run;
proc print data=A;
run;

Accepted Solutions
Solution
‎04-01-2017 01:34 PM
PROC Star
Posts: 277

## Re: Mix distribution simulation

Xusheng,

You are getting 10000 rows in your "showing simulation" table, because you have an OUTPUT statement after each of the assignment statements

S1 = rand("Normal", 0, 3); output;
S2 = rand("Normal", 5, 10); output;

The STREAMINIT function is mostly used if you want to replicate a series of pseudo-random numbers - using STREAMINIT means that you will get the same series if you use the same value in the call. If you just want a different series of pseudo-random numbers each time, there is no need for STREAMINIT.

Regards,

Søren

All Replies
Super User
Posts: 8,218

## Re: Mix distribution simulation

There are difference between where you create your sample and where you show your sample.

In the one case you assign 2500 to each group .. in the other you run the loop 5000 times, but assign samples to each group on every iteration.

Your use of two different call streaminit(x) calls will also cause a difference of which subjects are assigned. It is simply forcing the randomization table to a particular table. If you want the one to be exactly like the other, use the same value in each call.

Art, CEO, AnalystFinder.com

Contributor
Posts: 63

## Re: Mix distribution simulation

Thank you art297, your answer helped me to understanding the first part better.

For the second part where I run 5000 loops, why the print result show 10000 numbers for each state?

Solution
‎04-01-2017 01:34 PM
PROC Star
Posts: 277

## Re: Mix distribution simulation

Xusheng,

You are getting 10000 rows in your "showing simulation" table, because you have an OUTPUT statement after each of the assignment statements

S1 = rand("Normal", 0, 3); output;
S2 = rand("Normal", 5, 10); output;

The STREAMINIT function is mostly used if you want to replicate a series of pseudo-random numbers - using STREAMINIT means that you will get the same series if you use the same value in the call. If you just want a different series of pseudo-random numbers each time, there is no need for STREAMINIT.

Regards,

Søren

Contributor
Posts: 63

## Re: Mix distribution simulation

Thank you Soren,

After read your explanation I realized I was wrong and totally misunderstand the loop. Now I rewrite the code and it works, but does this loop have the same indication as the simulation?

Here is my code:

/* Showing simulation */
data A(drop=i);
call streaminit(12345);
do i = 1 to 3500;
S1 = rand("Normal", 0, 3); output;
end;
run;
data B(drop=i);
call streaminit(12345);
do i = 1 to 1500;
S2 = rand("Normal", 5, 10); output;
end;
run;
data AllRand;
merge A B;
run;
proc print
data=AllRand;
run;

Super User
Posts: 8,218

## Re: Mix distribution simulation

Why risk there being a difference? Could you just print the data set you initially created, i.e. states?

Art, CEO, AnalystFinder.com

Contributor
Posts: 63

## Re: Mix distribution simulation

Yes, I printed the result from the first part. And it makes sense.

Thank you.

☑ This topic is solved.