Skip to content

Instantly share code, notes, and snippets.

@theogf
Last active May 11, 2020 07:35
Show Gist options
  • Save theogf/cb4c0f0f626d7d31e4cff7b7227144f2 to your computer and use it in GitHub Desktop.
Save theogf/cb4c0f0f626d7d31e4cff7b7227144f2 to your computer and use it in GitHub Desktop.
Source file for the animation displayed in : https://twitter.com/theo_gf/status/1258015348420489217
using Distributions
using Plots; pyplot()
using ForwardDiff
using LaTeXStrings
λ = 3.0
μ = 2.0
s = 0.8
# α = Exponential(λ) # Measure alpha the starting point
α = Laplace(μ, s + 1.0)
β = Logistic(μ - 1.0, s) # Measure beta, the objective
C(p::Distribution, x) = cdf(p, x)
invC(p::Exponential, u) = - inv(p.θ) * log(1 - u)
invC(p::Logistic, u) = p.μ - p.θ * log(1/u - 1)
invC(p::Laplace, u) = p.μ - p.θ * sign(u - 0.5) * log(1 - 2 * abs(u - 0.5))
## Transformation from α to β
# t = 0 means we get identity : α
# t = 1 means we get full transformation : β
T(α, β, x, t) = t * invC(β, C(α, x)) + (1 - t) * x #invC(β, C(β, x))
# dT gives the gradient of this transformation
dT(α, β, x, t) = abs(first(
ForwardDiff.gradient(x -> T(α, β, x[1], t), [x])))
x = range(-3, 8, length = 200)
##
# Going from the exponential to the logistic
t = 1.0
default(lw = 3.0, legendfontsize = 15.0)
anim = @animate for t in 0:0.01:1.0
plot(x, x -> pdf(α, x), lab = L"\alpha" * " : $(nameof(typeof(α)))", title = "t = $t")
plot!(x, x -> pdf(β, x), lab = L"\beta" * " : $(nameof(typeof(β)))")
# plot!(x, x -> dT(p0, p1, x, t), lab = "(1-t)q + tp")
plot!(x, x -> dT(β, α, x, t) * pdf(α, T(β, α, x, t)), lab = L"(t T + (1-t) Id)_\sharp \alpha")
end
gif(anim, joinpath(@__DIR__, string(nameof(typeof(α))) * "_to_" * string(nameof(typeof(β))) * ".gif"), fps = 20)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment