SLIDE 1
Func%onal Probabilis%c Programming CUFP 2013 Avi Pfeffer - - PowerPoint PPT Presentation
Func%onal Probabilis%c Programming CUFP 2013 Avi Pfeffer - - PowerPoint PPT Presentation
Func%onal Probabilis%c Programming CUFP 2013 Avi Pfeffer Charles River Analy2cs apfeffer@cra.com Outline What is probabilistic programming? History Our Figaro language
SLIDE 2
SLIDE 3
Suppose you have some information
E.g., Brian ate pizza last night
You want to answer some questions based on this information
Is Brian a student? Is Brian a programmer?
There is uncertainty in the answers
3
The Problem
SLIDE 4
Create a joint probability distribution over the variables
P(Pizza, programmer, student) Either directly or by learning it from data
Assert the evidence
Brian ate pizza
Use probabilistic inference to get the answer
P(student, programmer | pizza)
4
Probabilistic Modeling
SLIDE 5
Probabilistic models in which variables are generated in order
Later variables can depend on earlier variables
Large number of variants, e.g.
Bayesian networks Hidden Markov models Probabilistic context free grammars Kalman filters Probabilistic relational models
5
Generative Models
SLIDE 6
Developing a new model requires implementing Representation Inference algorithm Learning algorithm
All three are significant challenges
Considered paper worthy
6
Building Generative Models
Can ¡we ¡make ¡this ¡easier? ¡
SLIDE 7
Expressive representation language
Capture wide variety of probabilistic models
Built-in inference and learning algorithms
Automatically apply to models written in the language
7
Probabilistic Programming Systems
SLIDE 8
Ordinary functional language: an expression describes a computation that produces a value let student = true in let programmer = student in let pizza = student && programmer in (student, programmer, pizza) Functional probabilistic programming language: an expression describes a random computation that produces a value let student = flip(0.7) in let programmer = if (student) flip(0.2) else flip(0.1) in let pizza = if (student && programmer) flip(0.9) else flip(0.3) in (student, programmer, pizza)
8
Functional Probabilistic Programming
SLIDE 9
let student = flip(0.7) in let programmer = if (student) flip(0.2) else flip(0.1) in let pizza = if (student && programmer) flip(0.9) else flip(0.3) in (student, programmer, pizza) Imagine running this program many times Each run generates a sample outcome In each run, each outcome has some probability of being generated The program defines a probability distribution over outcomes
9
Sampling Semantics
SLIDE 10
Turing complete language + probabilistic primitives
Naturally express wide range of probabilistic models
A number of general purpose algorithms have been developed
Structured variable elimination Markov chain Monte Carlo Importance sampling Factor graph compilation
10
Power of Functional Probabilistic Programming
SLIDE 11
PPLs aim to “democratize” model building
One should not need extensive training in ML or AI to build and code a model
This means that a PPL should (broadly) satisfy two main goals:
Usability
Intuitive to use Common design patterns easily expressed Integration into other/existing applications Extensible language Extensible reasoning
Power
Ability to represent a wide variety of models, data, etc Powerful and practical inference techniques
Making Probabilistic Programming Practical
11
SLIDE 12
With Daphne Koller and David McAllester, we first formulated the idea of probabilistic programming Lisp + flip Convoluted inference algorithm
Later found to be buggy
12
History | KMP 97
SLIDE 13
Representation
First practical probabilistic programming language OCaml like syntax Implemented in Ocaml
Inference
Exact inference using structured variable elimination Later implemented intelligent importance sampling
Limitations
Hard to integrate with applications and data No continuous variables
13
History | IBAL (2000-2007)
SLIDE 14
Representation
Embedded DSL in Scala Allows distributions over any data type Highly expressive constraint system also allows it to express non- generative models
Inference
Extensible library of inference algorithms Contains many of the most popular probabilistic inference algorithms, generalized to probabilistic programs
E.g., variable elimination, Metropolis-Hastings, particle filtering
New version to be released shortly
Parameter learning Decision making Improved algorithms
14
History | Figaro (2009-Present)
SLIDE 15
Goals of the Figaro Language
Implement a PPL in a widely-used language
Scala is widely-used Scala interoperability with Java also gives Figaro access to an even larger library
Provide a language to describe models with interacting components Object-oriented Provide a means to expressed directed and undirected models with general constraints Functional Extensibility and reuse of inference algorithms Object-oriented, traits Using Scala helps achieve all of these goals!
15
SLIDE 16
Element[T] is class of probabilistic models over type T Atomic elements Constant[T], Flip, Uniform, Geometric Compound elements built out of other elements If(Flip(0.8), Constant(0.5), Uniform(0,1))
16
Basic Figaro Concepts
SLIDE 17
Constant[T] is the monadic unit Chain[T,U] implements monadic bind
Use an Element[T] to generate T Apply a function to the T to generate an Element[U] Generate a U from the Element[U]
Chain(Uniform(0,1), (d: Double) => Normal(d, 0.5)) Apply[T,U] implements monadic fmap Apply(Uniform(0,1), (d: Double) => d * 2) Most Figaro compound elements implemented using monad
E.g., If
17
The Probability Monad
SLIDE 18
Any Element[T] can have conditions and constraints Condition: function from T T to Boolean
Specifies a property that must be satisfied for a value to have positive probability
Constraint: function from T to Double
Weights probability of value
Two purposes
Asserting evidence Specifying new kinds of models including undirected models
18
Conditions and Constraints
SLIDE 19
19
Example 1: Probabilistic Processes on Graphs
Google’s PageRank is a model of a probabilistic process on a graph
Directed edge from page A to page B if A links to B
Consider a random walk starting at any point in the graph
What is the probability a node will be reached in n steps?
SLIDE 20
Start by defining some data structures for a webpage graph
20
Random Walk in Figaro
class Edge(from: Int, to: Int) class Node(ID: int, edges: Set[Edge]) class Graph(nodes: Set[Nodes]) { def get(id: Int) = // return Node with ID == id } // function that randomly builds a graph given some params def graphGenProcess(params*): Element[Graph]
Define some parameters of the random walk
val numSteps: Element[Int] = Constant(10) val inputGraph: Element[Graph] = graphGenProcess(…) val startNode: Element[Int] = Uniform(inputGraph.nodes)
SLIDE 21
21
Random Walk in Figaro
// randomly move forward from a node def step(last: Int, g: Graph): Element[Int] = Uniform(g(last).edges.map(e => e.to)) val rWalk = Chain(inputGraph, numSteps, startNode, rFcn) def rFcn(g: Graph, remain: Int, n: Int): Element[List[Int]] = { if (remain == 1) Apply(step(n, g), (i: Int) => List(i)) else { val prev = rFcn(g, remain-1, n) val curr = step(Apply(prev, (l: List[Int]) => l.head), g) Apply(curr, prev, (i: Int, l: List[Int]) => I :: l) } }
SLIDE 22
People smoke with probability 0.6 Friends are 3 times as likely to have the same smoking habit than different Alice is friends with Bob, Bob is friends with Clara Alice smokes What is the probability that Clara smokes? Want a general solution that works for any friends network
Example 2: Network Analysis
SLIDE 23
Friends and Smokers | General Solution
// A per person
- n smok
mokes es wit ith h pr proba
- babilit
bility 0.6 0.6 clas lass Per erson
- n { val
al smok mokes es = Flip lip(0.6) 0.6) } // Friends iends ar are e thr hree ee times imes as as lik likel ely to
- ha
have e the he same ame // smoking moking ha habit bit than han dif differ erent ent def def cons constraint aint(pair pair: : (Boolean,
- olean, Boolean)
- olean)) =
if if (pair pair._1 ._1 == == pair pair._2 ._2) 3.0; 3.0; els else e 1.0 1.0 // Appl pply the he cons constraint aints to
- all
all pair pairs of
- f friends
iends def def appl pplyCons
- nstraint
aints(friends iends: : Lis List[Per erson]
- n]) {
for
- r { (p1,p2
p1,p2) ) ← friends iends } { (p1.s p1.smok mokes es ^^ ^^ p2.s p2.smok mokes es). ).ad addC dCons
- nstraint
aint(cons constraint aint) } } } }
SLIDE 24
// Set etting ing up up the he sit itua uation ion val al alice, alice, bob, bob, clar lara a = new new Per erson
- n
val al friends iends = Lis List((alice, alice, bob) bob), , (bob, bob, clar lara) a)) appl pplyCons
- nstraint
aints(friends iends) alice.s alice.smok mokes es.condit .condition( ion(true) ue) // Running unning inf infer erence ence and and quer querying ing val al algor algorit ithm hm = Var aria iableE bleElimina limination ion(clar lara.s a.smok mokes es) algor algorit ithm. hm.star art() () pr print intln( ln(algor algorit ithm. hm.pr proba
- babilit
bility(clar lara.s a.smok mokes es, , true) ue))
24
Friends and Smokers | Specific Situation
SLIDE 25
We observe an object (e.g. a vehicle on a road) We want to know what type of object it is We have some observations about it Inheritance hierarchies are a natural fit
25
Example 3: Hierarchical Reasoning
SLIDE 26
Every element
Has a name Belongs to an element collection
These are implicit arguments
A reference is a sequence of names
e.g., vehicle1.size
Starting with an element collection, you can get to the element associated with a reference
Go through sequence of nested element collections
There may be uncertainty in the identity of a reference
E.g., you don’t know what vehicle1 is Figaro always resolves the reference to the actual element in any given world
26
Referring to Elements
SLIDE 27
abs bstract act clas lass Vehic ehicle le ext xtends ends Element lementCollect
- llection
ion { val al siz ize: e: Element lement[Symbol] mbol] val al speed: peed: Element lement[Int nt] } clas lass Truc uck k ext xtends ends Vehic ehicle le { val al siz ize e = Select elect(0.25 0.25 ->
- > 'medium,
medium, 0.75 0.75 ->
- > 'big)
big)("s "siz ize", e", this his) val al speed peed = Unif Unifor
- rm(50,
50, 60, 60, 70) 70)("s "speed", peed", this his) } clas lass Pic ickup kup ext xtends ends Truc uck k {
- ver
erride ide val al speed peed = Unif Unifor
- rm(70,
70, 80) 80)("s "speed", peed", this his)
- ver
erride ide val al siz ize e = Cons
- nstant
ant('medium) medium)("s "siz ize", e", this his) } clas lass Twent entyWheeler heeler ext xtends ends Truc uck k … clas lass Car ar ext xtends ends Vehic ehicle le …
27
Defining the Class Hierarchy and Properties
SLIDE 28
- bject
- bject Vehic
ehicle le { def def gener generate( e(name: name: String) ing): : Element lement[Vehic ehicle] le] = Dist(0.6 -> Car.generate, 0.4 -> Truck.generate)(name, universe) }
- bject
- bject Truc
uck k { def def gener generate: e: Element lement[Vehic ehicle] le] = Dis ist(0.1 0.1 ->
- > Twent
entyWheeler heeler.gener .generate, e, 0.3 0.3 ->
- > Pic
ickup kup.gener .generate, e, 0.6 0.6 ->
- > Cons
- nstant
ant[Vehic ehicle] le](new new Truc uck) k)) }
- bject
- bject Pic
ickup kup { def def gener generate e … … }
- bject
- bject Twent
entyWheeler heeler { def def gener generate e … … }
- bject
- bject Car
ar { def def gener generate e … … }
28
Defining a Distribution Over Objects
SLIDE 29
val al my myVehic ehicle le = Vehic ehicle.gener le.generate( e("v "v1") 1") universe.assertEvidence(List(NamedEvidence("v1.size", Observation('medium))))
29
Instantiating and Observing Evidence
SLIDE 30
// Element representing the class name of the vehicle, // e.g. Truck val al clas lassName Name = shor hortClas lassName( Name(my myVehic ehicle) le) val al is isPic ickup kup = Appl pply(my myVehic ehicle, le, (v: : Vehic ehicle) le) => v.is .isIns nstanceOf anceOf[Pic ickup] kup]) val al alg alg = Var aria iableE bleElimina limination ion(is isPic ickup kup, , name) name) alg.start() println(alg.probability(isPickup, true) ue)) // Print int a a lis list of
- f clas
lass names names wit ith h their heir pr proba
- babilit
bilities ies println(alg.distribution(className).toList)
30
Querying The Model
SLIDE 31
Free and open-source, available now at www.cra.com/figaro
Tutorial available in release
Version 2.0 release imminent
Development will move to GitHub as of release https://github.com/p2t2
Contact me apfeffer@cra.com or figaro@cra.com
31