Created
January 10, 2020 03:45
-
-
Save cscherrer/47f0fc7597b4ffc11186d54cc4d8e577 to your computer and use it in GitHub Desktop.
Log-densities with less code, using MacroTools and SymPy
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
import PyCall,SymPy | |
using MLStyle | |
stats = PyCall.pyimport_conda("sympy.stats", "sympy"); | |
SymPy.import_from(stats) | |
sym(x) = SymPy.symbols(x); | |
macro ℓ(expr) | |
args = @match expr begin | |
Expr(head, args...) => return args | |
end | |
d = args[1] | |
ps = args[2:end] | |
quote | |
L = SymPy.density(stats.$d(:foo, sym.($ps)...)).pdf(sym(:x)) | |
SymPy.sympy.expand_log(log(L),force=true) | |
end | |
end | |
# julia> @ℓ Normal(μ,σ) | |
# | |
# 2 | |
# log(π) log(2) (x - μ) | |
# -log(σ) - ────── - ────── - ──────── | |
# 2 2 2 | |
# 2⋅σ |
Author
cscherrer
commented
Jan 12, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment