Git Product home page Git Product logo

deanie's Introduction

deanie

MIT License

deanie is an embedded probabilistic programming language. It can be used to denote, sample from, and perform inference on probabilistic programs.

Usage

Programs are written in a straightforward monadic style:

mixture :: Double -> Double -> Program Double
mixture a b = do
  p      <- beta a b
  accept <- bernoulli p
  if   accept
  then gaussian (negate 2) 0.5
  else gaussian 2 0.5

You can sample from them by first converting them into an RVar from random-fu:

> sample (rvar (mixture 1 3))

Sample many times from models using standard monadic combinators like 'replicateM':

> replicateM 1000 (sample (rvar (mixture 1 3)))

Or convert them to measures using a built-in interpreter:

> let nu = measure (mixture 1 3)
> let f = cdf nu

You can perform inference on models using rejection or importance sampling, or use a simple, stateful Metropolis backend. Here's a simple beta-bernoulli model, plus some observations to condition on:

betaBernoulli :: Double -> Double -> Program Bool
betaBernoulli a b = do
  p <- beta a b
  bernoulli p

observations :: [Bool]
observations = [True, True, False, True, False, False, True, True, True]

Here's one way to encode a posterior via rejection sampling:

rposterior :: Double -> Double -> Program Double
rposterior a b =
    grejection
      (\xs ys -> count xs == count ys)
      observations (beta a b) bernoulli
  where
    count = length . filter id

Here's another, via importance sampling:

iposterior :: Double -> Double -> Program (Double, Double)
iposterior a b =
  importance observations (beta a b) logDensityBernoulli

There are also some Monte Carlo convenience functions provided, such as a weighted average for weighted samples returned via importance sampling:

> samples <- replicateM 1000 (sample (rvar (iposterior 1 1)))
> print (mcw samples)
0.6369246537796793

Background

You can read about some of the theory and ideas behind this kind of language in some blog posts I've written.

deanie's People

Contributors

jtobin avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.