Statistical programming, matrix languages, and more

Numerical integration with if else statement with different lower limit of integration

Accepted Solution Solved
Reply
Super Contributor
Posts: 292
Accepted Solution

Numerical integration with if else statement with different lower limit of integration

Hello, The if else statement run well with the same limit of integration A , B or interval (A,B). However I want to modify the code such that the first do statement should have limit of integration (A,B) as it is, but the else statement limit should change from (A,B) to (F,B).

That is the if statement limit of integration is (A,B) while else is (F,B)??? The different is the the lower limit of integration should change for the else statement from A to F.

I added comment and bold the sections of the code that need modification. Please ANY HELP?????????????

Please any suggestion ?

Data sim;

Input A B C Beta P  F;

cards;

1 2  3  0.5  0   1

2 3  4  0.5  1   1

3 4.5 3  0.5 1   2

3 5 6  0.5   0   2

;

Run;

proc iml;

use sim;

read all var _NUM_ into DM;

close;

A = DM[,1]; B = DM[,2]; C = DM[,3];  Beta = DM[,4];  P = DM[,5]; F = DM[,6];

n = nrow(DM);

start Func(x) global( Beta_i, C_i , A_i, P_i);

if P_i=0 then do;

   return(  exp(-x)*exp(2-x +Beta_i*C_i)#(cdf("Normal", x-2+Beta_i*C_i + A_i)-cdf("Normal", x-1+C_i + A_i)) );  /* The limits of integration is:call quad(result, "Func", A || B,); leave it as it is*/

end;

else do;

return(  exp(-x)*exp(4-x +Beta_i*C_i)#(cdf("Normal", x-4 +Beta_i*C_i + A_i)-cdf("Normal", x-1+C_i + A_i)) );  /* The limits of integration is:call quad(result, "Func", A || B,); but I want to change it to call quad(result, "Func", F || B,) */

end;

finish;

answer = j(nrow(DM),1);

do i = 1 to nrow(DM);

   Beta_i = Beta; C_i = C; A_i=A; P_i=P; /* set global variables */

   call quad(result, "Func", A || B,);

   answer = result;

end;

create kaplan1n var{A  B C Beta P Answer };

append;

quit;


Accepted Solutions
Solution
‎10-02-2014 11:05 AM
SAS Super FREQ
Posts: 3,406

Re: Numerical integration with if else statement with different lower limit of integration

Seem reasonable. It is fine to have a complicated integrand, but I personally think it is more readable and maintainable to have two integrands. You could define Func1 to be the integrand for P_i=0 and Func2 for the other case. Then you can get rid of P_i as a GLOBAL variable and write

   if P_i=0 then do;

   call quad(result, "Func1", A || B);

   end;

   else do;

   call quad(result, "Func2", F || B);

   end;

View solution in original post


All Replies
SAS Super FREQ
Posts: 3,406

Re: Numerical integration with if else statement with different lower limit of integration

Read the doc for the QUAD subroutine.  The limits of integration are specified in the CALL QUAD statement, so it is inside the DO loop that you need to specify the limits of integration.

Super Contributor
Posts: 292

Re: Numerical integration with if else statement with different lower limit of integration

Hello Rick,

Is this correct? I changed the limit as shown below (BOLD).

Data sim;

Input A B C Beta P  F;

cards;

1 2  3  0.5  0   1

2 3  4  0.5  1   1

3 4.5 3  0.5 1   2

3 5 6  0.5   0   2

;

Run;

proc iml;

use sim;

read all var _NUM_ into DM;

close;

A = DM[,1]; B = DM[,2]; C = DM[,3];  Beta = DM[,4];  P = DM[,5]; F = DM[,6];

n = nrow(DM);

start Func(x) global( Beta_i, C_i , A_i, P_i);

if P_i=0 then do;

   return(  exp(-x)*exp(2-x +Beta_i*C_i)#(cdf("Normal", x-2+Beta_i*C_i + A_i)-cdf("Normal", x-1+C_i + A_i)) );  /* The limits of integration is:call quad(result, "Func", A || B,); leave it as it is*/

end;

else do;

return(  exp(-x)*exp(4-x +Beta_i*C_i)#(cdf("Normal", x-4 +Beta_i*C_i + A_i)-cdf("Normal", x-1+C_i + A_i)) );  /* The limits of integration is:call quad(result, "Func", A || B,); but I want to change it to call quad(result, "Func", F || B,) */

end;

finish;

answer = j(nrow(DM),1);

do i = 1 to nrow(DM);

  Beta_i = Beta; C_i = C; A_i=A; P_i=P; /* set global variables */

   if P_i=0 then do;

   call quad(result, "Func", A || B,);

   end;

   else do;

   call quad(result, "Func", F || B,);

   end;

   answer = result;

end;

create kaplan1n var{A  B C Beta P Answer };

append;

quit;

Solution
‎10-02-2014 11:05 AM
SAS Super FREQ
Posts: 3,406

Re: Numerical integration with if else statement with different lower limit of integration

Seem reasonable. It is fine to have a complicated integrand, but I personally think it is more readable and maintainable to have two integrands. You could define Func1 to be the integrand for P_i=0 and Func2 for the other case. Then you can get rid of P_i as a GLOBAL variable and write

   if P_i=0 then do;

   call quad(result, "Func1", A || B);

   end;

   else do;

   call quad(result, "Func2", F || B);

   end;

Super Contributor
Posts: 292

Re: Numerical integration with if else statement with different lower limit of integration

Thanks a lot.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 479 views
  • 0 likes
  • 2 in conversation