BookmarkSubscribeRSS Feed
Gick
Pyrite | Level 9

I need to create a cumulative distribution function (F) of the Gamma(a,b) distribution and then use this function to calculate Ws which I have noted profil_W in my program.
Here is the calculation formula for profil_W.

Profile_W=(k*F(k)+(k-2)*F(k-2))+(k-1)*F(k-1)+ab(2*F(k-1)-F( k-2)-F(k)).
The first part F follows the Gamma(a,b) law and the second part F follows the Gamma(a+1,b) law.
Note that the second part begins with ab(...).

Here is the code to calculate the 2 laws of Gamma followed by F that I noted "Fct_ab" the first and "Fct_A1b" the second.
k represents "Incid" in my formula. My problem is that I can't vary or calculate the functions F(k), F(k-1), F(k-2), etc.

 

Here is the code:

 

/*----------------------------------------------*/
proc contents date= tab_It;
run;
/*Fixed Gamma Law Parameter*/
%let a=1;
%let b=5;


/*Distribution of the Gamma(a,b) distribution*/
Data tab_W;
set tab_It;
if (%sysevalf(&b.)>0) and (%sysevalf(&b.)>0) ;
if (Incid >0);
Fct_ab=RAND('GAMMA', %sysevalf(&a.), %sysevalf(&b.));
Fct_A1b=RAND('GAMMA', %sysevalf(&a.)+1, %sysevalf(&b.));
profil_W=round((Incid*(Fct_ab)+(Incid-2)*Fct_ab-2*(Incid-1)*Fct_ab) +%sysevalf(&a.)*%sysevalf(&b.)*(2*Fct_A1b-Fct_A1b),0.01);
run;

 

 



7 REPLIES 7
ballardw
Super User

If a macro variable contains a value that is easily considered as a number you do not need all that %syseval junk.

 

Example:

%let a= 0.25;
%let b= 4;


data example;
   x = &a. * &b.;
run;

which results in the expected 1 for x. All macro variables are is text. If the value you have would be okay for a statement as is such as : x = 0.25 * 4 ; then that's all you need.

 

The function CDF in SAS returns a value from the cumulative distribution, the Quantile function returns a specific point from the distribution. Rand generates a random number from from the distribution so I doubt that is what you want.

"Generate a distribution" I would typically take to create fair portion of values from a distribution, not a single value as your code does. So I suspect that you need to provide more details. Possibly provide a link to what you mean by "Gamma law" as most of the internet search results I get are company names, gaming or image color correction related.

Gick
Pyrite | Level 9
Here is the link that explains the gamma distribution. Source wikipedia. https://en.wikipedia.org/wiki/Gamma_distribution

For the macro variables created, I tried to fix values ​​of a and b which are the parameters of the "gamma law". Once the calculation of the repartition function is good. I could change its parameter values.

In my code, I used %sysevalf() but the problem is to know if the calculation of my distribution functions are well calculated and also the profil_W variable.
Thank you for your comments and I am still on my hunger
ballardw
Super User

Time to start over. I know what a Gamma distribution is. Neither of the links you provide have anything related to "Gamma Law"

Your request starts with

I need to create a cumulative distribution function (F) of the Gamma(a,b) distribution and then use this function to calculate

You do not need to create a CDF for the gamma function. SAS provides two functions, one of which is very likely what you want either CDF or QUANTILE.

CDF would involve writing code to implement and calculate integrals. Which you have not shown even a slight attempt to do so. I have done such code but refuse to do for functions that SAS Provides and except for nearly-trivial examples wouldn't on this forum as the amount of work is excessive for a volunteer.

 

Look at the documentation for the CDF function, 

Returns a value from a cumulative probability distribution.

CDF('GAMMA', x, a <, λ>)
The CDF function for the gamma distribution returns the probability that an observation from a gamma distribution, with the shape parameter a and the scale parameter λ, is less than or equal to x.

 

X would be the point of the cumulative you are specifically interested in which could be a variable or fixed value, your choice.

 

If that is not what you want then likely the Quantile function is what you want.

 

 

 

Gick
Pyrite | Level 9
Thank you for that answer. I was not aware of this function (CDF).
Gick
Pyrite | Level 9
here is the calculation of the cumulative density function F(k) according to the Gamma distribution with parameter (a,b)
F_k=cdf('GAMMA', k, a, b);

of F(k-1), we have
F_k1=cdf('GAMMA', k-1, a, b);

Awaiting your comments
ballardw
Super User

As long as your K has the appropriate value then that is likely the correct call.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 1383 views
  • 3 likes
  • 2 in conversation