You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Jacobian of the solution with respect to auxiliary parameters is
136
137
computed using the implicit function theorem. Intermediate Jacobians
137
-
(of the the algebraic function's output with respect to the unknowns y
138
+
(of the algebraic function's output with respect to the unknowns y
138
139
and with respect to the auxiliary parameters theta) are computed using
139
140
Stan's automatic differentiation.
140
141
@@ -382,8 +383,92 @@ Internally the 1D integrator uses the double-exponential methods in the Boost 1D
382
383
383
384
The gradients of the integral are computed in accordance with the Leibniz integral rule. Gradients of the integrand are computed internally with Stan's automatic differentiation.
384
385
386
+
## Reduce-Sum Function {#functions-reduce}
385
387
386
-
## Higher-Order Map {#functions-map}
388
+
Stan provides a higher-order reduce function for summation. A function
389
+
which returns a scalar `g: U -> real` is mapped to every element of a
390
+
list of type `U[]`, `{ x1, x2, ... }` and all the results are
391
+
accumulated,
392
+
393
+
`g(x1) + g(x2) + ...`
394
+
395
+
For efficiency reasons the reduce function doesn't work with the
396
+
element-wise evaluated function `g` itself, but instead works through
397
+
evaluating partial sums, `f: U[] -> real`, where:
398
+
399
+
```
400
+
f({ x1 }) = g(x1)
401
+
f({ x1, x2 }) = g(x1) + g(x2)
402
+
f({ x1, x2, ... }) = g(x1) + g(x2) + ...
403
+
```
404
+
405
+
Mathematically the summation reduction is associative and forming
406
+
arbitrary partial sums in an aribitrary order will not change the
407
+
result. However, floating point numerics on computers only have
408
+
a limited precision such that associativity does not hold
409
+
exactly. This implies that the order of summation determines the exact
410
+
numerical result. For this reason, the higher-order reduce function is
411
+
available in two variants:
412
+
413
+
*`reduce_sum`: Automatically choose partial sums partitioning based on a dynamic
414
+
scheduling algorithm.
415
+
*`reduce_sum_static`: Compute the same sum as `reduce_sum`, but partition
416
+
the input in the same way for given data set (in `reduce_sum` this partitioning
417
+
might change depending on computer load). This should result in stable
418
+
numerical evaluations.
419
+
420
+
### Specifying the Reduce-sum Function
421
+
422
+
The higher-order reduce function takes a partial sum function `f`, an array argument `x`
423
+
(with one array element for each term in the sum), a recommended
424
+
`grainsize`, and a set of shared arguments. This representation allows
Returns the equivalent of `f(1, size(x), x, s1, s2, ...)`, but computes
434
+
the result in parallel by breaking the array `x` into independent
435
+
partial sums. `s1, s2, ...` are shared between all terms in the sum.
436
+
437
+
**`f`*: function literal referring to a function specifying the
438
+
partial sum operation. Refer to the [partial sum function](#functions-partial-sum).
439
+
**`x`*: array of `T`, one for each term of the reduction, `T` can be any type,
440
+
**`grainsize`*: For `reduce_sum`, `grainsize` is the recommended size of the partial sum (`grainsize = 1` means pick totally automatically). For `reduce_sum_static`, `grainsize` determines the maximum size of the partial sums, type `int`,
441
+
**`s1`*: first (optional) shared argument, type `T1`, where `T1` can be any type
442
+
**`s2`*: second (optional) shared argument, type `T2`, where `T2` can be any type,
443
+
**`...`*: remainder of shared arguments, each of which can be any type.
444
+
445
+
### The Partial sum Function {#functions-partial-sum}
446
+
447
+
The partial sum function must have the following signature where the type `T`, and the
448
+
types of all the shared arguments (`T1`, `T2`, ...) match those of the original
The partial sum function returns the sum of the `start` to `end` terms (inclusive) of the overall
456
+
calculations. The arguments to the partial sum function are:
457
+
458
+
**`start`*, the index of the first term of the partial sum, type `int`
459
+
460
+
**`end`*, the index of the last term of the partial sum (inclusive), type `int`
461
+
462
+
**`x_subset`*, the subset of `x` a given partial sum is responsible for computing, type `T[]`, where `T` matches the type of `x` in `reduce_sum` (`reduce_sum_static`)
463
+
464
+
**`s1`*, first shared argument, type `T1`, matching type of `s1` in `reduce_sum` (`reduce_sum_static`)
465
+
466
+
**`s2`*, second shared argument, type `T2`, matching type of `s2` in `reduce_sum` (`reduce_sum_static`)
467
+
468
+
**`...`*, remainder of shared arguments, with types matching those in `reduce_sum` (`reduce_sum_static`)
469
+
470
+
471
+
## Map-Rect Function {#functions-map}
387
472
388
473
Stan provides a higher-order map function. This allows map-reduce
389
474
functionality to be coded in Stan as described in the user's guide.
0 commit comments