11.1 Algebraic equation solver
Stan provides two built-in algebraic equation solvers, respectively based on Powell’s and Newton’s methods. The Newton method constitutes a more recent addition to Stan; its use is recommended for most problems. Although they look like other function applications, algebraic solvers are special in two ways.
First, an algebraic solver is a higher-order function, i.e. it takes another function as one of its arguments. Other functions in Stan which share this feature are the ordinary differential equation solvers (see section Ordinary Differential Equation (ODE) Solvers). Ordinary Stan functions do not allow functions as arguments.
Second, some of the arguments of the algebraic solvers are restricted to data only expressions. These expressions must not contain variables other than those declared in the data or transformed data blocks. Ordinary Stan functions place no restriction on the origin of variables in their argument expressions.
11.1.1 Specifying an algebraic equation as a function
An algebraic system is specified as an ordinary function in Stan within the function block. The algebraic system function must have this signature:
vector algebra_system(vector y, vector theta,
data array[] real x_r, array[] int x_i)
The algebraic system function should return the value of the algebraic function which goes to 0, when we plug in the solution to the algebraic system.
The argument of this function are:
y
, the unknowns we wish to solve fortheta
, parameter values used to evaluate the algebraic systemx_r
, data values used to evaluate the algebraic systemx_i
, integer data used to evaluate the algebraic system
The algebraic system function separates parameter values, theta
,
from data values, x_r
, for efficiency in propagating the derivatives
through the algebraic system.
11.1.2 Call to the algebraic solver
vector
algebra_solver
(function algebra_system, vector y_guess, vector theta, data array[] real x_r, array[] int x_i)
Solves the algebraic system, given an initial guess, using the Powell
hybrid algorithm.
Available since 2.17
vector
algebra_solver
(function algebra_system, vector y_guess, vector theta, data array[] real x_r, array[] int x_i, data real rel_tol, data real f_tol, int max_steps)
Solves the algebraic system, given an initial guess, using the Powell
hybrid algorithm with additional control parameters for the solver.
Available since 2.17
Note: In future releases, the function algebra_solver
will be deprecated
and replaced with algebra_solver_powell
.
vector
algebra_solver_newton
(function algebra_system, vector y_guess, vector theta, data array[] real x_r, array[] int x_i)
Solves the algebraic system, given an initial guess, using Newton’s method.
Available since 2.24
vector
algebra_solver_newton
(function algebra_system, vector y_guess, vector theta, data array[] real x_r, array[] int x_i, data real rel_tol, data real f_tol, int max_steps)
Solves the algebraic system, given an initial guess, using Newton’s method
with additional control parameters for the solver.
Available since 2.24
11.1.2.1 Arguments to the algebraic solver
The arguments to the algebraic solvers are as follows:
algebra_system
: function literal referring to a function specifying the system of algebraic equations with signature(vector, vector, array[] real, array[] int):vector
. The arguments represent (1) unknowns, (2) parameters, (3) real data, and (4) integer data, and the return value contains the value of the algebraic function, which goes to 0 when we plug in the solution to the algebraic system,y_guess
: initial guess for the solution, typevector
,theta
: parameters only, typevector
,x_r
: real data only, typearray[] real
, andx_i
: integer data only, typearray[] int
.
For more fine-grained control of the algebraic solver, these parameters can also be provided:
rel_tol
: relative tolerance for the algebraic solver, typereal
, data only,function_tol
: function tolerance for the algebraic solver, typereal
, data only,max_num_steps
: maximum number of steps to take in the algebraic solver, typeint
, data only.
11.1.2.2 Return value
The return value for the algebraic solver is an object of type
vector
, with values which, when plugged in as y
make the algebraic
function go to 0.
11.1.2.3 Sizes and parallel arrays
Certain sizes have to be consistent. The initial guess, return value of the solver, and return value of the algebraic function must all be the same size.
The parameters, real data, and integer data will be passed from the solver directly to the system function.
11.1.2.4 Algorithmic details
Stan offers two algebraic solvers: algebra_solver
and algebra_solver_newton
.
algebra_solver
is baed on the Powell hybrid method (Powell 1970),
which in turn uses first-order derivatives. The Stan code builds on
the implementation of the hybrid solver in the unsupported module for
nonlinear optimization problems of the Eigen library (Guennebaud, Jacob, et al. 2010).
This solver is in turn based on the algorithm developed for the
package MINPACK-1 (Jorge J. More 1980).
algebra_solver_newton
, uses Newton’s method,
also a first-order derivative based numerical solver.
The Stan code builds on the implementation in KINSOL
from the SUNDIALS suite (Hindmarsh et al. 2005).
For many problems, we find that algebra_solver_newton
is faster
than Powell’s method.
If however Newton’s method performs poorly, either failing to or requiring an excessively
long time to converge, the user should be prepared to switch
to algebra_solver
.
For both solvers, the Jacobian of the solution with respect to auxiliary parameters is computed using the implicit function theorem. Intermediate Jacobians (of the algebraic function’s output with respect to the unknowns y and with respect to the auxiliary parameters theta) are computed using Stan’s automatic differentiation.