@@ -34,7 +34,7 @@ function `g: U -> real`, which returns a scalar, to a list of type
3434over the results. For instance, for a sequence of ``` x ``` values of
3535type ``` U ``` , ``` { x1, x2, ... } ``` , we might compute the sum:
3636
37- ``` g(x1) + g(x2) + ... `` `
37+ ` g(x1) + g(x2) + ... `
3838
3939In probabilistic modeling this comes up when there are $N$
4040conditionally independent terms in a likelihood. Because of the
@@ -71,10 +71,10 @@ call graph resulting in stable numerical evaluation. This version
7171requires setting a sensible tuning parameter for good performance.
7272
7373The 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 `
7676specifies 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
7878automatic aggregation is performed), the ` reduce_sum_static ` variant
7979requires setting a sensible ` grainsize ` for good performance as
8080explained in [ more detail below] ( #reduce-sum-grainsize ) .
@@ -156,9 +156,7 @@ for(i in 1:size(x)) {
156156
157157Logistic regression is a useful example to clarify both the syntax
158158and 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```
164162data {
@@ -177,7 +175,6 @@ model {
177175
178176In this model predictions are made about the ` N ` outputs ` y ` using the
179177covariate ` x ` . The intercept and slope of the linear equation are to be estimated.
180-
181178The key point to getting this calculation to use reduce summation, is recognizing that
182179the statement:
183180
@@ -194,13 +191,10 @@ for(n in 1:N) {
194191
195192Now it is clear that the calculation is the sum of a number of conditionally
196193independent 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```
206200functions {
@@ -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```
219213target += partial_sum(1, N, y, x, beta); // Sum terms 1 to N of the likelihood
220214```
221215
222216In this example, ` y ` was chosen to be sliced over because there
223217is 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```
232226int grainsize = 100;
@@ -237,7 +231,7 @@ target += reduce_sum(partial_sum, y,
237231
238232The reduce summation facility automatically breaks the sum into roughly ` grainsize ` sized pieces
239233and 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```
243237functions {
@@ -269,7 +263,7 @@ model {
269263
270264The ` grainsize ` is a recommendation on how large each piece of
271265parallel 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
273267point as automatic aggregation of partial sums are performed. However,
274268for the static version the ` grainsize ` defines the maximal size of the
275269partial sums, e.g. the static variant will split the input sequence
0 commit comments