-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Hong Ge
committed
Aug 22, 2016
1 parent
3fe5441
commit b783424
Showing
7 changed files
with
128 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": true | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"#DP mixture of Gaussians as used in Anglican\n", | ||
"using Turing, Distributions\n", | ||
"eval(Turing, :(debug_level=-1))\n", | ||
"\n", | ||
"data = [1.0, 1.1, 1.2, -10, -15, -20, 0.01, 0.1, 0.05, 0]\n", | ||
"N = length(data)\n", | ||
"\n", | ||
"\n", | ||
"@model dpmixa begin\n", | ||
" @assume ϕ ~ Inf[Normal(0, 10)]\n", | ||
"\n", | ||
" dprintln(0, ϕ)\n", | ||
" urn = CRP(1.72)\n", | ||
" μ, classes = tzeros(N), tzeros(Int, N)\n", | ||
" for i in 1:N\n", | ||
" classes[i] = rand!(urn)\n", | ||
" μ[i] = ϕ[classes[i]]\n", | ||
" @observe data[i] ~ Normal(μ[i], 1)\n", | ||
" end\n", | ||
" K = length(unique(classes))\n", | ||
"\n", | ||
" @predict K μ\n", | ||
"end\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"collapsed": true | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"# Collect 50 samples from Particle Gibbs sampler\n", | ||
"@time resulta = sample(dpmixa, PG(20, 50))\n", | ||
"\n", | ||
"macroexpandTuring.TURING[:modelex]" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Julia 0.4.6", | ||
"language": "julia", | ||
"name": "julia-0.4" | ||
}, | ||
"language_info": { | ||
"file_extension": ".jl", | ||
"mimetype": "application/julia", | ||
"name": "julia", | ||
"version": "0.4.6" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
## a lazy, infinite collection of iid params | ||
type IArray | ||
distr :: Distribution | ||
vals :: Dict{Int,Any} | ||
end | ||
|
||
DD = Union{Distributions.Distribution{Distributions.Univariate, Distributions.ValueSupport}, | ||
Distributions.Distribution{Distributions.Multivariate, Distributions.ValueSupport}, | ||
Distributions.Distribution{Distributions.Matrixvariate, Distributions.ValueSupport}, | ||
ConjugatePriors.NormalGamma, ConjugatePriors.NormalWishart, | ||
ConjugatePriors.NormalInverseGamma, ConjugatePriors.NormalInverseWishart} | ||
|
||
IArray(distr::Distribution) = IArray(distr, Dict{Int,Any}()) | ||
IArray(distr::Distribution, vals::Dict{Int,Any}) = IArray(distr, vals) | ||
Distributions.logpdf(x :: IArray, t :: Bool) = mapreduce(v -> logpdf(x.distr, v, t), +, values(x.vals)) | ||
Distributions.logpdf(d :: DD, x :: IArray, t :: Bool) = logpdf(x :: IArray, t :: Bool) | ||
|
||
|
||
function Base.getindex(x :: IArray, i) | ||
global sampler | ||
if i in keys(x.vals) | ||
x.vals[i] | ||
else | ||
x.vals[i] = rand(current_trace(), x.distr) | ||
end | ||
end | ||
|
||
# This function is not compitable with replaying. | ||
# Base.setindex!(x :: IArray, val, key) = ( x.vals[key] = val ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters