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]:
x (false,false,false) (false,false,true) (false,true,false) (false,true,true) (true,false,false) (true,false,true) (true,true,false) (true,true,true) -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 -1.00 -0.95 -0.90 -0.85 -0.80 -0.75 -0.70 -0.65 -0.60 -0.55 -0.50 -0.45 -0.40 -0.35 -0.30 -0.25 -0.20 -0.15 -0.10 -0.05 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 0.50 0.55 0.60 0.65 0.70 0.75 0.80 0.85 0.90 0.95 1.00 1.05 1.10 1.15 1.20 1.25 1.30 1.35 1.40 1.45 1.50 1.55 1.60 1.65 1.70 1.75 1.80 1.85 1.90 1.95 2.00 -1 0 1 2 -1.0 -0.9 -0.8 -0.7 -0.6 -0.5 -0.4 -0.3 -0.2 -0.1 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 p(x)

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]:
x false true -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 -1.00 -0.95 -0.90 -0.85 -0.80 -0.75 -0.70 -0.65 -0.60 -0.55 -0.50 -0.45 -0.40 -0.35 -0.30 -0.25 -0.20 -0.15 -0.10 -0.05 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 0.50 0.55 0.60 0.65 0.70 0.75 0.80 0.85 0.90 0.95 1.00 1.05 1.10 1.15 1.20 1.25 1.30 1.35 1.40 1.45 1.50 1.55 1.60 1.65 1.70 1.75 1.80 1.85 1.90 1.95 2.00 -1 0 1 2 -1.0 -0.9 -0.8 -0.7 -0.6 -0.5 -0.4 -0.3 -0.2 -0.1 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 p(x)

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]:
x false true -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 -1.00 -0.95 -0.90 -0.85 -0.80 -0.75 -0.70 -0.65 -0.60 -0.55 -0.50 -0.45 -0.40 -0.35 -0.30 -0.25 -0.20 -0.15 -0.10 -0.05 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 0.50 0.55 0.60 0.65 0.70 0.75 0.80 0.85 0.90 0.95 1.00 1.05 1.10 1.15 1.20 1.25 1.30 1.35 1.40 1.45 1.50 1.55 1.60 1.65 1.70 1.75 1.80 1.85 1.90 1.95 2.00 -1 0 1 2 -1.0 -0.9 -0.8 -0.7 -0.6 -0.5 -0.4 -0.3 -0.2 -0.1 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 p(x)

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]:
x false true -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 -1.00 -0.95 -0.90 -0.85 -0.80 -0.75 -0.70 -0.65 -0.60 -0.55 -0.50 -0.45 -0.40 -0.35 -0.30 -0.25 -0.20 -0.15 -0.10 -0.05 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 0.50 0.55 0.60 0.65 0.70 0.75 0.80 0.85 0.90 0.95 1.00 1.05 1.10 1.15 1.20 1.25 1.30 1.35 1.40 1.45 1.50 1.55 1.60 1.65 1.70 1.75 1.80 1.85 1.90 1.95 2.00 -1 0 1 2 -1.0 -0.9 -0.8 -0.7 -0.6 -0.5 -0.4 -0.3 -0.2 -0.1 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 p(x)
In [15]: