# Comparing theory and stan implementation on sbc

**URL:** <https://discourse.mc-stan.org/t/comparing-theory-and-stan-implementation-on-sbc/16913>\
**Category:** General\
**Tags:** simulation-based-calibration\
**Created:** [July 23, 2020, 8:28am UTC](https://discourse.mc-stan.org/t/comparing-theory-and-stan-implementation-on-sbc/16913 "2020-07-23T08:28:25Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![hyunji.moon](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/hyunji.moon/32/9781_2.png) [@hyunji.moon](https://discourse.mc-stan.org/u/hyunji.moon)\
**Post date:** [July 23, 2020, 8:28am UTC](https://discourse.mc-stan.org/t/comparing-theory-and-stan-implementation-on-sbc/16913/1 "2020-07-23T08:28:25Z")

</div>

I was comparing [rstan sbc.R](https://github.com/stan-dev/rstan/blob/develop/rstan/rstan/R/SBC.R#L49) and stan user manual(+paper) and had some questions:

**1. Rank consistency**  
vignettes has: `int ranks_[1] = {pi > pi_};`

but [user manual](https://mc-stan.org/docs/2_23/stan-users-guide/simulation-based-calibration-1.html) and original paper, Validating Bayesian Inference Algorithms with Simulation-Based Calibration, uses

\sum\_{m=1}^{M} \mathbf{I}\left[\theta\_{k}^{(n, m)}\<\theta\_{k}^{\operatorname{sim}(n)}\right]

Would it be better to use `int ranks_[1] = {pi < pi_};` as the paper shows diagnostic techniques, which depend on the inequality direction.

**2. M, N**  
[stan manual](https://mc-stan.org/docs/2_23/stan-users-guide/sbc-in-stan.html) explains:

- M: the number of posterior draws per simulated data set
- N: total number of simulated data sets and fits

How many times of stan fit is supposed to happen in sbc? MN or N?  
If the answer is N, does M in [code](https://github.com/stan-dev/rstan/blob/5a97a4674779d743b42c22a97b694697273180c8/rstan/rstan/R/SBC.R#L32), correspond to N in user guide, as M times of sampling is happening?

**3. n\_eff consideration**  
Considering n\_eff, suggested by the above paper, would the following code improvement be o.k? `if(n_eff/M >= 1-n_eff_reltol && n_eff/M <= 1+n_eff_reltol)` is what @avehtari suggested [here](https://discourse.mc-stan.org/t/simulation-based-calibration-case-study-with-rstan/8804/28).

- question: can M substituted as `iter_sampling`?
- concern: `n_eff/iter_sampling` might have different value, once thinned. However, in current frame, thinning is not happening because of anesthetic chain as @bgoodri suggested [here](https://discourse.mc-stan.org/t/simulation-based-calibration-case-study-with-rstan/8804/30).  
original

```
    out <- sampling(stanmodel, data, pars = c("ranks_", if (has_log_lik) "log_lik"), include = TRUE,
      chains = 1L, cores = 1L, seed = S, save_warmup = FALSE, thin = 1L, ...)

```

with n\_eff (cmdstanr code)

```
    iter_sampling = 1000
    while(TRUE){
      out <- stanmodel$sample(data, chains = 1, iter_sampling = iter_sampling, seed = floor(S), parallel_chains = 1, save_warmup = FALSE, thin = 1)
      n_eff <- out$summary("lp__") %>% pull("ess_bulk")
      if(n_eff/iter_sampling >= 1-n_eff_reltol && n_eff/iter_sampling <= 1+n_eff_reltol) break
      iter_sampling = 2 * iter_sampling
    }

```

.

---

<div class="post-metadata">

**Author:** ![bbbales2](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bbbales2/32/77_2.png) [@bbbales2](https://discourse.mc-stan.org/u/bbbales2)\
**Post date:** [July 24, 2020, 11:54pm UTC](https://discourse.mc-stan.org/t/comparing-theory-and-stan-implementation-on-sbc/16913/2 "2020-07-24T23:54:45Z")

</div>

> [@hyunji.moon](#):
>
> Would it be better to use `int ranks_[1] = {pi < pi_};`

I don’t know. @jonah do you know this?

> [@hyunji.moon](#):
>
> How many times of stan fit is supposed to happen in sbc?

N is the number of Stan fits. M is the number of draws in the manual.

> [@hyunji.moon](#):
>
> If the answer is N, does M in [code](https://github.com/stan-dev/rstan/blob/5a97a4674779d743b42c22a97b694697273180c8/rstan/rstan/R/SBC.R#L32)

It looks like M in that code is number of stan fits.

> [@hyunji.moon](#):
>
> can M substituted as `iter_sampling` ?

> [@hyunji.moon](#):
>
> concern: `n_eff/iter_sampling` might have different value, once thinned

I guess M is the number of draws after thinning, and iter\_sampling is the number of draws before thinning.

I think we still do thinning in SBC, just what Bgoodrich is saying is you might choose the amount of thinning after you do all the calculations (because it is expensive to rerun the calculations and it won’t be obvious how to thin before you do them).

---

<div class="post-metadata">

**Author:** ![Jean\_Billie](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jean_billie/32/9186_2.png) [@Jean\_Billie](https://discourse.mc-stan.org/u/Jean_Billie)\
**Post date:** [July 26, 2020, 3:16am UTC](https://discourse.mc-stan.org/t/comparing-theory-and-stan-implementation-on-sbc/16913/3 "2020-07-26T03:16:56Z")

</div>

> [@hyunji.moon](#):
>
> **1. Rank consistency**

I guess, you are correct. The inequality sign is the opposite between [the paper](https://arxiv.org/pdf/1804.06788.pdf) and Stan.

Even if the interpretation of the histogram of ranks depends on the inequality direction, the uniformity dose not depend on the inequality.  
However, when the histogram is not uniformly distributed, then maybe, we have to interpret it by modifying the paper’s manner according to the inequality. E.g., If a histogram gerated by `rstan::sbc()` has the asymmetry form in page 10 Fig 7 (b) of [the paper](https://arxiv.org/pdf/1804.06788.pdf), then the interpretation will be modified.

> [@hyunji.moon](#):
>
> How many times of stan fit is supposed to happen in sbc? MN or N?

I guess the anwer is the product of the size of posteriors and the size of ranks, because we can specify the number of posterior samples and the number of samples of rank statistics as following manner. For each single rank statistic, the size `iter` of posterior samples are drawn, and thus, for all rank statistics, it is the product.

```
rstan::sbc(
stanmodel = stanmodel,
      data = data,
          M = M, The sample size of rank statistics
        iter = iter, The sample size of the posteriors
 ....)

```

I am a beginner, so, …dont trust me. I am also interested in this topic.  
Please correct me if I am wrong.

---

<div class="post-metadata">

**Author:** ![jonah](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jonah/32/200_2.png) [@jonah](https://discourse.mc-stan.org/u/jonah)\
**Post date:** [July 27, 2020, 6:14pm UTC](https://discourse.mc-stan.org/t/comparing-theory-and-stan-implementation-on-sbc/16913/4 "2020-07-27T18:14:43Z")

</div>

> [@bbbales2](#):
>
> > [@hyunji.moon](#):
> >
> > Would it be better to use `int ranks_[1] = {pi < pi_};`
> 
> I don’t know. @jonah do you know this?

Good question @hyunji.moon. I’m not actually super familiar with the SBC implementation in RStan. I do think it would be better if the code and paper were consistent with each other, but I don’t think it’s wrong to do it either way. I’m not an SBC expert though! We can check with @bgoodri though as he implemented SBC in RStan. Was there a particular reason to do the inequality this way?

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [July 27, 2020, 6:44pm UTC](https://discourse.mc-stan.org/t/comparing-theory-and-stan-implementation-on-sbc/16913/5 "2020-07-27T18:44:37Z")

</div>

It doesn’t matter for whether the histograms (now freqpolys) look uniform.

---

<div class="post-metadata">

**Author:** ![hyunji.moon](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/hyunji.moon/32/9781_2.png) [@hyunji.moon](https://discourse.mc-stan.org/u/hyunji.moon)\
**Post date:** [July 30, 2020, 10:27pm UTC](https://discourse.mc-stan.org/t/comparing-theory-and-stan-implementation-on-sbc/16913/6 "2020-07-30T22:27:47Z")

</div>

> [@Jean\_Billie](#):
>
> I guess the anwer is the product of the size of posteriors and the size of ranks

In this case, the number of fit itself is N, right? We draw M samples from one fit which happens N times. Thanks for the reply.

---

<div class="post-metadata">

**Author:** ![hyunji.moon](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/hyunji.moon/32/9781_2.png) [@hyunji.moon](https://discourse.mc-stan.org/u/hyunji.moon)\
**Post date:** [July 30, 2020, 10:35pm UTC](https://discourse.mc-stan.org/t/comparing-theory-and-stan-implementation-on-sbc/16913/7 "2020-07-30T22:35:36Z")

</div>

I agree, but as I have mentioned above, ‘as the paper shows diagnostic techniques, which depend on the inequality direction’ the inequality direction might matter in some cases, the direction of bias for example.

---

<div class="post-metadata">

**Author:** ![Juan\_Ignacio\_de\_Oyarbide](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/juan_ignacio_de_oyarbide/32/7447_2.png) [@Juan\_Ignacio\_de\_Oyarbide](https://discourse.mc-stan.org/u/Juan_Ignacio_de_Oyarbide)\
**Post date:** [October 29, 2021, 2:58pm UTC](https://discourse.mc-stan.org/t/comparing-theory-and-stan-implementation-on-sbc/16913/8 "2021-10-29T14:58:08Z")

</div>

> [@hyunji.moon](#):
>
> ∑m=1I[θ(n,m)k\<θsim(n)k]

Hello everyone,

I am sorry I am jumping into this topic with a basic question, but why don’t we compute the rank statistics within the data averaged posterior? If we do it for each simulated dataset of SBC, wouldn’t the uniformity depend on the number of draws? If the number of draws goes to infinity the posterior should be centered on the ground truth that we sampled and then used to generate the data.

I am also following the example in the manual: [27.3 SBC in Stan | Stan User’s Guide](https://mc-stan.org/docs/2_28/stan-users-guide/sbc-in-stan.html), which is also confusing for me.

Thank you,
