-
Notifications
You must be signed in to change notification settings - Fork 37
Making use of Symbolics.jl/SymbolicUtils.jl #234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 8 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
783d81d
relax constraints to allow use with Symbolics.jl
torfjelde 0421aed
added symbolic stuff
torfjelde 14ad023
added Symbolic as dependency
torfjelde 03067a0
added version bound to Symbolics
torfjelde 785242e
added another method for disambiguiation
torfjelde a3a9742
update Project.toml
torfjelde 284d85d
Merge branch 'master' into tor/symbolics
torfjelde 817533c
updated symbolics versioning
torfjelde daa1319
added some hacks to make it at least run
torfjelde 70c9997
updated symbolize
torfjelde 23141be
formatting
torfjelde 18ae91b
Merge branch 'master' into tor/symbolics
torfjelde b19700e
make it runnable on newer package versions
torfjelde File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,77 @@ | ||
| module Symbolic | ||
|
|
||
| import ..DynamicPPL | ||
| import ..DynamicPPL: Model, VarInfo, AbstractSampler, SampleFromPrior, VarName, DefaultContext | ||
|
|
||
| import Random | ||
| import Bijectors | ||
torfjelde marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| using Distributions | ||
| import Symbolics | ||
torfjelde marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| import Symbolics: SymbolicUtils | ||
|
|
||
| issym(x::Union{Symbolics.Num, SymbolicUtils.Symbolic}) = true | ||
torfjelde marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| issym(x) = false | ||
|
|
||
| include("rules.jl") | ||
| include("contexts.jl") | ||
|
|
||
| symbolize(args...; kwargs...) = symbolize(Random.GLOBAL_RNG, args...; kwargs...) | ||
| function symbolize( | ||
| rng::Random.AbstractRNG, | ||
| m::Model, | ||
| vi::VarInfo = VarInfo(m); | ||
| spl = SampleFromPrior(), | ||
| ctx = DefaultContext(), | ||
| include_data = false | ||
torfjelde marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) | ||
| m(rng, vi, spl, ctx); | ||
torfjelde marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| θ_orig = vi[spl] | ||
|
|
||
| # Symbolic `logpdf` for fixed observations. | ||
| Symbolics.@variables θ[1:length(θ_orig)] | ||
| vi = VarInfo(vi, spl, θ, zero(eltype(θ))); | ||
| m(rng, vi, spl, ctx); | ||
torfjelde marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return vi, θ | ||
| end | ||
|
|
||
| function dependencies(ctx::SymbolicContext, vn::VarName) | ||
| right = ctx.vn2rights[vn] | ||
| r = Symbolics.value(right) | ||
|
|
||
| if !issym(r) | ||
| # No dependencies. | ||
| return [] | ||
| end | ||
|
|
||
| args = SymbolicUtils.arguments(r) | ||
| return mapreduce(vcat, args) do a | ||
| Symbolics.get_variables(a) | ||
| end | ||
| end | ||
| function dependencies(ctx::SymbolicContext, symbolic = false) | ||
torfjelde marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| vn2var = ctx.vn2var | ||
| var2vn = Dict(values(vn2var) .=> keys(vn2var)) | ||
| return Dict( | ||
| (symbolic ? vn2var[vn] : vn) => map(x -> symbolic ? x : var2vn[x], dependencies(ctx, vn)) | ||
| for vn in keys(ctx.vn2var) | ||
torfjelde marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) | ||
| end | ||
|
|
||
| function dependencies(m::Model, symbolic = false) | ||
torfjelde marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ctx = SymbolicContext(DefaultContext()) | ||
| vi = symbolize(m, VarInfo(m), ctx = ctx) | ||
torfjelde marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return dependencies(ctx, symbolic) | ||
| end | ||
|
|
||
|
|
||
torfjelde marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| function symbolic_logp(m::Model) | ||
| vi, θ = symbolize(m) | ||
| lp = DynamicPPL.getlogp(vi) | ||
| lp_analytic = analytic_rw(Symbolics.value(lp)) | ||
| lp_analytic_num = addnum_rw(lp_analytic) | ||
|
|
||
| return lp_analytic_num, θ | ||
| end | ||
| end | ||
This file contains hidden or 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 @@ | ||
| struct SymbolicContext{Ctx} <: DynamicPPL.AbstractContext | ||
| ctx::Ctx | ||
| vn2var::Dict | ||
| vn2rights::Dict | ||
| end | ||
| SymbolicContext() = SymbolicContext(DefaultContext()) | ||
| SymbolicContext(ctx) = SymbolicContext(ctx, Dict(), Dict()) | ||
|
|
||
| # assume | ||
| function DynamicPPL.tilde(rng, ctx::SymbolicContext, sampler, right, vn::VarName, inds, vi) | ||
| if Symbolic.issym(right) || (haskey(vi, vn) && Symbolic.issym(vi[vn])) | ||
| # Distribution is symbolic OR variable is. | ||
| ctx.vn2var[vn] = vi[vn] | ||
| ctx.vn2rights[vn] = right | ||
| end | ||
|
|
||
| return DynamicPPL.tilde(rng, ctx.ctx, sampler, right, vn, inds, vi) | ||
| end | ||
|
|
||
|
|
||
torfjelde marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # TODO: Make it more useful when working with symbolic observations. | ||
| # observe | ||
| function DynamicPPL.tilde(ctx::SymbolicContext, sampler, right, left, vi) | ||
| if Symbolic.issym(right) || Symbolic.issym(left) | ||
| # TODO: implement | ||
| end | ||
|
|
||
| return DynamicPPL.tilde(ctx.ctx, sampler, right, left, vi) | ||
| end | ||
This file contains hidden or 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,72 @@ | ||
| import Bijectors | ||
| import Symbolics | ||
torfjelde marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| using Symbolics.SymbolicUtils | ||
|
|
||
| Symbolics.@register Bijectors.logpdf_with_trans(dist, r, istrans) | ||
|
|
||
| # Some predicates | ||
| isdist(d) = (d isa Type) && (d <: Distribution) | ||
| islogpdf(f::Function) = f === Distributions.logpdf | ||
| islogpdf(x) = false | ||
|
|
||
| # HACK: Apparently this is needed for disambiguiation. | ||
| # TODO: Open issue. | ||
| Symbolics.:<ₑ(a::Real, b::Symbolics.Num) = Symbolics.:<ₑ(Symbolics.value(a), Symbolics.value(b)) | ||
| Symbolics.:<ₑ(a::Symbolics.Num, b::Real) = Symbolics.:<ₑ(Symbolics.value(a), Symbolics.value(b)) | ||
torfjelde marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ############# | ||
| ### Rules ### | ||
| ############# | ||
| # HACK: We'll wrap rewriters to add back `Num`. This way we can get jacobians and whatnot at then end. | ||
| const rmnum_rule = @rule (~x) => Symbolics.value(~x) | ||
| const addnum_rule = @rule (~x) => Symbolics.Num(~x) | ||
|
|
||
| # In the case where we want to work directly with the `x ~ Distribution` statements, the following rules can be useful: | ||
| const logpdf_rule = @rule (~x ~ ~d) => Distributions.logpdf(Symbolics.Num(~d), Symbolics.Num(~x)); | ||
torfjelde marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const rand_rule = @rule (~x ~ ~d) => Distributions.rand(Symbolics.Num(~d)) | ||
|
|
||
| # We don't want to trace into `Bijectors.logpdf_with_trans`, so we just replace it with `logpdf`. | ||
| islogpdf_with_trans(f::Function) = f === Bijectors.logpdf_with_trans | ||
| islogpdf_with_trans(x) = false | ||
| const logpdf_with_trans_rule = @rule (~f::islogpdf_with_trans)(~dist, ~x, ~istrans) => logpdf(~dist, ~x) | ||
torfjelde marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| # Attempt to expand `logpdf` to get analytical expressions. | ||
| # The idea is that `getlogpdf(d, args)` should return a method of the following signature: | ||
| # | ||
| # f(args..., x) | ||
| # | ||
| # which returns the logpdf. | ||
| # HACK: this is very hacky but you get the idea | ||
| import Distributions: StatsFuns | ||
| function getlogpdf(d, args) | ||
| replacements = Dict( | ||
| :Normal => StatsFuns.normlogpdf, | ||
| :Gamma => StatsFuns.gammalogpdf | ||
| ) | ||
|
|
||
torfjelde marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| dsym = Symbol(d) | ||
| if haskey(replacements, dsym) | ||
| return replacements[dsym] | ||
| else | ||
| return d | ||
| end | ||
| end | ||
|
|
||
| const analytic_rule = @rule (~f::islogpdf)((~d::isdist)(~~args), ~x) => getlogpdf(~d, ~~args)(map(Symbolics.Num, (~~args))..., Symbolics.Num(~x)) | ||
|
|
||
torfjelde marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ################# | ||
| ### Rewriters ### | ||
| ################# | ||
| # TODO: these should probably be instantiated when needed, rather than here. | ||
| const analytic_rw = Rewriters.Postwalk( | ||
| Rewriters.Chain(( | ||
| rmnum_rule, # 0. Remove `Num` so we're only working stuff from `SymbolicUtils.jl`. | ||
| logpdf_with_trans_rule, # 1. Replace `logpdf_with_trans` with `logpdf`. | ||
| analytic_rule, # 2. Attempt to replace `logpdf` with analytic expression. | ||
| )) | ||
| ) | ||
|
|
||
| # So we add back `Num` to all terms to allow differentiation. | ||
| const rmnum_rw = Rewriters.Postwalk(Rewriters.PassThrough(rmnum_rule)) | ||
| const addnum_rw = Rewriters.Postwalk(Rewriters.PassThrough(addnum_rule)) | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.