title: Can't replicate RStan ESS code from Vehtari paper
body: <p>I am trying to replicate an ESS (effective sample size) calculation using the method of Vehtari et al. in: <a href="https://arxiv.org/pdf/1903.08008.pdf" rel="nofollow noreferrer">Rank-normalization, folding, and localization: An improved Rhat for assessing convergence of MCMC</a></p>
  
  <p>I am working from the code here:
  <a href="https://github.com/avehtari/rhat_ess/blob/master/code/monitornew.R" rel="nofollow noreferrer">https://github.com/avehtari/rhat_ess/blob/master/code/monitornew.R</a></p>
  
  <pre><code>  # Geyer's initial positive sequence
    rho_hat_t &lt;- rep.int(0, n_samples)
    t &lt;- 0
    rho_hat_even &lt;- 1
    rho_hat_t[t + 1] &lt;- rho_hat_even
    rho_hat_odd &lt;- 1 - (mean_var - mean(acov[t + 2, ])) / var_plus # 251
    rho_hat_t[t + 2] &lt;- rho_hat_odd
    while (t &lt; nrow(acov) - 5 &amp;&amp; !is.nan(rho_hat_even + rho_hat_odd) &amp;&amp;
           (rho_hat_even + rho_hat_odd &gt; 0)) {
      t &lt;- t + 2
      rho_hat_even = 1 - (mean_var - mean(acov[t + 1, ])) / var_plus # 256
      rho_hat_odd = 1 - (mean_var - mean(acov[t + 2, ])) / var_plus # 257
      if ((rho_hat_even + rho_hat_odd) &gt;= 0) {
        rho_hat_t[t + 1] &lt;- rho_hat_even
        rho_hat_t[t + 2] &lt;- rho_hat_odd
      }
    }
  </code></pre>
  
  <p>I can follow the code from the paper except when we get to equation  10 in the paper (calculating the cross-chain autocorrelation). The code (lines 251, 256 and 257) appears in the form:</p>
  
  <pre><code>1 - (mean_var - mean(acov[t + 1, ])) / var_plus
  </code></pre>
  
  <p>which is close to equation 10, except the missing the 's' terms from equation 10:</p>
  
  <p><img src="https://chart.googleapis.com/chart?cht=tx&amp;chl=%5Chat%7B%5Crho%7D_t%20%3D%201%20-%20%5Cfrac%7BW%20-%20%5Cfrac%7B1%7D%7BM%7D%5Csum_%7Bm%3D1%7D%5E%7BM%7Ds_m%5E2%5Chat%7B%5Crho%7D_%7Bt%2Cm%7D%7D%7B%5Cwidehat%7Bvar%7D%5E&plus;%7D" alt="\sqrt{foo}"></p>
  
  <p>I can't see anywhere in the code that this is somehow accounted for elsewhere in the way the calculation is being done. I have tried putting the 's' terms back into those lines of code and it makes a big difference to the final ESS value.</p>
  
  <p>Is anyone able to help me understand the discrepancy between paper and code?</p>
  
  <p>Thanks.</p>