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

I don't know the code for gamma distribution. I started off with this below, but I don't know where to go from there.

 

data proc iml;
sims=1000; *this is for the outer loop that says how many times the simulation is repeated;
size= 20; *this is the size of each simulated sample;
a=3;*parameters needed;
do i=1 to sims;
free sample;
do k=1 to size;
sample=RAND('GAMMA',a);
end;
print sample;
endMonte Carlo and Boot Strap Simulation.jpg

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

@ChrisHemedinger has provided DATA step code that will generate 1000 samples from the Gamma(2) distribution, but please modify his code to use

(KEEP=i x)

 

Each sample is size 20. The variable 'i' in the output data can be used to index each sample. For example

 

data sample (keep=i x);
	sims=1000;
	size= 20;
	a=3;
	do i=1 to sims;
		do k=1 to size;
			x=RAND('GAMMA',a);
			output;
		end;
	end;
run;

proc sgplot data=sample;
 WHERE i=1;   /* plot first sample */
 histogram x;
run;
proc sgplot data=sample;
 WHERE i=2;   /* plot second sample */
 histogram x;
run;

I assume your teacher taught about BY-group processing for efficiently analyzing each sample. Either that or he/she taught you to use a macro loop. Both methods are described in "Simulation in SAS: The slow way or the BY way ."_ Use whichever way was taught.

View solution in original post

7 REPLIES 7
PaigeMiller
Diamond | Level 26

We don't do your homework for you.

 

If you go ahead and generate code to attempt to solve this problem, and it doesn't work and you can't figure out why, then we can help you fix your code and even suggest better approaches.

--
Paige Miller
mickir1
Calcite | Level 5
I’m not asking you to do my homework, I’m asking for help figuring out the gamma distribution part.
PaigeMiller
Diamond | Level 26

@mickir1 wrote:
I’m not asking you to do my homework, I’m asking for help figuring out the gamma distribution part.

That's an extremely broad question.

 

Can you be much more specific about what you don't understand?

--
Paige Miller
ballardw
Super User

@mickir1 wrote:
I’m not asking you to do my homework, I’m asking for help figuring out the gamma distribution part.

Are you getting errors? Post the code and errors from the log into a code box opened with the forum {I} menu icon. The code box is important to retain formatting of error messages that may provide hints.

ChrisHemedinger
Community Manager

If you haven't already, you should review Rick Wicklin's articles about simulation, including this one:

 

https://blogs.sas.com/content/iml/2014/04/30/simulating-from-the-inverse-gamma-distribution-in-sas.h... (that's inverse Gamma, I know)

 

And the whole collection of them here:

 

https://blogs.sas.com/content/tag/simulation/

 

 Also...your code, as shared, is incorrect (syntax errors). This creates the sample you want, I think.

 

data sample (keep=x);
	sims=1000;
	size= 20;
	a=3;
	do i=1 to sims;
		do k=1 to size;
			x=RAND('GAMMA',a);
			output;
		end;
	end;
run;

proc sgplot data=sample;
 histogram x;
run;
It's time to register for SAS Innovate! Join your SAS user peers in Las Vegas on April 16-19 2024.
PeterClemmensen
Tourmaline | Level 20

3 things:

 

1) You write "data proc IML" in the top of your code. But from your syntax and your note "PROC IML" in your assignment, I'm guessing you want to use PROC IML. In that case, whenever you can vectorize instead of using a loop, vectorize instead of using a loop. Like this

 

proc iml;
   sims=1000;
   size=20;
   SimM=j(size, sims, 0);
   call randgen(SimM, "Gamma", 3);
quit;

 

This gives you a 20x1000 matrix of random variates from the Gamma distribution. This gives you a nice basis to calculate the statistics you want.

 

2)

 Udklip.PNG

It is not due until monday, so you've got plenty of time to...... 

 

3) Consult @Rick_SAS' blog The DO Loop. Also, his book Simulating Data With SAS is filled with theory and examples on the topic.

 

Good luck 🙂

Rick_SAS
SAS Super FREQ

@ChrisHemedinger has provided DATA step code that will generate 1000 samples from the Gamma(2) distribution, but please modify his code to use

(KEEP=i x)

 

Each sample is size 20. The variable 'i' in the output data can be used to index each sample. For example

 

data sample (keep=i x);
	sims=1000;
	size= 20;
	a=3;
	do i=1 to sims;
		do k=1 to size;
			x=RAND('GAMMA',a);
			output;
		end;
	end;
run;

proc sgplot data=sample;
 WHERE i=1;   /* plot first sample */
 histogram x;
run;
proc sgplot data=sample;
 WHERE i=2;   /* plot second sample */
 histogram x;
run;

I assume your teacher taught about BY-group processing for efficiently analyzing each sample. Either that or he/she taught you to use a macro loop. Both methods are described in "Simulation in SAS: The slow way or the BY way ."_ Use whichever way was taught.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 7 replies
  • 2033 views
  • 2 likes
  • 6 in conversation