Skip to content

Commit

Permalink
Add transform_logdensity.
Browse files Browse the repository at this point in the history
  • Loading branch information
tpapp committed Jun 30, 2018
1 parent 11ed57a commit 45639c1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/TransformVariables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ using LinearAlgebra: UpperTriangular
using DocStringExtensions: SIGNATURES
using Parameters: @unpack

export transform, transform_and_logjac, inverse
export transform, transform_and_logjac, transform_logdensity, inverse


# utilities
Expand Down Expand Up @@ -54,6 +54,11 @@ transform(t::TransformReals, x::RealVector) = first(_transform(t, NOLOGJAC, x))

transform_and_logjac(t::TransformReals, x::RealVector) = _transform(t, LOGJAC, x)

function transform_logdensity(t::TransformReals, f, x)
y, ℓ = transform_and_logjac(t, x)
+ f(y)
end

include("utilities.jl")
include("scalar.jl")
include("special_arrays.jl")
Expand Down
13 changes: 13 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,16 @@ end
@test y == y2
@test lj2 ljacc
end

@testset "transform logdensity" begin
# the density is p(σ) = σ⁻³
# let z = log(σ), so σ = exp(z)
# the transformed density is q(z) = -3z + z = -2z
f(σ) = -3*log(σ)
q(z) = -2*z
for _ in 1:1000
z = randn()
qz = transform_logdensity(to_ℝ₊, f, [z])
@test q(z) qz
end
end

0 comments on commit 45639c1

Please sign in to comment.