Skip to content

Commit

Permalink
Add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
xukai92 committed Nov 15, 2016
1 parent 2ac4070 commit 06a65ef
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 6 deletions.
8 changes: 6 additions & 2 deletions doc/genrst.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,13 @@ to_gen = Dict(
:title => "Sampler",
:list => ["IS", "SMC", "PG", "HMC"]
),
"tarray" => Dict(
"tarrayapi" => Dict(
:title => "TArray",
:list => ["TArray", "tzeros"]
),
"chainapi" => Dict(
:title => "Chain",
:list => ["Chain", "Sample"]
)
)

Expand All @@ -56,7 +60,7 @@ cd(joinpath(dirname(@__FILE__),"source")) do
for api in to_gen[fname][:list]
md = include_string("@doc $api")
if isa(md,Markdown.MD)
isa(md.content[1].content[1],Markdown.Code) || error("Incorrect docstring format: $D")
isa(md.content[1].content[1],Markdown.Code) || error("Incorrect docstring format: $api")

printrst(f,md)
else
Expand Down
47 changes: 47 additions & 0 deletions doc/source/chainapi.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
Chain
=========

.. function:: Chain(weight::Float64, value::Array{Sample})

A wrapper of output trajactory of samplers.

Example:

.. code-block:: julia
# Define a model
@model xxx begin
...
@predict mu sigma
end
# Run the inference engine
chain = sample(xxx, SMC(1000))
chain[:logevidence] # show the log model evidence
chain[:mu] # show the weighted trajactory for :mu
chain[:sigma] # show the weighted trajactory for :sigma
mean(chain[:mu]) # find the mean of :mu
mean(chain[:sigma]) # find the mean of :sigma
.. function:: Sample(weight::Float64, value::Dict{Symbol,Any})

A wrapper of output samples.

Example:

.. code-block:: julia
# Define a model
@model xxx begin
...
@predict mu sigma
end
# Run the inference engine
chain = sample(xxx, SMC(1000))
sample = chain[:mu][1] # get the first sample
sample.weight # show the weight of this sample
sample.value # show the value of this sample (a dictionary)
3 changes: 2 additions & 1 deletion doc/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ Contents

replayapi
samplerapi
tarray
tarrayapi
compilerapi
chainapi

.. toctree::
:maxdepth: 2
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/Turing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ StatsFuns.gammalogpdf(k::Real, θ::Real, x::Real) = -log(gamma(k)) - k * log(θ)
###########################################

# Turing essentials - modelling macros and inference algorithms
export @model, @assume, @observe, @predict, InferenceAlgorithm, HMC, IS, SMC, PG, sample
export @model, @assume, @observe, @predict, InferenceAlgorithm, HMC, IS, SMC, PG, sample, Chain, Sample

# Turing-safe data structures and associated functions
export TArray, tzeros, localcopy, IArray
Expand Down
61 changes: 59 additions & 2 deletions src/core/io.jl
Original file line number Diff line number Diff line change
@@ -1,16 +1,73 @@
# Some utility functions for extracting results.
#########################
# Sampler I/O Interface #
#########################

##########
# Sample #
##########

doc"""
Sample(weight::Float64, value::Dict{Symbol,Any})
A wrapper of output samples.
Example:
```julia
# Define a model
@model xxx begin
...
@predict mu sigma
end
# Run the inference engine
chain = sample(xxx, SMC(1000))
sample = chain[:mu][1] # get the first sample
sample.weight # show the weight of this sample
sample.value # show the value of this sample (a dictionary)
```
"""
type Sample
weight :: Float64 # particle weight
weight :: Float64 # particle weight
value :: Dict{Symbol,Any}
end

Base.getindex(s::Sample, v::Symbol) = Base.getindex(s.value, v)

#########
# Chain #
#########

doc"""
Chain(weight::Float64, value::Array{Sample})
A wrapper of output trajactory of samplers.
Example:
```julia
# Define a model
@model xxx begin
...
@predict mu sigma
end
# Run the inference engine
chain = sample(xxx, SMC(1000))
chain[:logevidence] # show the log model evidence
chain[:mu] # show the weighted trajactory for :mu
chain[:sigma] # show the weighted trajactory for :sigma
mean(chain[:mu]) # find the mean of :mu
mean(chain[:sigma]) # find the mean of :sigma
```
"""
type Chain
weight :: Float64 # log model evidence
value :: Array{Sample}
end

Chain() = Chain(0, Vector{Sample}())

function Base.show(io::IO, ch1::Chain)
Expand Down

0 comments on commit 06a65ef

Please sign in to comment.