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
- /
- Analytics
- /
- Stat Procs
- /
- RANGAM vs. RAND("Gamma", )

Topic Options

- Subscribe to 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
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

05-12-2016 05:04 PM

Hello,

I am trying to simulate data from a gamma distribution. I originally used the RAND function, from my understanding this is a newer function. However, I also tried the older function RANGAM. When using the same shape (and scale) parameters I get very different graphs when plotting the data. I'm obviously not understanding something correctly, could anyone advise on why one would use RAND vs. RANDGAM? I assumed there really shouldn't be any major differences.

Sample Code I tried

%let shape =0.15;

%let scale = 0.25;

data test;

do i = 1 to 1000;

do until (x>=0.2);

xnew=&scale.*rand("Gamma", &shape.);

xold=&scale.*randgam(1,&shape.);

end;

output;

end;

run;

proc univariate data=test;

var xnew xold;

histogram/gamma;

inset gamma/pos=ne;

run;

The data I'm trying to simulate needs to have a lower bound no less than 0.2, which is why I have the do until loop above. I'm hoping this is appropriate.

Thanks in advance for any help you can provide!

Accepted Solutions

Solution

05-12-2016
09:52 PM

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

05-12-2016 05:42 PM - edited 05-12-2016 05:44 PM

Hello @jonesk48,

The major difference between the RAND function (with first argument "Gamma") and the RANGAM function is the underlying pseudo-random number generator: The RAND function uses the famous Mersenne Twister, whereas the older RANGAM and other RANxxx functions use an older generator with inferior statistical properties. So, for professional simulations the RAND function should be preferred. Please see the documentation for more details. You can use the CALL STREAMINIT routine to specify a seed value (as you did in the first argument of RANGAM).

All Replies

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

05-12-2016 05:21 PM

Well.....Please ignore.... I realized I needed to debug my own code! I see there is no difference when I correct my code.

%let shape =0.15;

%let scale =0.25;

data test;

do i = 1 to 1000;

do until (xnew>=0.2);

xnew=&scale.*rand("Gamma", &shape.);

END;

OUTPUT;

end;

run;

data test1;

do i = 1 to 1000;

do until (xold>=0.2);

xnew=&scale.*rand("Gamma", &shape.);

xold=&scale.*rangam(100,&shape.);

END;

OUTPUT;

end;

run;

proc univariate data = test;

var xnew;

histogram/ gamma;

inset gamma/pos=ne;

run;

proc univariate data = test1;

var xold;

histogram/ gamma;

inset gamma/pos=ne;

run;

Solution

05-12-2016
09:52 PM

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

05-12-2016 05:42 PM - edited 05-12-2016 05:44 PM

Hello @jonesk48,

The major difference between the RAND function (with first argument "Gamma") and the RANGAM function is the underlying pseudo-random number generator: The RAND function uses the famous Mersenne Twister, whereas the older RANGAM and other RANxxx functions use an older generator with inferior statistical properties. So, for professional simulations the RAND function should be preferred. Please see the documentation for more details. You can use the CALL STREAMINIT routine to specify a seed value (as you did in the first argument of RANGAM).

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

05-12-2016 09:54 PM

@FreelanceReinhard Thank you for the explanation! I will stick with the RAND statement and will consider using the STREAMINIT option. Thanks for taking time to reply!

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

05-12-2016 09:04 PM

Check Rick's blog for the complete explanation RANDXXX() v.s. RAND() .

http://blogs.sas.com/content/iml/2013/07/10/stop-using-ranuni.html

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

05-12-2016 10:01 PM

Hi @Ksharp,

Thank you for providing the link to Rick's article. I skimmed over it but will definitely read it thoroughly tomorrow. Appreciate you taking the time to respond to my question.

Sincerely,

Kendra