Category Archives: Julia

Tupper’s self-referential formula in Julia

I was surprised when I came across on Tupper’s formula on twitter. I felt the compulsion to implement it in Julia.

The formula is expressed as

{1\over 2} < \left\lfloor \mathrm{mod}\left(\left\lfloor {y \over 17} \right\rfloor 2^{-17 \lfloor x \rfloor - \mathrm{mod}(\lfloor y\rfloor, 17)},2\right)\right\rfloor

and yields bitmap facsimile of itself.

In [1]:
k=big"960 939 379 918 958 884 971 672 962 127 852 754 715 004 339 660 129 306 651 505 519 271 702 802 395 266 424 689 642 842 174 350 718 121 267 153 782 770 623 355 993 237 280 874 144 307 891 325 963 941 337 723 487 857 735 749 823 926 629 715 517 173 716 995 165 232 890 538 221 612 403 238 855 866 184 013 235 585 136 048 828 693 337 902 491 454 229 288 667 081 096 184 496 091 705 183 454 067 827 731 551 705 405 381 627 380 967 602 565 625 016 981 482 083 418 783 163 849 115 590 225 610 003 652 351 370 343 874 461 848 378 737 238 198 224 849 863 465 033 159 410 054 974 700 593 138 339 226 497 249 461 751 545 728 366 702 369 745 461 014 655 997 933 798 537 483 143 786 841 806 593 422 227 898 388 722 980 000 748 404 719"
setprecision(BigFloat,10000);

In the above, the big integer is the magic number that lets us generate the image of the formula. I also need to setprecision of BigFloat to be very high, as rounding errors using the default precision does not get us the desired results. The implementation was inspired by the one in Python, but I see Julia a great deal more concise and clearer.

In [2]:
function tupper_field(k)
    field=Array{Bool}(17,106)
    for (ix,x) in enumerate(0.0:1:105.0), (iy,y) in enumerate(k:k+16)
        field[iy,107-ix]=1/2<floor(mod(floor(y/17)*2^(-17*floor(x)-mod(floor(y),17)),2))
    end
   field 
end
In [3]:
f=tupper_field(k);
using Images
img = colorview(Gray,.!f)
Out[3]:

I just inverted the boolean array here to get the desired bitmap output.

 

Sampling variation in effective sample size estimates (MCMC)

By: Tamás K. Papp

Re-posted from: https://tamaspapp.eu/post/ess-sampling/

Introduction

MCMC samples, used in Bayesian statistics, are not independent — in fact, unless one uses specialized methods or modern HMC, posterior draws are usually at highly autocorrelated. For independent draws,

\[
\text{variance of simulation mean} \propto \frac1N
\]

where \(N\) is the sample size, but for correlated draws, one has to scale the sample size with a factor

\[
\tau = \frac{1}{1+2\sum_{k=1}^\infty \rho_k}
\]

where \(\rho_k\) is the lag-\(k\) autocorrelation. \(\tau N\) is the effective sample size.

Usually, \(\rho_k\) is estimated from the data using the variogram

\[
V_k = \frac{1}{N-k} \sum_{i=1}^{N-k} x_i x_{i+k}
\]

from which we obtain

\[
\rho_k = 1-\frac{V_k}{2\text{var}(x)}
\]

where an estimator for the variance is also used. Then, to avoid using noisy estimates, we only add up to the last \(K\) where

\[
\rho_{K} + \rho_{K+1} \ge 0
\]

I will call \(K\) the last lag. Stan does something slightly different, using FFT for autocorrelations, and cutting off at the first negative \(\rho_K\), but for HMC this does not make a whole lot of difference.

The sampling variation

I was coding up the above calculation, and needed some unit tests. Surprisignly, I could not find anything on the sampling variation of \(\tau\), so I wrote some simulations in Julia (source code for everything). I did the following simulation exercise:

  1. for a given autocorrelation coefficient \(\phi\), simulate \(N\) draws from the AR(1) process
    \(
    x_t = \phi x_{t-1} + \sigma \epsilon_t
    \qquad
    \epsilon_t \sim \text{Normal}(0,1), \text{IID}
    \)
  2. calculate \(\tau\) and \(K\),
  3. repeat 1000 times and plot the results.

I use \(N=1000\) and \(N=10000\), as these would be typical sample sizes, first for a fairly efficient algorithm, then for a more stubborn but still manageable posterior.

IID samples

Let \(\phi=0\), then we expect \(\tau=1\) (red line in histogram, coefficient of variation on top).

Results with \(\phi=0\) (IID), \(N=1000\). (a) \(\tau\), (b) last lag \(K\), (c) scatterplot.

Results with $$\phi=0$$ (IID), $$N=1000$$. (a) $$\tau$$, (b) last lag $$K$$, (c) scatterplot.

Results with \(\phi=0\) (IID), \(N=10000\). (a) \(\tau\), (b) last lag \(K\), (c) scatterplot.

Results with $$\phi=0$$ (IID), $$N=10000$$. (a) $$\tau$$, (b) last lag $$K$$, (c) scatterplot.

With \(1000\) samples, there is a lot of variation in ESS: 800 could show up very easily in practice. \(600\) is not improbable either. Using up to \(10\) lags is not uncommon. For \(10000\) samples, the precision is improved considerably, we commonly use \(2\) or \(4\) lags. For both sample sizes, notice the high correlation between the last lag \(K\), and \(\tau\): given the method above, using more lags increases \(\tau^{-1}\), so this is to be expected.

AR(1) samples with \(\rho=0.5\)

This is a more autocorrelated process, here theory tells us that \(\tau\)=1/3.

Results with \(\phi=0.5\), \(N=1000\). (a) \(\tau\), (b) last lag \(K\), (c) scatterplot.

Results with $$\phi=0.5$$, $$N=1000$$. (a) $$\tau$$, (b) last lag $$K$$, (c) scatterplot.

Results with \(\phi=0.5\), \(N=10000\). (a) \(\tau\), (b) last lag \(K\), (c) scatterplot.

Results with $$\phi=0.5$$, $$N=10000$$. (a) $$\tau$$, (b) last lag $$K$$, (c) scatterplot.

Notice that \(\tau\) is now more dispersed, compared to the IID case. Even with 10000 samples, the coefficient of variation is 6%, with 1000 it is around 1/6. In practice, expect effective sample sizes all over the place.

AR(1) samples with \(\rho=0.8\)

This is an even more autocorrelated process, here theory tells us that \(\tau\)=1/9.

Results with \(\phi=0.8\), \(N=1000\). (a) \(\tau\), (b) last lag \(K\), (c) scatterplot.

Results with $$\phi=0.8$$, $$N=1000$$. (a) $$\tau$$, (b) last lag $$K$$, (c) scatterplot.

Results with \(\phi=0.8\), \(N=10000\). (a) \(\tau\), (b) last lag \(K\), (c) scatterplot.

Results with $$\phi=0.8$$, $$N=10000$$. (a) $$\tau$$, (b) last lag $$K$$, (c) scatterplot.

There is now so much variation that in order to get an estimate for ESS that we can use for comparing various MCMC implementations, we need to run much more than \(1000\) samples.

Conclusion

  1. For unit testing ESS calculations, I will need to use 10000 samples, with \(\pm10\) or similar error bands.

  2. As a rule of thumb, I would ignore less than 1.5x variation in ESS for 1000 samples, or run longer chains: it may be just random noise.

Bibliography

  • Gelman, Andrew, et al. 2013. Bayesian data analysis. 3rd edition. Chapman & Hall/CRC.
  • Stan Development Team. 2016. Stan Modeling Language Users Guide and Reference Manual, Version 2.15.0. http://mc-stan.org

Blog redesign

By: Tamás K. Papp

Re-posted from: https://tamaspapp.eu/post/blog-redesign/

I am in the process of rebuilding my personal website using Hugo. I tried various themes, including hugo-academic, but in the process of adapting them to my needs I realized that it is less work to write one from scratch.

The result is now 80% ready (blog works, automatic listing of research papers will take some more work), and the source is on Github. It is available under the CC-BY-SA license, feel free to adapt parts from it, not that there is anything special in there.

Hugo is really an excellent framework, it is clean, logical, and allows a lot of code reuse. I wasted spent most of the time on fiddling with CSS, and I am still not 100% satisfied with the result, but at some point I decided to stop. SCSS was extremely useful for writing organized CSS.

I am especially satisfied with moving the code highlighting to the generator side. The only non-static parts are now Disqus and MathJax.