BookmarkSubscribeRSS Feed
hyx1
Calcite | Level 5

Hello,

I am trying to solve an equation that looks like this:

0.5 = average value of f(alpha, beta, N(0,1)),

here f is the cdf of beta distribution.

Say I generate 10000 number x_i from N(0,1), for each x_i, I calculate the corresponding cdf value, f_i (alpha, beta,x_i), then I average all these 20000 f_i.

Given that beta=1 in my beta distribution, I would like to find alpha in my beta distribution.

I tried writing a code like this:

 

%let beta = 1;
%let ans = 0.5;
%let mean = 0;
%let std = 1;
%let num_normal=10000;

proc iml;
start Func(alpha);
sumfp = 0;
call streaminit(0);
do normal_i = 1 to &num_normal;
x = rand('normal',mean,std);
fp = CDF('BETA',x,alpha,&beta);
sumfp = sumfp + fp;
end;
return ( sumfp/&num_normal - &ans );
finish;
intervals = {0, 100};
soln = froot('Func',intervals);

 

It clearly did not work, and I have no idea how to proceed from here.

Please advise! Thanks!

4 REPLIES 4
ballardw
Super User

I think you should expand on just what this means: beta distribution of N(0,1).

If the N(0,1) is supposed to be normal distribution than "beta distribution" of that doesn't make much sense. So you may need to provide more of what you are thinking that has been collapsed to that phrase. Or did you mean on the interval [0,1] which is typically where beta distributions are defined?

hyx1
Calcite | Level 5
Sorry for the confusion. I just re-edited my post. Please let me know what you think.
ballardw
Super User

One thing you have

x = rand('normal',mean,std);

but from the macro variables you are showing should that be

x = rand('normal',&mean,&std);

I don't have access to Proc IML so can't run your code (or the possibly fix of the macro variables)

 

"Clearly did not work" is awful vague.

Are there errors in the log?: Post the code and log in a code box opened with the "</>" to maintain formatting of error messages.

No output? Post any log in a code box.

hyx1
Calcite | Level 5

Hi,

Yes, you are right. That was a typo on my original post, and my code did have & in front of my variables. Below is my updated code:

%let beta = 2;
%let pd = 0.0315;
%let mean = 0;
%let std = 0.1;
%let num_normal=30000;

proc iml;
start Func(alpha);
	sumfp = 0;
	call streaminit(0);
	do normal_i = 1 to &num_normal;
	x = rand('normal',&mean,&std);
	fp = CDF('BETA',x,alpha,&beta);
	sumfp = sumfp + fp;
	end;
	return ( sumfp/&num_normal - &pd );
finish Func;
intervals = {0, 100};
soln = froot('Func',intervals);

 Part of the error message I got is:

50       !  return ( sumfp/&num_normal - &pd );
51         finish Func;
NOTE: Module FUNC defined.
52         intervals = {0, 100};
53         soln = froot('Func',intervals);
ERROR: (execution) Invalid argument to function.

 operation : CDF at line 47 column 10
 operands  : *LIT1008, x, alpha, *LIT1009

Basically I tried to find a built-in SAS code/equation/app to solve equations, so I dont need to hard code it. I have found proc iml seems to be able to solve some equations, if there is any other built-in SAS code that can do the job please share with me!!

Thank you!

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 4 replies
  • 693 views
  • 0 likes
  • 2 in conversation