Git Product home page Git Product logo

stappl's Introduction

STAPPL: STAtically typed Probabilistic Programming Language

STAPPL is a compiler for the STAtically typed Probabilistic Programming Language.

How to Use

Install the necessary OCaml packages:

opam install . --deps-only

You can run the program using the following command:

dune exec -- stappl <program.stp>

This will parse and compile the program, perform inference, and save the distribution plot of the final query variable as a PNG image file.

You will also need a working Python environment with numpy and seaborn (and their dependencies), as seaborn is used to create the plot. It is recommend to create a Python virtual environment (optional), and then install numpy and seaborn inside the environment using pip:

pip install -U numpy seabron

Example

Here's an example program and its components:

Input Program

# student.stp
fun determine_grade(difficult, smart) {
  if difficult & smart then 0.8
    else if difficult & !smart then 0.3
    else if !difficult & smart then 0.95
    else 0.5
}

let difficult = sample(bernoulli(0.4)) in
let smart = sample(bernoulli(0.3)) in
let grade = bernoulli(determine_grade(difficult, smart)) in
let sat = bernoulli(
  if smart then 0.94 +. 0.01  # partial evaluate to 0.95
    else 0.2
) in
observe(grade, false);
observe(sat, true);
smart

Pretty Print (-pp)

((funs
  (((name determine_grade) (params (difficult smart))
    (body
     (If (And (Var difficult) (Var smart)) (Real 0.8)
      (If (And (Var difficult) (Not (Var smart))) (Real 0.3)
       (If (And (Not (Var difficult)) (Var smart)) (Real 0.95) (Real 0.5))))))))
 (exp
  (Assign difficult (Sample (Call bernoulli ((Real 0.4))))
   (Assign smart (Sample (Call bernoulli ((Real 0.3))))
    (Assign grade
     (Call bernoulli ((Call determine_grade ((Var difficult) (Var smart)))))
     (Assign sat
      (Call bernoulli
       ((If (Var smart) (Radd (Real 0.94) (Real 0.01)) (Real 0.2))))
      (Seq (Observe (Var grade) (Bool false))
       (Seq (Observe (Var sat) (Bool true)) (Var smart)))))))))

Graph Mode (-graph)

((vertices (X1 X2 X3 X4)) (arcs ((X1 X3) (X2 X3) (X2 X4)))
 (pmdf_map
  ((X1 (Call bernoulli ((Value 0.4)))) (X2 (Call bernoulli ((Value 0.3))))
   (X3
    (If (Value true)
     (Call bernoulli
      ((If (Bop && (Value true) (Bop && (Var X1) (Var X2))) (Value 0.8)
        (If
         (Bop && (Bop && (Value true) (Uop not (Bop && (Var X1) (Var X2))))
          (Bop && (Var X1) (Uop ! (Var X2))))
         (Value 0.3)
         (If
          (Bop &&
           (Bop && (Bop && (Value true) (Uop not (Bop && (Var X1) (Var X2))))
            (Uop not (Bop && (Var X1) (Uop ! (Var X2)))))
           (Bop && (Uop ! (Var X1)) (Var X2)))
          (Value 0.95) (Value 0.5))))))
     (Value 1)))
   (X4
    (If (Value true)
     (Call bernoulli
      ((If (Bop && (Value true) (Var X2)) (Value 0.95) (Value 0.2))))
     (Value 1)))))
 (obs_map ((X3 (Value false)) (X4 (Value true)))))

Inference Mode

student.png

License

STAPPL is available under the MIT license. See the LICENSE file for more info.

stappl's People

Contributors

zeta611 avatar yhs0602 avatar imgbot[bot] avatar limitepsilon avatar shapespeare avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

limitepsilon

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.