using DataStructures, Stochy
INFO: Load Gadfly before Stochy to extend plot function.
identity(function repeat(##7973,f::Function,n::Int64) (##7974->begin Stochy.Thunk(()->begin if ##7974 ##7973(list()) else f(##7975->begin Stochy.Thunk(()->begin (##7977->begin Stochy.Thunk(()->begin repeat(##7976->begin Stochy.Thunk(()->begin ##7973(cons(##7975,##7976)) end) end,f,##7977) end) end)(n - 1) end) end) end end) end)(n < 1) end)
Stochy.debug(false)
false
@pp function loop(f,n)
n > 0 && begin; f(); loop(f,n-1); end
end
loop (generic function with 1 method)
function loop2(k,f,n)
if n > 0
f() do _
loop2(k,f,n-1)
end
else
k(nothing)
end
end
loop2 (generic function with 1 method)
@pp begin
loop2(10) do
println("x")
end
:done
end
x x x x x x x x x x
:done
function repeat2(k,f,n)
if n==1
f(val->k(list(val)))
else
f(h->repeat2(t->k(cons(h,t)), f, n-1))
end
end
repeat2 (generic function with 1 method)
function repeat3(k,f,n)
ret = list()
for _ in 1:n
ret = cons(f(identity), ret)
end
k(ret)
end
repeat3 (generic function with 1 method)
Stochy.debug(false)
ret = @pp repeat2(10) do
flip() ? :head : :tail
end
println(typeof(ret))
ret
Cons{Symbol}
list(:tail, :head, :tail, :tail, :head, :head, :head, :tail, :head, :head)
obslist = @pp repeat2(()->:head, 10)
@time posterior = @pp pmcmc(200,200) do
local faircoin = flip(0.999) # Prior.
local coin = () -> flip(faircoin ? 0.5 : 0.95) ? :head : :tail
local flips = repeat2(coin, 10)
factor(flips == obslist ? 0 : -Inf)
#loop(10) do
# factor(coin() == :head ? 0 : -Inf)
#end
faircoin
end
elapsed time: 16.615144245 seconds (2688771928 bytes allocated, 16.79% gc time)
Empirical([false=>0.2238,true=>0.7762])
obsarray = [:head for _ in 1:10]
@time posterior = @pp pmcmc(1000,100) do
local faircoin = flip(0.999) # Prior.
local coin = () -> flip(faircoin ? 0.5 : 0.95) ? :head : :tail
local flips = [coin(),coin(),coin(),coin(),coin(),coin(),coin(),coin(),coin(),coin()]
factor(flips == obsarray ? 0 : -Inf)
faircoin
end
elapsed time: 51.466698294 seconds (8476414016 bytes allocated, 14.41% gc time)
Empirical([false=>0.38907,true=>0.61093])
exactposterior = @pp enum() do
local faircoin = flip(0.999)
observe(Bernoulli(faircoin ? 0.5 : 0.95),
true, true, true, true, true, true, true, true, true, true)
faircoin
end
Categorical([false=>0.3803139420033197,true=>0.6196860579966804])
@pp ~Categorical([0.5,0.5], [:head,:tail])
:tail
list(:head, :tail) == list()
false