In [1]:
using Stochy, Stochy.GadflySupport, Gadfly, DataStructures

In [2]:
@pp function rainprior()
flip(0.3)
end

Out[2]:
rainprior (generic function with 1 method)
In [3]:
@pp function sprinklerprior()
flip(0.2)
end

Out[3]:
sprinklerprior (generic function with 1 method)
In [4]:
@pp function wetgrassprior(rain, sprinkler)
flip(
!rain && !sprinkler ? 0.05 :
!rain && sprinkler  ? 0.9  :
rain  && !sprinkler ? 0.9  :
rain  && sprinkler  ? 0.95 : :unreachable)
end

Out[4]:
wetgrassprior (generic function with 1 method)
In [5]:
@pp function model()
local
r = rainprior(),
s = sprinklerprior(),
w = wetgrassprior(r,s)
tuple(r,s,w)
end

Out[5]:
model (generic function with 1 method)
In [6]:
@pp model() # Generate a sample from the model.

Out[6]:
(false,false,false)

Joint Distribution¶

In [7]:
joint = @pp enum() do
model()
end

Out[7]:
Discrete({(false,true,false),(true,false,false),(true,true,false),(false,true,true),(false,false,false),(true,false,true),(false,false,true),(true,true,true)}, [0.014,0.024,0.003,0.126,0.532,0.216,0.028,0.057])
In [8]:
bar(joint)

Out[8]:

Marginalization¶

In [9]:
prior = @pp enum() do
local triple = model()
triple[1] # rain
end

Out[9]:
Discrete({false,true}, [0.7,0.3])
In [10]:
bar(prior)

Out[10]:

Conditioning¶

In [11]:
d1 = @pp enum() do
local triple = model()
factor(triple[3] ? 0 : -Inf) # Observe the grass is wet.
triple[1]
end

Out[11]:
Discrete({false,true}, [0.360656,0.639344])
In [12]:
bar(d1)

Out[12]:

Explaining Away¶

In [13]:
d2 = @pp enum() do
local triple = model()
factor(triple[3] && triple[2] ? 0 : -Inf) # Also observe that the sprinkler is on.
triple[1]
end

Out[13]:
Discrete({false,true}, [0.688525,0.311475])
In [14]:
bar(d2)

Out[14]:
In [15]: