Fluorite | Level 6

## Trouble generating random matrix from Bernoulli Distribution

Hi all,

I am struggling to generate a matrix, let's call it "forecast", such that each element forecast[i,j] is drawn from a different Bernoulli distribution with probability corresponding to another matrix element, say bernoulli_p[i,j]. The log displays no errors, but when I print the forecast matrix, it only has blank values.

Here is the relevant part of the code I have been using:

do i=1 to n_farms;
do j=7 to n_months;
if avc[i,j]>0 then stoch_avc[i,j]=(avc[i,j]+u[i,j]);

if avc[i,j]=0 then exit_z[i,j]=.;
exit_z[i,j]=-2.929548+FALL[i,j]*(-0.027052)+Winter[i,j]*0.61746+spring[i,j]*(-0.075117)+
PRICE[i,j-1]*(-0.078526)+PRICE[i,j-2]*(-0.004963)+PRICE[i,j-3]*0.117736+PRICE[i,j-4]*(-0.042244)+PRICE[i,j-5]*0.001874+PRICE[i,j-6]*(-0.069283)+
AVC[i,j-1]*(-0.552019)+AVC[i,j-2]*(-0.017645)+AVC[i,j-3]*0.615848+AVC[i,j-4]*(-0.173533)+AVC[i,j-5]*0.174981+AVC[i,j-6]*0.043233;

if exit_z[i,j]^=. then bernoulli_p[i,j]=cdf('normal',exit_z[i,j]);
if exit_z[i,j]=. then bernoulli_p[i,j]=.;

call randseed(datetime());
forecast=j(n_farms,n_months,.);
call randgen(forecast[i,j],"bernoulli",bernoulli_p[i,j]);

end;
end;

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Barite | Level 11

## Re: Trouble generating random matrix from Bernoulli Distribution

I think there are three problems that I can see with your code.

• Did you intend the second loop to run from 7?  If n_months is less than 7 then the second do loop will not iterate at all.
• As the statement 'forecast=j(n_farms,n_months,.)' is inside the nested loops, it will set all the elements to missing on every iteration, so it needs to be moved outside of the loops.
• The first parameter in CALL RANDGEN should be the name of matrix which is to be entirely filled with random numbers in one call.  It will not work when you try to set individual elements.  Make a single call to RANDGEN at the end of the code.

Putting all of this together the code will look something like this:

do i=1 to n_farms;
do j=1 to n_months;
< code to set each element of the matrix bernoulli_p here >;
end;
end;

call randseed(1729);
forecast=j(n_farms,n_months,.);
call randgen(forecast,'Bernoulli',bernoulli_p);

3 REPLIES 3
Super User

## Re: Trouble generating random matrix from Bernoulli Distribution

Can you post the data about matrix forecast[i,j] and bernoulli_p[i,j] ?  and the output matrix you want ?

Barite | Level 11

## Re: Trouble generating random matrix from Bernoulli Distribution

I think there are three problems that I can see with your code.

• Did you intend the second loop to run from 7?  If n_months is less than 7 then the second do loop will not iterate at all.
• As the statement 'forecast=j(n_farms,n_months,.)' is inside the nested loops, it will set all the elements to missing on every iteration, so it needs to be moved outside of the loops.
• The first parameter in CALL RANDGEN should be the name of matrix which is to be entirely filled with random numbers in one call.  It will not work when you try to set individual elements.  Make a single call to RANDGEN at the end of the code.

Putting all of this together the code will look something like this:

do i=1 to n_farms;
do j=1 to n_months;
< code to set each element of the matrix bernoulli_p here >;
end;
end;

call randseed(1729);
forecast=j(n_farms,n_months,.);
call randgen(forecast,'Bernoulli',bernoulli_p);

SAS Super FREQ