Skip to content

Instantly share code, notes, and snippets.

@cscherrer
Created January 10, 2020 03:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cscherrer/47f0fc7597b4ffc11186d54cc4d8e577 to your computer and use it in GitHub Desktop.
Save cscherrer/47f0fc7597b4ffc11186d54cc4d8e577 to your computer and use it in GitHub Desktop.
Log-densities with less code, using MacroTools and SymPy
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⋅σ
@cscherrer
Copy link
Author

julia> # μ: location
       # σ: scale
       # τ: precision or concentration
       # λ: rate
       # ν: degrees of freedom
       # k: degrees of freedom (integer)
       # α,β: shapes


       @ℓ Arcsin(a,b)
  log(-a + x)   log(b - x)         
- ─────────── - ────────── - log(π)
       2            2              

julia> @ℓ Benini(α,β,σ)
                                          2      ⎛α   2β(log(x) - log(σ))⎞
-α(log(x) - log(σ)) - β(log(x) - log(σ))  + log⎜─ + ─────────────────────⎟
                                                 ⎝x             x          ⎠

julia> @ℓ Beta(α,β)
(α - 1)log(x) +- 1)log(1 - x) - log(Β(α, β))

julia> @ℓ BetaPrime(α,β)
(-α - β)log(x + 1) +- 1)log(x) - log(Β(α, β))

julia> @ℓ Cauchy(μ,σ)
             ⎛           2⎞         
             ⎜    (x - μ) ⎟         
-log(σ) - log⎜1 + ────────⎟ - log(π)
             ⎜        2   ⎟         
             ⎝       σ    ⎠         

julia> @ℓ Chi(k)
   2                                              
  x    ⎛    k⎞                              ⎛ ⎛k⎞⎞
- ── +1 - ─⎟log(2) + (k - 1)log(x) - log⎜Γ⎜─⎟⎟
  22⎠                              ⎝ ⎝2⎠⎠

julia> @ℓ ChiNoncentral(k,λ)
                                    2    2                                    
  k(log(x) + log(λ))              x    λ                ⎛       ⎛k         ⎞⎞
- ─────────────────── + klog(x) - ── - ── + log(λ) + log⎜besseli⎜─ - 1, xλ⎟⎟
           2                       2    2                ⎝       ⎝2         ⎠⎠

julia> @ℓ ChiSquared(k)
  klog(2)   x   ⎛k    ⎞             ⎛ ⎛k⎞⎞
- ──────── -+ ⎜─ - 1log(x) - log⎜Γ⎜─⎟⎟
     2       22    ⎠             ⎝ ⎝2⎠⎠

julia> @ℓ Dagum(p,a,b) # shape, shape, scale
                                     ⎛   a    ⎞                           
                                     ⎜⎛x⎞     ⎟                           
ap(-log(b) + log(x)) + (-p - 1)log⎜⎜─⎟  + 1+ log(a) + log(p) - log(x)
                                     ⎝⎝b⎠     ⎠                           

julia> @ℓ Erlang(k,λ)
klog(λ) - xλ + (k - 1)log(x) - log(Γ(k))

julia> @ℓ Exponential(λ)
-xλ + log(λ)

julia> @ℓ FDistribution(ν1,ν2)
ν1(log(x) + log(ν1))   ν2log(ν2)   (-ν1 - ν2)log(xν1 + ν2)               ⎛ ⎛ν1  ν2⎞⎞
───────────────────── + ────────── + ───────────────────────── - log(x) - log⎜Β⎜──, ──⎟⎟
          2                 2                    2                           ⎝ ⎝2   2 ⎠⎠

julia> @ℓ FisherZ(ν1,ν2)
       ν1log(ν1)   ν2log(ν2)   ⎛  ν1   ν2⎞    ⎛    2x     ⎞      ⎛ ⎛ν1  ν2⎞⎞         
xν1 + ────────── + ────────── +- ── - ──⎟log⎝ν1+ ν2⎠ - log⎜Β⎜──, ──⎟⎟ + log(2)
           2            22    2 ⎠                        ⎝ ⎝2   2 ⎠⎠         

julia> @ℓ Frechet(shape,scale,x_min)
                                                                                 -shape
                                                                       ⎛x - xₘᵢₙ⎞      
(-shape - 1)(-log(scale) + log(x - xₘᵢₙ)) - log(scale) + log(shape) - ⎜────────⎟      
                                                                       ⎝ scale  ⎠      

julia> @ℓ Gamma(shape,scale)
                                                           x  
-shapelog(scale) + (shape - 1)log(x) - log(Γ(shape)) - ─────
                                                         scale

julia> @ℓ GammaInverse(shape,scale)
  scale                                                         
- ───── + shapelog(scale) + (-shape - 1)log(x) - log(Γ(shape))
    x                                                           

julia> @ℓ Gompertz(scale, shape)
                 scalex                                  
scalex - shape+ shape + log(scale) + log(shape)

julia> @ℓ Gumbel(μ,σ)
           -(x - σ)         
           ─────────        
               μ       x - σ
-log(μ) -- ─────
                         μ  

julia> @ℓ Kumaraswamy(α,β)
                            ⎛     α⎞                  
(α - 1)log(x) +- 1)log⎝1 - x ⎠ + log(α) + log(β)

julia> @ℓ Laplace(μ,σ)
                   │x - μ│
-log(σ) - log(2) - ───────
                      σ   

julia> @ℓ LogNormal(scale,shape)
                                                          2
                       log(π)   log(2)   (-scale + log(x)) 
-log(shape) - log(x) - ────── - ────── - ──────────────────
                         2        2                  2     
                                              2shape      

julia> @ℓ Normal(μ,σ)
                                   2
          log(π)   log(2)   (x - μ) 
-log(σ) - ────── - ────── - ────────
            2        2           2  
                              2σ   

julia> @ℓ Rayleigh(σ)
    2                     
   x                      
- ──── + log(x) - 2log(σ)
     2                    
  2σ                     

julia> @ℓ StudentT(ν)
             ⎛ 2    ⎞                          
⎛  ν   1⎞    ⎜x     ⎟   log(ν)      ⎛ ⎛     ν⎞⎞
⎜-- ─⎟log⎜── + 1- ────── - log⎜Β⎜1/2, ─⎟⎟
⎝  2   2⎠    ⎝ν     ⎠     2         ⎝ ⎝     2⎠⎠

julia> @ℓ VonMises(μ,τ)
τcos(x - μ) - log(besseli(0, τ)) - log(π) - log(2)

julia> @ℓ Pareto(σ, shape)
shapelog(σ) + (-shape - 1)log(x) + log(shape)

julia> @ℓ WignerSemicircle(radius)
                    ⎛      2    2⎞                  
                 log⎝radius  - x ⎠                  
-2log(radius) + ───────────────── - log(π) + log(2)
                         2                          

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment