Using TransformVariables.jl to Numerically Handle Jacobian Adjustment of Simple Problem

Here is a simple generative DAG where a Jacobian adjustment is required to get the logpdf of the posterior distribution for y.

I can code this function to work with the DynamicHMC.jl suite of packages if I manually add the Jacobian adjustment - which is great. I cannot, however, figure out how to use TransformVariables.jl (or different package) so that I avoid the analytic calculation of the Jacobian function. I am pretty sure the computer will calculate the adjustment for me, I just can’t figure out which function (e.g. TransformVariables.CustomTransform or transform_logdensity ???) or how to use the function? I’ve tried many combos and can really use some help. Thanks!

Here is the working code where I want to replace the analytic Jacobian adjustment with one performed computationally:

using Distributions, TransformVariables, LogDensityProblems, DynamicHMC, DynamicHMC.Diagnostics, Parameters, Statistics, Random
## create problem type where winnings (w) is observed
struct spinProblem{T <: AbstractVector}
    w::T # winnings of previous players
end

## make function factory for this problem type
function (problem::spinProblem)(θ)
    @unpack y = θ   # extract the parameter
    @unpack w = problem   # extract the data

    # log likelihood accumulated in llSpinProb
    # prior on y
    llSpinProb = logpdf(Uniform(0,10),y)

    # likelihood
    for i in 1:length(w)
        ## let x_i = w_i/y be uniform(0,1)
        x_i = w[i] / y
        llSpinProb += logpdf(Uniform(0,1),x_i) #data
        llSpinProb += log(1/y) # Jacobian Adjustment - HOW TO REPLACE THIS WITH NUMERICAL APPROX
    end

    return(llSpinProb)
end

p2 = spinProblem([3.2,7.6,4.1,1.1,2.4]) ## fake data
p2((y = 7.5,)) # infeasible
p2((y = 8,)) # feasible
p2((y = 9.5,)) # feasible (less likely)

I am continuing to educate myself and now believe that TransformVariables.jl is used to go from the unconstrained space that an HMC sampler prefers to the constrained space of a probability model. It is not a general package for any old probability transform using a Jacobian. I now see LogDensityProblems.jl might be the right avenue for computationally computed Jacobian transforms; maybe? My search continues … any hints are appreciated :slight_smile: