9.3 Higher-Order Map

Stan provides a higher-order map function. This allows map-reduce functionality to be coded in Stan as described in the user’s guide.

9.3.1 Specifying the Mapped Function

The function being mapped must have a signature identical to that of the function f in the following declaration.

 vector f(vector phi, vector theta,
          data real[] x_r, data int[] x_i);

The map function returns the sequence of results for the particular shard being evaluated. The arguments to the mapped function are:

  • phi, the sequence of parameters shared across shards

  • theta, the sequence of parameters specific to this shard

  • x_r, sequence of real-valued data

  • x_i, sequence of integer data

All input for the mapped function must be packed into these sequences and all output from the mapped function must be packed into a single vector. The vector of output from each mapped function is concatenated into the final result.

9.3.2 Rectangular Map

The rectangular map function operates on rectangular (not ragged) data structures, with parallel data structures for job-specific parameters, job-specific real data, and job-specific integer data.

vector map_rect(F f, vector phi, vector[] theta, data real[,] x_r, data int[,] x_i)
Return the concatenation of the results of applying the function f, of type (vector, vector, real[], int[]):vector elementwise, i.e., f(phi, theta[n], x_r[n], x_i[n]) for each n in 1:N, where N is the size of the parallel arrays of job-specific/local parameters theta, real data x_r, and integer data x_r. The shared/global parameters phi are passed to each invocation of f.