Skip to content

Commit 8831163

Browse files
committed
address review comments
1 parent df152e4 commit 8831163

File tree

2 files changed

+29
-35
lines changed

2 files changed

+29
-35
lines changed

src/functions-reference/higher-order_functions.Rmd

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ cat(' * <a href="functions-algebraic-solver.html">Algebraic Equation Solver</a>\
1111
cat(' * <a href="functions-ode-solver.html">Ordinary Differential Equation (ODE) Solvers</a>\n')
1212
cat(' * <a href="functions-1d-integrator.html">1D Integrator</a>\n')
1313
cat(' * <a href="functions-reduce.html">Reduce-Sum</a>\n')
14-
cat(' * <a href="functions-map.html">Higher-Order Map</a>\n')
14+
cat(' * <a href="functions-map.html">Map-Rect</a>\n')
1515
}
1616
```
1717

@@ -386,11 +386,11 @@ The gradients of the integral are computed in accordance with the Leibniz integr
386386
## Reduce-Sum Function {#functions-reduce}
387387

388388
Stan provides a higher-order reduce function for summation. A function
389-
`g: U -> real`, which returns a scalar, is mapped to every element of
390-
a list of type `U[]`, `{ x1, x2, ... }` and all the results are
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
391391
accumulated,
392392

393-
```g(x1) + g(x2) + ...```
393+
`g(x1) + g(x2) + ...`
394394

395395
For efficiency reasons the reduce function doesn't work with the
396396
element-wise evaluated function `g` itself, but instead works through
@@ -410,12 +410,12 @@ exactly. This implies that the order of summation determines the exact
410410
numerical result. For this reason, the higher-order reduce function is
411411
available in two variants:
412412

413-
* `reduce_sum`: Automatically forms partial sums resulting usually in good
414-
performance without further tuning.
415-
* `reduce_sum_static`: Creates for the same input always the same
416-
call graph resulting in stable numerical evaluation. This version
417-
requires setting a tuning parameter which controls the maximal size of partial
418-
sums formed.
413+
* `reduce_sum`: Compute partial sums automatically. This usually
414+
results in good performance without further tuning.
415+
* `reduce_sum_static`: For the same input, always create the same call
416+
graph. This results in stable numerical evaluation. This version
417+
requires setting a tuning parameter which controls the maximal size of
418+
partial sums formed.
419419

420420
### Specifying the Reduce-sum Function
421421

@@ -438,7 +438,7 @@ partial sums. `s1, s2, ...` are shared between all terms in the sum.
438438
partial sum operation. Refer to the [partial sum function](#functions-partial-sum).
439439
* *`x`*: array of `T`, one for each term of the reduction, `T` can be any type,
440440
* *`grainsize`*: recommended number of terms in each reduce call, set
441-
to one to estimate automatically for `reduce_sum` while for
441+
to 1 to estimate automatically for `reduce_sum` while for
442442
`reduce_sum_static` this determines the maximal size of the partial sums, type `int`,
443443
* *`s1`*: first (optional) shared argument, type `T1`, where `T1` can be any type
444444
* *`s2`*: second (optional) shared argument, type `T2`, where `T2` can be any type,

src/stan-users-guide/parallelization.Rmd

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function `g: U -> real`, which returns a scalar, to a list of type
3434
over the results. For instance, for a sequence of ```x``` values of
3535
type ```U```, ```{ x1, x2, ... }```, we might compute the sum:
3636

37-
```g(x1) + g(x2) + ...```
37+
`g(x1) + g(x2) + ...`
3838

3939
In probabilistic modeling this comes up when there are $N$
4040
conditionally independent terms in a likelihood. Because of the
@@ -71,10 +71,10 @@ call graph resulting in stable numerical evaluation. This version
7171
requires setting a sensible tuning parameter for good performance.
7272

7373
The tuning parameter is the so-called `grainsize`. For the
74-
`reduce_sum` version the `grainsize` is merely a suggested partial sum
75-
size while for the `reduce_sum_static` version the `grainsize`
74+
`reduce_sum` version, the `grainsize` is merely a suggested partial sum
75+
size, while for the `reduce_sum_static` version the `grainsize`
7676
specifies the maximal partial sum size. While for `reduce_sum` a
77-
`grainsize` of one commonly leads to good performance already (since
77+
`grainsize` of 1 commonly leads to good performance already (since
7878
automatic aggregation is performed), the `reduce_sum_static` variant
7979
requires setting a sensible `grainsize` for good performance as
8080
explained in [more detail below](#reduce-sum-grainsize).
@@ -156,9 +156,7 @@ for(i in 1:size(x)) {
156156

157157
Logistic regression is a useful example to clarify both the syntax
158158
and semantics of reduce summation and how it can be used to speed up a typical
159-
model.
160-
161-
A basic logistic regression can be coded in Stan as:
159+
model. A basic logistic regression can be coded in Stan as:
162160

163161
```
164162
data {
@@ -177,7 +175,6 @@ model {
177175

178176
In this model predictions are made about the `N` outputs `y` using the
179177
covariate `x`. The intercept and slope of the linear equation are to be estimated.
180-
181178
The key point to getting this calculation to use reduce summation, is recognizing that
182179
the statement:
183180

@@ -194,13 +191,10 @@ for(n in 1:N) {
194191

195192
Now it is clear that the calculation is the sum of a number of conditionally
196193
independent Bernoulli log probability statements, which is the condition where
197-
reduce summation is useful.
198-
199-
To use the reduce summation, a function must be written that can be used to compute
200-
arbitrary partial sums of the total sum.
201-
202-
Using the interface defined in [Reduce-Sum](#reduce-sum), such a function
203-
can be written like:
194+
reduce summation is useful. To use the reduce summation, a function
195+
must be written that can be used to compute arbitrary partial sums of
196+
the total sum. Using the interface defined in
197+
[Reduce-Sum](#reduce-sum), such a function can be written like:
204198

205199
```
206200
functions {
@@ -213,20 +207,20 @@ functions {
213207
}
214208
```
215209

216-
And the likelihood statement in the model can now be written:
210+
The likelihood statement in the model can now be written:
217211

218212
```
219213
target += partial_sum(1, N, y, x, beta); // Sum terms 1 to N of the likelihood
220214
```
221215

222216
In this example, `y` was chosen to be sliced over because there
223217
is one term in the summation per value of `y`. Technically `x` would have
224-
worked as well. Use whatever conceptually makes the most sense.
225-
226-
Because `x` is a shared argument, it is subset accordingly with `start:end`.
227-
228-
With this function, reduce summation can be used to automatically parallelize the
229-
likelihood:
218+
worked as well. Use whatever conceptually makes the most
219+
sense for a given model, e.g. slice over independent terms like
220+
conditionally independent observations or groups of observations as in
221+
hierarchical models. Because `x` is a shared argument, it is subset
222+
accordingly with `start:end`. With this function, reduce summation can
223+
be used to automatically parallelize the likelihood:
230224

231225
```
232226
int grainsize = 100;
@@ -237,7 +231,7 @@ target += reduce_sum(partial_sum, y,
237231

238232
The reduce summation facility automatically breaks the sum into roughly `grainsize` sized pieces
239233
and computes them in parallel. `grainsize = 1` specifies that the grainsize should
240-
be estimated automatically. The final model looks like:
234+
be estimated automatically. The final model looks as:
241235

242236
```
243237
functions {
@@ -269,7 +263,7 @@ model {
269263

270264
The `grainsize` is a recommendation on how large each piece of
271265
parallel work is (how many terms it contains). When using the
272-
non-static version, it is recommended to choose one as a starting
266+
non-static version, it is recommended to choose 1 as a starting
273267
point as automatic aggregation of partial sums are performed. However,
274268
for the static version the `grainsize` defines the maximal size of the
275269
partial sums, e.g. the static variant will split the input sequence

0 commit comments

Comments
 (0)