Probability Monad

The Probability
Monad
prof. dr. ir. Tom Schrijvers
Pure Functional
Programming
2
Main Building Block in FP
A -> B
like
total functions
in Math
3
Main Building Block in FP
sum :: [Int] -> Int
sum []
= 0
sum (x:xs) = x + sum xs
4
Need a Partial Function?
Simulate it!
A -> Maybe B
data Maybe x
= Just x
| Nothing
5
Partial Function Example
data Maybe x
= Just x
| Nothing
lookup :: Eq k => [(k,v)] -> k -> Maybe v lookup [] k = Nothing
lookup ((k’,v):l) k
| k’ == k
= Just v
| otherwise = lookup l k
6
Non-Deterministic Function?
Simulate it!
A -> [B]
data [x]
= []
| x : [x]
7
Non-Determinism Example
data [x]
= []
| x : [x]
member :: Eq k => [(k,v)] -> k -> [v] member [] k = []
member ((k’,v):l)
| k’ == k
= v : member l k
| otherwise = member l k
8
Other Effect?
Simulate it!
A -> M B
9
Probabilistic Functional
Programming
10
Probabilistic Function
A -> Dist B
a
11
Example Primitive
·◀·▶· :: a -> Prob -> a -> Dist a
p
x◀p▶y
1-p
x
12
y
Example Primitive
·◀·▶· :: a -> Prob -> a -> Dist a
data Gender
= Male
| Female
0.5
gender :: Dist Gender
gender = Male◀0.5▶Female
0.5
♂
13
♀
Example Primitive
·◀·▶· :: a -> Prob -> a -> Dist a
biasedCoin :: Prob -> Dist Bool
biasedCoin p = False◀p▶True
14
Other Primitives
Discrete
uniform :: [a] -> Dist a
categorical :: [(a,Prob)] -> Dist a
Continuous
normal :: Double -> Double -> Dist Double
beta :: Double -> Double -> Dist Double
gamma :: Double -> Double -> Dist Double
15
Example Primitive
normal :: Double -> Double -> Dist Double
μ
σ
height :: Gender -> Dist Double
height Male
= normal 180 8
height Female = normal 170 6
♂
♀
16
Composition
17
Function Composition
(.) :: (b -> c) -> (a -> b) -> (a -> c)
f, g :: Int -> Int
f x = x + 1
g x = x * 2
h = g . f
18
> h 0
2
> h 1
4
Properties
id :: a -> a
id x = x
id . f
f . id
f . (g . h)
= f = f
= (f . g) . h
19
Function Application
(⌴) :: (a -> b) -> a -> b
> h
6
f :: Int -> Int
f x = x + 1
> f 4
5
h = f 5
20
Problem
f :: A -> Dist B g :: B -> Dist C
g . f
21
Problem
f :: A -> Dist B g :: B -> Dist C
g . f
ill-typed!!!
21
Problem
f :: A -> Dist B g :: B -> Dist C
g . f
ill-typed!!!
21
types
don’t
match
Kleisli Composition
(<.>) :: (b -> M c) -> (a -> M b) -> (a -> M c)
22
Kleisli Composition
(<.>) :: (b -> Dist c) -> (a -> Dist b) -> (a -> Dist c)
f, g :: Int -> Dist Int
f x =
x ◀0.5▶ x + 1
g x = x-1 ◀0.5▶ x
h = g <.> f
23
Kleisli Composition
(<.>) :: (b -> Dist c) -> (a -> Dist b) -> (a -> Dist c)
f, g :: Int -> Dist Int
f x =
x ◀0.5▶ x + 1
g x = x-1 ◀0.5▶ x
h = g <.> f
23
Kleisli Composition
(<.>) :: (b -> Dist c) -> (a -> Dist b) -> (a -> Dist c)
f, g :: Int -> Dist Int
f x =
x ◀0.5▶ x + 1
g x = x-1 ◀0.5▶ x
0.5
0.25
0.25
h = g <.> f
x-1 x x+1
23
Properties
return :: a -> Dist a
return x = x ◀0.5▶ x
1
x
return <.> f
f <.> return
f <.> (g <.> h)
= f = f
= (f <.> g) <.> h
24
Monadic Application
(>>=) :: M a -> (a -> M b) -> M b
25
Monadic Application
(>>=) :: Dist a -> (a -> Dist b) -> Dist b
gender :: Dist Gender
height :: Gender -> Dist Double
h :: Dist Double
h = gender >>= height
26
Monad Structure
<M,(>>=),return>
+ 3 properties
27
Example
data Person = P Gender Double
person :: Dist
person = gender
height g
return (P
Person
>>= (\g ->
>>= (\h ->
g h)))
28
inspired by A. Dries, SoICT 2015
Syntactic Sugar
data Person = P Gender Double
person :: Dist Person
person = do g <- gender
h <- height g
return (P g h)
29
Example: Replication
people :: Int -> Dist [Person]
people n = replicateM n person
replicateM :: Monad m => Int -> m a -> m [a]
replicateM 0 p = return []
replicateM n p = = do x <- p
xs <- replicateM (n-1) p
return (x:xs)
30
Probabilistic Branching
·◀·▶· :: a -> Prob -> a -> Dist a
∙◁∙▷∙ :: Dist a-> Prob -> Dist a
-> Dist a
m◁p▷n = (m◀p▶n) >>= id
31
Example
∙◁∙▷∙ :: Dist a-> Prob -> Dist a
-> Dist a
list :: Dist [Bool]
list =
return [] ◁ 0.7 ▷ (do h <- True ◀ 0.5 ▶ False
t <- list
return (h:t))
32
Evaluating Models
33
Distribution
Only for discrete distributions
distribution :: Dist a -> [(a,Prob)]
gender :: Dist Gender
gender = Male◀0.5▶Female
> distribution gender
[(Male,0.5),(Female,0.5)]
34
Sampling
Also for continuous distributions
sample :: StdGen -> Dist a -> a
gender :: Dist Gender
gender = Male◀0.5▶Female
> let rng = mkStdGen 0
> sample rng (replicateM 5 gender)
[Male,Male,Female,Male,Female]
35
Summary
36
Probability Monad
★
Purely functional model of
probabilistic programming
★
where the monadic structure
provides composition
37
Relevant Work
☞ Stochastic lambda calculus and monads of probability
distributions. Ramsey & Pfeffer. POPL 2002.
☞ Probabilistic functional programming in Haskell. Erwig &
Kollmansberger. Journal of Functional Programming,
2006.
☞ Just do It: Simple Monadic Equational Reasoning.
Gibbons & Hinze. ICFP 2011.
☞ Practical Probabilistic Programming with Monads.
Scibior et al. Haskell 2015.
38
Relevant Work
of worklambda
has been
put into
graphical
☞A lot
Stochastic
calculus
and combining
monads of probability
models
with first-order
Some
of the
distributions.
Ramsey & logic,
Pfeffer.…POPL
2002.
in this
category
include …
☞languages
Probabilistic
functional
programming
in ProbLog,…
Haskell. Erwig
&
Those
languagesJournal
are more
expressive
than
Kollmansberger.
of Functional
Programming,
graphical
2006. models, but they still can not express
as the
Dirichlet
Process.Reasoning.
☞models
Just dosuch
It: Simple
Monadic
Equational
Gibbons & Hinze. ICFP 2011.
☞ Practical Probabilistic Programming with Monads.
Scibior et al. Haskell 2015.
38
Recent Work
☞ Fixing Non-determinism. Alexander Vandenbroucke et
al. IFL 2015.
Functional model for:
Non-determinism + Tabling
39
Recent Work
☞ Fixing Non-determinism. Alexander Vandenbroucke et
al. IFL 2015.
Functional model for:
Non-determinism + Tabling
Functional model for:
Probabilities + Tabling?
39
vLambda
Questions?
prof. dr. ir. Tom Schrijvers