tweag / monad-bayes

A library for probabilistic programming in Haskell.
MIT License
410 stars 62 forks source link

Generalize from double: with type family #177

Open reubenharry opened 2 years ago

netlify[bot] commented 2 years ago

Deploy Preview for monad-bayes ready!

Name Link
Latest commit 6cb62a008ff9c5a24fabea65a9838add7be4af40
Latest deploy log https://app.netlify.com/sites/monad-bayes/deploys/630cbc5f5c634e0009501197
Deploy Preview https://deploy-preview-177--monad-bayes.netlify.app
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

reubenharry commented 2 years ago

@mknorps @idontgetoutmuch If we still want HMC, we'll need to merge/rebase this branch: it generalizes from Double in a way that allows for automatic differentiation.

turion commented 2 years ago

@reubenharry What is missing? Do you need any help? We're working around statistics only supporting Double right now, right?

idontgetoutmuch commented 2 years ago

Do we need statistics? Would mwc-random be enough? I am sure @Shimuuar would accept a PR to generalise the types e.g. here https://hackage.haskell.org/package/mwc-random-0.15.0.2/docs/System-Random-MWC-Distributions.html provided we didn't affect performance.

BTW we should also check performance before and after generalising Double to Num a (or whatever).

turion commented 2 years ago

mwc-random also pins the implementations of the distributions to Double.

idontgetoutmuch commented 2 years ago

mwc-random also pins the implementations of the distributions to Double.

I understand but I think the maintainer would be sympathetic to generalising it provided we did not impact performance.

reubenharry commented 2 years ago

@turion So the ad library supports the inverse error function, so we can at least currently do normal distributions without statistics. Less so for the other distributions in the MonadSample class. I don't know if we mind the sampler working in terms of Double, but if we do, we'd indeed want to request a change to mwc-random or move to something else.

Shimuuar commented 2 years ago

I'm not against generalization but it's kind of tricky. Some generators would be easy: exponential do not require anything beyond log. Others may be trickier. Generator for normal distribution uses ziggurat method and corresponding tables are only available for Doubles, some may use math-functions which provide only versions for Doubles as well

turion commented 2 years ago

Generator for normal distribution uses ziggurat method and corresponding tables are only available for Doubles

Why is that? It seems to me that the table is calculated in Haskell as well, so one could calculate several tables, possibly creating a type class for this.

Shimuuar commented 2 years ago

Why ziggurat? It's fast. Last time I run benchmarks (several years before random 1.2) it was only tens of percent slower than generation of Double. I didn't compare it with inversion method though.

Creating type class is possible but that's road leads to type class per distribution.