3 ms·
Working through this and other sampling algorithms was a real eye-opening experience for me, and really helped me develop an intuition for statistics in general
by mokus 11y ago
Working through this and other sampling algorithms was a real eye-opening experience for me, and really helped me develop an intuition for statistics in general. It was definitely worthwhile. I'm still rather proud of some code I wrote back in 2009 to implement the Ziggurat algorithm in an unusually general way[1]. It's in Haskell so I apologize in advance for the limited audience, but it's rather well documented (to my surprise... I don't always find that to be the case in my old code, or even my not-so-old stuff).
I always especially liked the fact that it constructed its table lazily and could do so given nothing but a handful of functions and a constant (the PDF itself, its inverse, its definite integral from 0, and the limit of that integral at infinity). From those pieces, it builds the tables and an algorithm to sample, and optionally also recursively transforms those inputs as necessary to lazily construct another ziggurat representation of the tail, ad infinitum. Basically, you can throw any monotone PDF you want at it and not have to worry about solving or approximating the tail analytically.
[1]https://github.com/mokus0/random-fu/blob/master/random-fu/src/Data/Random/Distribution/Ziggurat.hs https://github.com/mokus0/random-fu/blob/master/random-fu/sr...
- redcalx 11y agoVery cool. Note that my C# version is fixed to use 128 segments based on George Marsaglia's original calculations; Calculating them dynamically for a given PDF is an impressive achievement indeed. E.g. I did wonder if there was a more optimal number of segments, and of course yours isn't limited to the Gaussian.