Derivatives

Subsections:

Nonlinear modeling techniques require the calculation of derivatives of certain variables with respect to other variables. The MODEL procedure includes an analytic differentiator that determines the model derivatives and generates program code to compute these derivatives. When parameters are estimated, the MODEL procedure takes the derivatives of the equation with respect to the parameters. When the model is solved, Newton’s method requires the derivatives of the equations with respect to the variables solved for.

PROC MODEL uses exact mathematical formulas for derivatives of non-user-defined functions. For other functions, numerical derivatives are computed and used.

The differentiator differentiates the entire model program, including the conditional logic and flow of control statements. Delayed definitions, as when the LAG of a program variable is referred to before the variable is assigned a value, are also differentiated correctly.

The differentiator includes optimization features that produce efficient code for the calculation of derivatives. However, when flow of control statements such as GOTO statements are used, the optimization process is impeded, and less efficient code for derivatives might be produced. Optimization is also reduced by conditional statements, iterative DO loops, and multiple assignments to the same variable.

The table of derivatives is printed with the LISTDER option. The code generated for the computation of the derivatives is printed with the LISTCODE option.

Derivative Variables

When the differentiator needs to generate code to evaluate the expression for the derivative of a variable, the result is stored in a special derivative variable. Derivative variables are not created when the derivative expression reduces to a previously computed result, a variable, or a constant. The names of derivative variables, which might sometimes appear in the printed output, have the form @obj /@wrt, where obj is the variable whose derivative is being taken and wrt is the variable that the differentiation is with respect to. For example, the derivative variable for the derivative of Y with respect to X is named @Y/@X.

The derivative variables can be accessed or used as part of the model program using the GETDER() function.

GETDER(x, a )

the derivative of x with respect to a.

GETDER(x, a, b )

the second derivative of x with respect to a and b.

The main purpose of the GETDER() function is for surfacing the derivatives so they can be stored in a data set for further processing. Only derivatives that are implied by the problem are available to the GETDER() function. When derivatives are requested that aren’t already created, a missing value will be returned. The derivative of the GETDER() function is always zero so the results of the GETDER() function shouldn’t be used in any of the equations in the FIT or the SOLVE statement.

The following example adds the gradient of the PRED.y value with respect to the parameters to the OUT= data set.

proc model data=line ;
   y = a1 + b1**2 *x1 + c1*x2;
   Dy_a1 = getder(PRED.y,a1);
   Dy_b1 = getder(PRED.y,b1);
   Dy_c1 = getder(PRED.y,c1);
   outvars Dy_a1 Dy_b1 Dy_c1;
   fit y / out=grad;
run;