API
Soss.AbstractModelSoss.DoSoss._unwrap_typeSoss.afterSoss.beforeSoss.importanceSampleSoss.likelihoodSoss.predictiveSoss.priorSoss.pruneSoss.withmeasures
Soss.AbstractModel — TypeAbstractModel{A,B,M,Args,Obs}Gives an abstract type for all Soss models
Type variables ending in T are type-level representations used to reconstruct
N gives the Names of arguments (each a Symbol) B gives the Body, as an Expr M gives the Module where the model is defined
Soss.Do — MethodDo(m, xs...)Returns a model transformed by adding xs... to arguments. The remainder of the body remains the same, consistent with Judea Pearl's "Do" operator. Unneeded arguments are trimmed.
Examples
m = @model (n, k) begin
β ~ Gamma()
α ~ Gamma()
θ ~ Beta(α, β)
x ~ Binomial(n, θ)
z ~ Binomial(k, α / (α + β))
end;
Do(m, :θ)
# output
@model (n, k, θ) begin
β ~ Gamma()
α ~ Gamma()
x ~ Binomial(n, θ)
z ~ Binomial(k, α / (α + β))
endSoss._unwrap_type — Methodwe use this to avoid introduce static type parameters for generated functions
Soss.after — Methodafter(m::Model, xs...; strict=false)Transforms m by moving xs to arguments. If strict=true, only descendants of xs are retained in the body. Otherwise, the remaining variables in the body are unmodified. Unused arguments are trimmed.
predictive(m::Model, xs...) = after(m, xs..., strict = true)
Do(m::Model, xs...) = after(m, xs..., strict = false)
Example
julia> m = @model (n, k) begin
β ~ Gamma()
α ~ Gamma()
θ ~ Beta(α, β)
x ~ Binomial(n, θ)
z ~ Binomial(k, α / (α + β))
end;
julia> Soss.after(m, :θ, strict=false) # same as Do(m, :θ)
@model (n, k, θ) begin
β ~ Gamma()
α ~ Gamma()
x ~ Binomial(n, θ)
z ~ Binomial(k, α / (α + β))
end
julia> Soss.after(m, :θ, strict = true) # same as predictive(m, :θ)
@model (n, θ) begin
x ~ Binomial(n, θ)
endSoss.before — Methodbefore(m::Model, xs...; inclusive=true, strict=true)Transforms m by retaining all ancestors of any of xs if strict=true; if strict=false, retains all variables that are not descendants of any xs. Note that adding more variables to xs cannot result in a larger model. If inclusive=true, xs is considered to be an ancestor of itself and is always included in the returned Model. Unneeded arguments are trimmed.
prune(m::Model, xs...) = before(m, xs..., inclusive = false, strict = false)
prior(m::Model, xs...) = before(m, xs..., inclusive = true, strict = true)
Examples
julia> m = @model (n, k) begin
β ~ Gamma()
α ~ Gamma()
θ ~ Beta(α, β)
x ~ Binomial(n, θ)
z ~ Binomial(k, α / (α + β))
end;
julia> Soss.before(m, :θ, inclusive = true, strict = true)
@model begin
β ~ Gamma()
α ~ Gamma()
θ ~ Beta(α, β)
end
julia> Soss.before(m, :θ, inclusive = true, strict = false)
@model k begin
β ~ Gamma()
α ~ Gamma()
θ ~ Beta(α, β)
z ~ Binomial(k, α / (α + β))
end
julia> Soss.before(m, :θ, inclusive = false, strict = true) # same as Soss.prior(m, :θ)
@model begin
β ~ Gamma()
α ~ Gamma()
end
julia> Soss.before(m, :θ, inclusive=false, strict=false) # same as Soss.prune(m, :θ)
@model k begin
β ~ Gamma()
α ~ Gamma()
z ~ Binomial(k, α / (α + β))
endSoss.importanceSample — FunctionimportanceSample(p(p_args), q(q_args), observed_data)Sample from q, and weight the result to behave as if the sample were taken from p. For example,
``` julia> p = @model begin x ~ Normal() y ~ Normal(x,1) |> iid(5) end;
julia> q = @model μ,σ begin x ~ Normal(μ,σ) end;
julia> y = rand(p()).y;
julia> importanceSample(p(),q(μ=0.0, σ=0.5), (y=y,)) Weighted(-7.13971.4 ,(x = -0.12280566635062592,) ````
Soss.likelihood — Methodlikelihood(m, xs...)Return a model with only the specified variables in the body. Required dependencies will be included as arguments.
Soss.predictive — Methodpredictive(m, xs...)Returns a model transformed by adding xs... to arguments with a body containing only statements that depend on xs, or statements that are depended upon by children of xs through an open path. Unneeded arguments are trimmed.
Examples
m = @model (n, k) begin
β ~ Gamma()
α ~ Gamma()
θ ~ Beta(α, β)
x ~ Binomial(n, θ)
z ~ Binomial(k, α / (α + β))
end;
predictive(m, :θ)
# output
@model (n, θ) begin
x ~ Binomial(n, θ)
endSoss.prior — Methodprior(m, xs...)Returns the minimal model required to sample random variables xs.... Useful for extracting a prior distribution from a joint model m by designating xs... and the variables they depend on as the prior and hyperpriors.
Example
m = @model n begin
α ~ Gamma()
β ~ Gamma()
θ ~ Beta(α,β)
x ~ Binomial(n, θ)
end;
Soss.prior(m, :x)
# output
@model begin
β ~ Gamma()
α ~ Gamma()
θ ~ Beta(α, β)
endSoss.prune — Methodprune(m, xs...)Returns a model transformed by removing xs... and all variables that depend on xs.... Unneeded arguments are also removed.
Examples
m = @model n begin
α ~ Gamma()
β ~ Gamma()
θ ~ Beta(α,β)
x ~ Binomial(n, θ)
end;
prune(m, :θ)
# output
@model begin
β ~ Gamma()
α ~ Gamma()
endm = @model n begin
α ~ Gamma()
β ~ Gamma()
θ ~ Beta(α,β)
x ~ Binomial(n, θ)
end;
prune(m, :n)
# output
@model begin
β ~ Gamma()
α ~ Gamma()
θ ~ Beta(α, β)
endSoss.withmeasures — Methodwithmeasures(m::Model) -> Modeljulia> m = @model begin σ ~ HalfNormal() y ~ For(10) do j Normal(0,σ) end end;
julia> mdists = Soss.withmeasures(m) @model begin _σdist = HalfNormal() σ ~ σdist ydist = For(10) do j Normal(0, σ) end y ~ ydist end
julia> ydist = rand(mdists()).y_dist For{GeneralizedGenerated.Closure{function = (σ, M, j;) -> begin M.Normal(0, σ) end,Tuple{Float64,Module}},Tuple{Int64},Normal{Float64},Float64}(GeneralizedGenerated.Closure{function = (σ, M, j;) -> begin M.Normal(0, σ) end,Tuple{Float64,Module}}((0.031328640120683524, Main)), (10,))
julia> rand(ydist) 10-element Array{Float64,1}: 0.03454891487870426 0.008832782323408313 -0.007395186925623771 -0.030669004243492004 -0.01728630026691135 0.011892877715064682 0.025576319363013512 -0.029323425779917773 -0.020502677724193594 0.04612690097957398