This is an old version, view current version.

6.11 Higher-Order Functions

There are several expression constructions in Stan that act as higher-order functions.4

The higher-order functions and the signature of their argument functions are listed in the higher-order functions table.

Higher-order Functions Table. Higher-order functions in Stan with their argument function types. The first group of arguments has no restrictions. The second group of arguments, consisting of a real and integer array in all cases, must be expressions involving only data and literals.

function unrestricted args data args return type
algebra_solver vector, vector real[], int[] vector
integrate_1d, real, real, real[] real[], int[] real
integrate_ode_X, real, real[], real[] real[], int[] real[]
map_rect vector, vector real[], int[] vector
reduce_sum T[], T1, T2, ... real

T, T1, T2, and the types of ... can be any Stan type.

For example, the integrate_ode_rk45 function can be used to integrate differential equations in Stan:

functions {
  real[] foo(real t, real[] y, real[] theta, real[] x_r, int[] x_i) {
int<lower=1> T;
real y0[2];
real t0;
real ts[T];
real theta[1];
real x_r[0];
int x_i[0];
real y_hat[T, 2] = integrate_ode_rk45(foo, y0, t0, ts, theta, x_r, x_i);

The function argument is foo, the name of the user-defined function; as shown in the higher-order functions table, foo takes a real, three more real arrays, and an integer array as arguments and returns a real array.

Functions Passed by Reference

The function argument to higher-order functions is always passed as the first argument. This function argument must be provided as the name of a user-defined or built-in function. No quotes are necessary.

Data-Restricted Arguments

Some of the arguments to higher-order functions are restricted to data. This means they must be expressions containing only data variables, transformed data variables, or literals; the may contain arbitrary functions applied to data variables or literals, but must not contain parameters, transformed parameters, or local variables from any block other than transformed data.

For user-defined functions the qualifier data may be prepended to the type to restrict the argument to data-only variables.

  1. Internally, they are implemented as their own expression types because Stan doesn’t have object-level functional types (yet).