hyunjimoon / DataInDM

Workflow iteration starting from 15.879
3 stars 0 forks source link

Parallelize loglik computation using reduce_sum #12

Open hyunjimoon opened 2 years ago

hyunjimoon commented 2 years ago

Write a new function in a for loop fashion so that loglik computation can be parallelized (using +'s associative). New component for function block, change ~ to += for model and gq block

//function
real partial_sum_lpmf(int[] slice_n_redcards,
                        int start, int end,
                        int[] n_games,
                        vector rating,
                        vector beta) {
    return binomial_logit_lupmf(slice_n_redcards |
                               n_games[start:end],
                               beta[1] + beta[2] * rating[start:end]);
  }
// model
...
  target += reduce_sum(partial_sum_lupmf, n_redcards, grainsize,
                       n_games, rating, beta);

Ref Start from Sebastian's Gelblog intro.

  1. A case study which adapts Richard McElreath’s intro to map_rect for reduce_sum
  2. User manual introduction to reduce_sum parallelism with a simple example as well: 23.1 Reduce-Sum
  3. Function reference: 9.4 Reduce-Sum Function